Coder Social home page Coder Social logo

dedenne's Introduction

Dedenne

Cute little generators!

Dedenne implements generators, a la the unstable language feature, over async/await in completely stable Rust.

Simple Usage

use dedenne::*;

let mut generator = Generator::new(|y, init| async move {
  for x in 0..init {
    y.ield(x).await;
  }
  for x in (0..init).rev() {
    y.ield(x).await;
  }

  "All done!"
});

assert_eq!(
  generator.start(3), GeneratorResponse::Yielding(0)
);
assert_eq!(
  generator.resume(), GeneratorResponse::Yielding(1)
);
assert_eq!(
  generator.resume(), GeneratorResponse::Yielding(2)
);
assert_eq!(
  generator.resume(), GeneratorResponse::Yielding(2)
);
assert_eq!(
  generator.resume(), GeneratorResponse::Yielding(1)
);
assert_eq!(
  generator.resume(), GeneratorResponse::Yielding(0)
);
assert_eq!(
  generator.resume(), GeneratorResponse::Done("All done!")
);

For a larger example, check out this simple TUI interface.

panic! vs unreachable!

If something in Dedenne panic!s, then it's a user error. Make sure to await your y.ields, and don't call resume after a generator's exhausted.

If something in Dedenne panics with an unreachable! message, then it's a problem with Dedenne. Please file a bug report if it does.

Prior Art

I am not the first person to have this idea. However, I think Dedenne is the only crate that supports mapping over iterators.

  • generator. Doesn't support a starting argument, which means you can't use it as a mapping iterator. Is always stackful.
  • genawaiter. Has some convenience macros that make it nicer to make generators. Also lets you really tune how the generators are stored (stackful or allocating).
  • Unstable lang feature. Doesn't support passing values in to a generator, afaict. Also requires nightly rust and has been bikeshedding for 6 years as of time of writing.

dedenne's People

Contributors

gamma-delta 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.