embedded_storage_file/
backend_vec.rs

1use crate::synhronous::{NorMemory, BufferBackend, Error};
2use std::vec::Vec;
3
4pub type NorMemoryInram<const READ_SIZE: usize, const WRITE_SIZE: usize, const ERASE_SIZE: usize> =
5    NorMemory<Vec<u8>, READ_SIZE, WRITE_SIZE, ERASE_SIZE>;
6
7impl<const READ_SIZE: usize, const WRITE_SIZE: usize, const ERASE_SIZE: usize>
8    NorMemoryInram<READ_SIZE, WRITE_SIZE, ERASE_SIZE>
9{
10    pub fn new(size: usize) -> Self {
11        Self {
12            buffer: vec![0; size],
13        }
14    }
15}
16
17impl BufferBackend for Vec<u8> {
18    fn with_data<F>(&self, from: usize, to: usize, mut f: F) -> Result<(), Error>
19    where
20        F: FnMut(&[u8]) -> Result<(), Error>,
21    {
22        if to > self.len() {
23            return Err(Error::OutOfBounds);
24        }
25        if from > to {
26            return Err(Error::OutOfBounds);
27        }
28        let r = &self[from..to];
29        f(r)
30    }
31
32    fn with_data_mut<F>(&mut self, from: usize, to: usize, mut f: F) -> Result<(), Error>
33    where
34        F: FnMut(&mut [u8]) -> Result<(), Error>,
35    {
36        if to > self.len() {
37            return Err(Error::OutOfBounds);
38        }
39        if from > to {
40            return Err(Error::OutOfBounds);
41        }
42        let r = &mut self[from..to];
43        f(r)
44    }
45
46    fn size(&self) -> usize {
47        self.len()
48    }
49}