-
Notifications
You must be signed in to change notification settings - Fork 35
m: Remove futures-lite dependency #36
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| use event_listener::{Event, EventListener}; | ||
| use futures_lite::ready; | ||
|
|
||
| use std::fmt; | ||
| use std::future::Future; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,6 @@ use std::sync::atomic::{AtomicUsize, Ordering}; | |
| use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker}; | ||
|
|
||
| use event_listener::{Event, EventListener}; | ||
| use futures_lite::future; | ||
|
|
||
| /// The current state of the `OnceCell`. | ||
| #[derive(Copy, Clone, PartialEq, Eq)] | ||
|
|
@@ -427,7 +426,7 @@ impl<T> OnceCell<T> { | |
|
|
||
| // Slow path: initialize the value. | ||
| // The futures provided should never block, so we can use `now_or_never`. | ||
| now_or_never(self.initialize_or_wait(move || future::ready(closure()), &mut Blocking))?; | ||
| now_or_never(self.initialize_or_wait(move || async move { closure() }, &mut Blocking))?; | ||
notgull marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| debug_assert!(self.is_initialized()); | ||
|
|
||
| // SAFETY: We know that the value is initialized, so it is safe to | ||
|
|
@@ -594,7 +593,7 @@ impl<T> OnceCell<T> { | |
| // but we do not have the ability to initialize it. | ||
| // | ||
| // We need to wait the initialization to complete. | ||
| future::poll_fn(|cx| { | ||
| PollFn::new(|cx| { | ||
notgull marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| match event_listener.take() { | ||
| None => { | ||
| event_listener = Some(self.active_initializers.listen()); | ||
|
|
@@ -758,7 +757,7 @@ impl<T> Drop for OnceCell<T> { | |
| } | ||
|
|
||
| /// Either return the result of a future now, or panic. | ||
| fn now_or_never<T>(f: impl Future<Output = T>) -> T { | ||
| fn now_or_never<T>(mut f: impl Future<Output = T>) -> T { | ||
| const NOOP_WAKER: RawWakerVTable = RawWakerVTable::new(clone, wake, wake_by_ref, drop); | ||
|
|
||
| unsafe fn wake(_: *const ()) {} | ||
|
|
@@ -768,18 +767,39 @@ fn now_or_never<T>(f: impl Future<Output = T>) -> T { | |
| } | ||
| unsafe fn drop(_: *const ()) {} | ||
|
|
||
| futures_lite::pin!(f); | ||
| // SAFETY: We don't move the future after we pin it here. | ||
| let future = unsafe { Pin::new_unchecked(&mut f) }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I strongly recommend that you use the same variable names here. Otherwise, it is very easy to access the owned value incorrectly. Simple shadowing is not sufficient to prevent misuse, but in this case it is fine because it is on the outermost scope. (This is a common oversight, I sometimes see crates make the false claim that simple shadowing is "doing the exact same thing as pin_mut".) Well, it would be better to duplicate the pin macro as you have done for the ready macro. |
||
|
|
||
| let waker = unsafe { Waker::from_raw(RawWaker::new(ptr::null(), &NOOP_WAKER)) }; | ||
|
|
||
| // Poll the future exactly once. | ||
| let mut cx = Context::from_waker(&waker); | ||
|
|
||
| match f.poll(&mut cx) { | ||
| match future.poll(&mut cx) { | ||
| Poll::Ready(value) => value, | ||
| Poll::Pending => unreachable!("future not ready"), | ||
| } | ||
| } | ||
|
|
||
| /// A future that runs a function on poll. | ||
| struct PollFn<F>(F); | ||
|
|
||
| impl<T, F: FnMut(&mut Context<'_>) -> Poll<T>> PollFn<F> { | ||
| fn new(f: F) -> Self { | ||
| Self(f) | ||
| } | ||
| } | ||
|
|
||
| impl<F> Unpin for PollFn<F> {} | ||
|
|
||
| impl<T, F: FnMut(&mut Context<'_>) -> Poll<T>> Future for PollFn<F> { | ||
| type Output = T; | ||
|
|
||
| fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> { | ||
| (self.0)(cx) | ||
| } | ||
| } | ||
|
|
||
| /// The strategy for polling an `event_listener::EventListener`. | ||
| trait Strategy { | ||
| /// Poll the event listener. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.