Skip to content

Commit

Permalink
Remove uses of anyhow
Browse files Browse the repository at this point in the history
This allows crates using epserde to unwrap errors and `match` them.

In my case, I would like to display a custom message on WrongTypeHash/VersionMismatch with
instructions to regenerate files with the current epserde version.
  • Loading branch information
progval committed Aug 16, 2024
1 parent d309a71 commit 8a94c29
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
4 changes: 3 additions & 1 deletion epserde/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ bitflags = {version="2.4.2", default-features=false }
xxhash-rust = {version="0.8.8", default-features=false, features=["xxh3"] }
epserde-derive = { version="=0.6.0", optional = true }
#epserde-derive = { path="../epserde-derive", optional = true }
anyhow = "1.0.79"
thiserror = "1.0.63"
sealed = "0.5.0"
maligned = "0.2.1"
common_traits = "0.10.2"
mem_dbg = {version="0.2.1", features=["maligned", "mmap-rs"]}

[dev-dependencies]
anyhow = "1.0.79"

[features]
default = ["std", "mmap-rs", "derive"]
derive = ["epserde-derive"]
Expand Down
39 changes: 30 additions & 9 deletions epserde/src/deser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,17 @@ pub trait Deserialize: TypeHash + ReprHash + DeserializeInner {
/// a higher alignment requirement will cause an [alignment error](`Error::AlignmentError`).
fn load_mem<'a>(
path: impl AsRef<Path>,
) -> anyhow::Result<MemCase<<Self as DeserializeInner>::DeserType<'a>>> {
) -> Result<MemCase<<Self as DeserializeInner>::DeserType<'a>>> {
let align_to = align_of::<MemoryAlignment>();
if align_of::<Self>() > align_to {
return Err(Error::AlignmentError.into());
}
let file_len = path.as_ref().metadata()?.len() as usize;
let mut file = std::fs::File::open(path)?;
let file_len = path
.as_ref()
.metadata()
.map_err(Error::FileMetadataError)?
.len() as usize;
let mut file = std::fs::File::open(path).map_err(Error::FileOpenError)?;
// Round up to u128 size
let capacity = file_len + crate::pad_align_to(file_len, align_to);

Expand Down Expand Up @@ -131,9 +135,13 @@ pub trait Deserialize: TypeHash + ReprHash + DeserializeInner {
fn load_mmap<'a>(
path: impl AsRef<Path>,
flags: Flags,
) -> anyhow::Result<MemCase<<Self as DeserializeInner>::DeserType<'a>>> {
let file_len = path.as_ref().metadata()?.len() as usize;
let mut file = std::fs::File::open(path)?;
) -> Result<MemCase<<Self as DeserializeInner>::DeserType<'a>>> {
let file_len = path
.as_ref()
.metadata()
.map_err(Error::FileMetadataError)?
.len() as usize;
let mut file = std::fs::File::open(path).map_err(Error::FileOpenError)?;
let capacity = file_len + crate::pad_align_to(file_len, 16);

let mut uninit: MaybeUninit<MemCase<<Self as DeserializeInner>::DeserType<'_>>> =
Expand Down Expand Up @@ -175,9 +183,13 @@ pub trait Deserialize: TypeHash + ReprHash + DeserializeInner {
fn mmap<'a>(
path: impl AsRef<Path>,
flags: Flags,
) -> anyhow::Result<MemCase<<Self as DeserializeInner>::DeserType<'a>>> {
let file_len = path.as_ref().metadata()?.len();
let file = std::fs::File::open(path)?;
) -> Result<MemCase<<Self as DeserializeInner>::DeserType<'a>>> {
let file_len = path
.as_ref()
.metadata()
.map_err(Error::FileMetadataError)?
.len();
let file = std::fs::File::open(path).map_err(Error::FileOpenError)?;

let mut uninit: MaybeUninit<MemCase<<Self as DeserializeInner>::DeserType<'_>>> =
MaybeUninit::uninit();
Expand Down Expand Up @@ -326,6 +338,9 @@ pub trait DeserializeHelper<T: CopySelector> {
/// Errors that can happen during deserialization.
pub enum Error {
#[error("Error reading stats for file during ε-serde deserialization: {0}")]
/// [`Deserialize::load_full`] could not stat the provided file.
FileMetadataError(std::io::Error),
#[error("Error opening file during ε-serde deserialization: {0}")]
/// [`Deserialize::load_full`] could not open the provided file.
FileOpenError(std::io::Error),
#[error("Read error during ε-serde deserialization")]
Expand Down Expand Up @@ -391,4 +406,10 @@ The serialized type is '{expected_type_name}' and the deserialized type is '{got
expected: u64,
got: u64,
},
/// Error returned by [`std::alloc::Layout::from_size_align`]
#[error("Incompatible memory alignment: {0}")]
LayoutError(#[from] std::alloc::LayoutError),
/// Error returned by [`std::alloc::Layout::from_size_align`]
#[error("Could not mmap: {0}")]
MmapError(#[from] mmap_rs::Error),
}

0 comments on commit 8a94c29

Please sign in to comment.