Skip to content

Commit

Permalink
Merge pull request #19 from Kloenk/coding-style
Browse files Browse the repository at this point in the history
apply coding style
  • Loading branch information
alex authored Oct 5, 2020
2 parents c17d290 + 0d05ea2 commit f60edb2
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 29 deletions.
3 changes: 1 addition & 2 deletions drivers/char/rust_example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use kernel::prelude::*;

module!{
module! {
type: RustExample,
name: b"rust_example",
author: b"Rust for Linux Contributors",
Expand Down Expand Up @@ -48,4 +48,3 @@ impl Drop for RustExample {
println!("Rust Example (exit)");
}
}

5 changes: 2 additions & 3 deletions rust/kernel/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-2.0

use std::path::PathBuf;
use std::env;
use std::path::PathBuf;

const INCLUDED_TYPES: &[&str] = &["file_system_type", "mode_t", "umode_t", "ctl_table"];
const INCLUDED_FUNCTIONS: &[&str] = &[
Expand Down Expand Up @@ -92,8 +92,7 @@ fn main() {
println!("cargo:rerun-if-env-changed=RUST_BINDGEN_CFLAGS");

let kernel_dir = "../../";
let cflags = env::var("RUST_BINDGEN_CFLAGS")
.expect("Must be invoked from kernel makefile");
let cflags = env::var("RUST_BINDGEN_CFLAGS").expect("Must be invoked from kernel makefile");

let kernel_args = prepare_cflags(&cflags, &kernel_dir);

Expand Down
12 changes: 2 additions & 10 deletions rust/kernel/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@

//! The `kernel` prelude

pub use alloc::{
string::String,
borrow::ToOwned,
};
pub use alloc::{borrow::ToOwned, string::String};

pub use module::module;

pub use super::{
println,
KernelResult,
KernelModule,
};

pub use super::{println, KernelModule, KernelResult};
3 changes: 2 additions & 1 deletion rust/kernel/src/user_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use crate::c_types;
use crate::error;

extern "C" {
fn rust_helper_access_ok(addr: *const c_types::c_void, len: c_types::c_ulong) -> c_types::c_int;
fn rust_helper_access_ok(addr: *const c_types::c_void, len: c_types::c_ulong)
-> c_types::c_int;
}

/// A reference to an area in userspace memory, which can be either
Expand Down
67 changes: 54 additions & 13 deletions rust/module/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

extern crate proc_macro;

use proc_macro::{TokenStream, TokenTree, Group, Delimiter, token_stream};
use proc_macro::{token_stream, Delimiter, Group, TokenStream, TokenTree};

fn expect_ident(it: &mut token_stream::IntoIter) -> String {
if let TokenTree::Ident(ident) = it.next().unwrap() {
Expand Down Expand Up @@ -78,13 +78,24 @@ fn get_byte_string(it: &mut token_stream::IntoIter, expected_name: &str) -> Stri
byte_string[2..byte_string.len() - 1].to_string()
}

fn __build_modinfo_string_base(module: &str, field: &str, content: &str, variable: &str, builtin: bool) -> String {
fn __build_modinfo_string_base(
module: &str,
field: &str,
content: &str,
variable: &str,
builtin: bool,
) -> String {
let string = if builtin {
// Built-in modules prefix their modinfo strings by `module.`
format!("{module}.{field}={content}", module=module, field=field, content=content)
format!(
"{module}.{field}={content}",
module = module,
field = field,
content = content
)
} else {
// Loadable modules' modinfo strings go as-is
format!("{field}={content}", field=field, content=content)
format!("{field}={content}", field = field, content = content)
};

format!(
Expand All @@ -94,23 +105,39 @@ fn __build_modinfo_string_base(module: &str, field: &str, content: &str, variabl
#[used]
pub static {variable}: [u8; {length}] = *b\"{string}\\0\";
",
cfg = if builtin { "#[cfg(not(MODULE))]" } else { "#[cfg(MODULE)]" },
cfg = if builtin {
"#[cfg(not(MODULE))]"
} else {
"#[cfg(MODULE)]"
},
variable = variable,
length = string.len() + 1,
string = string,
)
}

fn __build_modinfo_string_variable(module: &str, field: &str) -> String {
format!("__{module}_{field}", module=module, field=field)
format!("__{module}_{field}", module = module, field = field)
}

fn build_modinfo_string_only_builtin(module: &str, field: &str, content: &str) -> String {
__build_modinfo_string_base(module, field, content, &__build_modinfo_string_variable(module, field), true)
__build_modinfo_string_base(
module,
field,
content,
&__build_modinfo_string_variable(module, field),
true,
)
}

fn build_modinfo_string_only_loadable(module: &str, field: &str, content: &str) -> String {
__build_modinfo_string_base(module, field, content, &__build_modinfo_string_variable(module, field), false)
__build_modinfo_string_base(
module,
field,
content,
&__build_modinfo_string_variable(module, field),
false,
)
}

fn build_modinfo_string(module: &str, field: &str, content: &str) -> String {
Expand All @@ -119,8 +146,13 @@ fn build_modinfo_string(module: &str, field: &str, content: &str) -> String {
}

fn build_modinfo_string_param(module: &str, field: &str, param: &str, content: &str) -> String {
let variable = format!("__{module}_{field}_{param}", module=module, field=field, param=param);
let content = format!("{param}:{content}", param=param, content=content);
let variable = format!(
"__{module}_{field}_{param}",
module = module,
field = field,
param = param
);
let content = format!("{param}:{content}", param = param, content = content);
__build_modinfo_string_base(module, field, &content, &variable, true)
+ &__build_modinfo_string_base(module, field, &content, &variable, false)
}
Expand Down Expand Up @@ -202,8 +234,18 @@ pub fn module(ts: TokenStream) -> TokenStream {
t => panic!("Unrecognized type {}", t),
};

params_modinfo.push_str(&build_modinfo_string_param(&name, "parmtype", &param_name, &param_kernel_type));
params_modinfo.push_str(&build_modinfo_string_param(&name, "parm", &param_name, &param_description));
params_modinfo.push_str(&build_modinfo_string_param(
&name,
"parmtype",
&param_name,
&param_kernel_type,
));
params_modinfo.push_str(&build_modinfo_string_param(
&name,
"parm",
&param_name,
&param_description,
));
params_modinfo.push_str(
&format!(
"
Expand Down Expand Up @@ -352,4 +394,3 @@ pub fn module(ts: TokenStream) -> TokenStream {
initcall_section = ".initcall6.init"
).parse().unwrap()
}

2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ignore = [ "rust/shlex" ]

0 comments on commit f60edb2

Please sign in to comment.