1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#[cfg(feature = "alloc")]
mod heap;
mod r#static;

#[cfg(feature = "alloc")]
pub use heap::HeapDataStore;

pub use r#static::StaticDataStore;

#[derive(Debug)]
#[non_exhaustive]
pub enum SliceDataStoreError {
    OutOfMemory,
}

impl super::KvDataAccess for [u8] {
    type Error = SliceDataStoreError;

    fn read(&self, address: u32, dst: &mut [u8]) -> Result<usize, Self::Error> {
        let addr = address as usize;
        let end = addr + dst.len();
        if end > self.len() {
            return Err(<Self as super::KvDataAccess>::Error::OutOfMemory);
        }
        dst.copy_from_slice(&self[addr..end]);
        Ok(dst.len())
    }

    fn write(&mut self, address: u32, data: &[u8]) -> Result<usize, Self::Error> {
        let addr = address as usize;
        let end = addr + data.len();
        if end > self.len() {
            return Err(<Self as super::KvDataAccess>::Error::OutOfMemory);
        }
        self[addr..end].copy_from_slice(data);
        Ok(data.len())
    }
}