Skip to content

Commit

Permalink
Overhaul error reporting (#1144)
Browse files Browse the repository at this point in the history
This PR overhauls error reporting, replacing our `Option` returns
with `Result`s. The `Err` type of these results provide both the
cause of the failure, and access to the underlying source of the
conversion.

Makes progress towards #1139.
  • Loading branch information
jswrenn authored May 3, 2024
1 parent dd7dc7d commit 5f2478d
Show file tree
Hide file tree
Showing 8 changed files with 906 additions and 284 deletions.
2 changes: 1 addition & 1 deletion src/byteorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
//!
//! impl<B: SplitByteSlice> UdpPacket<B> {
//! fn parse(bytes: B) -> Option<UdpPacket<B>> {
//! let (header, body) = Ref::new_from_prefix(bytes)?;
//! let (header, body) = Ref::new_from_prefix(bytes).ok()?;
//! Some(UdpPacket { header, body })
//! }
//!
Expand Down
12 changes: 6 additions & 6 deletions src/deprecated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ where
#[doc(hidden)]
#[inline]
pub fn new_slice(bytes: B) -> Option<Ref<B, [T]>> {
Self::new(bytes)
Self::new(bytes).ok()
}
}

Expand All @@ -34,7 +34,7 @@ where
#[doc(hidden)]
#[inline(always)]
pub fn new_slice_unaligned(bytes: B) -> Option<Ref<B, [T]>> {
Ref::new_unaligned(bytes)
Ref::new_unaligned(bytes).ok()
}
}

Expand Down Expand Up @@ -74,15 +74,15 @@ where
#[doc(hidden)]
#[inline]
pub fn new_slice_from_prefix(bytes: B, count: usize) -> Option<(Ref<B, [T]>, B)> {
Ref::with_trailing_elements_from_prefix(bytes, count)
Ref::with_trailing_elements_from_prefix(bytes, count).ok()
}

#[deprecated(since = "0.8.0", note = "replaced by `Ref::with_trailing_elements_from_suffix`")]
#[must_use = "has no side effects"]
#[doc(hidden)]
#[inline]
pub fn new_slice_from_suffix(bytes: B, count: usize) -> Option<(B, Ref<B, [T]>)> {
Ref::with_trailing_elements_from_suffix(bytes, count)
Ref::with_trailing_elements_from_suffix(bytes, count).ok()
}
}

Expand All @@ -99,7 +99,7 @@ where
#[must_use = "has no side effects"]
#[inline(always)]
pub fn new_slice_unaligned_from_prefix(bytes: B, count: usize) -> Option<(Ref<B, [T]>, B)> {
Ref::with_trailing_elements_unaligned_from_prefix(bytes, count)
Ref::with_trailing_elements_unaligned_from_prefix(bytes, count).ok()
}

#[deprecated(
Expand All @@ -110,6 +110,6 @@ where
#[must_use = "has no side effects"]
#[inline(always)]
pub fn new_slice_unaligned_from_suffix(bytes: B, count: usize) -> Option<(B, Ref<B, [T]>)> {
Ref::with_trailing_elements_unaligned_from_suffix(bytes, count)
Ref::with_trailing_elements_unaligned_from_suffix(bytes, count).ok()
}
}
Loading

0 comments on commit 5f2478d

Please sign in to comment.