Comments (2)
Inspired by your idea, I proposed this PR.
BTW, code review wanted.
from futures-lite.
I tried to express this combinator using an existing API
async fn run() {
use {
futures_lite::{stream, StreamExt},
std::{pin, time::Duration},
tokio::time,
};
// an origin stream
let stream = stream::iter(0..5).then(|i| async move {
time::sleep(Duration::from_secs(1)).await;
i
});
// wrap items in option, I need some mark that the original stream is finished
let stream = stream.map(Some).chain(stream::once(None));
// instead of `take_until` it will be a stream with a single item
let cancel = stream::once_future(async {
tokio::signal::ctrl_c().await.unwrap();
println!("cancelled");
None
});
// merge two streams
let stream = stream
.or(cancel)
.take_while(|opt| opt.is_some())
.map(|opt| opt.unwrap());
// use the final stream
let mut stream = pin::pin!(stream);
while let Some(item) = stream.next().await {
println!("item {item}");
}
}
Overall it's pretty acceptable to me. It would be especially cool to replace .take_while(|opt| opt.is_some()).map(|opt| opt.unwrap())
with .map_while(|opt| opt)
.
But I think it would be simpler and more convenient to have the first class method.
from futures-lite.
Related Issues (20)
- future::Either or something similar? HOT 2
- Drop `future::{ready,pending}` at the next bigger release
- stream::Race never finishes HOT 4
- question: What is the proper way how to wait for a `BufReader::read_line` to finish? HOT 6
- Accept async functions for `StreamExt::map()` and others HOT 3
- Add `dedupe()` to `StreamExt` HOT 2
- Method for creating a stream from a future HOT 1
- parking vs std::thread::park HOT 1
- `select!` & `select_biased!` as in `futures-util` HOT 9
- Panic in read_line? HOT 1
- msrv 1.48 broken HOT 6
- Remove the waker-fn dependency
- Possible deadlock when using `smol::io::split`
- map_ok from TryStreamExt and buffered from StreamExt HOT 2
- Recreating concurrent futures combinators in smol HOT 2
- Add a trait for abstracing over executors HOT 12
- `or` but for different output types HOT 2
- support fuse in FutureExt HOT 3
- Should `future::zip` drop completed futures? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from futures-lite.