From 66005b8b6fe09ebee0f2eee460cb1c1cad606a33 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 5 Aug 2021 16:57:48 +0800 Subject: [PATCH] xous: fix various clippy warnings This stylistic cleanup fixes several clippy warnings. Signed-off-by: Sean Cross --- xous-rs/src/arch/native.rs | 32 ++++++++++++++++---------------- xous-rs/src/definitions.rs | 11 ++++++++--- xous-rs/src/string.rs | 2 +- xous-rs/src/syscall.rs | 9 ++++++++- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/xous-rs/src/arch/native.rs b/xous-rs/src/arch/native.rs index d97ee8971..27461798c 100644 --- a/xous-rs/src/arch/native.rs +++ b/xous-rs/src/arch/native.rs @@ -143,8 +143,8 @@ pub fn process_to_args(call: usize, init: &ProcessInit) -> [usize; 8] { call, u32::from_le_bytes(init.key.0[0..4].try_into().unwrap()) as _, u32::from_le_bytes(init.key.0[4..8].try_into().unwrap()) as _, - u32::from_le_bytes(init.key.0[8..12].try_into().unwrap()) as _, - u32::from_le_bytes(init.key.0[12..16].try_into().unwrap()) as _, + 0, + 0, 0, 0, 0, @@ -167,7 +167,7 @@ pub fn create_thread_0_pre(f: &fn() -> U) -> core::result::Result( where U: Send + 'static, { - let start = unsafe { core::mem::transmute(f) }; + let start = f as usize; create_thread_n_post(start, 0, 0, 0, 0, thread_id) } @@ -189,7 +189,7 @@ pub fn create_thread_1_pre( where U: Send + 'static, { - let start = unsafe { core::mem::transmute(*f) }; + let start = *f as usize; create_thread_n_pre(start, arg1, &0, &0, &0) } @@ -201,7 +201,7 @@ pub fn create_thread_1_post( where U: Send + 'static, { - let start = unsafe { core::mem::transmute(f) }; + let start = f as usize; create_thread_n_post(start, arg1, 0, 0, 0, thread_id) } @@ -213,7 +213,7 @@ pub fn create_thread_2_pre( where U: Send + 'static, { - let start = unsafe { core::mem::transmute(*f) }; + let start = *f as usize; create_thread_n_pre(start, arg1, arg2, &0, &0) } @@ -226,7 +226,7 @@ pub fn create_thread_2_post( where U: Send + 'static, { - let start = unsafe { core::mem::transmute(f) }; + let start = f as usize; create_thread_n_post(start, arg1, arg2, 0, 0, thread_id) } @@ -239,7 +239,7 @@ pub fn create_thread_3_pre( where U: Send + 'static, { - let start = unsafe { core::mem::transmute(*f) }; + let start = *f as usize; create_thread_n_pre(start, arg1, arg2, arg3, &0) } @@ -253,7 +253,7 @@ pub fn create_thread_3_post( where U: Send + 'static, { - let start = unsafe { core::mem::transmute(f) }; + let start = f as usize; create_thread_n_post(start, arg1, arg2, arg3, 0, thread_id) } @@ -267,7 +267,7 @@ pub fn create_thread_4_pre( where U: Send + 'static, { - let start = unsafe { core::mem::transmute(*f) }; + let start = *f as usize; create_thread_n_pre(start, arg1, arg2, arg3, arg4) } @@ -282,7 +282,7 @@ pub fn create_thread_4_post( where U: Send + 'static, { - let start = unsafe { core::mem::transmute(f) }; + let start = f as usize; create_thread_n_post(start, arg1, arg2, arg3, arg4, thread_id) } @@ -295,7 +295,7 @@ where U: Send + 'static, { create_thread_n_pre( - unsafe { core::mem::transmute(*f) }, + *f as usize, unsafe { core::mem::transmute(arg) }, &0, &0, @@ -313,7 +313,7 @@ where U: Send + 'static, { create_thread_n_post( - unsafe { core::mem::transmute(f) }, + f as usize, unsafe { core::mem::transmute(&arg) }, 0, 0, @@ -321,9 +321,9 @@ where thread_id, ) // If we succeeded, the variable will be moved into the caller. Drop it from here. - .and_then(|f| { + .map(|f| { core::mem::forget(arg); - Ok(f) + f }) } diff --git a/xous-rs/src/definitions.rs b/xous-rs/src/definitions.rs index ed3796f7e..643fc7fb2 100644 --- a/xous-rs/src/definitions.rs +++ b/xous-rs/src/definitions.rs @@ -214,7 +214,7 @@ pub fn pid_from_usize(src: usize) -> core::result::Result { if src > u8::MAX as _ { return Err(Error::InvalidPID); } - Ok(PID::new(src as u8).ok_or(Error::InvalidPID)?) + PID::new(src as u8).ok_or(Error::InvalidPID) } #[repr(usize)] @@ -504,7 +504,7 @@ impl Message { pub fn memory_message(&self) -> Option<&MemoryMessage> { match self { - Message::MutableBorrow(mem) | Message::Borrow(mem) | Message::Move(mem) => Some(&mem), + Message::MutableBorrow(mem) | Message::Borrow(mem) | Message::Move(mem) => Some(mem), Message::BlockingScalar(_) | Message::Scalar(_) => None, } } @@ -519,7 +519,7 @@ impl Message { pub fn scalar_message(&self) -> Option<&ScalarMessage> { match self { Message::MutableBorrow(_) | Message::Borrow(_) | Message::Move(_) => None, - Message::BlockingScalar(scalar) | Message::Scalar(scalar) => Some(&scalar), + Message::BlockingScalar(scalar) | Message::Scalar(scalar) => Some(scalar), } } @@ -643,6 +643,11 @@ impl Drop for MessageEnvelope { } impl MemoryRange { + /// # Safety + /// + /// This allows for creating a `MemoryRange` from any arbitrary pointer, + /// so it is imperitive that this only be used to point to valid, page-aligned + /// ranges. pub unsafe fn new(addr: usize, size: usize) -> core::result::Result { assert!( addr != 0, diff --git a/xous-rs/src/string.rs b/xous-rs/src/string.rs index 44a8771c4..4da58ba35 100644 --- a/xous-rs/src/string.rs +++ b/xous-rs/src/string.rs @@ -65,7 +65,7 @@ impl String { /// awful, O(N) implementation because we have to iterate through the entire string /// and decode variable-length utf8 characters, until we can't. pub fn pop(&mut self) -> Option { - if self.len() < 1 { + if self.is_empty() { return None; } // first, make a copy of the string diff --git a/xous-rs/src/syscall.rs b/xous-rs/src/syscall.rs index a17dd81ec..4877a441c 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -1371,7 +1371,7 @@ pub fn wait_event() { } #[deprecated( - since = "0.2", + since = "0.2.0", note = "Please use create_thread_n() or create_thread()" )] pub fn create_thread_simple( @@ -1579,6 +1579,13 @@ pub fn destroy_server(sid: SID) -> core::result::Result<(), Error> { /// Disconnect the specified connection ID and mark it as free. This /// connection ID may be reused by the server in the future, so ensure /// no other threads are using the connection ID before disposing of it. +/// +/// # Safety +/// +/// This function must only be called when the connection is no longer in +/// use. Calling this function when the connection ID is in use will result +/// in kernel errors or, if the CID is reused, silent failures due to +/// messages going to the wrong server. pub unsafe fn disconnect(cid: CID) -> core::result::Result<(), Error> { rsyscall(SysCall::Disconnect(cid)).and_then(|result| { if let Result::Ok = result {