Compare commits

..

2 Commits

Author SHA1 Message Date
Astro 159b5c97d9 rectify #![no_std] accident 2020-12-11 20:29:16 +01:00
Astro 602cc1f0f3 append(): check for not_modified 2020-12-11 20:13:33 +01:00
2 changed files with 30 additions and 2 deletions

View File

@ -1,4 +1,4 @@
//#![no_std] #![no_std]
use core::{mem::size_of, str}; use core::{mem::size_of, str};
use byteorder::{ByteOrder, BigEndian}; use byteorder::{ByteOrder, BigEndian};
@ -153,7 +153,21 @@ impl<B: StoreBackend> Store<B> {
} }
fn append(&mut self, key: &str, value: &[u8]) -> Result<(), Error<B::Error>> { fn append(&mut self, key: &str, value: &[u8]) -> Result<(), Error<B::Error>> {
let free_offset = self.get_bytes_used()?; let free_offset = {
let mut iter = Iter::new(self.backend.data().as_ref());
let mut not_modified = false;
while let Some(result) = iter.next() {
let (record_key, record_value) = result?;
if key.as_bytes() == record_key {
not_modified = value == record_value;
}
}
if not_modified {
return Ok(())
}
iter.offset
};
unsafe { self.append_at(free_offset, key.as_bytes(), value)? }; unsafe { self.append_at(free_offset, key.as_bytes(), value)? };
Ok(()) Ok(())
} }

View File

@ -130,6 +130,20 @@ fn write_many() {
assert_eq!(store.read("bar").unwrap().unwrap(), b"SPAM"); assert_eq!(store.read("bar").unwrap().unwrap(), b"SPAM");
} }
#[test]
#[cfg(feature = "postcard-values")]
fn write_value_many() {
let mut store = make_store();
store.write_value("quux", (false, 3u8, -42i16)).unwrap();
for i in 0..1000u32 {
store.write_value("bar", i).unwrap();
assert_eq!(store.read_value("bar"), Ok(Some(i)));
store.write_value("foo", i % 2 == 0).unwrap();
assert_eq!(store.read_value("foo"), Ok(Some(i % 2 == 0)));
}
assert_eq!(store.read_value("quux"), Ok(Some((false, 3u8, -42i16))));
}
#[test] #[test]
fn no_flash() { fn no_flash() {
let mut store = Store::new(no_flash::NoFlash); let mut store = Store::new(no_flash::NoFlash);