diff --git a/26_kv/src/storage/memory.rs b/26_kv/src/storage/memory.rs index f3cc3ef..7429f3e 100644 --- a/26_kv/src/storage/memory.rs +++ b/26_kv/src/storage/memory.rs @@ -31,9 +31,14 @@ impl Storage for MemTable { Ok(table.get(key).map(|v| v.value().clone())) } - fn set(&self, table: &str, key: String, value: Value) -> Result, KvError> { + fn set( + &self, + table: &str, + key: impl Into, + value: impl Into, + ) -> Result, KvError> { let table = self.get_or_create_table(table); - Ok(table.insert(key, value)) + Ok(table.insert(key.into(), value.into())) } fn contains(&self, table: &str, key: &str) -> Result { diff --git a/26_kv/src/storage/mod.rs b/26_kv/src/storage/mod.rs index 63261f2..6ad910c 100644 --- a/26_kv/src/storage/mod.rs +++ b/26_kv/src/storage/mod.rs @@ -11,7 +11,12 @@ pub trait Storage { /// 从一个 HashTable 里获取一个 key 的 value fn get(&self, table: &str, key: &str) -> Result, KvError>; /// 从一个 HashTable 里设置一个 key 的 value,返回旧的 value - fn set(&self, table: &str, key: String, value: Value) -> Result, KvError>; + fn set( + &self, + table: &str, + key: impl Into, + value: impl Into, + ) -> Result, KvError>; /// 查看 HashTable 中是否有 key fn contains(&self, table: &str, key: &str) -> Result; /// 从 HashTable 中删除一个 key @@ -94,10 +99,10 @@ mod tests { fn test_basi_interface(store: impl Storage) { // 第一次 set 会创建 table,插入 key 并返回 None(之前没值) - let v = store.set("t1", "hello".into(), "world".into()); + let v = store.set("t1", "hello", "world"); assert!(v.unwrap().is_none()); // 再次 set 同样的 key 会更新,并返回之前的值 - let v1 = store.set("t1", "hello".into(), "world1".into()); + let v1 = store.set("t1", "hello", "world1"); assert_eq!(v1, Ok(Some("world".into()))); // get 存在的 key 会得到最新的值 @@ -123,8 +128,8 @@ mod tests { } fn test_get_all(store: impl Storage) { - store.set("t2", "k1".into(), "v1".into()).unwrap(); - store.set("t2", "k2".into(), "v2".into()).unwrap(); + store.set("t2", "k1", "v1").unwrap(); + store.set("t2", "k2", "v2").unwrap(); let mut data = store.get_all("t2").unwrap(); data.sort_by(|a, b| a.partial_cmp(b).unwrap()); assert_eq!( @@ -137,8 +142,8 @@ mod tests { } fn test_get_iter(store: impl Storage) { - store.set("t2", "k1".into(), "v1".into()).unwrap(); - store.set("t2", "k2".into(), "v2".into()).unwrap(); + store.set("t2", "k1", "v1").unwrap(); + store.set("t2", "k2", "v2").unwrap(); let mut data: Vec<_> = store.get_iter("t2").unwrap().collect(); data.sort_by(|a, b| a.partial_cmp(b).unwrap()); assert_eq!( diff --git a/26_kv/src/storage/sleddb.rs b/26_kv/src/storage/sleddb.rs index c321e73..df8756c 100644 --- a/26_kv/src/storage/sleddb.rs +++ b/26_kv/src/storage/sleddb.rs @@ -36,9 +36,15 @@ impl Storage for SledDb { flip(result) } - fn set(&self, table: &str, key: String, value: Value) -> Result, KvError> { + fn set( + &self, + table: &str, + key: impl Into, + value: impl Into, + ) -> Result, KvError> { + let key = key.into(); let name = SledDb::get_full_key(table, &key); - let data: Vec = value.try_into()?; + let data: Vec = value.into().try_into()?; let result = self.0.insert(name, data)?.map(|v| v.as_ref().try_into()); flip(result) diff --git a/31_ffi/bzlib_sys/src/bindings.rs b/31_ffi/bzlib_sys/src/bindings.rs index 95e6c27..6114a05 100644 --- a/31_ffi/bzlib_sys/src/bindings.rs +++ b/31_ffi/bzlib_sys/src/bindings.rs @@ -1,5 +1,3 @@ -#![allow(unknown_lints)] -#![allow(clippy::all)] /* automatically generated by rust-bindgen 0.59.1 */ pub const BZ_RUN: u32 = 0; diff --git a/31_ffi/bzlib_sys/src/lib.rs b/31_ffi/bzlib_sys/src/lib.rs index f68e716..96d5063 100644 --- a/31_ffi/bzlib_sys/src/lib.rs +++ b/31_ffi/bzlib_sys/src/lib.rs @@ -7,6 +7,7 @@ use anyhow::{anyhow, Result}; use std::mem; +#[allow(clippy::all)] mod bindings; pub use bindings::*;