Skip to content
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

Why Unpin bound for Select? #2902

Open
tisonkun opened this issue Dec 19, 2024 · 1 comment
Open

Why Unpin bound for Select? #2902

tisonkun opened this issue Dec 19, 2024 · 1 comment

Comments

@tisonkun
Copy link

Hi @taiki-e!

I found futures-rs Select has an unpin bound:

pub struct Select<A, B> {
    inner: Option<(A, B)>,
}

Seems it is for returning the unfinished future as part of the result, but I'm not quite sure for its real-world user scenarios.

Instead, in my crate fastimer I implement a Select struct without the Unpin bound:

#[derive(Debug, Clone)]
pub enum Either<A, B> {
    Left(A),
    Right(B),
}

#[must_use = "futures do nothing unless you `.await` or poll them"]
#[derive(Debug)]
#[pin_project::pin_project]
pub struct Select<A, B> {
    #[pin]
    a: A,
    #[pin]
    b: B,
}

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

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.project();
        if let Poll::Ready(a) = this.a.poll(cx) {
            return Poll::Ready(Either::Left(a));
        }
        if let Poll::Ready(b) = this.b.poll(cx) {
            return Poll::Ready(Either::Right(b));
        }
        Poll::Pending
    }
}

I'd like to know the design background and understand if one is better than the other.

Possibly related commit - 151d68c

@tisonkun
Copy link
Author

I found:

https://github.com/smol-rs/futures-lite/blob/445f3fc922bea7993713f0f80b9efa96b152f7ce/FEATURES.md?plain=1#L187

.. that describes a similar combinator in futures-lite.

How should I set expectations between futures-rs and futures-lite? Especially given that (1) futures-rs is under the rust-lang org (2) they seems both maintained by @taiki-e.

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