embedded_storage_inmemory/
lib.rs

1#![allow(unused)]
2
3use core::ops::{Bound, Range, RangeBounds};
4
5use embedded_storage::nor_flash::{ErrorType, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash};
6#[cfg(feature = "nightly")]
7use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash};
8
9pub struct MemFlash<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> {
10    pub mem: [u8; SIZE],
11}
12
13#[derive(Debug)]
14pub struct MemFlashError;
15
16impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE> {
17    pub const fn new(fill: u8) -> Self {
18        Self { mem: [fill; SIZE] }
19    }
20
21    fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), MemFlashError> {
22        let len = bytes.len();
23        bytes.copy_from_slice(&self.mem[offset as usize..offset as usize + len]);
24        Ok(())
25    }
26
27    fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), MemFlashError> {
28        let offset = offset as usize;
29        assert!(bytes.len() % WRITE_SIZE == 0);
30        assert!(offset % WRITE_SIZE == 0);
31        assert!(offset + bytes.len() <= SIZE);
32
33        for ((offset, mem_byte), new_byte) in self.mem.iter_mut().enumerate().skip(offset).take(bytes.len()).zip(bytes) {
34            assert_eq!(0xFF, *mem_byte, "Offset {} is not erased", offset);
35            *mem_byte = *new_byte;
36        }
37
38        Ok(())
39    }
40
41    fn erase(&mut self, from: u32, to: u32) -> Result<(), MemFlashError> {
42        let from = from as usize;
43        let to = to as usize;
44        assert!(from % ERASE_SIZE == 0);
45        assert!(to % ERASE_SIZE == 0, "To: {}, erase size: {}", to, ERASE_SIZE);
46        for i in from..to {
47            self.mem[i] = 0xFF;
48        }
49        Ok(())
50    }
51
52    pub fn program(&mut self, offset: u32, bytes: &[u8]) -> Result<(), MemFlashError> {
53        let offset = offset as usize;
54        assert!(bytes.len() % WRITE_SIZE == 0);
55        assert!(offset % WRITE_SIZE == 0);
56        assert!(offset + bytes.len() <= SIZE);
57
58        self.mem[offset..offset + bytes.len()].copy_from_slice(bytes);
59
60        Ok(())
61    }
62}
63
64impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> Default for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE> {
65    fn default() -> Self {
66        Self::new(0xFF)
67    }
68}
69
70impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> ErrorType for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE> {
71    type Error = MemFlashError;
72}
73
74impl NorFlashError for MemFlashError {
75    fn kind(&self) -> NorFlashErrorKind {
76        NorFlashErrorKind::Other
77    }
78}
79
80impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> ReadNorFlash
81    for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
82{
83    const READ_SIZE: usize = 1;
84
85    fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
86        self.read(offset, bytes)
87    }
88
89    fn capacity(&self) -> usize {
90        SIZE
91    }
92}
93
94impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> NorFlash for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE> {
95    const WRITE_SIZE: usize = WRITE_SIZE;
96    const ERASE_SIZE: usize = ERASE_SIZE;
97
98    fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
99        self.write(offset, bytes)
100    }
101
102    fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
103        self.erase(from, to)
104    }
105}
106
107#[cfg(feature = "nightly")]
108impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> AsyncReadNorFlash
109    for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
110{
111    const READ_SIZE: usize = 1;
112
113    async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
114        self.read(offset, bytes)
115    }
116
117    fn capacity(&self) -> usize {
118        SIZE
119    }
120}
121
122#[cfg(feature = "nightly")]
123impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> AsyncNorFlash
124    for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
125{
126    const WRITE_SIZE: usize = WRITE_SIZE;
127    const ERASE_SIZE: usize = ERASE_SIZE;
128
129    async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
130        self.write(offset, bytes)
131    }
132
133    async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
134        self.erase(from, to)
135    }
136}
137
138#[cfg(test)]
139mod tests {
140    use crate::MemFlash;
141
142    #[test]
143    fn test_write() {
144        let mut flash = MemFlash::<1024, 4, 1>::new(0xFF);
145        flash.erase(0, 1024).unwrap();
146        flash.write(512, b"hello").unwrap();
147
148        let mut rx = [0; 5];
149        flash.read(512, &mut rx).unwrap();
150        assert_eq!(&rx, b"hello");
151    }
152}