Skip to content

Commit

Permalink
Split out NotifyExt from Notify trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
AldaronLau committed Feb 19, 2023
1 parent 5bc14ba commit fd89bff
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 55 deletions.
11 changes: 8 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,32 @@ and this project adheres to [Semantic Versioning](https://jeronlau.tk/semver/).

## [0.14.0] - Unreleased
### Added
- `notify::pending()`
- `notify::ready()`
- `NotifyExt` trait, split off from `Notify`
- `NotifyExt` to the prelude
- `notify::pending()`, `notify::Pending`
- `notify::ready()`, `notify::Ready`
- `notify::future_fn()` to match the rest of the async ecosystem
- `notify::poll_fn()` to match the rest of the async ecosystem
- `notify::Map`, returned from `Notify::map()`
- `notify::Next`, returned from `Notify::next()`
- Missing `impl Debug for BoxNotify` (was already on `LocalBoxNotify`).
- Missing `impl Debug for BoxNotify` (was already on `LocalBoxNotify`)

### Changed
- Rename `Notifier` to `notify::Notify`
- Rename `BoxNotifier` to `notify::BoxNotify`
- Rename `LocalBoxNotifier` to `notify::LocalBoxNotify`
- Rename `Poller` to `PollFn`
- Rename `Loop` to `FutureFn`
- Rename `Join` to `Loop`

### Removed
- Unused dependency `pin_utils`
- `Poller::new()`; use `notify::poll_fn()` instead
- `Loop::new()`; use `notify::future_fn()` instead
- `Loop::pin()`; use `notify::future_fn()` instead
- Third generic for `FutureFn`
- `Notify::map()`, use `NotifyExt::map()` instead
- `Notify::next()`, use `NotifyExt::next()` instead

## [0.13.1] - 2023-01-28
### Fixed
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub mod prelude {

#[doc(no_inline)]
pub use crate::{
notify::{BoxNotify, Fuse, LocalBoxNotify, Notify},
notify::{BoxNotify, Fuse, LocalBoxNotify, Notify, NotifyExt},
Spawn,
};

Expand Down
108 changes: 57 additions & 51 deletions src/notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,55 +59,6 @@ pub trait Notify {
/// - `Poll::Pending` - Not ready yet
/// - `Poll::Ready(val)` - Ready with next value
fn poll_next(self: Pin<&mut Self>, t: &mut Task<'_>) -> Poll<Self::Event>;

/// Get the next [`Self::Event`]
///
/// # Usage
/// ```rust
/// use pasts::prelude::*;
///
/// struct MyAsyncIter;
///
/// impl Notify for MyAsyncIter {
/// type Event = Option<u32>;
///
/// fn poll_next(self: Pin<&mut Self>, _: &mut Task<'_>) -> Poll<Self::Event> {
/// Ready(Some(1))
/// }
/// }
///
/// #[async_main::async_main]
/// async fn main(_spawner: impl Spawn) {
/// let mut count = 0;
/// let mut async_iter = MyAsyncIter;
/// let mut iterations = 0;
/// while let Some(i) = async_iter.next().await {
/// count += i;
/// iterations += 1;
/// if iterations == 3 {
/// break;
/// }
/// }
/// assert_eq!(count, 3);
/// }
/// ```
#[inline]
fn next(&mut self) -> Next<'_, Self>
where
Self: Sized + Unpin,
{
Next(self)
}

/// Transform produced [`Self::Event`]s with a function.
fn map<B, F>(self, f: F) -> Map<Self, F>
where
Self: Sized + Unpin,
{
let noti = self;

Map { noti, f }
}
}

impl<N> Notify for Box<N>
Expand Down Expand Up @@ -164,7 +115,62 @@ where
}
}

/// The [`Future`] returned from [`Notify::next()`]
/// An extension trait for [`Notify`]s that provides a variety of convenient
/// adapters.
pub trait NotifyExt: Notify {
/// Get the next [`Notify::Event`]
///
/// # Usage
/// ```rust
/// use pasts::prelude::*;
///
/// struct MyAsyncIter;
///
/// impl Notify for MyAsyncIter {
/// type Event = Option<u32>;
///
/// fn poll_next(self: Pin<&mut Self>, _: &mut Task<'_>) -> Poll<Self::Event> {
/// Ready(Some(1))
/// }
/// }
///
/// #[async_main::async_main]
/// async fn main(_spawner: impl Spawn) {
/// let mut count = 0;
/// let mut async_iter = MyAsyncIter;
/// let mut iterations = 0;
/// while let Some(i) = async_iter.next().await {
/// count += i;
/// iterations += 1;
/// if iterations == 3 {
/// break;
/// }
/// }
/// assert_eq!(count, 3);
/// }
/// ```
#[inline]
fn next(&mut self) -> Next<'_, Self>
where
Self: Sized + Unpin,
{
Next(self)
}

/// Transform produced [`Notify::Event`]s with a function.
fn map<B, F>(self, f: F) -> Map<Self, F>
where
Self: Sized + Unpin,
{
let noti = self;

Map { noti, f }
}
}

impl<N: Notify> NotifyExt for N {}

/// The [`Future`] returned from [`NotifyExt::next()`]
#[derive(Debug)]
pub struct Next<'a, N>(&'a mut N)
where
Expand Down Expand Up @@ -211,7 +217,7 @@ impl<F: Future> Notify for Option<F> {
}
}

/// The [`Notify`] returned from [`Notify::map()`]
/// The [`Notify`] returned from [`NotifyExt::map()`]
#[derive(Debug)]
pub struct Map<N, F> {
noti: N,
Expand Down

0 comments on commit fd89bff

Please sign in to comment.