12 个版本
0.4.7 | 2020年6月10日 |
---|---|
0.4.6 | 2019年8月23日 |
0.4.5 | 2019年3月14日 |
0.4.4 | 2019年1月25日 |
0.1.0 | 2017年3月19日 |
#472 in 并发
每月 41 次下载
在 2 crates 中使用
69KB
1.5K SLoC
frappe - Rust 的 FRP 库
Frappe 是一个基于事件的并发 FRP 库。它旨在提供一种简单、高效且 Rust 风格的方式,以声明式方式编写交互式应用程序。
事件在流中处理,可以使用信号进行累积和读取。此外,可以使用 Stream::next
方法将流事件转换为 future,因此您可以通过 async/await 监听它们。
使用方法
use frappe::Sink;
fn main() {
// values are sent from a sink..
let sink = Sink::new();
// ..into a stream chain
let stream = sink.stream().inspect(|a| println!("--sent: {}", a));
// `hold` creates a Signal that stores the last value sent to the stream
let last = stream.hold(0);
// stream callbacks receive a MaybeOwned<T> argument, so we need to deref the value
let sum = stream.fold(0, |acc, n| acc + *n);
let half_even = stream
// the methods filter, map, fold are analogous to Iterator operations
.filter(|n| n % 2 == 0)
.map(|n| *n / 2)
.fold(Vec::new(), |mut vec, n| {
vec.push(*n);
vec
}) // note: .collect::<Vec<_>>() does the same
.map(|v| format!("{:?}", v));
// we can send individual values
sink.send(6);
sink.send(42);
sink.send(-1);
// or multiple ones at once
sink.feed(10..15);
// `sample` gets a copy of the value stored in the signal
println!("last: {}", last.sample());
// printing a signal samples it
println!("sum: {}", sum);
println!("half_even: {}", half_even);
}
您还可以查看 frappe-gtk 示例,以获取更多关于 GUI 应用程序的复杂使用示例。
依赖项
~1.5MB
~26K SLoC