GithubHelp home page GithubHelp logo

Comments (2)

LebranceBW avatar LebranceBW commented on August 30, 2024 1

Inspired by your idea, I proposed this PR.
BTW, code review wanted.

from futures-lite.

nanoqsh avatar nanoqsh commented on August 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.