-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compute an error when encoding/decoding in bytes
- Loading branch information
1 parent
f3213da
commit 1fd8522
Showing
19 changed files
with
190 additions
and
183 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
use std::borrow::Cow; | ||
use std::error::Error as StdError; | ||
|
||
/// A boxed `Send + Sync + 'static` error. | ||
pub type BoxedError = Box<dyn StdError + Send + Sync + 'static>; | ||
|
||
pub trait BytesEncode<'a> { | ||
type EItem: ?Sized + 'a; | ||
|
||
fn bytes_encode(item: &'a Self::EItem) -> Option<Cow<'a, [u8]>>; | ||
fn bytes_encode(item: &'a Self::EItem) -> Result<Cow<'a, [u8]>, BoxedError>; | ||
} | ||
|
||
pub trait BytesDecode<'a> { | ||
type DItem: 'a; | ||
|
||
fn bytes_decode(bytes: &'a [u8]) -> Option<Self::DItem>; | ||
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError>; | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
use std::borrow::Cow; | ||
|
||
use heed_traits::{BytesDecode, BytesEncode}; | ||
use heed_traits::{BoxedError, BytesDecode, BytesEncode}; | ||
|
||
/// Describes an [`prim@str`]. | ||
pub struct Str; | ||
|
||
impl BytesEncode<'_> for Str { | ||
type EItem = str; | ||
|
||
fn bytes_encode(item: &Self::EItem) -> Option<Cow<[u8]>> { | ||
Some(Cow::Borrowed(item.as_bytes())) | ||
fn bytes_encode(item: &Self::EItem) -> Result<Cow<[u8]>, BoxedError> { | ||
Ok(Cow::Borrowed(item.as_bytes())) | ||
} | ||
} | ||
|
||
impl<'a> BytesDecode<'a> for Str { | ||
type DItem = &'a str; | ||
|
||
fn bytes_decode(bytes: &'a [u8]) -> Option<Self::DItem> { | ||
std::str::from_utf8(bytes).ok() | ||
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError> { | ||
std::str::from_utf8(bytes).map_err(Into::into) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
use std::borrow::Cow; | ||
|
||
use heed_traits::{BytesDecode, BytesEncode}; | ||
use bytemuck::PodCastError; | ||
use heed_traits::{BoxedError, BytesDecode, BytesEncode}; | ||
|
||
/// Describes the `()` type. | ||
pub struct Unit; | ||
|
||
impl BytesEncode<'_> for Unit { | ||
type EItem = (); | ||
|
||
fn bytes_encode(_item: &Self::EItem) -> Option<Cow<[u8]>> { | ||
Some(Cow::Borrowed(&[])) | ||
fn bytes_encode(_item: &Self::EItem) -> Result<Cow<[u8]>, BoxedError> { | ||
Ok(Cow::Borrowed(&[])) | ||
} | ||
} | ||
|
||
impl BytesDecode<'_> for Unit { | ||
type DItem = (); | ||
|
||
fn bytes_decode(bytes: &[u8]) -> Option<Self::DItem> { | ||
fn bytes_decode(bytes: &[u8]) -> Result<Self::DItem, BoxedError> { | ||
if bytes.is_empty() { | ||
Some(()) | ||
Ok(()) | ||
} else { | ||
None | ||
Err(PodCastError::SizeMismatch.into()) | ||
} | ||
} | ||
} |
Oops, something went wrong.