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
//! Streams: values that arrive over time, such as the events of an
//! [`EventStream`](crate::http::sse::EventStream).
//!
//! A [`Stream`] is the async version of an `Iterator`, so you may have to wait for each value.
//! [`StreamExt`] gives every stream its methods, such as `map`, `filter`, `then`, and `chain`.
//! Like any trait's methods, you can call them only where `StreamExt` is imported.
//! [`TryStreamExt`] adds methods for streams of `Result`s, such as `try_collect`.
//!
//! These traits come from the `futures-util` crate. Laravel re-exports them, so an application
//! doesn't need to depend on that crate.
//!
//! ```
//! use laravel::stream::{self, StreamExt};
//!
//! # #[tokio::main(flavor = "current_thread")]
//! # async fn main() {
//! let doubled: Vec<u32> = stream::iter([1, 2, 3]).map(|n| n * 2).collect().await;
//!
//! assert_eq!(doubled, [2, 4, 6]);
//! # }
//! ```
//!
//! To write a stream as a loop, use [`stream!`] and `yield` each value. The loop runs as the
//! stream is read, so it may `.await` between values:
//!
//! ```
//! use laravel::stream::{StreamExt, stream};
//!
//! # #[tokio::main(flavor = "current_thread")]
//! # async fn main() {
//! let countdown = stream! {
//! for n in (1..=3).rev() {
//! yield n;
//! }
//! };
//!
//! assert_eq!(countdown.collect::<Vec<_>>().await, [3, 2, 1]);
//! # }
//! ```
//!
//! [`try_stream!`] makes a stream of `Result`s, so the loop may use `?`. The stream sends the
//! error as its last item:
//!
//! ```
//! use std::num::ParseIntError;
//!
//! use laravel::stream::{TryStreamExt, try_stream};
//!
//! # #[tokio::main(flavor = "current_thread")]
//! # async fn main() {
//! let numbers = try_stream! {
//! for line in ["1", "2", "three"] {
//! yield line.parse::<u32>()?;
//! }
//! };
//! let numbers: Result<Vec<u32>, ParseIntError> = numbers.try_collect().await;
//!
//! assert!(numbers.is_err());
//! # }
//! ```
//!
//! These macros come from the `async-stream` crate.
//!
//! Methods that stream rows, such as a query's `cursor` and `chunk_by_id`, return a
//! [`BoxStream`], so a function that takes their stream can name its type:
//!
//! ```no_run
//! use laravel::model::model;
//! use laravel::stream::{BoxStream, TryStreamExt};
//!
//! #[model]
//! pub struct Post {
//! pub id: i64,
//! pub title: String,
//! }
//!
//! async fn titles(mut pages: BoxStream<'static, laravel::Result<Vec<Post>>>) -> laravel::Result<Vec<String>> {
//! let mut titles = Vec::new();
//! while let Some(page) = pages.try_next().await? {
//! titles.extend(page.into_iter().map(|post| post.title));
//! }
//!
//! Ok(titles)
//! }
//!
//! # async fn example() -> laravel::Result<()> {
//! let titles = titles(Post::query().chunk_by_id(100)).await?;
//! # Ok(()) }
//! ```
pub use ;