Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compile errors if c_int is not i32 on some platforms #68

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ impl<Storage: driver::Storage> 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);
}

Expand Down Expand Up @@ -549,7 +549,7 @@ impl<Storage: driver::Storage> 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
Expand Down Expand Up @@ -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),
);

Expand Down Expand Up @@ -969,7 +969,7 @@ impl<S: driver::Storage> 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)
Expand Down
17 changes: 13 additions & 4 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,18 @@ impl From<Error> 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<i32> 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,
Expand All @@ -203,11 +206,17 @@ impl From<i32> for Error {
}
}

impl From<i16> for Error {
fn from(value: i16) -> Self {
Self::from(value as i32)
}
}

pub fn error_code_from<T>(result: Result<T>) -> ll::lfs_error {
result.err().unwrap_or(Error::Success).into()
}

pub fn result_from<T>(return_value: T, error_code: ll::lfs_error) -> Result<T> {
pub fn result_from<T, E: Into<Error>>(return_value: T, error_code: E) -> Result<T> {
let error: Error = error_code.into();
match error {
Error::Success => Ok(return_value),
Expand Down