Skip to content

Commit

Permalink
rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
adamrk committed Feb 26, 2021
1 parent 8c8b479 commit 923540b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
5 changes: 1 addition & 4 deletions rust/kernel/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ pub struct Buffer<'a> {

impl<'a> Buffer<'a> {
pub fn new(slice: &'a mut [u8]) -> Self {
Buffer {
slice,
pos: 0,
}
Buffer { slice, pos: 0 }
}

pub fn bytes_written(&self) -> usize {
Expand Down
30 changes: 20 additions & 10 deletions rust/kernel/module_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::fmt::Write;
/// Types that can be used for module parameters.
/// Note that displaying the type in `sysfs` will fail if `to_string` returns
/// more than `kernel::PAGE_SIZE` bytes (including an additional null terminator).
pub trait ModuleParam : core::fmt::Display + core::marker::Sized {
pub trait ModuleParam: core::fmt::Display + core::marker::Sized {
/// Setting this to `true` allows the parameter to be passed without an
/// argument (e.g. just `module.param` instead of `module.param=foo`).
const NOARG_ALLOWED: bool;
Expand All @@ -23,23 +23,33 @@ pub trait ModuleParam : core::fmt::Display + core::marker::Sized {
///
/// If `val` is non-null then it must point to a valid null-terminated
/// string. The `arg` field of `param` must be an instance of `Self`.
unsafe extern "C" fn set_param(val: *const crate::c_types::c_char, param: *const crate::bindings::kernel_param) -> crate::c_types::c_int {
let arg = if val.is_null() { None } else { Some(crate::c_types::c_string_bytes(val)) };
unsafe extern "C" fn set_param(
val: *const crate::c_types::c_char,
param: *const crate::bindings::kernel_param,
) -> crate::c_types::c_int {
let arg = if val.is_null() {
None
} else {
Some(crate::c_types::c_string_bytes(val))
};
match Self::try_from_param_arg(arg) {
Some(new_value) => {
let old_value = (*param).__bindgen_anon_1.arg as *mut Self;
let _ = core::ptr::replace(old_value, new_value);
0
}
None => crate::error::Error::EINVAL.to_kernel_errno()
None => crate::error::Error::EINVAL.to_kernel_errno(),
}
}

/// # Safety
///
/// `buf` must be a buffer of length at least `kernel::PAGE_SIZE` that is
/// writeable. The `arg` field of `param` must be an instance of `Self`.
unsafe extern "C" fn get_param(buf: *mut crate::c_types::c_char, param: *const crate::bindings::kernel_param) -> crate::c_types::c_int {
unsafe extern "C" fn get_param(
buf: *mut crate::c_types::c_char,
param: *const crate::bindings::kernel_param,
) -> crate::c_types::c_int {
let slice = core::slice::from_raw_parts_mut(buf as *mut u8, crate::PAGE_SIZE);
let mut buf = crate::buffer::Buffer::new(slice);
match write!(buf, "{}\0", *((*param).__bindgen_anon_1.arg as *mut Self)) {
Expand All @@ -61,7 +71,7 @@ pub trait ModuleParam : core::fmt::Display + core::marker::Sized {
/// otherwise are parsed as octal. Anything else is parsed as decimal. A
/// leading `+` or `-` is also permitted. Any string parsed by `kstrtol` or
/// `kstrtoul` will be successfully parsed.
trait ParseInt : Sized {
trait ParseInt: Sized {
fn from_str_radix(src: &str, radix: u32) -> Result<Self, core::num::ParseIntError>;
fn checked_neg(self) -> Option<Self>;

Expand Down Expand Up @@ -106,7 +116,7 @@ macro_rules! impl_parse_int {
self.checked_neg()
}
}
}
};
}

impl_parse_int!(i8);
Expand All @@ -131,7 +141,7 @@ macro_rules! impl_module_param {
<$ty as crate::module_param::ParseInt>::from_str(utf8)
}
}
}
};
}

macro_rules! make_param_ops {
Expand All @@ -143,11 +153,11 @@ macro_rules! make_param_ops {
} else {
0
},
set: Some(<$ty as crate::module_param::ModuleParam>::set_param),
set: Some(<$ty as crate::module_param::ModuleParam>::set_param),
get: Some(<$ty as crate::module_param::ModuleParam>::get_param),
free: Some(<$ty as crate::module_param::ModuleParam>::free),
};
}
};
}

impl_module_param!(i8);
Expand Down

0 comments on commit 923540b

Please sign in to comment.