diff --git a/heed-traits/src/lib.rs b/heed-traits/src/lib.rs index 4f1b9227..66c883db 100644 --- a/heed-traits/src/lib.rs +++ b/heed-traits/src/lib.rs @@ -1,13 +1,17 @@ use std::borrow::Cow; +use std::error::Error as StdError; + +/// A boxed `Send + Sync + 'static` error. +pub type BoxedError = Box; pub trait BytesEncode<'a> { type EItem: ?Sized + 'a; - fn bytes_encode(item: &'a Self::EItem) -> Option>; + fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError>; } pub trait BytesDecode<'a> { type DItem: 'a; - fn bytes_decode(bytes: &'a [u8]) -> Option; + fn bytes_decode(bytes: &'a [u8]) -> Result; } diff --git a/heed-types/Cargo.toml b/heed-types/Cargo.toml index 99309edf..d207ad9b 100644 --- a/heed-types/Cargo.toml +++ b/heed-types/Cargo.toml @@ -10,7 +10,7 @@ edition = "2018" [dependencies] bincode = { version = "1.2.1", optional = true } -bytemuck = { version = "1.5.0", features = ["extern_crate_alloc"] } +bytemuck = { version = "1.5.0", features = ["extern_crate_alloc", "extern_crate_std"] } byteorder = "1.4.2" heed-traits = { version = "0.7.0", path = "../heed-traits" } serde = { version = "1.0.117", optional = true } diff --git a/heed-types/src/cow_slice.rs b/heed-types/src/cow_slice.rs index 64d161a6..48f30b19 100644 --- a/heed-types/src/cow_slice.rs +++ b/heed-types/src/cow_slice.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; use bytemuck::{pod_collect_to_vec, try_cast_slice, AnyBitPattern, NoUninit, PodCastError}; -use heed_traits::{BytesDecode, BytesEncode}; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; /// Describes a slice that must be [memory aligned] and /// will be reallocated if it is not. @@ -23,19 +23,19 @@ pub struct CowSlice(std::marker::PhantomData); impl<'a, T: NoUninit> BytesEncode<'a> for CowSlice { type EItem = [T]; - fn bytes_encode(item: &'a Self::EItem) -> Option> { - try_cast_slice(item).map(Cow::Borrowed).ok() + fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError> { + try_cast_slice(item).map(Cow::Borrowed).map_err(Into::into) } } impl<'a, T: AnyBitPattern + NoUninit> BytesDecode<'a> for CowSlice { type DItem = Cow<'a, [T]>; - fn bytes_decode(bytes: &'a [u8]) -> Option { + fn bytes_decode(bytes: &'a [u8]) -> Result { match try_cast_slice(bytes) { - Ok(items) => Some(Cow::Borrowed(items)), - Err(PodCastError::AlignmentMismatch) => Some(Cow::Owned(pod_collect_to_vec(bytes))), - Err(_) => None, + Ok(items) => Ok(Cow::Borrowed(items)), + Err(PodCastError::AlignmentMismatch) => Ok(Cow::Owned(pod_collect_to_vec(bytes))), + Err(error) => Err(error.into()), } } } diff --git a/heed-types/src/cow_type.rs b/heed-types/src/cow_type.rs index 8282dfb7..7f4ff151 100644 --- a/heed-types/src/cow_type.rs +++ b/heed-types/src/cow_type.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; use bytemuck::{bytes_of, bytes_of_mut, try_from_bytes, AnyBitPattern, NoUninit, PodCastError}; -use heed_traits::{BytesDecode, BytesEncode}; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; /// Describes a type that must be [memory aligned] and /// will be reallocated if it is not. @@ -29,23 +29,23 @@ pub struct CowType(std::marker::PhantomData); impl<'a, T: NoUninit> BytesEncode<'a> for CowType { type EItem = T; - fn bytes_encode(item: &'a Self::EItem) -> Option> { - Some(Cow::Borrowed(bytes_of(item))) + fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError> { + Ok(Cow::Borrowed(bytes_of(item))) } } impl<'a, T: AnyBitPattern + NoUninit> BytesDecode<'a> for CowType { type DItem = Cow<'a, T>; - fn bytes_decode(bytes: &'a [u8]) -> Option { + fn bytes_decode(bytes: &'a [u8]) -> Result { match try_from_bytes(bytes) { - Ok(item) => Some(Cow::Borrowed(item)), + Ok(item) => Ok(Cow::Borrowed(item)), Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned) => { let mut item = T::zeroed(); bytes_of_mut(&mut item).copy_from_slice(bytes); - Some(Cow::Owned(item)) + Ok(Cow::Owned(item)) } - Err(_) => None, + Err(error) => Err(error.into()), } } } diff --git a/heed-types/src/lib.rs b/heed-types/src/lib.rs index b65ce519..68596753 100644 --- a/heed-types/src/lib.rs +++ b/heed-types/src/lib.rs @@ -37,6 +37,8 @@ mod serde_bincode; #[cfg(feature = "serde-json")] mod serde_json; +use heed_traits::BoxedError; + pub use self::cow_slice::CowSlice; pub use self::cow_type::CowType; pub use self::integer::*; @@ -62,8 +64,8 @@ pub struct DecodeIgnore; impl heed_traits::BytesDecode<'_> for DecodeIgnore { type DItem = (); - fn bytes_decode(_bytes: &[u8]) -> Option { - Some(()) + fn bytes_decode(_bytes: &[u8]) -> Result { + Ok(()) } } diff --git a/heed-types/src/owned_slice.rs b/heed-types/src/owned_slice.rs index c566f436..6b442793 100644 --- a/heed-types/src/owned_slice.rs +++ b/heed-types/src/owned_slice.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; use bytemuck::{try_cast_slice, AnyBitPattern, NoUninit}; -use heed_traits::{BytesDecode, BytesEncode}; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; use crate::CowSlice; @@ -23,15 +23,15 @@ pub struct OwnedSlice(std::marker::PhantomData); impl<'a, T: NoUninit> BytesEncode<'a> for OwnedSlice { type EItem = [T]; - fn bytes_encode(item: &'a Self::EItem) -> Option> { - try_cast_slice(item).map(Cow::Borrowed).ok() + fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError> { + try_cast_slice(item).map(Cow::Borrowed).map_err(Into::into) } } impl<'a, T: AnyBitPattern + NoUninit> BytesDecode<'a> for OwnedSlice { type DItem = Vec; - fn bytes_decode(bytes: &[u8]) -> Option { + fn bytes_decode(bytes: &[u8]) -> Result { CowSlice::::bytes_decode(bytes).map(Cow::into_owned) } } diff --git a/heed-types/src/owned_type.rs b/heed-types/src/owned_type.rs index a88bc0e1..aa890bae 100644 --- a/heed-types/src/owned_type.rs +++ b/heed-types/src/owned_type.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; use bytemuck::{bytes_of, AnyBitPattern, NoUninit}; -use heed_traits::{BytesDecode, BytesEncode}; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; use crate::CowType; @@ -29,15 +29,15 @@ pub struct OwnedType(std::marker::PhantomData); impl<'a, T: NoUninit> BytesEncode<'a> for OwnedType { type EItem = T; - fn bytes_encode(item: &'a Self::EItem) -> Option> { - Some(Cow::Borrowed(bytes_of(item))) + fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError> { + Ok(Cow::Borrowed(bytes_of(item))) } } impl<'a, T: AnyBitPattern + NoUninit> BytesDecode<'a> for OwnedType { type DItem = T; - fn bytes_decode(bytes: &[u8]) -> Option { + fn bytes_decode(bytes: &[u8]) -> Result { CowType::::bytes_decode(bytes).map(Cow::into_owned) } } diff --git a/heed-types/src/serde_bincode.rs b/heed-types/src/serde_bincode.rs index 464aceb4..37fefe01 100644 --- a/heed-types/src/serde_bincode.rs +++ b/heed-types/src/serde_bincode.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; -use heed_traits::{BytesDecode, BytesEncode}; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; use serde::{Deserialize, Serialize}; /// Describes a type that is [`Serialize`]/[`Deserialize`] and uses `bincode` to do so. @@ -14,8 +14,8 @@ where { type EItem = T; - fn bytes_encode(item: &'a Self::EItem) -> Option> { - bincode::serialize(item).map(Cow::Owned).ok() + fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError> { + bincode::serialize(item).map(Cow::Owned).map_err(Into::into) } } @@ -25,8 +25,8 @@ where { type DItem = T; - fn bytes_decode(bytes: &'a [u8]) -> Option { - bincode::deserialize(bytes).ok() + fn bytes_decode(bytes: &'a [u8]) -> Result { + bincode::deserialize(bytes).map_err(Into::into) } } diff --git a/heed-types/src/serde_json.rs b/heed-types/src/serde_json.rs index 2327e259..f1f1c3be 100644 --- a/heed-types/src/serde_json.rs +++ b/heed-types/src/serde_json.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; -use heed_traits::{BytesDecode, BytesEncode}; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; use serde::{Deserialize, Serialize}; /// Describes a type that is [`Serialize`]/[`Deserialize`] and uses `serde_json` to do so. @@ -14,8 +14,8 @@ where { type EItem = T; - fn bytes_encode(item: &Self::EItem) -> Option> { - serde_json::to_vec(item).map(Cow::Owned).ok() + fn bytes_encode(item: &Self::EItem) -> Result, BoxedError> { + serde_json::to_vec(item).map(Cow::Owned).map_err(Into::into) } } @@ -25,8 +25,8 @@ where { type DItem = T; - fn bytes_decode(bytes: &'a [u8]) -> Option { - serde_json::from_slice(bytes).ok() + fn bytes_decode(bytes: &'a [u8]) -> Result { + serde_json::from_slice(bytes).map_err(Into::into) } } diff --git a/heed-types/src/str.rs b/heed-types/src/str.rs index 9c40c542..4d00ef2e 100644 --- a/heed-types/src/str.rs +++ b/heed-types/src/str.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; -use heed_traits::{BytesDecode, BytesEncode}; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; /// Describes an [`prim@str`]. pub struct Str; @@ -8,15 +8,15 @@ pub struct Str; impl BytesEncode<'_> for Str { type EItem = str; - fn bytes_encode(item: &Self::EItem) -> Option> { - Some(Cow::Borrowed(item.as_bytes())) + fn bytes_encode(item: &Self::EItem) -> Result, BoxedError> { + Ok(Cow::Borrowed(item.as_bytes())) } } impl<'a> BytesDecode<'a> for Str { type DItem = &'a str; - fn bytes_decode(bytes: &'a [u8]) -> Option { - std::str::from_utf8(bytes).ok() + fn bytes_decode(bytes: &'a [u8]) -> Result { + std::str::from_utf8(bytes).map_err(Into::into) } } diff --git a/heed-types/src/unaligned_slice.rs b/heed-types/src/unaligned_slice.rs index 3ab36943..7d13a1df 100644 --- a/heed-types/src/unaligned_slice.rs +++ b/heed-types/src/unaligned_slice.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; use bytemuck::{try_cast_slice, AnyBitPattern, NoUninit}; -use heed_traits::{BytesDecode, BytesEncode}; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; /// Describes a type that is totally borrowed and doesn't /// depends on any [memory alignment]. @@ -16,16 +16,16 @@ pub struct UnalignedSlice(std::marker::PhantomData); impl<'a, T: NoUninit> BytesEncode<'a> for UnalignedSlice { type EItem = [T]; - fn bytes_encode(item: &'a Self::EItem) -> Option> { - try_cast_slice(item).map(Cow::Borrowed).ok() + fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError> { + try_cast_slice(item).map(Cow::Borrowed).map_err(Into::into) } } impl<'a, T: AnyBitPattern> BytesDecode<'a> for UnalignedSlice { type DItem = &'a [T]; - fn bytes_decode(bytes: &'a [u8]) -> Option { - try_cast_slice(bytes).ok() + fn bytes_decode(bytes: &'a [u8]) -> Result { + try_cast_slice(bytes).map_err(Into::into) } } diff --git a/heed-types/src/unaligned_type.rs b/heed-types/src/unaligned_type.rs index f2756a9b..886aeceb 100644 --- a/heed-types/src/unaligned_type.rs +++ b/heed-types/src/unaligned_type.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; use bytemuck::{bytes_of, try_from_bytes, AnyBitPattern, NoUninit}; -use heed_traits::{BytesDecode, BytesEncode}; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; /// Describes a slice that is totally borrowed and doesn't /// depends on any [memory alignment]. @@ -22,16 +22,16 @@ pub struct UnalignedType(std::marker::PhantomData); impl<'a, T: NoUninit> BytesEncode<'a> for UnalignedType { type EItem = T; - fn bytes_encode(item: &'a Self::EItem) -> Option> { - Some(Cow::Borrowed(bytes_of(item))) + fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError> { + Ok(Cow::Borrowed(bytes_of(item))) } } impl<'a, T: AnyBitPattern> BytesDecode<'a> for UnalignedType { type DItem = &'a T; - fn bytes_decode(bytes: &'a [u8]) -> Option { - try_from_bytes(bytes).ok() + fn bytes_decode(bytes: &'a [u8]) -> Result { + try_from_bytes(bytes).map_err(Into::into) } } diff --git a/heed-types/src/unit.rs b/heed-types/src/unit.rs index f53152ae..fee8bd56 100644 --- a/heed-types/src/unit.rs +++ b/heed-types/src/unit.rs @@ -1,6 +1,7 @@ use std::borrow::Cow; -use heed_traits::{BytesDecode, BytesEncode}; +use bytemuck::PodCastError; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; /// Describes the `()` type. pub struct Unit; @@ -8,19 +9,19 @@ pub struct Unit; impl BytesEncode<'_> for Unit { type EItem = (); - fn bytes_encode(_item: &Self::EItem) -> Option> { - Some(Cow::Borrowed(&[])) + fn bytes_encode(_item: &Self::EItem) -> Result, BoxedError> { + Ok(Cow::Borrowed(&[])) } } impl BytesDecode<'_> for Unit { type DItem = (); - fn bytes_decode(bytes: &[u8]) -> Option { + fn bytes_decode(bytes: &[u8]) -> Result { if bytes.is_empty() { - Some(()) + Ok(()) } else { - None + Err(PodCastError::SizeMismatch.into()) } } } diff --git a/heed/src/db/polymorph.rs b/heed/src/db/polymorph.rs index 354fdd12..2453a665 100644 --- a/heed/src/db/polymorph.rs +++ b/heed/src/db/polymorph.rs @@ -157,7 +157,7 @@ impl PolyDatabase { { assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; let mut key_val = unsafe { crate::into_val(&key_bytes) }; let mut data_val = mem::MaybeUninit::uninit(); @@ -169,7 +169,7 @@ impl PolyDatabase { match result { Ok(()) => { let data = unsafe { crate::from_val(data_val.assume_init()) }; - let data = DC::bytes_decode(data).ok_or(Error::Decoding)?; + let data = DC::bytes_decode(data).map_err(Error::Decoding)?; Ok(Some(data)) } Err(e) if e.not_found() => Ok(None), @@ -232,13 +232,13 @@ impl PolyDatabase { assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); let mut cursor = RoCursor::new(txn, self.dbi)?; - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; cursor.move_on_key_greater_than_or_equal_to(&key_bytes)?; match cursor.move_on_prev() { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Ok(Some((key, data))), - (_, _) => Err(Error::Decoding), + (Ok(key), Ok(data)) => Ok(Some((key, data))), + (Err(e), _) | (_, Err(e)) => Err(Error::Decoding(e)), }, Ok(None) => Ok(None), Err(e) => Err(e), @@ -300,7 +300,7 @@ impl PolyDatabase { assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); let mut cursor = RoCursor::new(txn, self.dbi)?; - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; let result = match cursor.move_on_key_greater_than_or_equal_to(&key_bytes) { Ok(Some((key, data))) if key == &key_bytes[..] => Ok(Some((key, data))), Ok(_) => cursor.move_on_prev(), @@ -309,8 +309,8 @@ impl PolyDatabase { match result { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Ok(Some((key, data))), - (_, _) => Err(Error::Decoding), + (Ok(key), Ok(data)) => Ok(Some((key, data))), + (Err(e), _) | (_, Err(e)) => Err(Error::Decoding(e)), }, Ok(None) => Ok(None), Err(e) => Err(e), @@ -372,7 +372,7 @@ impl PolyDatabase { assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); let mut cursor = RoCursor::new(txn, self.dbi)?; - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; let entry = match cursor.move_on_key_greater_than_or_equal_to(&key_bytes)? { Some((key, data)) if key > &key_bytes[..] => Some((key, data)), Some((_key, _data)) => cursor.move_on_next()?, @@ -381,8 +381,8 @@ impl PolyDatabase { match entry { Some((key, data)) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Ok(Some((key, data))), - (_, _) => Err(Error::Decoding), + (Ok(key), Ok(data)) => Ok(Some((key, data))), + (Err(e), _) | (_, Err(e)) => Err(Error::Decoding(e)), }, None => Ok(None), } @@ -443,11 +443,11 @@ impl PolyDatabase { assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); let mut cursor = RoCursor::new(txn, self.dbi)?; - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; match cursor.move_on_key_greater_than_or_equal_to(&key_bytes) { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Ok(Some((key, data))), - (_, _) => Err(Error::Decoding), + (Ok(key), Ok(data)) => Ok(Some((key, data))), + (Err(e), _) | (_, Err(e)) => Err(Error::Decoding(e)), }, Ok(None) => Ok(None), Err(e) => Err(e), @@ -499,8 +499,8 @@ impl PolyDatabase { let mut cursor = RoCursor::new(txn, self.dbi)?; match cursor.move_on_first() { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Ok(Some((key, data))), - (_, _) => Err(Error::Decoding), + (Ok(key), Ok(data)) => Ok(Some((key, data))), + (Err(e), _) | (_, Err(e)) => Err(Error::Decoding(e)), }, Ok(None) => Ok(None), Err(e) => Err(e), @@ -552,8 +552,8 @@ impl PolyDatabase { let mut cursor = RoCursor::new(txn, self.dbi)?; match cursor.move_on_last() { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Ok(Some((key, data))), - (_, _) => Err(Error::Decoding), + (Ok(key), Ok(data)) => Ok(Some((key, data))), + (Err(e), _) | (_, Err(e)) => Err(Error::Decoding(e)), }, Ok(None) => Ok(None), Err(e) => Err(e), @@ -912,11 +912,11 @@ impl PolyDatabase { let start_bound = match range.start_bound() { Bound::Included(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Included(bytes.into_owned()) } Bound::Excluded(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Excluded(bytes.into_owned()) } Bound::Unbounded => Bound::Unbounded, @@ -924,11 +924,11 @@ impl PolyDatabase { let end_bound = match range.end_bound() { Bound::Included(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Included(bytes.into_owned()) } Bound::Excluded(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Excluded(bytes.into_owned()) } Bound::Unbounded => Bound::Unbounded, @@ -1003,11 +1003,11 @@ impl PolyDatabase { let start_bound = match range.start_bound() { Bound::Included(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Included(bytes.into_owned()) } Bound::Excluded(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Excluded(bytes.into_owned()) } Bound::Unbounded => Bound::Unbounded, @@ -1015,11 +1015,11 @@ impl PolyDatabase { let end_bound = match range.end_bound() { Bound::Included(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Included(bytes.into_owned()) } Bound::Excluded(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Excluded(bytes.into_owned()) } Bound::Unbounded => Bound::Unbounded, @@ -1081,11 +1081,11 @@ impl PolyDatabase { let start_bound = match range.start_bound() { Bound::Included(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Included(bytes.into_owned()) } Bound::Excluded(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Excluded(bytes.into_owned()) } Bound::Unbounded => Bound::Unbounded, @@ -1093,11 +1093,11 @@ impl PolyDatabase { let end_bound = match range.end_bound() { Bound::Included(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Included(bytes.into_owned()) } Bound::Excluded(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Excluded(bytes.into_owned()) } Bound::Unbounded => Bound::Unbounded, @@ -1172,11 +1172,11 @@ impl PolyDatabase { let start_bound = match range.start_bound() { Bound::Included(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Included(bytes.into_owned()) } Bound::Excluded(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Excluded(bytes.into_owned()) } Bound::Unbounded => Bound::Unbounded, @@ -1184,11 +1184,11 @@ impl PolyDatabase { let end_bound = match range.end_bound() { Bound::Included(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Included(bytes.into_owned()) } Bound::Excluded(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Excluded(bytes.into_owned()) } Bound::Unbounded => Bound::Unbounded, @@ -1248,7 +1248,7 @@ impl PolyDatabase { { assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); - let prefix_bytes = KC::bytes_encode(prefix).ok_or(Error::Encoding)?; + let prefix_bytes = KC::bytes_encode(prefix).map_err(Error::Encoding)?; let prefix_bytes = prefix_bytes.into_owned(); RoCursor::new(txn, self.dbi).map(|cursor| RoPrefix::new(cursor, prefix_bytes)) } @@ -1317,7 +1317,7 @@ impl PolyDatabase { { assert_eq!(self.env_ident, txn.txn.env.env_mut_ptr() as usize); - let prefix_bytes = KC::bytes_encode(prefix).ok_or(Error::Encoding)?; + let prefix_bytes = KC::bytes_encode(prefix).map_err(Error::Encoding)?; let prefix_bytes = prefix_bytes.into_owned(); RwCursor::new(txn, self.dbi).map(|cursor| RwPrefix::new(cursor, prefix_bytes)) } @@ -1373,7 +1373,7 @@ impl PolyDatabase { { assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); - let prefix_bytes = KC::bytes_encode(prefix).ok_or(Error::Encoding)?; + let prefix_bytes = KC::bytes_encode(prefix).map_err(Error::Encoding)?; let prefix_bytes = prefix_bytes.into_owned(); RoCursor::new(txn, self.dbi).map(|cursor| RoRevPrefix::new(cursor, prefix_bytes)) } @@ -1442,7 +1442,7 @@ impl PolyDatabase { { assert_eq!(self.env_ident, txn.txn.env.env_mut_ptr() as usize); - let prefix_bytes = KC::bytes_encode(prefix).ok_or(Error::Encoding)?; + let prefix_bytes = KC::bytes_encode(prefix).map_err(Error::Encoding)?; let prefix_bytes = prefix_bytes.into_owned(); RwCursor::new(txn, self.dbi).map(|cursor| RwRevPrefix::new(cursor, prefix_bytes)) } @@ -1492,8 +1492,8 @@ impl PolyDatabase { { assert_eq!(self.env_ident, txn.txn.env.env_mut_ptr() as usize); - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).map_err(Error::Encoding)?; let mut key_val = unsafe { crate::into_val(&key_bytes) }; let mut data_val = unsafe { crate::into_val(&data_bytes) }; @@ -1554,8 +1554,8 @@ impl PolyDatabase { { assert_eq!(self.env_ident, txn.txn.env.env_mut_ptr() as usize); - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).map_err(Error::Encoding)?; let mut key_val = unsafe { crate::into_val(&key_bytes) }; let mut data_val = unsafe { crate::into_val(&data_bytes) }; @@ -1615,7 +1615,7 @@ impl PolyDatabase { { assert_eq!(self.env_ident, txn.txn.env.env_mut_ptr() as usize); - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; let mut key_val = unsafe { crate::into_val(&key_bytes) }; let result = unsafe { diff --git a/heed/src/iter/iter.rs b/heed/src/iter/iter.rs index c50db7fa..804e814f 100644 --- a/heed/src/iter/iter.rs +++ b/heed/src/iter/iter.rs @@ -56,8 +56,8 @@ where match result { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), }, Ok(None) => None, Err(e) => Some(Err(e)), @@ -79,8 +79,8 @@ where match result { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), }, Ok(None) => None, Err(e) => Some(Err(e)), @@ -148,8 +148,8 @@ impl<'txn, KC, DC> RwIter<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).map_err(Error::Encoding)?; self.cursor.put_current(&key_bytes, &data_bytes) } @@ -176,8 +176,8 @@ impl<'txn, KC, DC> RwIter<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).map_err(Error::Encoding)?; self.cursor.append(&key_bytes, &data_bytes) } @@ -223,8 +223,8 @@ where match result { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), }, Ok(None) => None, Err(e) => Some(Err(e)), @@ -246,8 +246,8 @@ where match result { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), }, Ok(None) => None, Err(e) => Some(Err(e)), @@ -308,8 +308,8 @@ where match result { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), }, Ok(None) => None, Err(e) => Some(Err(e)), @@ -331,8 +331,8 @@ where match result { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), }, Ok(None) => None, Err(e) => Some(Err(e)), @@ -400,8 +400,8 @@ impl<'txn, KC, DC> RwRevIter<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).map_err(Error::Encoding)?; self.cursor.put_current(&key_bytes, &data_bytes) } @@ -414,8 +414,8 @@ impl<'txn, KC, DC> RwRevIter<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).map_err(Error::Encoding)?; self.cursor.append(&key_bytes, &data_bytes) } @@ -461,8 +461,8 @@ where match result { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), }, Ok(None) => None, Err(e) => Some(Err(e)), @@ -484,8 +484,8 @@ where match result { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), }, Ok(None) => None, Err(e) => Some(Err(e)), diff --git a/heed/src/iter/prefix.rs b/heed/src/iter/prefix.rs index f8063ce1..9941613d 100644 --- a/heed/src/iter/prefix.rs +++ b/heed/src/iter/prefix.rs @@ -72,8 +72,8 @@ where Ok(Some((key, data))) => { if key.starts_with(&self.prefix) { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -101,8 +101,8 @@ where Ok(Some((key, data))) => { if key.starts_with(&self.prefix) { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -175,8 +175,8 @@ impl<'txn, KC, DC> RwPrefix<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).map_err(Error::Encoding)?; self.cursor.put_current(&key_bytes, &data_bytes) } @@ -203,8 +203,8 @@ impl<'txn, KC, DC> RwPrefix<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).map_err(Error::Encoding)?; self.cursor.append(&key_bytes, &data_bytes) } @@ -253,8 +253,8 @@ where Ok(Some((key, data))) => { if key.starts_with(&self.prefix) { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -282,8 +282,8 @@ where Ok(Some((key, data))) => { if key.starts_with(&self.prefix) { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -352,8 +352,8 @@ where Ok(Some((key, data))) => { if key.starts_with(&self.prefix) { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -383,8 +383,8 @@ where Ok(Some((key, data))) => { if key.starts_with(&self.prefix) { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -457,8 +457,8 @@ impl<'txn, KC, DC> RwRevPrefix<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).map_err(Error::Encoding)?; self.cursor.put_current(&key_bytes, &data_bytes) } @@ -485,8 +485,8 @@ impl<'txn, KC, DC> RwRevPrefix<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).map_err(Error::Encoding)?; self.cursor.append(&key_bytes, &data_bytes) } @@ -535,8 +535,8 @@ where Ok(Some((key, data))) => { if key.starts_with(&self.prefix) { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -566,8 +566,8 @@ where Ok(Some((key, data))) => { if key.starts_with(&self.prefix) { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None diff --git a/heed/src/iter/range.rs b/heed/src/iter/range.rs index ff3c1a9c..9e51825a 100644 --- a/heed/src/iter/range.rs +++ b/heed/src/iter/range.rs @@ -113,8 +113,8 @@ where if must_be_returned { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -148,8 +148,8 @@ where if must_be_returned { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -233,8 +233,8 @@ impl<'txn, KC, DC> RwRange<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).map_err(Error::Encoding)?; self.cursor.put_current(&key_bytes, &data_bytes) } @@ -261,8 +261,8 @@ impl<'txn, KC, DC> RwRange<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).map_err(Error::Encoding)?; self.cursor.append(&key_bytes, &data_bytes) } @@ -318,8 +318,8 @@ where if must_be_returned { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -353,8 +353,8 @@ where if must_be_returned { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -441,8 +441,8 @@ where if must_be_returned { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -478,8 +478,8 @@ where if must_be_returned { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -563,8 +563,8 @@ impl<'txn, KC, DC> RwRevRange<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).map_err(Error::Encoding)?; self.cursor.put_current(&key_bytes, &data_bytes) } @@ -591,8 +591,8 @@ impl<'txn, KC, DC> RwRevRange<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).map_err(Error::Encoding)?; self.cursor.append(&key_bytes, &data_bytes) } @@ -648,8 +648,8 @@ where if must_be_returned { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -685,8 +685,8 @@ where if must_be_returned { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None diff --git a/heed/src/lazy_decode.rs b/heed/src/lazy_decode.rs index b3b4b8e0..3ec3b64e 100644 --- a/heed/src/lazy_decode.rs +++ b/heed/src/lazy_decode.rs @@ -1,6 +1,6 @@ use std::marker; -use crate::{Error, Result}; +use heed_traits::BoxedError; /// Lazily decode the data bytes, it can be used to avoid CPU intensive decoding /// before making sure we really need to decode it (e.g. based on the key). @@ -10,8 +10,8 @@ pub struct LazyDecode(marker::PhantomData); impl<'a, C: 'static> heed_traits::BytesDecode<'a> for LazyDecode { type DItem = Lazy<'a, C>; - fn bytes_decode(bytes: &'a [u8]) -> Option { - Some(Lazy { data: bytes, _phantom: marker::PhantomData }) + fn bytes_decode(bytes: &'a [u8]) -> Result { + Ok(Lazy { data: bytes, _phantom: marker::PhantomData }) } } @@ -23,7 +23,7 @@ pub struct Lazy<'a, C> { } impl<'a, C: heed_traits::BytesDecode<'a>> Lazy<'a, C> { - pub fn decode(&self) -> Result { - C::bytes_decode(self.data).ok_or(Error::Decoding) + pub fn decode(&self) -> Result { + C::bytes_decode(self.data) } } diff --git a/heed/src/lib.rs b/heed/src/lib.rs index 514374ae..141207ab 100644 --- a/heed/src/lib.rs +++ b/heed/src/lib.rs @@ -71,7 +71,7 @@ pub use self::lazy_decode::{Lazy, LazyDecode}; pub use self::mdb::error::Error as MdbError; use self::mdb::ffi::{from_val, into_val}; pub use self::mdb::flags; -pub use self::traits::{BytesDecode, BytesEncode}; +pub use self::traits::{BoxedError, BytesDecode, BytesEncode}; pub use self::txn::{RoTxn, RwTxn}; /// An error that encapsulates all possible errors in this crate. @@ -79,8 +79,8 @@ pub use self::txn::{RoTxn, RwTxn}; pub enum Error { Io(io::Error), Mdb(MdbError), - Encoding, - Decoding, + Encoding(BoxedError), + Decoding(BoxedError), InvalidDatabaseTyping, DatabaseClosing, BadOpenOptions, @@ -91,8 +91,8 @@ impl fmt::Display for Error { match self { Error::Io(error) => write!(f, "{}", error), Error::Mdb(error) => write!(f, "{}", error), - Error::Encoding => f.write_str("error while encoding"), - Error::Decoding => f.write_str("error while decoding"), + Error::Encoding(error) => write!(f, "error while encoding: {}", error), + Error::Decoding(error) => write!(f, "error while decoding: {}", error), Error::InvalidDatabaseTyping => { f.write_str("database was previously opened with different types") }