Skip to content

Simpler Select implementation #2110

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
Lucretiel opened this issue Apr 2, 2020 · 1 comment
Closed

Simpler Select implementation #2110

Lucretiel opened this issue Apr 2, 2020 · 1 comment

Comments

@Lucretiel
Copy link
Contributor

I'd like to propose a variant on select, perhaps called first or race (the latter copied from javascript). Unlike select, it would simply discard other results from incomplete futures. This allows it to have a much simpler implementation, and to allow non-unpin futures. It'd look like this:

#![derive(Debug, Clone, Default)]
struct First<A, B> {
    future1: A,
    future2: B
}

impl<A: Future, B: Future> Future for Select<A, B> {
    type Output = Either<A::Output, B::Output>;

    fn poll(self: Poll<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        // This will obviously have to use a pin projection
        match self.future1.poll(cx) => {
            Poll::Ready(a) => Poll::Ready(Either::Left(a)),
            Poll::Pending => self.future2.map(|b| Either::Right(b)).poll(cx),
        }
    }
}

impl<A: FusedFuture, B: FusedFuture> FusedFuture for Select<A, B> {
    fn is_terminated(&self) -> bool {
        self.first.is_terminated() || self.second.is_terminated()
    }
}

The use case here would be for combining with more ephemeral futures, such as timeouts, or any other case where you only care about the first completed future, rather than trying to track and preserve the set of futures being selected over.

@Lucretiel
Copy link
Contributor Author

Never mind, this is a duplicate of #2031

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant