Giter VIP home page Giter VIP logo

remit's Introduction

Remit

Crates.io version docs.rs status Crates.io license

Rust generators implemented through async/await syntax.

The pinned implementation is stack-based, and the boxed is heap-based. No fancy macros and a simple API. Values can be lazily or eagerly yielded.

This crate is inherently no-std, and the default alloc feature can be disabled.

Some behaviors exhibited by the lack of alloc are not part of the SemVer. For example, not awaiting before another remit, without alloc, is unspecified behavior.

Usage

Add to dependencies:

[dependencies]
remit = "0.1.1"

Example code:

use std::pin::pin;
use remit::{Generator, Remit};

async fn gen(remit: Remit<'_, usize>) {
    remit.value(42).await;
    // Does not need to be limited
    for i in 1.. {
        remit.value(i).await
    }
}
for item in pin!(Generator::new()).of(gen).take(10) {
    println!("{item}");
    // Prints 42, 1, 2, 3, 4, 5, 6, 7, 8, 9
}
assert_eq!(vec![42, 1, 2, 3], pin!(Generator::new()).of(gen).take(4).collect::<Vec<_>>());
/* // Rust has trouble determining the lifetime
assert_eq!(
    vec![1],
    pin!(Generator::new())
        .of(|remit: Remit<'_, usize>| async move { remit.value(1).await; })
        .collect::<Vec<_>>(),
);
*/
assert_eq!(vec![42, 1], Generator::boxed(gen).take(2).collect::<Vec<_>>());
assert_eq!(vec![1], Generator::boxed(|remit| async move { remit.value(1).await; }).collect::<Vec<_>>());

fn iter() -> impl Iterator<Item=usize> {
    Generator::boxed(gen)
}

async fn scream<D: std::fmt::Display>(iter: impl Iterator<Item=D>, remit: Remit<'_, String>) {
    for person in iter {
        remit.value(format!("{person} scream!")).await
    }
    remit.value("... for ice cream!".to_string());
}
let expected: Vec<String> = ["You scream!", "I scream!", "We all scream!", "... for ice cream!"].iter().map(ToString::to_string).collect();
assert_eq!(
    expected,
    pin!(Generator::new()).parameterized(scream, ["You", "I", "We all"].iter()).collect::<Vec<String>>(),
);
assert_eq!(
    expected,
    Generator::boxed(|remit| scream(["You", "I", "We all"].iter(), remit)).collect::<Vec<String>>(),
);

License

MIT or APACHE-2, at your option.

See respective LICENSE files.

remit's People

Contributors

wolvereness avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

remit's Issues

Is There a Way to Take a Reference in a Parameter?

I've got the following contrived example I'm trying to get to work, based on my use-case:

use std::pin::pin;

use remit::*;

fn main() {
    let data = String::from("hi");

    async fn gen(data: &String, remit: Remit<'_, usize>) {
        remit.value(data.len());
    }

    for item in pin!(Generator::new()).parameterized(gen, &data) {
        dbg!(item);
    }
}

But I'm getting an error:

error[E0597]: `data` does not live long enough
   --> crates/bones_bevy_renderer/examples/test_gitignore.rs:12:59
    |
6   |     let data = String::from("hi");
    |         ---- binding `data` declared here
...
12  |     for item in pin!(Generator::new()).parameterized(gen, &data) {
    |                 ------------------------------------------^^^^^-
    |                 |                                         |
    |                 |                                         borrowed value does not live long enough
    |                 argument requires that `data` is borrowed for `'static`
...
15  | }
    | - `data` dropped here while still borrowed
    |
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
   --> /home/zicklag/.cargo/registry/src/index.crates.io-6f17d22bba15001f/remit-0.1.3/src/lib.rs:286:16
    |
286 |             G: for<'a> RemitWithLifetime<'a, T, (X,)>,
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0597`.
error: could not compile `bones_bevy_renderer` (example "test_gitignore") due to previous error

Is there a way around this? It seems like maybe it's a compiler limitation. I'm not totally understanding the code but it seems like RemitWithLifetime isn't doing it's job of preventing the 'static bound.

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.