Compare commits
2 Commits
91d69474ed
...
159b5c97d9
Author | SHA1 | Date |
---|---|---|
Astro | 159b5c97d9 | |
Astro | 602cc1f0f3 |
18
src/lib.rs
18
src/lib.rs
|
@ -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(())
|
||||||
}
|
}
|
||||||
|
|
14
src/test.rs
14
src/test.rs
|
@ -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);
|
||||||
|
|
Loading…
Reference in New Issue