From 0d05ea2259bd821445f1b3091199082217255184 Mon Sep 17 00:00:00 2001 From: Finn Behrens Date: Mon, 5 Oct 2020 13:23:01 +0200 Subject: add rustfmt.toml Signed-off-by: Finn Behrens --- drivers/char/rust_example/src/lib.rs | 3 +- rust/kernel/build.rs | 5 ++- rust/kernel/src/prelude.rs | 12 ++----- rust/kernel/src/user_ptr.rs | 3 +- rust/module/src/lib.rs | 67 +++++++++++++++++++++++++++++------- rustfmt.toml | 2 ++ 6 files changed, 63 insertions(+), 29 deletions(-) create mode 100644 rustfmt.toml diff --git a/drivers/char/rust_example/src/lib.rs b/drivers/char/rust_example/src/lib.rs index e8405f12ce44..fe74656d977d 100644 --- a/drivers/char/rust_example/src/lib.rs +++ b/drivers/char/rust_example/src/lib.rs @@ -5,7 +5,7 @@ use kernel::prelude::*; -module!{ +module! { type: RustExample, name: b"rust_example", author: b"Rust for Linux Contributors", @@ -48,4 +48,3 @@ impl Drop for RustExample { println!("Rust Example (exit)"); } } - diff --git a/rust/kernel/build.rs b/rust/kernel/build.rs index 5a3794fdc995..0897186b3f92 100644 --- a/rust/kernel/build.rs +++ b/rust/kernel/build.rs @@ -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] = &[ @@ -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); diff --git a/rust/kernel/src/prelude.rs b/rust/kernel/src/prelude.rs index 75a5017488a4..36d9e2104f68 100644 --- a/rust/kernel/src/prelude.rs +++ b/rust/kernel/src/prelude.rs @@ -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}; diff --git a/rust/kernel/src/user_ptr.rs b/rust/kernel/src/user_ptr.rs index 19ecb711e24a..986043c4596f 100644 --- a/rust/kernel/src/user_ptr.rs +++ b/rust/kernel/src/user_ptr.rs @@ -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 diff --git a/rust/module/src/lib.rs b/rust/module/src/lib.rs index 82dac6780b9c..b8bdbc9eb847 100644 --- a/rust/module/src/lib.rs +++ b/rust/module/src/lib.rs @@ -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() { @@ -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!( @@ -94,7 +105,11 @@ 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, @@ -102,15 +117,27 @@ fn __build_modinfo_string_base(module: &str, field: &str, content: &str, variabl } 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 { @@ -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) } @@ -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", ¶m_name, ¶m_kernel_type)); - params_modinfo.push_str(&build_modinfo_string_param(&name, "parm", ¶m_name, ¶m_description)); + params_modinfo.push_str(&build_modinfo_string_param( + &name, + "parmtype", + ¶m_name, + ¶m_kernel_type, + )); + params_modinfo.push_str(&build_modinfo_string_param( + &name, + "parm", + ¶m_name, + ¶m_description, + )); params_modinfo.push_str( &format!( " @@ -352,4 +394,3 @@ pub fn module(ts: TokenStream) -> TokenStream { initcall_section = ".initcall6.init" ).parse().unwrap() } - diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 000000000000..25cc4a14bf20 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,2 @@ +ignore = [ "rust/shlex" ] + -- cgit v1.2.3