write_value(): don't consume value parameter

This commit is contained in:
Astro 2020-12-11 20:55:30 +01:00
parent 159b5c97d9
commit 4db466ffc4
2 changed files with 10 additions and 10 deletions

View File

@ -186,9 +186,9 @@ impl<B: StoreBackend> Store<B> {
/// encode with `postcard`, write at `key`
#[cfg(feature = "postcard-values")]
pub fn write_value<T: Serialize>(&mut self, key: &str, value: T) -> Result<(), Error<B::Error>> {
pub fn write_value<T: Serialize>(&mut self, key: &str, value: &T) -> Result<(), Error<B::Error>> {
let mut buf = [0; 64];
let data = postcard::to_slice(&value, &mut buf)
let data = postcard::to_slice(value, &mut buf)
.map_err(Error::Encode)?;
self.write(key, data)
}

View File

@ -83,7 +83,7 @@ fn write_read_str_utf8() {
#[cfg(feature = "postcard-values")]
fn write_read_value_str() {
let mut store = make_store();
store.write_value("foo", "bar").unwrap();
store.write_value("foo", &"bar").unwrap();
assert_eq!(store.read_value("foo"), Ok(Some("bar")));
}
@ -91,7 +91,7 @@ fn write_read_value_str() {
#[cfg(feature = "postcard-values")]
fn write_read_value_u32() {
let mut store = make_store();
store.write_value("foo", 42005u32).unwrap();
store.write_value("foo", &42005u32).unwrap();
assert_eq!(store.read_value("foo"), Ok(Some(42005u32)));
}
@ -99,7 +99,7 @@ fn write_read_value_u32() {
#[cfg(feature = "postcard-values")]
fn write_read_value_i64() {
let mut store = make_store();
store.write_value("foo", -99999999i64).unwrap();
store.write_value("foo", &-99999999i64).unwrap();
assert_eq!(store.read_value("foo"), Ok(Some(-99999999i64)));
}
@ -107,7 +107,7 @@ fn write_read_value_i64() {
#[cfg(feature = "postcard-values")]
fn write_read_value_f32() {
let mut store = make_store();
store.write_value("foo", 3.75e17f32).unwrap();
store.write_value("foo", &3.75e17f32).unwrap();
assert_eq!(store.read_value("foo"), Ok(Some(3.75e17f32)));
}
@ -115,7 +115,7 @@ fn write_read_value_f32() {
#[cfg(feature = "postcard-values")]
fn write_read_value_f64() {
let mut store = make_store();
store.write_value("foo", -1.999e-13f64).unwrap();
store.write_value("foo", &-1.999e-13f64).unwrap();
assert_eq!(store.read_value("foo"), Ok(Some(-1.999e-13f64)));
}
@ -134,11 +134,11 @@ fn write_many() {
#[cfg(feature = "postcard-values")]
fn write_value_many() {
let mut store = make_store();
store.write_value("quux", (false, 3u8, -42i16)).unwrap();
store.write_value("quux", &(false, 3u8, -42i16)).unwrap();
for i in 0..1000u32 {
store.write_value("bar", i).unwrap();
store.write_value("bar", &i).unwrap();
assert_eq!(store.read_value("bar"), Ok(Some(i)));
store.write_value("foo", i % 2 == 0).unwrap();
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))));