caio.co/de/cantine

Simplify code

Id
2b9ff35be73c767bb1b64cd6126eb8b7da6a5fca
Author
Caio
Commit time
2020-03-24T15:11:22+01:00

Modified cantine/src/database/readerwriter.rs

@@ -59,21 +59,17
}

pub fn find_by_id(&'a self, id: u64) -> Option<Result<T>> {
- if let Some(&offset) = self.id_index.get(&id) {
- Some(bincode::deserialize(&self.data[offset..]).map_err(|_| {
+ self.id_index.get(&id).map(|offset| {
+ bincode::deserialize(&self.data[*offset..]).map_err(|_| {
io::Error::new(io::ErrorKind::InvalidData, "Failure decoding at offset")
- }))
- } else {
- None
- }
+ })
+ })
}

pub fn find_by_uuid(&'a self, uuid: &Uuid) -> Option<Result<T>> {
- if let Some(&id) = self.uuid_index.get(uuid) {
- self.find_by_id(id)
- } else {
- None
- }
+ self.uuid_index
+ .get(uuid)
+ .and_then(|id| self.find_by_id(*id))
}

pub fn id_for_uuid(&self, uuid: &Uuid) -> Option<&u64> {