Skip to content

Commit

Permalink
Fix various CI errors
Browse files Browse the repository at this point in the history
Fixes the following issues:

- Failed to build on MSRV due to Unpin bound
- Failed to build on no_std, as I didn't import Box
- Failed to build on portable-atomic, as Arc didn't implement Unpin
  • Loading branch information
notgull committed Apr 4, 2023
1 parent e001c7a commit 723c328
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ portable-atomic = ["portable-atomic-util", "portable_atomic_crate"]

[dependencies]
parking = { version = "2.0.0", optional = true }
portable-atomic-util = { version = "0.1.1", default-features = false, optional = true, features = ["alloc"] }
portable-atomic-util = { version = "0.1.2", default-features = false, optional = true, features = ["alloc"] }

[dependencies.portable_atomic_crate]
package = "portable-atomic"
Expand Down
33 changes: 17 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ mod notify;

use alloc::boxed::Box;

use core::borrow::Borrow;
use core::fmt;
use core::future::Future;
use core::marker::PhantomPinned;
use core::mem::ManuallyDrop;
use core::ops::Deref;
use core::pin::Pin;
use core::ptr;
use core::task::{Context, Poll, Waker};
Expand All @@ -99,7 +99,7 @@ use sync::{Arc, WithMut};

pub use notify::{Additional, IntoNotification, Notification, Notify, Tag, TagWith};

/// Useful trait for listeners.
/// Useful traits for notifications.
pub mod prelude {
pub use crate::{IntoNotification, Notification};
}
Expand Down Expand Up @@ -129,7 +129,7 @@ struct Inner<T> {
list: sys::List<T>,
}

