You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR is an attempt at getting rid of the BoxedError type we currently use everywhere.
That allows us to use a « real » type when an error arises in the BytesEncode or BytesDecode traits.
The new traits
/// A trait that represents an encoding structure.pubtraitBytesEncode<'a>{typeEItem: ?Sized + 'a;typeErr;fnbytes_encode(item:&'aSelf::EItem) -> Result<Cow<'a,[u8]>,Self::Err>;}/// A trait that represents a decoding structure.pubtraitBytesDecode<'a>{typeDItem:'a;typeErr;fnbytes_decode(bytes:&'a[u8]) -> Result<Self::DItem,Self::Err>;}
I called the type Err to do something similar to the FromStr trait from the stdlib.
The new Error enum
/// An error that encapsulates all possible errors in this crate.#[derive(Debug)]pubenumError<E,D>{Io(io::Error),Mdb(MdbError),Encoding(E),Decoding(D),InvalidDatabaseTyping,DatabaseClosing,BadOpenOptions{/// The options that were used to originaly open this env.options:EnvOpenOptions,/// The env opened with the original options.env:Env,},}
I had to make the Error enum generic over the decoding and encoding error.
For most functions, that do not add any complexity because we use our Result type;
/// Either a success or an [`Error`].pubtypeResult<T,E = Infallible,D = Infallible> = result::Result<T,Error<E,D>>;
That set E and D to Infallible by default.
My issue
For some methods like the following one, we can return an error while encoding the key OR the value + while decoding the key OR the value:
I tried to introduce more variants to the Error enum to represent the key encoding/decoding and data encoding/decoding possible errors.
Unfortunately, it's not that easy to convert the generic Error types between them. Some functions return a Result<Infallible, Infallible, Infallible, Infallible> and it is not easy to convert that into a Result<KE, KD, DE, DD>.
For some methods like the following one, we can return an error while encoding the key OR the value + while decoding the key OR the value:
Instead of adding two variants to the enum, a new enum can be made that can hold two (different) encoding errors (or use a crate like either). The method in your example can then return Result<Option<(KC::DItem, DC::DItem)>, KC::Err, Either<KC::Err, DC::Err>>.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR is an attempt at getting rid of the
BoxedErrortype we currently use everywhere.That allows us to use a « real » type when an error arises in the
BytesEncodeorBytesDecodetraits.The new traits
I called the type
Errto do something similar to theFromStrtrait from the stdlib.The new
ErrorenumI had to make the
Errorenum generic over the decoding and encoding error.For most functions, that do not add any complexity because we use our
Resulttype;That set
EandDtoInfallibleby default.My issue
For some methods like the following one, we can return an error while encoding the key OR the value + while decoding the key OR the value:
One solution could be adding two other variants to the
Errorenum.