Skip to content

Commit

Permalink
feat: add into_inner and Error impl to Full (#43)
Browse files Browse the repository at this point in the history
Signed-off-by: Eliza Weisman <[email protected]>
  • Loading branch information
hawkw authored Mar 15, 2022
1 parent 463d542 commit 527a639
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ pub struct Ref<'slot, T> {
new_state: usize,
}

/// Error returned when sending a message failed because a channel is at capacity.
#[derive(Eq, PartialEq)]
/// Error indicating that a `push` operation failed because a queue was at
/// capacity.
///
/// This is returned by the [`ThingBuf::push`] and [`ThingBuf::push_ref`] (and
/// [`StaticThingBuf::push`]/[`StaticThingBuf::push_ref`]) methods.
#[derive(PartialEq, Eq)]
pub struct Full<T = ()>(T);

/// State variables for the atomic ring buffer algorithm.
Expand Down Expand Up @@ -524,6 +528,18 @@ impl<T> Slot<T> {

unsafe impl<T: Sync> Sync for Slot<T> {}

// === impl Full ===

impl<T> Full<T> {
/// Unwraps the inner `T` value held by this error.
///
/// This method allows recovering the original message when sending to a
/// channel has failed.
pub fn into_inner(self) -> T {
self.0
}
}

impl<T> fmt::Debug for Full<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Full(..)")
Expand All @@ -532,6 +548,9 @@ impl<T> fmt::Debug for Full<T> {

impl<T> fmt::Display for Full<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("channel full")
f.write_str("queue at capacity")
}
}

#[cfg(feature = "std")]
impl<T> std::error::Error for Full<T> {}
2 changes: 2 additions & 0 deletions src/mpsc/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use core::fmt;
/// [`StaticSender::try_send`]: super::StaticSender::try_send
/// [`StaticSender::try_send_ref`]: super::StaticSender::try_send_ref
#[non_exhaustive]
#[derive(PartialEq, Eq)]
pub enum TrySendError<T = ()> {
/// The data could not be sent on the channel because the channel is
/// currently full and sending would require waiting for capacity.
Expand All @@ -29,6 +30,7 @@ pub enum TrySendError<T = ()> {
/// [`StaticSender::send`]: super::StaticSender::send
/// [`StaticSender::send_ref`]: super::StaticSender::send_ref
/// [`Receiver`]: super::Receiver
#[derive(PartialEq, Eq)]
pub struct Closed<T = ()>(pub(crate) T);

// === impl Closed ===
Expand Down

0 comments on commit 527a639

Please sign in to comment.