From 7efc289fd2029283dfd043a5f35ec6941ce48326 Mon Sep 17 00:00:00 2001 From: bytedream Date: Wed, 10 Jul 2024 11:15:00 +0200 Subject: [PATCH] Fix compile errors if c_int is not i32 --- src/fs.rs | 8 ++++---- src/io.rs | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/fs.rs b/src/fs.rs index 2f8be1706..b49c96169 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -467,7 +467,7 @@ impl Filesystem<'_, Storage> { attribute.size = cmp::min(attr_max, return_code as u32) as usize; return Ok(Some(attribute)); } - if return_code == ll::lfs_error_LFS_ERR_NOATTR { + if matches!(return_code.into(), Error::NoAttribute) { return Ok(None); } @@ -549,7 +549,7 @@ impl Filesystem<'_, Storage> { /// C callback interface used by LittleFS to sync data with the lower level interface below the /// filesystem. Note that this function currently does nothing. - extern "C" fn lfs_config_sync(_c: *const ll::lfs_config) -> i32 { + extern "C" fn lfs_config_sync(_c: *const ll::lfs_config) -> c_int { // println!("in lfs_config_sync"); // Do nothing; we presume that data is synchronized. 0 @@ -851,7 +851,7 @@ impl OpenOptions { &mut fs.alloc.borrow_mut().state, addr_of_mut!(alloc.state), path.as_ptr(), - self.0.bits() as i32, + self.0.bits() as c_int, addr_of!(alloc.config), ); @@ -969,7 +969,7 @@ impl io::Seek for File<'_, '_, S> { &mut self.fs.alloc.borrow_mut().state, addr_of_mut!((*(*self.alloc.borrow_mut())).state), pos.off(), - pos.whence(), + pos.whence() as c_int, ) }; io::result_from(return_code as usize, return_code) diff --git a/src/io.rs b/src/io.rs index b8ad5fdc1..db67382c5 100644 --- a/src/io.rs +++ b/src/io.rs @@ -173,15 +173,18 @@ impl From for ll::lfs_error { Error::NoMemory => ll::lfs_error_LFS_ERR_NOMEM, Error::NoAttribute => ll::lfs_error_LFS_ERR_NOATTR, Error::FilenameTooLong => ll::lfs_error_LFS_ERR_NAMETOOLONG, - Error::Unknown(error_code) => error_code, + Error::Unknown(error_code) => error_code as ll::lfs_error, } } } impl From for Error { fn from(error_code: i32) -> Error { - match error_code { - n if n >= 0 => Error::Success, + if error_code >= 0 { + return Error::Success; + }; + + match error_code as ll::lfs_error { // negative codes ll::lfs_error_LFS_ERR_IO => Error::Io, ll::lfs_error_LFS_ERR_CORRUPT => Error::Corruption, @@ -203,11 +206,17 @@ impl From for Error { } } +impl From for Error { + fn from(value: i16) -> Self { + Self::from(value as i32) + } +} + pub fn error_code_from(result: Result) -> ll::lfs_error { result.err().unwrap_or(Error::Success).into() } -pub fn result_from(return_value: T, error_code: ll::lfs_error) -> Result { +pub fn result_from>(return_value: T, error_code: E) -> Result { let error: Error = error_code.into(); match error { Error::Success => Ok(return_value),