-
Notifications
You must be signed in to change notification settings - Fork 339
Replace select!/try_select! with Future::{race,try_race} #405
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
212bfb2
init Future::select
yoshuawuyts 99ffed4
implement Future::select
yoshuawuyts 9964696
try_select
yoshuawuyts 14e39b3
fixes
yoshuawuyts 4821783
works
yoshuawuyts 311c32a
pass clippy
yoshuawuyts c04b35c
please clippy
yoshuawuyts ebe29ac
implement feedback from stjepan
yoshuawuyts 679da06
rename select to race
yoshuawuyts 1dc2057
fmt
yoshuawuyts 64de8d1
Merge branch 'master' into unmacro-select
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use std::pin::Pin; | ||
|
||
use async_macros::MaybeDone; | ||
use pin_project_lite::pin_project; | ||
|
||
use crate::task::{Context, Poll}; | ||
use std::future::Future; | ||
|
||
pin_project! { | ||
#[allow(missing_docs)] | ||
#[allow(missing_debug_implementations)] | ||
pub struct Race<L, R> | ||
where | ||
L: Future, | ||
R: Future<Output = L::Output> | ||
{ | ||
#[pin] left: MaybeDone<L>, | ||
#[pin] right: MaybeDone<R>, | ||
} | ||
} | ||
|
||
impl<L, R> Race<L, R> | ||
where | ||
L: Future, | ||
R: Future<Output = L::Output>, | ||
{ | ||
pub(crate) fn new(left: L, right: R) -> Self { | ||
Self { | ||
left: MaybeDone::new(left), | ||
right: MaybeDone::new(right), | ||
} | ||
} | ||
} | ||
|
||
impl<L, R> Future for Race<L, R> | ||
where | ||
L: Future, | ||
R: Future<Output = L::Output>, | ||
{ | ||
type Output = L::Output; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let this = self.project(); | ||
|
||
let mut left = this.left; | ||
if Future::poll(Pin::new(&mut left), cx).is_ready() { | ||
return Poll::Ready(left.take().unwrap()); | ||
} | ||
|
||
let mut right = this.right; | ||
if Future::poll(Pin::new(&mut right), cx).is_ready() { | ||
return Poll::Ready(right.take().unwrap()); | ||
} | ||
|
||
Poll::Pending | ||
} | ||
} |
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,66 @@ | ||
use std::pin::Pin; | ||
|
||
use async_macros::MaybeDone; | ||
use pin_project_lite::pin_project; | ||
|
||
use crate::task::{Context, Poll}; | ||
use std::future::Future; | ||
|
||
pin_project! { | ||
#[allow(missing_docs)] | ||
#[allow(missing_debug_implementations)] | ||
pub struct TryRace<L, R> | ||
where | ||
L: Future, | ||
R: Future<Output = L::Output> | ||
{ | ||
#[pin] left: MaybeDone<L>, | ||
#[pin] right: MaybeDone<R>, | ||
} | ||
} | ||
|
||
impl<L, R> TryRace<L, R> | ||
where | ||
L: Future, | ||
R: Future<Output = L::Output>, | ||
{ | ||
pub(crate) fn new(left: L, right: R) -> Self { | ||
Self { | ||
left: MaybeDone::new(left), | ||
right: MaybeDone::new(right), | ||
} | ||
} | ||
} | ||
|
||
impl<L, R, T, E> Future for TryRace<L, R> | ||
where | ||
L: Future<Output = Result<T, E>>, | ||
R: Future<Output = L::Output>, | ||
{ | ||
type Output = L::Output; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let this = self.project(); | ||
let mut left_errored = false; | ||
|
||
// Check if the left future is ready & successful. Continue if not. | ||
let mut left = this.left; | ||
if Future::poll(Pin::new(&mut left), cx).is_ready() { | ||
if left.as_ref().output().unwrap().is_ok() { | ||
return Poll::Ready(left.take().unwrap()); | ||
} else { | ||
left_errored = true; | ||
} | ||
} | ||
|
||
// Check if the right future is ready & successful. Return err if left | ||
// future also resolved to err. Continue if not. | ||
let mut right = this.right; | ||
let is_ready = Future::poll(Pin::new(&mut right), cx).is_ready(); | ||
if is_ready && (right.as_ref().output().unwrap().is_ok() || left_errored) { | ||
return Poll::Ready(right.take().unwrap()); | ||
} | ||
|
||
Poll::Pending | ||
} | ||
} |
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
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.