1use embedded_storage::ReadStorage;
2use embedded_storage::Storage;
3
4pub struct InMemoryStorage<const N: usize> {
5 buf: [u8; N],
6}
7
8impl<const N: usize> Default for InMemoryStorage<N> {
9 fn default() -> Self {
10 Self { buf: [0u8; N] }
11 }
12}
13
14impl<const N: usize> ReadStorage for InMemoryStorage<N> {
15 type Error = ();
16
17 fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
18 let offset = offset as usize;
19 let size = offset + bytes.len();
20 bytes.copy_from_slice(&self.buf[offset..size]);
21 Ok(())
22 }
23
24 fn capacity(&self) -> usize {
25 N
26 }
27}
28
29impl<const N: usize> Storage for InMemoryStorage<N> {
30 fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
31 let offset = offset as usize;
32 let size = offset + bytes.len();
33 self.buf[offset..size].copy_from_slice(bytes);
34 Ok(())
35 }
36}