use super::*; use prost::Message; fn decode_proto(data: &[u8]) -> Option { Message::decode(data).ok() } fn encode_proto(data: &T) -> Bytes { data.encode_to_vec().into() } impl KvdbProtoExt for T where T: Kvdb {} #[async_trait] pub trait KvdbProtoExt: Kvdb { async fn get_proto(&self, key: Key) -> Option { let val = self.get(key).await; val.and_then(|v| decode_proto(&v)) } async fn get_many_proto(&self, keys: Vec) -> HashMap { let data = self.get_many(keys).await; data.into_iter().filter_map(|(k, v)| decode_proto(&v).map(|v| (k, v))).collect() } async fn set_proto(&self, key: Key, value: &T) { let val = encode_proto(value); self.set(key, val).await; } async fn set_many_proto(&self, data: HashMap) { let data = data.into_iter().map(|(k, v)| (k, encode_proto(&v))).collect(); self.set_many(data).await; } }