1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
//! # Rust implementation of the [callbag spec][callbag-spec] for reactive/iterable programming
//!
//! Basic [callbag][callbag-spec] factories and operators to get started with.
//!
//! **Highlights:**
//!
//! - Supports reactive stream programming
//! - Supports iterable programming (also!)
//! - Same operator works for both of the above
//! - Extensible
//!
//! Imagine a hybrid between an [Observable][tc39-observable] and an
//! [(Async)Iterable][tc39-async-iteration], that's what callbags are all about. It's all done with
//! a few simple callbacks, following the [callbag spec][callbag-spec].
//!
//! # Examples
//!
//! ## Reactive programming examples
//!
//! Pick the first 5 odd numbers from a clock that ticks every second, then start observing them:
//!
//! ```
//! use async_nursery::Nursery;
//! use crossbeam_queue::SegQueue;
//! use std::{sync::Arc, time::Duration};
//!
//! use callbag::{filter, for_each, interval, map, pipe, take};
//!
//! let (nursery, nursery_out) = Nursery::new(async_executors::AsyncStd);
//!
//! let actual = Arc::new(SegQueue::new());
//!
//! pipe!(
//! interval(Duration::from_millis(1_000), nursery.clone()),
//! map(|x| x + 1),
//! filter(|x| x % 2 == 1),
//! take(5),
//! for_each({
//! let actual = Arc::clone(&actual);
//! move |x| {
//! println!("{}", x);
//! actual.push(x);
//! }
//! }),
//! );
//!
//! drop(nursery);
//! async_std::task::block_on(nursery_out);
//!
//! assert_eq!(
//! &{
//! let mut v = vec![];
//! for _i in 0..actual.len() {
//! v.push(actual.pop().unwrap());
//! }
//! v
//! }[..],
//! [1, 3, 5, 7, 9]
//! );
//! ```
//!
//! ## Iterable programming examples
//!
//! From a range of numbers, pick 5 of them and divide them by 4, then start pulling those one by one:
//!
//! ```
//! use crossbeam_queue::SegQueue;
//! use std::sync::Arc;
//!
//! use callbag::{for_each, from_iter, map, pipe, take};
//!
//! #[derive(Clone)]
//! struct Range {
//! i: usize,
//! to: usize,
//! }
//!
//! impl Range {
//! fn new(from: usize, to: usize) -> Self {
//! Range { i: from, to }
//! }
//! }
//!
//! impl Iterator for Range {
//! type Item = usize;
//!
//! fn next(&mut self) -> Option<Self::Item> {
//! let i = self.i;
//! if i <= self.to {
//! self.i += 1;
//! Some(i)
//! } else {
//! None
//! }
//! }
//! }
//!
//! let actual = Arc::new(SegQueue::new());
//!
//! pipe!(
//! from_iter(Range::new(40, 99)),
//! take(5),
//! map(|x| x as f64 / 4.0),
//! for_each({
//! let actual = Arc::clone(&actual);
//! move |x| {
//! println!("{}", x);
//! actual.push(x);
//! }
//! }),
//! );
//!
//! assert_eq!(
//! &{
//! let mut v = vec![];
//! for _i in 0..actual.len() {
//! v.push(actual.pop().unwrap());
//! }
//! v
//! }[..],
//! [10.0, 10.25, 10.5, 10.75, 11.0]
//! );
//! ```
//!
//! # API
//!
//! The list below shows what's included.
//!
//! ## Source factories
//!
//! - [from_iter][crate::from_iter()]
//! - [interval][crate::interval()]
//!
//! ## Sink factories
//!
//! - [for_each][crate::for_each()]
//!
//! ## Transformation operators
//!
//! - [map][crate::map()]
//! - [scan][crate::scan()]
//! - [flatten][crate::flatten()]
//!
//! ## Filtering operators
//!
//! - [take][crate::take()]
//! - [skip][crate::skip()]
//! - [filter][crate::filter()]
//!
//! ## Combination operators
//!
//! - [merge!][crate::merge!]
//! - [concat!][crate::concat!]
//! - [combine!][crate::combine!]
//!
//! ## Utilities
//!
//! - [share][crate::share()]
//! - [pipe!][crate::pipe!]
//!
//! # Terminology
//!
//! - **source**: a callbag that delivers data
//! - **sink**: a callbag that receives data
//! - **puller sink**: a sink that actively requests data from the source
//! - **pullable source**: a source that delivers data only on demand (on receiving a request)
//! - **listener sink**: a sink that passively receives data from the source
//! - **listenable source**: source which sends data to the sink without waiting for requests
//! - **operator**: a callbag based on another callbag which applies some operation
//!
//! [callbag-spec]: https://github.com/callbag/callbag
//! [tc39-async-iteration]: https://github.com/tc39/proposal-async-iteration
//! [tc39-observable]: https://github.com/tc39/proposal-observable
#[cfg(feature = "combine")]
pub use crate::combine::combine;
#[cfg(feature = "concat")]
pub use crate::concat::concat;
pub use crate::core::*;
#[cfg(feature = "filter")]
pub use crate::filter::filter;
#[cfg(feature = "flatten")]
pub use crate::flatten::flatten;
#[cfg(feature = "for_each")]
pub use crate::for_each::for_each;
#[cfg(feature = "from_iter")]
pub use crate::from_iter::from_iter;
#[cfg(feature = "interval")]
pub use crate::interval::interval;
#[cfg(feature = "map")]
pub use crate::map::map;
#[cfg(feature = "merge")]
pub use crate::merge::merge;
#[cfg(feature = "scan")]
pub use crate::scan::scan;
#[cfg(feature = "share")]
pub use crate::share::share;
#[cfg(feature = "skip")]
pub use crate::skip::skip;
#[cfg(feature = "take")]
pub use crate::take::take;
#[cfg(feature = "combine")]
mod combine;
#[cfg(feature = "concat")]
mod concat;
mod core;
#[cfg(feature = "filter")]
mod filter;
#[cfg(feature = "flatten")]
mod flatten;
#[cfg(feature = "for_each")]
mod for_each;
#[cfg(feature = "from_iter")]
mod from_iter;
#[cfg(feature = "interval")]
mod interval;
#[cfg(feature = "map")]
mod map;
#[cfg(feature = "merge")]
mod merge;
#[cfg(feature = "pipe")]
mod pipe;
#[cfg(feature = "scan")]
mod scan;
#[cfg(feature = "share")]
mod share;
#[cfg(feature = "skip")]
mod skip;
#[cfg(feature = "take")]
mod take;
#[doc = include_str!("../README.md")]
#[cfg(doctest)]
pub struct ReadmeDoctests;