diff --git a/.rustfmt.toml b/.rustfmt.toml index 06a65ad..efd9f8e 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -8,7 +8,6 @@ error_on_line_overflow = true # Override the default formatting style. # See https://internals.rust-lang.org/t/running-rustfmt-on-rust-lang-rust-and-other-rust-lang-repositories/8732/81. use_small_heuristics = "Max" -# This is the default of 2024 edition https://github.com/rust-lang/rust/pull/114764. # This is unstable (tracking issue: https://github.com/rust-lang/rustfmt/issues/3370) overflow_delimited_expr = true # This is unstable (tracking issue: https://github.com/rust-lang/rustfmt/issues/4991). @@ -27,6 +26,7 @@ use_try_shorthand = true # Set the default settings again to always apply the proper formatting without # being affected by the editor settings. edition = "2021" +style_edition = "2024" hard_tabs = false newline_style = "Unix" tab_spaces = 4 diff --git a/build.rs b/build.rs index 3d22267..8212f7c 100644 --- a/build.rs +++ b/build.rs @@ -237,4 +237,4 @@ mod version { } } } -use self::version::{rustc_version, Version}; +use self::version::{Version, rustc_version}; diff --git a/src/c_str.rs b/src/c_str.rs index c480cae..6c5a04f 100644 --- a/src/c_str.rs +++ b/src/c_str.rs @@ -14,7 +14,8 @@ /// use semihosting::{c, fs}; /// /// fs::write(c!("a.txt"), "abc")?; -/// fs::write(c!(concat!("b", ".txt")), "def")?; // concat! in c! is also supported +/// // concat! in c! is also supported +/// fs::write(c!(concat!("b", ".txt")), "def")?; /// # Ok::<(), semihosting::io::Error>(()) /// ``` /// diff --git a/src/experimental/env.rs b/src/experimental/env.rs index ebff39a..1f1287e 100644 --- a/src/experimental/env.rs +++ b/src/experimental/env.rs @@ -32,7 +32,7 @@ impl fmt::Debug for Args { } mod sys { - pub(crate) use self::imp::{args_bytes, ArgsBytes}; + pub(crate) use self::imp::{ArgsBytes, args_bytes}; const NUL: u8 = b'\0'; @@ -87,10 +87,10 @@ mod sys { mod imp { use core::cell::Cell; - use super::{next_from_cmdline, NUL}; + use super::{NUL, next_from_cmdline}; use crate::{ io, - sys::arm_compat::{sys_get_cmdline, CommandLine}, + sys::arm_compat::{CommandLine, sys_get_cmdline}, }; pub(crate) struct ArgsBytes { @@ -128,7 +128,7 @@ mod sys { use core::cell::Cell; - use super::{next_from_cmdline, NUL}; + use super::{NUL, next_from_cmdline}; use crate::{ io, sys::mips::{mips_argc, mips_argn, mips_argnlen}, diff --git a/src/experimental/time.rs b/src/experimental/time.rs index 6eb8c58..152b6c0 100644 --- a/src/experimental/time.rs +++ b/src/experimental/time.rs @@ -173,11 +173,7 @@ mod sys { let (res, overflowed) = this.overflowing_add(rhs); (res, overflowed ^ (rhs < 0)) }; - if b { - None - } else { - Some(a) - } + if b { None } else { Some(a) } } let mut secs = checked_add_unsigned(self.tv_sec, other.as_secs())?; @@ -201,11 +197,7 @@ mod sys { let (res, overflowed) = this.overflowing_sub(rhs); (res, overflowed ^ (rhs < 0)) }; - if b { - None - } else { - Some(a) - } + if b { None } else { Some(a) } } let mut secs = checked_sub_unsigned(self.tv_sec, other.as_secs())?; diff --git a/src/fs.rs b/src/fs.rs index ec0fdf6..b2035dc 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -11,8 +11,10 @@ //! ```no_run //! use semihosting::{c, fs}; //! -//! fs::write(c"a.txt", "abc")?; // with C string literal -//! fs::write(c!("b.txt"), "123")?; // with c! macro +//! // with C string literal +//! fs::write(c"a.txt", "abc")?; +//! // with c! macro +//! fs::write(c!("b.txt"), "123")?; //! # Ok::<(), semihosting::io::Error>(()) //! ``` //! diff --git a/src/io/impls.rs b/src/io/impls.rs index e8bd832..7a55adf 100644 --- a/src/io/impls.rs +++ b/src/io/impls.rs @@ -330,11 +330,7 @@ impl Write for &mut [u8] { // } #[inline] fn write_all(&mut self, data: &[u8]) -> io::Result<()> { - if self.write(data)? == data.len() { - Ok(()) - } else { - Err(io::Error::WRITE_ALL_EOF) - } + if self.write(data)? == data.len() { Ok(()) } else { Err(io::Error::WRITE_ALL_EOF) } } #[inline] fn flush(&mut self) -> io::Result<()> { diff --git a/src/io/mod.rs b/src/io/mod.rs index 7c018be..aaa398f 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -18,7 +18,7 @@ mod error; mod impls; #[cfg(feature = "stdio")] -pub use self::stdio::{stderr, stdin, stdout, IsTerminal, Stderr, Stdin, Stdout}; +pub use self::stdio::{IsTerminal, Stderr, Stdin, Stdout, stderr, stdin, stdout}; #[cfg(feature = "stdio")] #[cfg_attr(docsrs, doc(cfg(feature = "stdio")))] mod stdio; @@ -43,11 +43,7 @@ pub(crate) fn default_read_exact(this: &mut R, mut buf: &mut [ Err(e) => return Err(e), } } - if buf.is_empty() { - Ok(()) - } else { - Err(Error::READ_EXACT_EOF) - } + if buf.is_empty() { Ok(()) } else { Err(Error::READ_EXACT_EOF) } } /// The `no_std` subset of `std::io::Read`. diff --git a/src/panicking.rs b/src/panicking.rs index ac63978..3cfd9ae 100644 --- a/src/panicking.rs +++ b/src/panicking.rs @@ -24,7 +24,7 @@ fn _panic(_info: &core::panic::PanicInfo<'_>) -> ! { { use core::{ffi::c_void, ptr}; - use unwinding::abi::{UnwindContext, UnwindReasonCode, _Unwind_Backtrace, _Unwind_GetIP}; + use unwinding::abi::{_Unwind_Backtrace, _Unwind_GetIP, UnwindContext, UnwindReasonCode}; extern "C" fn callback( unwind_ctx: &UnwindContext<'_>, diff --git a/src/sys/arm_compat/fs.rs b/src/sys/arm_compat/fs.rs index 157dd4d..da7da6d 100644 --- a/src/sys/arm_compat/fs.rs +++ b/src/sys/arm_compat/fs.rs @@ -2,7 +2,7 @@ use core::ffi::CStr; -use super::{errno, sys_flen, sys_open, sys_seek, OpenMode}; +use super::{OpenMode, errno, sys_flen, sys_open, sys_seek}; pub(crate) use super::{sys_remove as unlink, sys_rename as rename}; use crate::{ fd::{BorrowedFd, OwnedFd}, diff --git a/src/sys/arm_compat/mod.rs b/src/sys/arm_compat/mod.rs index 3647434..33ced8b 100644 --- a/src/sys/arm_compat/mod.rs +++ b/src/sys/arm_compat/mod.rs @@ -17,11 +17,11 @@ pub(crate) mod fs; pub mod syscall; use core::{ - ffi::{c_void, CStr}, + ffi::{CStr, c_void}, mem::{self, MaybeUninit}, }; -use self::syscall::{syscall, syscall0, syscall_readonly, OperationNumber, ParamRegR, ParamRegW}; +use self::syscall::{OperationNumber, ParamRegR, ParamRegW, syscall, syscall_readonly, syscall0}; use crate::{ fd::{BorrowedFd, OwnedFd, RawFd}, io::{Error, RawOsError, Result}, @@ -118,11 +118,7 @@ pub struct CommandLine { /// [SYS_CLOCK (0x10)](https://github.com/ARM-software/abi-aa/blob/2024Q3/semihosting/semihosting.rst#sys-clock-0x10) pub fn sys_clock() -> Result { let res = unsafe { syscall0(OperationNumber::SYS_CLOCK) }; - if res.int() == -1 { - Err(Error::from_raw_os_error(sys_errno())) - } else { - Ok(res.usize()) - } + if res.int() == -1 { Err(Error::from_raw_os_error(sys_errno())) } else { Ok(res.usize()) } } /// [SYS_CLOSE (0x02)](https://github.com/ARM-software/abi-aa/blob/2024Q3/semihosting/semihosting.rst#sys-close-0x02) @@ -200,11 +196,7 @@ pub fn sys_exit_extended(reason: ExitReason, subcode: usize) { pub fn sys_flen(fd: BorrowedFd<'_>) -> Result { let args = [ParamRegR::fd(fd)]; let res = unsafe { syscall_readonly(OperationNumber::SYS_FLEN, ParamRegR::block(&args)) }; - if res.int() == -1 { - Err(Error::from_raw_os_error(sys_errno())) - } else { - Ok(res.usize()) - } + if res.int() == -1 { Err(Error::from_raw_os_error(sys_errno())) } else { Ok(res.usize()) } } /// [SYS_GET_CMDLINE (0x15)](https://github.com/ARM-software/abi-aa/blob/2024Q3/semihosting/semihosting.rst#sys-get-cmdline-0x15) @@ -330,11 +322,7 @@ pub fn sys_readc() -> u8 { pub fn sys_remove(path: &CStr) -> Result<()> { let args = [ParamRegR::c_str(path), ParamRegR::usize(path.to_bytes().len())]; let res = unsafe { syscall_readonly(OperationNumber::SYS_REMOVE, ParamRegR::block(&args)) }; - if res.usize() == 0 { - Ok(()) - } else { - Err(Error::from_raw_os_error(sys_errno())) - } + if res.usize() == 0 { Ok(()) } else { Err(Error::from_raw_os_error(sys_errno())) } } /// [SYS_RENAME (0x0F)](https://github.com/ARM-software/abi-aa/blob/2024Q3/semihosting/semihosting.rst#sys-rename-0x0f) @@ -346,11 +334,7 @@ pub fn sys_rename(from: &CStr, to: &CStr) -> Result<()> { ParamRegR::usize(to.to_bytes().len()), ]; let res = unsafe { syscall_readonly(OperationNumber::SYS_RENAME, ParamRegR::block(&args)) }; - if res.usize() == 0 { - Ok(()) - } else { - Err(Error::from_raw_os_error(sys_errno())) - } + if res.usize() == 0 { Ok(()) } else { Err(Error::from_raw_os_error(sys_errno())) } } // TODO: resolve safety @@ -359,11 +343,7 @@ pub fn sys_rename(from: &CStr, to: &CStr) -> Result<()> { pub unsafe fn sys_seek(fd: BorrowedFd<'_>, abs_pos: usize) -> Result<()> { let args = [ParamRegR::fd(fd), ParamRegR::usize(abs_pos)]; let res = unsafe { syscall_readonly(OperationNumber::SYS_SEEK, ParamRegR::block(&args)) }; - if res.usize() == 0 { - Ok(()) - } else { - Err(Error::from_raw_os_error(sys_errno())) - } + if res.usize() == 0 { Ok(()) } else { Err(Error::from_raw_os_error(sys_errno())) } } /// [SYS_SYSTEM (0x12)](https://github.com/ARM-software/abi-aa/blob/2024Q3/semihosting/semihosting.rst#sys-system-0x12) @@ -376,11 +356,7 @@ pub fn sys_system(cmd: &CStr) -> usize { /// [SYS_TICKFREQ (0x31)](https://github.com/ARM-software/abi-aa/blob/2024Q3/semihosting/semihosting.rst#sys-tickfreq-0x31) pub fn sys_tickfreq() -> Result { let res = unsafe { syscall0(OperationNumber::SYS_TICKFREQ) }; - if res.int() == -1 { - Err(Error::from_raw_os_error(sys_errno())) - } else { - Ok(res.usize()) - } + if res.int() == -1 { Err(Error::from_raw_os_error(sys_errno())) } else { Ok(res.usize()) } } /// [SYS_TIME (0x11)](https://github.com/ARM-software/abi-aa/blob/2024Q3/semihosting/semihosting.rst#sys-time-0x11) diff --git a/src/sys/mips/fs.rs b/src/sys/mips/fs.rs index fe45cb0..d55e58f 100644 --- a/src/sys/mips/fs.rs +++ b/src/sys/mips/fs.rs @@ -3,8 +3,8 @@ use core::ffi::CStr; use super::{ - errno, mips_fstat, mips_lseek, mips_open, SeekWhence::SEEK_SET, O_APPEND, O_CREAT, O_EXCL, - O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY, + O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY, SeekWhence::SEEK_SET, errno, + mips_fstat, mips_lseek, mips_open, }; pub(crate) use super::{mips_fstat as metadata, mips_unlink as unlink, uhi_stat as Metadata}; use crate::{ diff --git a/src/sys/mips/mod.rs b/src/sys/mips/mod.rs index 6718006..8e6e296 100644 --- a/src/sys/mips/mod.rs +++ b/src/sys/mips/mod.rs @@ -19,8 +19,8 @@ use core::{ }; use self::syscall::{ - syscall0, syscall1_readonly, syscall2, syscall2_readonly, syscall3, syscall3_readonly, - syscall4, syscall4_readonly, OperationCode, ParamRegR, ParamRegW, RetReg, + OperationCode, ParamRegR, ParamRegW, RetReg, syscall0, syscall1_readonly, syscall2, + syscall2_readonly, syscall3, syscall3_readonly, syscall4, syscall4_readonly, }; use crate::{ fd::{BorrowedFd, OwnedFd, RawFd}, @@ -198,32 +198,20 @@ pub unsafe fn mips_lseek(fd: BorrowedFd<'_>, offset: isize, whence: SeekWhence) ParamRegR::usize(whence as usize), ) }; - if res.int() == -1 { - Err(from_errno(errno)) - } else { - Ok(res.usize()) - } + if res.int() == -1 { Err(from_errno(errno)) } else { Ok(res.usize()) } } pub fn mips_unlink(path: &CStr) -> Result<()> { let (res, errno) = unsafe { syscall1_readonly(OperationCode::UHI_UNLINK, ParamRegR::c_str(path)) }; - if res.usize() == 0 { - Ok(()) - } else { - Err(from_errno(errno)) - } + if res.usize() == 0 { Ok(()) } else { Err(from_errno(errno)) } } pub fn mips_fstat(fd: BorrowedFd<'_>) -> Result { let mut buf: uhi_stat = unsafe { mem::zeroed() }; let (res, errno) = unsafe { syscall2(OperationCode::UHI_FSTAT, ParamRegW::fd(fd), ParamRegW::ref_(&mut buf)) }; - if res.usize() == 0 { - Ok(buf) - } else { - Err(from_errno(errno)) - } + if res.usize() == 0 { Ok(buf) } else { Err(from_errno(errno)) } } #[cfg(feature = "stdio")] pub(crate) fn is_terminal(fd: BorrowedFd<'_>) -> bool { @@ -240,11 +228,7 @@ pub fn mips_argc() -> usize { pub fn mips_argnlen(n: usize) -> Result { let (res, errno) = unsafe { syscall1_readonly(OperationCode::UHI_ARGNLEN, ParamRegR::usize(n)) }; - if res.int() == -1 { - Err(from_errno(errno)) - } else { - Ok(res.usize()) - } + if res.int() == -1 { Err(from_errno(errno)) } else { Ok(res.usize()) } } pub unsafe fn mips_argn(n: usize, buf: *mut u8) -> Result<()> { diff --git a/src/sys/mod.rs b/src/sys/mod.rs index 8e880a1..b1c3a63 100644 --- a/src/sys/mod.rs +++ b/src/sys/mod.rs @@ -68,7 +68,7 @@ mod reg; #[cfg(feature = "fs")] pub(crate) use self::arch::fs; #[cfg(feature = "stdio")] -pub(crate) use self::arch::{is_terminal, stderr, stdin, stdout, StdioFd}; +pub(crate) use self::arch::{StdioFd, is_terminal, stderr, stdin, stdout}; #[cfg(any(feature = "stdio", feature = "fs"))] pub(crate) use self::arch::{read, write}; pub(crate) use self::{ diff --git a/src/sys/reg.rs b/src/sys/reg.rs index 5900150..35de2d3 100644 --- a/src/sys/reg.rs +++ b/src/sys/reg.rs @@ -1,7 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT use core::{ - ffi::{c_int, c_void, CStr}, + ffi::{CStr, c_int, c_void}, marker::PhantomData, };