no_flash: init

This commit is contained in:
Astro 2020-12-11 18:07:39 +01:00
parent 57758cdc9a
commit a0f34e945d
4 changed files with 45 additions and 2 deletions

View File

@ -13,7 +13,6 @@ For details see `trait StoreBackend`.
## TODO
* NoFlash backend
* read_int()
* write_str()
* automatic value coercion

View File

@ -9,6 +9,7 @@ mod iter;
use iter::Iter;
mod fmt;
use fmt::FmtWrapper;
pub mod no_flash;
mod test;
pub trait StoreBackend {

33
src/no_flash.rs Normal file
View File

@ -0,0 +1,33 @@
use crate::StoreBackend;
use core::fmt;
/// expect this for any write operation to `NoFlash`
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NoFlashError;
impl fmt::Display for NoFlashError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "no flash")
}
}
/// a valid `StoreBackend` that can be used for configurations lacking a flash
#[derive(Debug, Clone, Copy)]
pub struct NoFlash;
impl StoreBackend for NoFlash {
type Data = [u8; 0];
fn data(&self) -> &Self::Data {
&[]
}
type Error = NoFlashError;
fn erase(&mut self) -> Result<(), Self::Error> {
Err(NoFlashError)
}
fn program(&mut self, _: usize, _: &[u8]) -> Result<(), Self::Error> {
Err(NoFlashError)
}
}

View File

@ -1,6 +1,6 @@
#![cfg(test)]
use crate::{Store, StoreBackend};
use crate::{Error, no_flash, Store, StoreBackend, WriteError};
const SIZE: usize = 1024;
@ -77,3 +77,13 @@ fn write_many() {
assert_eq!(store.read("foo").unwrap().unwrap(), b"do not compact away");
assert_eq!(store.read("bar").unwrap().unwrap(), b"SPAM");
}
#[test]
fn no_flash() {
let mut store = Store::new(no_flash::NoFlash);
assert_eq!(store.read("foo").unwrap(), None);
assert_eq!(
store.write("foo", b"bar"),
Err(Error::Write(WriteError::Backend(no_flash::NoFlashError)))
);
}