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

m: Remove unnecessary dependencies from event-listener-strategy #50

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions strategy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ exclude = ["/.*"]

[dependencies]
event-listener = { path = "..", version = "2", default-features = false }
pin-project-lite = "0.2.9"
pin-utils = "0.1.0"

[features]
default = ["std"]
Expand Down
78 changes: 55 additions & 23 deletions strategy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ use core::task::{Context, Poll};

use event_listener::EventListener;

#[doc(hidden)]
pub use pin_project_lite::pin_project;

/// A wrapper around an [`EventListenerFuture`] that can be easily exported for use.
///
/// This type implements [`Future`], has a `_new()` constructor, and a `wait()` method
Expand Down Expand Up @@ -134,12 +131,9 @@ macro_rules! easy_wrapper {
$(#[$wait_meta:meta])*
$wait_vis: vis wait();
) => {
$crate::pin_project! {
$(#[$meta])*
$vis struct $name {
#[pin]
_inner: $crate::FutureWrapper<$inner>
}
$(#[$meta])*
$vis struct $name {
_inner: $crate::FutureWrapper<$inner>
}

impl $name {
Expand All @@ -166,7 +160,14 @@ macro_rules! easy_wrapper {
self: ::core::pin::Pin<&mut Self>,
context: &mut ::core::task::Context<'_>
) -> ::core::task::Poll<Self::Output> {
self.project()._inner.poll(context)
// SAFETY: We are pinned, so our inner type must be pinned too.
// Uses get_unchecked then new_unchecked instead of map_unchecked_mut because it
// doesn't work on 1.39 yet... looks like it works on 1.42, but it's not worth it
// to bump MSRV yet
unsafe {
Pin::new_unchecked(&mut self.get_unchecked_mut()._inner)
.poll(context)
}
}
}
};
Expand Down Expand Up @@ -225,15 +226,12 @@ pub trait EventListenerFuture {
}
}

pin_project_lite::pin_project! {
/// A wrapper around an [`EventListenerFuture`] that implements [`Future`].
///
/// [`Future`]: core::future::Future
#[derive(Debug, Clone)]
pub struct FutureWrapper<F: ?Sized> {
#[pin]
inner: F,
}
/// A wrapper around an [`EventListenerFuture`] that implements [`Future`].
///
/// [`Future`]: core::future::Future
#[derive(Debug, Clone)]
pub struct FutureWrapper<F: ?Sized> {
inner: F,
}

impl<F: EventListenerFuture> FutureWrapper<F> {
Expand Down Expand Up @@ -266,13 +264,19 @@ impl<F: ?Sized> FutureWrapper<F> {
/// Get a pinned mutable reference to the inner future.
#[inline]
pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut F> {
self.project().inner
// SAFETY: We are pinned, so our inner type must be pinned too.
// Uses get_unchecked then new_unchecked instead of map_unchecked_mut because it doesn't
// work on 1.39 yet... looks like it works on 1.42, but it's not worth it to bump MSRV yet
unsafe { Pin::new_unchecked(&mut Pin::get_unchecked_mut(self).inner) }
}

/// Get a pinned reference to the inner future.
#[inline]
pub fn get_pin_ref(self: Pin<&Self>) -> Pin<&F> {
self.project_ref().inner
// SAFETY: We are pinned, so our inner type must be pinned too.
// Uses get_ref then new_unchecked instead of map_unchecked_mut because it doesn't
// work on 1.39 yet... looks like it works on 1.42, but it's not worth it to bump MSRV yet
unsafe { Pin::new_unchecked(&Pin::get_ref(self).inner) }
}
}

Expand All @@ -288,8 +292,7 @@ impl<F: EventListenerFuture + ?Sized> Future for FutureWrapper<F> {

#[inline]
fn poll(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> {
self.project()
.inner
self.get_pin_mut()
.poll_with_strategy(&mut NonBlocking::default(), context)
}
}
Expand Down Expand Up @@ -418,3 +421,32 @@ impl Future for Ready {
Poll::Ready(())
}
}

#[allow(unused)]
mod make_sure_easy_wrapper_builds_on_msrv {
use super::*;

struct MyFuture;

impl EventListenerFuture for MyFuture {
type Output = ();

fn poll_with_strategy<'a, S: Strategy<'a>>(
self: Pin<&mut Self>,
_strategy: &mut S,
_context: &mut S::Context,
) -> Poll<Self::Output> {
Poll::Ready(())
}
}

easy_wrapper! {
pub(super) struct MyFutureWrapper(MyFuture => ());
pub(super) wait();
}

#[test]
fn test() {
let _future = MyFutureWrapper::_new(MyFuture);
}
}