Code Monkey home page Code Monkey logo

select-loop's Introduction

select_loop!

Provides a select_loop! macro allowing to loop awaiting for multiple streams and/or futures, and is aimed to be used instead of using tokio's select! macro inside a loop, which may cause issues with cancelation safety.

Select loops are a nice design pattern to write async tasks continuously awaiting for events/messages while managing an internal state without mutexes, since only one event/message is processed at a time.

Usage

The body of the select_loop! macro looks like a match statement with additional syntax.

  • F my_future => |item| ..., allows to await for a Future. Once the Future resolves the branch is executed.
  • S my_stream => |item| ..., allows to await for a Stream. Each time the Stream outputs an item the branch is executed.

The macro takes ownership of the Future/Stream and requires it to be Unpin.

Additionally it is possible to hook on various stages of the select loop:

  • @before => ... is executed just before any branch is executed, but after one of the futures or stream has outputed an item.
  • @after => ... is executed just after any branch is executed.
  • @exhausted => ... is executed when all futures and streams are exhausted. The value returned by the last @exhausted branch is returned by the select_loop! (it corresponds to a break ...; inside the macro expansion).

As the macro expands into a Rust loop, break and continue can be used like with normal loops. break followed by a value can even be used as long as all break return values of the same type, and that at least one @exhausted branch exists and the last one returns a value of the same type. If you have multiple nested loops, you can refer to the select loop using the 'select_loop lifetime.

Examples

Examples are available here

select-loop's People

Contributors

nanocryk avatar

Watchers

 avatar

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.