write(): accept both &[u8] and &str for value
This commit is contained in:
parent
f4245ec937
commit
fb28e8a9b1
|
@ -147,7 +147,8 @@ impl<B: StoreBackend> Store<B> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// store a buffer `value` at `key`
|
/// store a buffer `value` at `key`
|
||||||
pub fn write(&mut self, key: &str, value: &[u8]) -> Result<(), Error<B::Error>> {
|
pub fn write<V: AsRef<[u8]>>(&mut self, key: &str, value: V) -> Result<(), Error<B::Error>> {
|
||||||
|
let value = value.as_ref();
|
||||||
match self.append(key, value) {
|
match self.append(key, value) {
|
||||||
Err(Error::Write(WriteError::SpaceExhausted)) => {
|
Err(Error::Write(WriteError::SpaceExhausted)) => {
|
||||||
self.compact()?;
|
self.compact()?;
|
||||||
|
|
14
src/test.rs
14
src/test.rs
|
@ -59,6 +59,20 @@ fn write_read() {
|
||||||
assert_eq!(store.read("foo").unwrap().unwrap(), b"bar");
|
assert_eq!(store.read("foo").unwrap().unwrap(), b"bar");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn write_read_str() {
|
||||||
|
let mut store = make_store();
|
||||||
|
store.write("foo", "bar").unwrap();
|
||||||
|
assert_eq!(store.read_str("foo").unwrap().unwrap(), "bar");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn write_read_str_utf8() {
|
||||||
|
let mut store = make_store();
|
||||||
|
store.write("réseau", "point à point").unwrap();
|
||||||
|
assert_eq!(store.read_str("réseau").unwrap().unwrap(), "point à point");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn write_read_value_str() {
|
fn write_read_value_str() {
|
||||||
let mut store = make_store();
|
let mut store = make_store();
|
||||||
|
|
Loading…
Reference in New Issue