write_value(): don't consume value parameter
This commit is contained in:
parent
159b5c97d9
commit
4db466ffc4
|
@ -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)
|
||||
}
|
||||
|
|
16
src/test.rs
16
src/test.rs
|
@ -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))));
|
||||
|
|
Loading…
Reference in New Issue