-
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 all commits
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 |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ name = "async-lock" | |
| version = "2.6.0" | ||
| authors = ["Stjepan Glavina <[email protected]>"] | ||
| edition = "2018" | ||
| rust-version = "1.43" | ||
| rust-version = "1.48" | ||
| description = "Async synchronization primitives" | ||
| license = "Apache-2.0 OR MIT" | ||
| repository = "https://github.com/smol-rs/async-lock" | ||
|
|
@@ -15,12 +15,12 @@ categories = ["asynchronous", "concurrency"] | |
| exclude = ["/.*"] | ||
|
|
||
| [dependencies] | ||
| futures-lite = "1.11.0" | ||
| event-listener = "2.5.1" | ||
|
|
||
| [dev-dependencies] | ||
| async-channel = "1.5.0" | ||
| fastrand = "1.4.0" | ||
| futures-lite = "1.12.0" | ||
|
|
||
| [target.'cfg(any(target_arch = "wasm32", target_arch = "wasm64"))'.dev-dependencies] | ||
| wasm-bindgen-test = "0.3" | ||
| 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,9 @@ 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 || std::future::ready(closure()), &mut Blocking), | ||
| )?; | ||
| debug_assert!(self.is_initialized()); | ||
|
|
||
| // SAFETY: We know that the value is initialized, so it is safe to | ||
|
|
@@ -594,22 +595,13 @@ 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| { | ||
| match event_listener.take() { | ||
| None => { | ||
| event_listener = Some(self.active_initializers.listen()); | ||
| } | ||
| Some(evl) => { | ||
| if let Err(evl) = strategy.poll(evl, cx) { | ||
| event_listener = Some(evl); | ||
| return Poll::Pending; | ||
| } | ||
| } | ||
| match event_listener.take() { | ||
| None => { | ||
| event_listener = Some(self.active_initializers.listen()); | ||
| } | ||
|
|
||
| Poll::Ready(()) | ||
| }) | ||
| .await; | ||
| Some(evl) => strategy.poll(evl).await, | ||
| } | ||
| } | ||
| State::Uninitialized => { | ||
| // Try to move the cell into the initializing state. | ||
|
|
@@ -758,7 +750,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,42 +760,48 @@ 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) }; | ||
|
Collaborator
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"), | ||
| } | ||
| } | ||
|
|
||
| /// The strategy for polling an `event_listener::EventListener`. | ||
| trait Strategy { | ||
| /// The future that can be polled to wait on the listener. | ||
| type Fut: Future<Output = ()>; | ||
|
|
||
| /// Poll the event listener. | ||
| fn poll(&mut self, evl: EventListener, ctx: &mut Context<'_>) -> Result<(), EventListener>; | ||
| fn poll(&mut self, evl: EventListener) -> Self::Fut; | ||
| } | ||
|
|
||
| /// The strategy for blocking the current thread on an `EventListener`. | ||
| struct Blocking; | ||
|
|
||
| impl Strategy for Blocking { | ||
| fn poll(&mut self, evl: EventListener, _: &mut Context<'_>) -> Result<(), EventListener> { | ||
| type Fut = std::future::Ready<()>; | ||
|
|
||
| fn poll(&mut self, evl: EventListener) -> Self::Fut { | ||
| evl.wait(); | ||
| Ok(()) | ||
| std::future::ready(()) | ||
| } | ||
| } | ||
|
|
||
| /// The strategy for polling an `EventListener` in an async context. | ||
| struct NonBlocking; | ||
|
|
||
| impl Strategy for NonBlocking { | ||
| fn poll(&mut self, mut evl: EventListener, ctx: &mut Context<'_>) -> Result<(), EventListener> { | ||
| match Pin::new(&mut evl).poll(ctx) { | ||
| Poll::Pending => Err(evl), | ||
| Poll::Ready(()) => Ok(()), | ||
| } | ||
| type Fut = EventListener; | ||
|
|
||
| fn poll(&mut self, evl: EventListener) -> Self::Fut { | ||
| evl | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.