impl<T: Unpin> Inner<T> {
impl<T> Inner<T> {
fn new() -> Self {
Self {
notified: AtomicUsize::new(core::usize::MAX),
Expand Down Expand Up @@ -180,14 +180,14 @@ impl<T> fmt::Debug for Event<T> {
}
}

impl<T: Unpin> Default for Event<T> {
impl<T> Default for Event<T> {
#[inline]
fn default() -> Self {
Self::with_tag()
}
}

impl<T: Unpin> Event<T> {
impl<T> Event<T> {
/// Creates a new `Event` with a tag type.
///
/// # Examples
Expand Down Expand Up @@ -347,7 +347,7 @@ impl<T: Unpin> Event<T> {

if let Some(inner) = self.try_inner() {
let limit = if notify.is_additional() {
usize::MAX
core::usize::MAX
} else {
notify.count()
};
Expand Down Expand Up @@ -600,15 +600,15 @@ impl<T> Drop for Event<T> {
/// If a notified listener is dropped without receiving a notification, dropping will notify
/// another active listener. Whether one *additional* listener will be notified depends on what
/// kind of notification was delivered.
pub struct EventListener<T: Unpin = ()>(Listener<T, Arc<Inner<T>>>);
pub struct EventListener<T = ()>(Listener<T, Arc<Inner<T>>>);

impl<T: Unpin> fmt::Debug for EventListener<T> {
impl<T> fmt::Debug for EventListener<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("EventListener { .. }")
}
}

impl<T: Unpin> EventListener<T> {
impl<T> EventListener<T> {
/// Create a new `EventListener` that will wait for a notification from the given [`Event`].
pub fn new(event: &Event<T>) -> Self {
let inner = event.inner();
Expand Down Expand Up @@ -762,15 +762,15 @@ impl<T: Unpin> EventListener<T> {
}
}

impl<T: Unpin> Future for EventListener<T> {
impl<T> Future for EventListener<T> {
type Output = T;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.listener().poll_internal(cx)
}
}

struct Listener<T: Unpin, B: Deref<Target = Inner<T>> + Unpin> {
struct Listener<T, B: Borrow<Inner<T>> + Unpin> {
/// The reference to the original event.
event: B,

Expand All @@ -781,10 +781,10 @@ struct Listener<T: Unpin, B: Deref<Target = Inner<T>> + Unpin> {
_pin: PhantomPinned,
}

unsafe impl<T: Send + Unpin, B: Deref<Target = Inner<T>> + Unpin + Send> Send for Listener<T, B> {}
unsafe impl<T: Send + Unpin, B: Deref<Target = Inner<T>> + Unpin + Sync> Sync for Listener<T, B> {}
unsafe impl<T: Send, B: Borrow<Inner<T>> + Unpin + Send> Send for Listener<T, B> {}
unsafe impl<T: Send, B: Borrow<Inner<T>> + Unpin + Sync> Sync for Listener<T, B> {}

impl<T: Unpin, B: Deref<Target = Inner<T>> + Unpin> Listener<T, B> {
impl<T, B: Borrow<Inner<T>> + Unpin> Listener<T, B> {
/// Pin-project this listener.
fn project(self: Pin<&mut Self>) -> (&Inner<T>, Pin<&mut Option<sys::Listener<T>>>) {
// SAFETY: `event` is `Unpin`, and `listener`'s pin status is preserved
Expand All @@ -793,7 +793,7 @@ impl<T: Unpin, B: Deref<Target = Inner<T>> + Unpin> Listener<T, B> {
event, listener, ..
} = self.get_unchecked_mut();

(&*event, Pin::new_unchecked(listener))
((*event).borrow(), Pin::new_unchecked(listener))
}
}

Expand Down Expand Up @@ -910,7 +910,7 @@ impl<T: Unpin, B: Deref<Target = Inner<T>> + Unpin> Listener<T, B> {
}
}

impl<T: Unpin, B: Deref<Target = Inner<T>> + Unpin> Drop for Listener<T, B> {
impl<T, B: Borrow<Inner<T>> + Unpin> Drop for Listener<T, B> {
fn drop(&mut self) {
// If we're being dropped, we need to remove ourself from the list.
let (inner, listener) = unsafe { Pin::new_unchecked(self).project() };
Expand Down Expand Up @@ -949,6 +949,7 @@ impl<T> State<T> {
}

/// If this state was notified, return the tag associated with the notification.
#[allow(unused)]
fn notified(self) -> Option<T> {
match self {
Self::Notified { tag, .. } => Some(tag),
Expand Down
19 changes: 11 additions & 8 deletions src/no_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ use core::num::NonZeroUsize;
use core::ops;
use core::pin::Pin;

use alloc::boxed::Box;
use alloc::vec::Vec;

impl<T: Unpin> crate::Inner<T> {
impl<T> crate::Inner<T> {
/// Locks the list.
fn try_lock(&self) -> Option<ListGuard<'_, T>> {
self.list.inner.try_lock().map(|guard| ListGuard {
Expand Down Expand Up @@ -218,7 +219,7 @@ pub(crate) struct List<T> {
queue: Queue<T>,
}

impl<T: Unpin> List<T> {
impl<T> List<T> {
pub(super) fn new() -> List<T> {
List {
inner: Mutex::new(ListenerSlab::new()),
Expand All @@ -228,15 +229,15 @@ impl<T: Unpin> List<T> {
}

/// The guard returned by [`Inner::lock`].
pub(crate) struct ListGuard<'a, T: Unpin> {
pub(crate) struct ListGuard<'a, T> {
/// Reference to the inner state.
pub(crate) inner: &'a crate::Inner<T>,

/// The locked list.
pub(crate) guard: Option<MutexGuard<'a, ListenerSlab<T>>>,
}

impl<T: Unpin> ListGuard<'_, T> {
impl<T> ListGuard<'_, T> {
#[cold]
fn process_nodes_slow(
&mut self,
Expand All @@ -254,21 +255,21 @@ impl<T: Unpin> ListGuard<'_, T> {
}
}

impl<T: Unpin> ops::Deref for ListGuard<'_, T> {
impl<T> ops::Deref for ListGuard<'_, T> {
type Target = ListenerSlab<T>;

fn deref(&self) -> &Self::Target {
self.guard.as_ref().unwrap()
}
}

impl<T: Unpin> ops::DerefMut for ListGuard<'_, T> {
impl<T> ops::DerefMut for ListGuard<'_, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.guard.as_mut().unwrap()
}
}

impl<T: Unpin> Drop for ListGuard<'_, T> {
impl<T> Drop for ListGuard<'_, T> {
fn drop(&mut self) {
let Self { inner, guard } = self;
let mut list = guard.take().unwrap();
Expand Down Expand Up @@ -443,7 +444,7 @@ pub(crate) struct ListenerSlab<T> {
first_empty: NonZeroUsize,
}

impl<T: Unpin> ListenerSlab<T> {
impl<T> ListenerSlab<T> {
/// Create a new, empty list.
pub(crate) fn new() -> Self {
Self {
Expand Down Expand Up @@ -665,6 +666,8 @@ pub(crate) enum Listener<T> {
_EatLifetime(PhantomData<T>),
}

impl<T> Unpin for Listener<T> {}

impl<T> PartialEq for Listener<T> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
Expand Down
2 changes: 1 addition & 1 deletion src/no_std/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub(crate) struct TaskWaiting {
entry_id: AtomicUsize,
}

impl<T: Unpin> Node<T> {
impl<T> Node<T> {
pub(crate) fn listener() -> (Self, Arc<TaskWaiting>) {
// Create a new `TaskWaiting` structure.
let task_waiting = Arc::new(TaskWaiting {
Expand Down
2 changes: 1 addition & 1 deletion src/notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ macro_rules! impl_for_numeric_types {
type Tag = ();
type Notify = Notify;

#[allow(unused_comparisons)]
fn into_notification(self) -> Self::Notify {
#[allow(unused_comparisons)]
if self < 0 {
panic!("negative notification count");
}
Expand Down

0 comments on commit 723c328

Please sign in to comment.