Skip to content

Commit

Permalink
Build tests for no-default-features into C/I
Browse files Browse the repository at this point in the history
  • Loading branch information
notgull committed Mar 31, 2023
1 parent 1ec136a commit 2ac3be0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
run: cargo check -Z features=dev_dep
- run: cargo test --all
- run: cargo test --no-default-features --tests
- run: cargo build -p event-listener-strategy --no-default-features
- name: Install cargo-hack
uses: taiki-e/install-action@cargo-hack
- run: rustup target add thumbv7m-none-eabi
Expand All @@ -56,6 +57,7 @@ jobs:
- name: Install Rust
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
- run: cargo build --all
- run: cargo build --all --no-default-features

clippy:
runs-on: ubuntu-latest
Expand Down
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ use core::pin::Pin;
use core::ptr;
use core::task::{Context, Poll, Waker};

#[cfg(feature = "std")]
use parking::{Parker, Unparker};
#[cfg(feature = "std")]
use std::time::{Duration, Instant};

Expand Down Expand Up @@ -661,7 +663,7 @@ impl<B: Borrow<Inner> + Unpin> Listener<B> {

std::thread_local! {
/// Cached thread-local parker/unparker pair.
static PARKER: RefCell<Option<(parking::Parker, Task)>> = RefCell::new(None);
static PARKER: RefCell<Option<(Parker, Task)>> = RefCell::new(None);
}

// Try to borrow the thread-local parker/unparker pair.
Expand Down Expand Up @@ -693,7 +695,7 @@ impl<B: Borrow<Inner> + Unpin> Listener<B> {
fn wait_with_parker(
self: Pin<&mut Self>,
deadline: Option<Instant>,
parker: &parking::Parker,
parker: &Parker,
unparker: TaskRef<'_>,
) -> bool {
let (inner, mut listener) = self.project();
Expand Down Expand Up @@ -818,7 +820,7 @@ enum Task {

/// An unparker that wakes up a thread.
#[cfg(feature = "std")]
Unparker(parking::Unparker),
Unparker(Unparker),
}

impl Task {
Expand Down Expand Up @@ -855,7 +857,7 @@ enum TaskRef<'a> {

/// An unparker that wakes up a thread.
#[cfg(feature = "std")]
Unparker(&'a parking::Unparker),
Unparker(&'a Unparker),
}

impl TaskRef<'_> {
Expand Down
52 changes: 41 additions & 11 deletions src/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,24 @@ impl crate::Inner {
let entry = unsafe {
// SAFETY: We never move out the `link` field.
let listener = match listener.get_unchecked_mut() {
listener @ None => listener.insert(Listener {
link: UnsafeCell::new(Link {
state: Cell::new(State::Created),
prev: Cell::new(inner.tail),
next: Cell::new(None),
}),
_pin: PhantomPinned,
}),
listener @ None => {
// TODO: Use Option::insert once the MSRV is high enough.
*listener = Some(Listener {
link: UnsafeCell::new(Link {
state: Cell::new(State::Created),
prev: Cell::new(inner.tail),
next: Cell::new(None),
}),
_pin: PhantomPinned,
});

listener.as_mut().unwrap()
}
Some(_) => return,
};

// Get the inner pointer.
&mut *listener.link.get()
&*listener.link.get()
};

// Replace the tail with the new entry.
Expand Down Expand Up @@ -123,7 +128,7 @@ impl crate::Inner {
let entry = unsafe {
// SAFETY: We never move out the `link` field.
let listener = listener.as_mut().get_unchecked_mut().as_mut()?;
&mut *listener.link.get()
&*listener.link.get()
};

// Take out the state and check it.
Expand Down Expand Up @@ -183,7 +188,7 @@ impl Inner {
match next {
None => self.tail = prev,
Some(n) => unsafe {
n.as_ref().next.set(prev);
n.as_ref().prev.set(prev);
},
}

Expand Down Expand Up @@ -340,5 +345,30 @@ mod tests {
// Remove one.
assert_eq!(inner.remove(listen2, false), Some(State::Created));
assert_eq!(inner.lock().len, 2);

// Remove another.
assert_eq!(inner.remove(listen1, false), Some(State::Created));
assert_eq!(inner.lock().len, 1);
}

#[test]
fn drop_non_notified() {
let inner = crate::Inner::new();
make_listeners!(listen1, listen2, listen3);

// Register the listeners.
inner.insert(listen1.as_mut());
inner.insert(listen2.as_mut());
inner.insert(listen3.as_mut());

// Notify one.
inner.notify(1, false);

// Remove one.
inner.remove(listen3, true);

// Remove the rest.
inner.remove(listen1, true);
inner.remove(listen2, true);
}
}

0 comments on commit 2ac3be0

Please sign in to comment.