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

feat: add stream::from_fn #123

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

feat: add stream::from_fn #123

wants to merge 7 commits into from

Conversation

nanoqsh
Copy link
Contributor

@nanoqsh nanoqsh commented Feb 3, 2025

This adds an analogue of std::iter::from_fn for streams.

Also creates a new feature async-closure that allows to use newer compiler version 1.85 or later to use async closures. Crates for older versions may use this function without the feature but it'll be more limited.

@taiki-e
Copy link
Collaborator

taiki-e commented Feb 5, 2025

Thanks for the PR. This feature is currently not tested in CI, so could you add cargo test --all-features to the following?

- run: cargo test
- run: cargo test --no-default-features
- run: cargo test --no-default-features --features alloc

@nanoqsh
Copy link
Contributor Author

nanoqsh commented Feb 5, 2025

Thanks for the PR. This feature is currently not tested in CI, so could you add cargo test --all-features to the following?

- run: cargo test
- run: cargo test --no-default-features
- run: cargo test --no-default-features --features alloc

Initially I added ignore to the example with async closure to be able to generate documentation and run tests on Rust earlier than 1.85. The syntax of async closure prevents its use from being hidden behind cfg(feature = "async-closure") attribute, it still won't compile.

Copy link
Member

@notgull notgull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My main concern is the use of the feature flag. Is there a case where I can write a crate using futures-lite with this feature flag turned off, that can then fail to compile if another crate in the dependency tree turns it on?

@nanoqsh
Copy link
Contributor Author

nanoqsh commented Feb 18, 2025

Is there a case where I can write a crate using futures-lite with this feature flag turned off, that can then fail to compile if another crate in the dependency tree turns it on?

Yes, this code will compile without the feature and will not with it:

fn a<F, U>(f: F) -> impl Stream<Item = u8>
where
    F: FnMut() -> U,
    U: Future<Output = u8>,
{
    stream::from_fn(f)
    // the trait `AsyncFnMut()` is not implemented for `F`
}

I expected it to work, but at the moment this is a problem

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

Successfully merging this pull request may close these issues.

3 participants