Skip to content

Commit

Permalink
core: Remove generic-array dependency
Browse files Browse the repository at this point in the history
By refactoring the Attribute struct using const generics, we can get rid
of the (public) generic-array dependency.
  • Loading branch information
robin-nitrokey committed Aug 8, 2024
1 parent 6178eb0 commit 79eb4d4
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Removed `Path::from_bytes_with_nul_unchecked`. Use `CStr::from_bytes_with_nul_unchecked` and `Path::from_cstr_unchecked` instead.
- Removed `From<littlefs2::path::Error> for littlefs2::io::Error`.
- Removed `object_safe::OpenOptionsCallback`.
- Removed `consts::ATTRBYTES_MAX_TYPE`.

[#47]: https://github.com/trussed-dev/littlefs2/pull/47
[#57]: https://github.com/trussed-dev/littlefs2/pull/57
Expand Down
1 change: 0 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ repository.workspace = true

[dependencies]
bitflags = "2.6.0"
generic-array = "0.14"
heapless = "0.7"
serde = { version = "1", default-features = false, features = ["derive"], optional = true }

Expand Down
2 changes: 0 additions & 2 deletions core/src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
pub const PATH_MAX: usize = 255;
pub const PATH_MAX_PLUS_ONE: usize = PATH_MAX + 1;
pub const ATTRBYTES_MAX: u32 = 1_022;
#[allow(non_camel_case_types)]
pub type ATTRBYTES_MAX_TYPE = generic_array::typenum::consts::U1022;
21 changes: 16 additions & 5 deletions core/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use bitflags::bitflags;

use crate::path::{Path, PathBuf};

pub type Bytes<SIZE> = generic_array::GenericArray<u8, SIZE>;

bitflags! {
/// Definition of file open flags which can be mixed and matched as appropriate. These definitions
/// are reminiscent of the ones defined by POSIX.
Expand Down Expand Up @@ -92,15 +90,16 @@ impl Metadata {
/// [`Filesystem::clear_attribute`](struct.Filesystem.html#method.clear_attribute).
pub struct Attribute {
id: u8,
pub data: Bytes<crate::consts::ATTRBYTES_MAX_TYPE>,
pub size: usize,
data: [u8; crate::consts::ATTRBYTES_MAX as _],
// invariant: size <= data.len()
size: usize,
}

impl Attribute {
pub fn new(id: u8) -> Self {
Attribute {
id,
data: Default::default(),
data: [0; crate::consts::ATTRBYTES_MAX as _],
size: 0,
}
}
Expand All @@ -115,6 +114,10 @@ impl Attribute {
&self.data[..len]
}

pub fn size(&self) -> usize {
self.size
}

pub fn set_data(&mut self, data: &[u8]) -> &mut Self {
let attr_max = crate::consts::ATTRBYTES_MAX as _;
let len = cmp::min(attr_max, data.len());
Expand All @@ -125,6 +128,14 @@ impl Attribute {
}
self
}

pub fn buffer_mut(&mut self) -> &mut [u8] {
&mut self.data
}

pub fn set_size(&mut self, size: usize) {
self.size = cmp::min(size, self.data.len());
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod io;
mod object_safe;
mod path;

pub use consts::{ATTRBYTES_MAX, ATTRBYTES_MAX_TYPE, PATH_MAX, PATH_MAX_PLUS_ONE};
pub use consts::{ATTRBYTES_MAX, PATH_MAX, PATH_MAX_PLUS_ONE};
pub use fs::{Attribute, DirEntry, FileOpenFlags, FileType, Metadata};
pub use io::{Error, OpenSeekFrom, Read, Result, Seek, SeekFrom, Write};
pub use object_safe::{DirEntriesCallback, DynFile, DynFilesystem, FileCallback, Predicate};
Expand Down
2 changes: 1 addition & 1 deletion src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// Re-export of `typenum::consts`.
pub use generic_array::typenum::consts::*;

pub use littlefs2_core::{ATTRBYTES_MAX, ATTRBYTES_MAX_TYPE, PATH_MAX, PATH_MAX_PLUS_ONE};
pub use littlefs2_core::{ATTRBYTES_MAX, PATH_MAX, PATH_MAX_PLUS_ONE};

pub const FILENAME_MAX_PLUS_ONE: u32 = 255 + 1;
pub const FILEBYTES_MAX: u32 = crate::ll::LFS_FILE_MAX as _;
Expand Down
10 changes: 5 additions & 5 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::ptr::addr_of;
use core::ptr::addr_of_mut;
use core::{
cell::{RefCell, UnsafeCell},
cmp, mem, slice,
mem, slice,
};
use generic_array::typenum::marker_traits::Unsigned;
use littlefs2_sys as ll;
Expand Down Expand Up @@ -425,13 +425,13 @@ impl<Storage: driver::Storage> Filesystem<'_, Storage> {
&mut self.alloc.borrow_mut().state,
path.as_ptr(),
id,
&mut attribute.data as *mut _ as *mut c_void,
attribute.buffer_mut() as *mut _ as *mut c_void,
attr_max,
)
};

if return_code >= 0 {
attribute.size = cmp::min(attr_max, return_code as u32) as usize;
attribute.set_size(return_code as usize);
return Ok(Some(attribute));
}
if return_code == ll::lfs_error_LFS_ERR_NOATTR {
Expand All @@ -457,8 +457,8 @@ impl<Storage: driver::Storage> Filesystem<'_, Storage> {
&mut self.alloc.borrow_mut().state,
path.as_ptr(),
attribute.id(),
&attribute.data as *const _ as *const c_void,
attribute.size as u32,
attribute.data() as *const _ as *const c_void,
attribute.size() as u32,
)
};

Expand Down

0 comments on commit 79eb4d4

Please sign in to comment.