Files
littlefs2/tests/test_serde.rs
T
Robin Krahl 9fdeadcf0f Remove trait and result arguments from macros
The const_ram_storage! and ram_storage! macros used to have trait and
result arguments to set the storage trait to implement and the result
type to use.  As this defaulted to relative paths, it made the result of
the macro dependent on the context and could cause confusion.  At the
same time, there is no real use case for substituting these types.

This patch removes the arguments for good and just uses
$crate::driver::Storage and $crate::io::Result instead.
2025-02-28 13:18:15 +01:00

53 lines
1.5 KiB
Rust

use littlefs2::{fs::Filesystem, path::Path, ram_storage};
use serde::{Deserialize, Serialize};
use ssmarshal::{deserialize, serialize};
#[derive(Serialize, Deserialize, PartialEq, Debug, Default /*, Desse, DesseSized*/)]
struct Entity {
z: [u8; 16],
// x: u32,
x: f32,
y: u64,
}
ram_storage!(tiny);
#[test]
fn main() {
let mut ram = Ram::default();
let mut storage = RamStorage::new(&mut ram);
Filesystem::format(&mut storage).unwrap();
let mut alloc = Filesystem::allocate();
let fs = Filesystem::mount(&mut alloc, &mut storage).unwrap();
let entity = Entity::default();
let mut buf = [0u8; core::mem::size_of::<Entity>()];
assert_eq!(buf.len(), 32);
let size = serialize(&mut buf, &entity).unwrap();
assert_eq!(size, 28);
fs.write(
Path::from_bytes_with_nul(b"entity.bin\0").unwrap(),
&buf[..size],
)
.unwrap();
fs.open_file_and_then(
Path::from_bytes_with_nul(b"entity.bin\0").unwrap(),
|file| file.read(&mut buf),
)
.unwrap();
// let mut alloc = File::allocate();
// let mut file = File::create("entity.bin", &mut alloc, &mut fs, &mut storage).unwrap();
// file.write(&mut fs, &mut storage, &buf[..size]).unwrap();
// file.sync(&mut fs, &mut storage).unwrap();
// let mut file = File::open("entity.bin", &mut alloc, &mut fs, &mut storage).unwrap();
// file.read(&mut fs, &mut storage, &mut buf).unwrap();
let read_entity: Entity = deserialize(&buf).unwrap().0;
assert_eq!(read_entity, entity);
}