Skip to content

Commit 3278ea5

Browse files
committed
add private function ivcec_to_u32 in keyvalue
1 parent b7d2daf commit 3278ea5

File tree

1 file changed

+14
-26
lines changed

1 file changed

+14
-26
lines changed

src/database/keyvalue.rs

+14-26
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,9 @@ macro_rules! impl_batch_operations {
164164
fn del_last_index(&mut self, keychain: KeychainKind) -> Result<Option<u32>, Error> {
165165
let key = MapKey::LastIndex(keychain).as_map_key();
166166
let res = self.remove(key);
167-
let res = $process_delete!(res);
168-
169-
match res {
170-
None => Ok(None),
171-
Some(b) => {
172-
let array: [u8; 4] = b.as_ref().try_into().map_err(|_| Error::InvalidU32Bytes(b.to_vec()))?;
173-
let val = u32::from_be_bytes(array);
174-
Ok(Some(val))
175-
}
176-
}
167+
$process_delete!(res)
168+
.map(ivec_to_u32)
169+
.transpose()
177170
}
178171

179172
fn del_sync_time(&mut self) -> Result<Option<SyncTime>, Error> {
@@ -347,14 +340,7 @@ impl Database for Tree {
347340
fn get_last_index(&self, keychain: KeychainKind) -> Result<Option<u32>, Error> {
348341
let key = MapKey::LastIndex(keychain).as_map_key();
349342
self.get(key)?
350-
.map(|b| -> Result<_, Error> {
351-
let array: [u8; 4] = b
352-
.as_ref()
353-
.try_into()
354-
.map_err(|_| Error::InvalidU32Bytes(b.to_vec()))?;
355-
let val = u32::from_be_bytes(array);
356-
Ok(val)
357-
})
343+
.map(ivec_to_u32)
358344
.transpose()
359345
}
360346

@@ -383,20 +369,22 @@ impl Database for Tree {
383369

384370
Some(new.to_be_bytes().to_vec())
385371
})?
386-
.map_or(Ok(0), |b| -> Result<_, Error> {
387-
let array: [u8; 4] = b
388-
.as_ref()
389-
.try_into()
390-
.map_err(|_| Error::InvalidU32Bytes(b.to_vec()))?;
391-
let val = u32::from_be_bytes(array);
392-
Ok(val)
393-
})?;
372+
.map_or(Ok(0), ivec_to_u32)?;
394373

395374
Tree::flush(self).map(|_| ())?;
396375
Ok(last_index)
397376
}
398377
}
399378

379+
fn ivec_to_u32(b: sled::IVec) -> Result<u32, Error> {
380+
let array: [u8; 4] = b
381+
.as_ref()
382+
.try_into()
383+
.map_err(|_| Error::InvalidU32Bytes(b.to_vec()))?;
384+
let val = u32::from_be_bytes(array);
385+
Ok(val)
386+
}
387+
400388
impl BatchDatabase for Tree {
401389
type Batch = sled::Batch;
402390

0 commit comments

Comments
 (0)