read_value()/write_value(): put behind feature postcard-values
This commit is contained in:
parent
fb28e8a9b1
commit
532ba8381f
|
@ -11,7 +11,11 @@ categories = ["embedded", "database"]
|
|||
license = "GPL-3"
|
||||
edition = "2018"
|
||||
|
||||
[features]
|
||||
default = ["postcard-values"]
|
||||
postcard-values = ["postcard", "serde"]
|
||||
|
||||
[dependencies]
|
||||
byteorder = { version = "1", default-features = false }
|
||||
serde = { version = "1.0", default-features = false }
|
||||
postcard = "0.5"
|
||||
serde = { version = "1.0", default-features = false, optional = true }
|
||||
postcard = { version = "0.5", optional = true }
|
||||
|
|
|
@ -4,7 +4,9 @@ use core::{fmt, str};
|
|||
pub enum Error<B> {
|
||||
Read(ReadError),
|
||||
Write(WriteError<B>),
|
||||
#[cfg(feature = "postcard-values")]
|
||||
Encode(postcard::Error),
|
||||
#[cfg(feature = "postcard-values")]
|
||||
Decode(postcard::Error),
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
use core::{mem::size_of, str};
|
||||
use byteorder::{ByteOrder, BigEndian};
|
||||
#[cfg(feature = "postcard-values")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
mod error;
|
||||
|
@ -59,10 +60,12 @@ impl<B: StoreBackend> Store<B> {
|
|||
.map(|value| str::from_utf8(value)
|
||||
.map_err(ReadError::Utf8Error)
|
||||
)
|
||||
.transpose())
|
||||
.transpose()
|
||||
)
|
||||
}
|
||||
|
||||
/// read from `key`, decode with `postcard`
|
||||
#[cfg(feature = "postcard-values")]
|
||||
pub fn read_value<'a, T: Deserialize<'a>>(&'a self, key: &str) -> Result<Option<T>, Error<B::Error>> {
|
||||
self.read(key)?
|
||||
.map(
|
||||
|
@ -159,6 +162,7 @@ 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>> {
|
||||
let mut buf = [0; 64];
|
||||
let data = postcard::to_slice(&value, &mut buf)
|
||||
|
|
|
@ -74,6 +74,7 @@ fn write_read_str_utf8() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "postcard-values")]
|
||||
fn write_read_value_str() {
|
||||
let mut store = make_store();
|
||||
store.write_value("foo", "bar").unwrap();
|
||||
|
@ -81,6 +82,7 @@ fn write_read_value_str() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "postcard-values")]
|
||||
fn write_read_value_u32() {
|
||||
let mut store = make_store();
|
||||
store.write_value("foo", 42005u32).unwrap();
|
||||
|
@ -88,6 +90,7 @@ fn write_read_value_u32() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "postcard-values")]
|
||||
fn write_read_value_i64() {
|
||||
let mut store = make_store();
|
||||
store.write_value("foo", -99999999i64).unwrap();
|
||||
|
@ -95,6 +98,7 @@ fn write_read_value_i64() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "postcard-values")]
|
||||
fn write_read_value_f32() {
|
||||
let mut store = make_store();
|
||||
store.write_value("foo", 3.75e17f32).unwrap();
|
||||
|
@ -102,6 +106,7 @@ fn write_read_value_f32() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "postcard-values")]
|
||||
fn write_read_value_f64() {
|
||||
let mut store = make_store();
|
||||
store.write_value("foo", -1.999e-13f64).unwrap();
|
||||
|
|
Loading…
Reference in New Issue