Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize waker size for requirements #40

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{dealloc, Channel};
use super::{dealloc, Channel, DefaultWaker};
use core::fmt;
use core::mem;
use core::ptr::NonNull;
Expand All @@ -8,21 +8,21 @@ use core::ptr::NonNull;
/// has already been dropped.
///
/// The message that could not be sent can be retreived again with [`SendError::into_inner`].
pub struct SendError<T> {
channel_ptr: NonNull<Channel<T>>,
pub struct SendError<T, W = DefaultWaker> {
channel_ptr: NonNull<Channel<T, W>>,
}

unsafe impl<T: Send> Send for SendError<T> {}
unsafe impl<T: Sync> Sync for SendError<T> {}
unsafe impl<T: Send, W> Send for SendError<T, W> {}
unsafe impl<T: Sync, W> Sync for SendError<T, W> {}

impl<T> SendError<T> {
impl<T, W> SendError<T, W> {
/// # Safety
///
/// By calling this function, the caller semantically transfers ownership of the
/// channel's resources to the created `SendError`. Thus the caller must ensure that the
/// pointer is not used in a way which would violate this ownership transfer. Moreover,
/// the caller must assert that the channel contains a valid, initialized message.
pub(crate) const unsafe fn new(channel_ptr: NonNull<Channel<T>>) -> Self {
pub(crate) const unsafe fn new(channel_ptr: NonNull<Channel<T, W>>) -> Self {
Self { channel_ptr }
}

Expand All @@ -35,7 +35,7 @@ impl<T> SendError<T> {
mem::forget(self);

// SAFETY: we have ownership of the channel
let channel: &Channel<T> = unsafe { channel_ptr.as_ref() };
let channel: &Channel<T, W> = unsafe { channel_ptr.as_ref() };

// SAFETY: we know that the message is initialized according to the safety requirements of
// `new`
Expand All @@ -54,7 +54,7 @@ impl<T> SendError<T> {
}
}

impl<T> Drop for SendError<T> {
impl<T, W> Drop for SendError<T, W> {
fn drop(&mut self) {
// SAFETY: we have ownership of the channel and require that the message is initialized
// upon construction
Expand All @@ -65,20 +65,20 @@ impl<T> Drop for SendError<T> {
}
}

impl<T> fmt::Display for SendError<T> {
impl<T, W> fmt::Display for SendError<T, W> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
"sending on a closed channel".fmt(f)
}
}

impl<T> fmt::Debug for SendError<T> {
impl<T, W> fmt::Debug for SendError<T, W> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "SendError<{}>(_)", stringify!(T))
}
}

#[cfg(feature = "std")]
impl<T> std::error::Error for SendError<T> {}
impl<T, W> std::error::Error for SendError<T, W> {}

/// An error returned from the blocking [`Receiver::recv`](crate::Receiver::recv) method.
///
Expand Down
Loading
Loading