-
Notifications
You must be signed in to change notification settings - Fork 13.4k
move async unsafe fn
into #![feature(async_unsafe)]
#62518
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// run-pass | ||
|
||
// edition:2018 | ||
// aux-build:arc_wake.rs | ||
// aux-build:wake_once.rs | ||
|
||
#![feature(async_await, async_unsafe)] | ||
|
||
extern crate arc_wake; | ||
extern crate wake_once; | ||
|
||
use std::future::Future; | ||
use std::sync::{ | ||
Arc, | ||
atomic::{self, AtomicUsize}, | ||
}; | ||
use std::task::{Context, Poll}; | ||
use arc_wake::ArcWake; | ||
use wake_once::wake_and_yield_once; | ||
|
||
pub struct Counter { | ||
wakes: AtomicUsize, | ||
} | ||
|
||
impl ArcWake for Counter { | ||
fn wake(self: Arc<Self>) { | ||
Self::wake_by_ref(&self) | ||
} | ||
fn wake_by_ref(arc_self: &Arc<Self>) { | ||
arc_self.wakes.fetch_add(1, atomic::Ordering::SeqCst); | ||
} | ||
} | ||
|
||
async unsafe fn unsafe_async_fn(x: u8) -> u8 { | ||
wake_and_yield_once().await; | ||
x | ||
} | ||
|
||
struct Foo; | ||
|
||
trait Bar { | ||
fn foo() {} | ||
} | ||
|
||
impl Foo { | ||
async fn async_assoc_item(x: u8) -> u8 { | ||
unsafe { | ||
unsafe_async_fn(x).await | ||
} | ||
} | ||
|
||
async unsafe fn async_unsafe_assoc_item(x: u8) -> u8 { | ||
unsafe_async_fn(x).await | ||
} | ||
} | ||
|
||
fn test_future_yields_once_then_returns<F, Fut>(f: F) | ||
where | ||
F: FnOnce(u8) -> Fut, | ||
Fut: Future<Output = u8>, | ||
{ | ||
let mut fut = Box::pin(f(9)); | ||
let counter = Arc::new(Counter { wakes: AtomicUsize::new(0) }); | ||
let waker = ArcWake::into_waker(counter.clone()); | ||
let mut cx = Context::from_waker(&waker); | ||
assert_eq!(0, counter.wakes.load(atomic::Ordering::SeqCst)); | ||
assert_eq!(Poll::Pending, fut.as_mut().poll(&mut cx)); | ||
assert_eq!(1, counter.wakes.load(atomic::Ordering::SeqCst)); | ||
assert_eq!(Poll::Ready(9), fut.as_mut().poll(&mut cx)); | ||
} | ||
|
||
fn main() { | ||
macro_rules! test { | ||
($($fn_name:expr,)*) => { $( | ||
test_future_yields_once_then_returns($fn_name); | ||
)* } | ||
} | ||
|
||
test! { | ||
Foo::async_assoc_item, | ||
|x| { | ||
async move { | ||
unsafe { unsafe_async_fn(x).await } | ||
} | ||
}, | ||
|x| { | ||
async move { | ||
unsafe { Foo::async_unsafe_assoc_item(x).await } | ||
} | ||
}, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// run-pass | ||
|
||
// edition:2018 | ||
// aux-build:arc_wake.rs | ||
// aux-build:wake_once.rs | ||
|
||
#![feature(async_await, async_unsafe, await_macro)] | ||
|
||
extern crate arc_wake; | ||
extern crate wake_once; | ||
|
||
use std::future::Future; | ||
use std::sync::{ | ||
Arc, | ||
atomic::{self, AtomicUsize}, | ||
}; | ||
use std::task::{Context, Poll}; | ||
use arc_wake::ArcWake; | ||
use wake_once::wake_and_yield_once; | ||
|
||
pub struct Counter { | ||
wakes: AtomicUsize, | ||
} | ||
|
||
impl ArcWake for Counter { | ||
fn wake(self: Arc<Self>) { | ||
Self::wake_by_ref(&self) | ||
} | ||
fn wake_by_ref(arc_self: &Arc<Self>) { | ||
arc_self.wakes.fetch_add(1, atomic::Ordering::SeqCst); | ||
} | ||
} | ||
|
||
async unsafe fn unsafe_async_fn(x: u8) -> u8 { | ||
await!(wake_and_yield_once()); | ||
x | ||
} | ||
|
||
struct Foo; | ||
|
||
trait Bar { | ||
fn foo() {} | ||
} | ||
|
||
impl Foo { | ||
async fn async_assoc_item(x: u8) -> u8 { | ||
unsafe { | ||
await!(unsafe_async_fn(x)) | ||
} | ||
} | ||
|
||
async unsafe fn async_unsafe_assoc_item(x: u8) -> u8 { | ||
await!(unsafe_async_fn(x)) | ||
} | ||
} | ||
|
||
fn test_future_yields_once_then_returns<F, Fut>(f: F) | ||
where | ||
F: FnOnce(u8) -> Fut, | ||
Fut: Future<Output = u8>, | ||
{ | ||
let mut fut = Box::pin(f(9)); | ||
let counter = Arc::new(Counter { wakes: AtomicUsize::new(0) }); | ||
let waker = ArcWake::into_waker(counter.clone()); | ||
let mut cx = Context::from_waker(&waker); | ||
assert_eq!(0, counter.wakes.load(atomic::Ordering::SeqCst)); | ||
assert_eq!(Poll::Pending, fut.as_mut().poll(&mut cx)); | ||
assert_eq!(1, counter.wakes.load(atomic::Ordering::SeqCst)); | ||
assert_eq!(Poll::Ready(9), fut.as_mut().poll(&mut cx)); | ||
} | ||
|
||
fn main() { | ||
macro_rules! test { | ||
($($fn_name:expr,)*) => { $( | ||
test_future_yields_once_then_returns($fn_name); | ||
)* } | ||
} | ||
|
||
test! { | ||
Foo::async_assoc_item, | ||
|x| { | ||
async move { | ||
unsafe { await!(unsafe_async_fn(x)) } | ||
} | ||
}, | ||
|x| { | ||
async move { | ||
unsafe { await!(Foo::async_unsafe_assoc_item(x)) } | ||
} | ||
}, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// edition:2018 | ||
|
||
use std::pin::Pin; | ||
use std::future::Future; | ||
use std::task::{Context, Poll}; | ||
|
||
pub struct WakeOnceThenComplete(bool); | ||
|
||
pub fn wake_and_yield_once() -> WakeOnceThenComplete { WakeOnceThenComplete(false) } | ||
|
||
impl Future for WakeOnceThenComplete { | ||
type Output = (); | ||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> { | ||
if self.0 { | ||
Poll::Ready(()) | ||
} else { | ||
cx.waker().wake_by_ref(); | ||
self.0 = true; | ||
Poll::Pending | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.