diff --git a/ctru-rs/src/console.rs b/ctru-rs/src/console.rs index e0e6277..e640109 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -37,6 +37,7 @@ impl Console { /// The first two arguments are the desired coordinates of the top-left corner /// of the console, and the second pair is the new width and height /// + /// # Safety /// This function is unsafe because it does not validate that the input will produce /// a console that actually fits on the screen pub unsafe fn set_window(&mut self, x: i32, y: i32, width: i32, height: i32) { diff --git a/ctru-rs/src/services/fs.rs b/ctru-rs/src/services/fs.rs index c4e95e0..848e8a5 100644 --- a/ctru-rs/src/services/fs.rs +++ b/ctru-rs/src/services/fs.rs @@ -233,7 +233,7 @@ pub struct Metadata { /// .open("foo.txt") /// .unwrap(); /// ``` -#[derive(Clone)] +#[derive(Clone, Default)] pub struct OpenOptions { read: bool, write: bool, @@ -318,10 +318,7 @@ impl Fs { if r < 0 { Err(crate::Error::from(r)) } else { - Ok(Archive { - handle: handle, - id: id, - }) + Ok(Archive { handle, id }) } } } @@ -430,10 +427,7 @@ impl File { crate::Error::from(r), )) } else { - Ok(Metadata { - attributes: 0, - size: size, - }) + Ok(Metadata { attributes: 0, size }) } } } @@ -496,6 +490,9 @@ impl Metadata { /// Returns the size, in bytes, this metadata is for. /// /// Directories return size = 0. + // We don't want an is_empty function because directories always have a + // zero size. + #[allow(clippy::len_without_is_empty)] pub fn len(&self) -> u64 { self.size } @@ -506,14 +503,7 @@ impl OpenOptions { /// /// All options are initially set to `false` pub fn new() -> OpenOptions { - OpenOptions { - read: false, - write: false, - append: false, - truncate: false, - create: false, - arch_handle: 0, - } + Self::default() } /// Sets the option for read access. @@ -753,8 +743,8 @@ pub fn create_dir_all>(arch: &Archive, path: P) -> IoResult<()> { /// Given a path, query the file system to get information about a file, directory, etc pub fn metadata>(arch: &Archive, path: P) -> IoResult { - let maybe_file = File::open(&arch, path.as_ref()); - let maybe_dir = read_dir(&arch, path.as_ref()); + let maybe_file = File::open(arch, path.as_ref()); + let maybe_dir = read_dir(arch, path.as_ref()); match (maybe_file, maybe_dir) { (Ok(file), _) => file.metadata(), (_, Ok(_dir)) => Ok(Metadata { @@ -828,8 +818,8 @@ pub fn read_dir>(arch: &Archive, path: P) -> IoResult { } else { Ok(ReadDir { handle: Dir(handle), - root: root, - arch: arch, + root, + arch, }) } } @@ -897,7 +887,7 @@ fn to_utf16(path: &Path) -> WideCString { } // Adapted from sys/windows/fs.rs in libstd -fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] { +fn truncate_utf16_at_nul(v: &[u16]) -> &[u16] { match v.iter().position(|c| *c == 0) { // don't include the 0 Some(i) => &v[..i], @@ -933,7 +923,7 @@ unsafe fn read_to_end_uninitialized(r: &mut dyn Read, buf: &mut Vec) -> IoRe } let buf_slice = slice::from_raw_parts_mut( - buf.as_mut_ptr().offset(buf.len() as isize), + buf.as_mut_ptr().add(buf.len()), buf.capacity() - buf.len(), ); diff --git a/ctru-rs/src/services/hid.rs b/ctru-rs/src/services/hid.rs index 7f27d80..18790f1 100644 --- a/ctru-rs/src/services/hid.rs +++ b/ctru-rs/src/services/hid.rs @@ -106,10 +106,16 @@ impl Hid { } } +impl Default for TouchPosition { + fn default() -> Self { + TouchPosition(ctru_sys::touchPosition { px: 0, py: 0 }) + } +} + impl TouchPosition { /// Create a new TouchPosition instance. pub fn new() -> Self { - TouchPosition(ctru_sys::touchPosition { px: 0, py: 0 }) + Self::default() } /// Returns the current touch position in pixels. @@ -121,10 +127,16 @@ impl TouchPosition { } } +impl Default for CirclePosition { + fn default() -> Self { + CirclePosition(ctru_sys::circlePosition { dx: 0, dy: 0 }) + } +} + impl CirclePosition { /// Create a new CirclePosition instance. pub fn new() -> Self { - CirclePosition(ctru_sys::circlePosition { dx: 0, dy: 0 }) + Self::default() } /// Returns the current circle pad position in (x, y) form. diff --git a/ctru-rs/src/thread.rs b/ctru-rs/src/thread.rs index cf2507b..e12a554 100644 --- a/ctru-rs/src/thread.rs +++ b/ctru-rs/src/thread.rs @@ -49,7 +49,7 @@ use std::time::Duration; /// Thread factory, which can be used in order to configure the properties of /// a new thread. -#[derive(Debug)] +#[derive(Debug, Default)] pub struct Builder { // The size of the stack for the spawned thread in bytes stack_size: Option, @@ -78,11 +78,7 @@ impl Builder { /// handler.join().unwrap(); /// ``` pub fn new() -> Builder { - Builder { - stack_size: None, - priority: None, - affinity: None, - } + Self::default() } /// Sets the size of the stack (in bytes) for the new thread. @@ -102,6 +98,7 @@ impl Builder { /// ``` /// /// [stack-size]: ./index.html#stack-size + #[must_use] pub fn stack_size(mut self, size: usize) -> Builder { self.stack_size = Some(size); self @@ -112,6 +109,7 @@ impl Builder { /// Low values gives the thread higher priority. For userland apps, this has /// to be within the range of 0x18 to 0x3F inclusive. The main thread usually /// has a priority of 0x30, but not always. + #[must_use] pub fn priority(mut self, priority: i32) -> Builder { self.priority = Some(priority); self @@ -134,6 +132,7 @@ impl Builder { /// /// Processes in the BASE memory region can always create threads on /// processors #2 and #3. + #[must_use] pub fn affinity(mut self, affinity: i32) -> Builder { self.affinity = Some(affinity); self @@ -188,7 +187,7 @@ impl Builder { // If no priority value is specified, spawn with the same // priority as the parent thread - let priority = priority.unwrap_or_else(|| imp::Thread::priority()); + let priority = priority.unwrap_or_else(imp::Thread::priority); // If no affinity is specified, spawn on the default core (determined by // the application's Exheader) @@ -434,6 +433,8 @@ pub fn park() { } loop { m = thread.inner.cvar.wait(m).unwrap(); + + #[allow(clippy::single_match)] match thread .inner .state @@ -891,7 +892,6 @@ mod imp { use std::convert::TryInto; use std::io; use std::mem; - use std::ptr; use std::time::Duration; use libc; @@ -929,11 +929,11 @@ mod imp { false, ); - return if handle == ptr::null_mut() { + return if handle.is_null() { Err(io::Error::from_raw_os_error(libc::EAGAIN)) } else { mem::forget(p); // ownership passed to the new thread - Ok(Thread { handle: handle }) + Ok(Thread { handle }) }; extern "C" fn thread_func(start: *mut libc::c_void) { diff --git a/ctru-sys/src/lib.rs b/ctru-sys/src/lib.rs index ccc6540..dc32370 100644 --- a/ctru-sys/src/lib.rs +++ b/ctru-sys/src/lib.rs @@ -1,6 +1,7 @@ #![allow(non_upper_case_globals)] #![allow(non_camel_case_types)] #![allow(non_snake_case)] +#![allow(clippy::all)] #![no_std] include!("bindings.rs");