add private function ivcec_to_u32 in keyvalue

This commit is contained in:
KaFai Choi 2022-03-23 13:46:16 +07:00 committed by KaFai Choi
parent e68d3b9e63
commit 5ff8320e3b
No known key found for this signature in database
GPG Key ID: C93944C459E21542

View File

@ -166,16 +166,9 @@ macro_rules! impl_batch_operations {
fn del_last_index(&mut self, keychain: KeychainKind) -> Result<Option<u32>, Error> { fn del_last_index(&mut self, keychain: KeychainKind) -> Result<Option<u32>, Error> {
let key = MapKey::LastIndex(keychain).as_map_key(); let key = MapKey::LastIndex(keychain).as_map_key();
let res = self.remove(key); let res = self.remove(key);
let res = $process_delete!(res); $process_delete!(res)
.map(ivec_to_u32)
match res { .transpose()
None => Ok(None),
Some(b) => {
let array: [u8; 4] = b.as_ref().try_into().map_err(|_| Error::InvalidU32Bytes(b.to_vec()))?;
let val = u32::from_be_bytes(array);
Ok(Some(val))
}
}
} }
fn del_sync_time(&mut self) -> Result<Option<SyncTime>, Error> { fn del_sync_time(&mut self) -> Result<Option<SyncTime>, Error> {
@ -357,16 +350,7 @@ impl Database for Tree {
fn get_last_index(&self, keychain: KeychainKind) -> Result<Option<u32>, Error> { fn get_last_index(&self, keychain: KeychainKind) -> Result<Option<u32>, Error> {
let key = MapKey::LastIndex(keychain).as_map_key(); let key = MapKey::LastIndex(keychain).as_map_key();
self.get(key)? self.get(key)?.map(ivec_to_u32).transpose()
.map(|b| -> Result<_, Error> {
let array: [u8; 4] = b
.as_ref()
.try_into()
.map_err(|_| Error::InvalidU32Bytes(b.to_vec()))?;
let val = u32::from_be_bytes(array);
Ok(val)
})
.transpose()
} }
fn get_sync_time(&self) -> Result<Option<SyncTime>, Error> { fn get_sync_time(&self) -> Result<Option<SyncTime>, Error> {
@ -393,15 +377,17 @@ impl Database for Tree {
Some(new.to_be_bytes().to_vec()) Some(new.to_be_bytes().to_vec())
})? })?
.map_or(Ok(0), |b| -> Result<_, Error> { .map_or(Ok(0), ivec_to_u32)
}
}
fn ivec_to_u32(b: sled::IVec) -> Result<u32, Error> {
let array: [u8; 4] = b let array: [u8; 4] = b
.as_ref() .as_ref()
.try_into() .try_into()
.map_err(|_| Error::InvalidU32Bytes(b.to_vec()))?; .map_err(|_| Error::InvalidU32Bytes(b.to_vec()))?;
let val = u32::from_be_bytes(array); let val = u32::from_be_bytes(array);
Ok(val) Ok(val)
})
}
} }
impl BatchDatabase for Tree { impl BatchDatabase for Tree {