#rx #frp #reactivex

nightly rxrust

A Rust implementation of Reactive Extensions

17 releases (11 breaking)

0.12.0 Apr 28, 2021
0.11.0 Mar 1, 2021
0.10.0 Dec 5, 2020
0.9.1 Aug 25, 2020
0.1.0 Jul 31, 2019

#44 in Asynchronous

Download history 22/week @ 2021-01-14 6/week @ 2021-01-21 4/week @ 2021-01-28 10/week @ 2021-02-04 78/week @ 2021-02-11 79/week @ 2021-02-18 33/week @ 2021-02-25 18/week @ 2021-03-04 7/week @ 2021-03-11 5/week @ 2021-03-18 98/week @ 2021-03-25 31/week @ 2021-04-01 29/week @ 2021-04-08 109/week @ 2021-04-15 25/week @ 2021-04-22 32/week @ 2021-04-29

170 downloads per month
Used in herald

MIT license

245KB
7.5K SLoC

rxRust: a zero cost Rust implementation of Reactive Extensions

codecov

Usage

Add this to your Cargo.toml:

[dependencies]
rxrust = "0.10.0"

Example

use rxrust:: prelude::*;

let mut numbers = observable::from_iter(0..10);
// crate a even stream by filter
let even = numbers.clone().filter(|v| v % 2 == 0);
// crate an odd stream by filter
let odd = numbers.clone().filter(|v| v % 2 != 0);

// merge odd and even stream again
even.merge(odd).subscribe(|v| print!("{} ", v, ));
// "0 1 2 3 4 5 6 7 8 9" will be printed.

Clone Stream

In rxrust almost all extensions consume the upstream. So when you try to subscribe a stream twice, the compiler will complain.

 # use rxrust::prelude::*;
 let o = observable::from_iter(0..10);
 o.subscribe(|_| { println!("consume in first")} );
 o.subscribe(|_| { println!("consume in second")} );

In this case, we must clone the stream.

 # use rxrust::prelude::*;
 let o = observable::from_iter(0..10);
 o.clone().subscribe(|_| {println!("consume in first")});
 o.clone().subscribe(|_| {println!("consume in second")});

Scheduler

rxrust use the runtime of the Future as the scheduler, LocalPool and ThreadPool in futures::executor can be used as schedulers directly, and tokio::runtime::Runtime also supported, but need enable the feature futures-scheduler. Across LocalScheduler and SharedScheduler to implement custom Scheduler.

use rxrust::prelude::*;
use futures::executor::ThreadPool;

let pool_scheduler = ThreadPool::new().unwrap();
observable::from_iter(0..10)
  .subscribe_on(pool_scheduler.clone())
  .map(|v| v*2)
  .into_shared()
  .observe_on(pool_scheduler)
  .into_shared()
  .subscribe(|v| {println!("{},", v)});

Converts from a Future

just use observable::from_future to convert a Future to an observable sequence.

use rxrust::prelude::*;
use futures::{ future, executor::LocalPool };

let mut local_scheduler = LocalPool::new();
observable::from_future(future::ready(1), local_scheduler.spawner())
  .subscribe(move |v| println!("subscribed with {}", v));

// Wait `LocalPool` finish.
local_scheduler.run();

A from_future_result function also provided to propagating error from Future.

Missing Features List

See missing features to know what rxRust does not have yet.

All contributions are welcome

We are looking for contributors! Feel free to open issues for asking questions, suggesting features or other things!

Help and contributions can be any of the following:

  • use the project and report issues to the project issues page
  • documentation and README enhancement (VERY important)
  • continuous improvement in a ci Pipeline
  • implement any unimplemented operator, remember to create a pull request before you start your code, so other people know you are work on it.

Dependencies

~2–4.5MB
~88K SLoC