write(): accept both &[u8] and &str for value

This commit is contained in:
Astro 2020-12-11 19:08:56 +01:00
parent f4245ec937
commit fb28e8a9b1
2 changed files with 16 additions and 1 deletions

View File

@ -147,7 +147,8 @@ impl<B: StoreBackend> Store<B> {
}
/// 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) {
Err(Error::Write(WriteError::SpaceExhausted)) => {
self.compact()?;

View File

@ -59,6 +59,20 @@ fn write_read() {
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]
fn write_read_value_str() {
let mut store = make_store();