pub fn read_with_options<'a, R, T>(
reader: &'a mut R,
options: Options,
) -> impl Iterator<Item = Result<T, Error>> + 'awhere
R: Read + 'a,
T: DeserializeOwned + 'a,Expand description
Create an iterator over YAML documents from any std::io::Read, with configurable options.
This is the multi-document counterpart to from_reader_with_options. It does not load
the entire input into memory. Instead, it streams the reader, deserializing one document
at a time into values of type T, yielding them through the returned iterator. Documents
that are completely empty or null-like (e.g., "", ~, or null) are skipped.
Generic parameters
R: the concrete reader type that implementsstd::io::Read. You rarely need to spell this out; it is almost always inferred from thereadervalue you pass in. When using turbofish, you can write_for this parameter to let the compiler infer it.T: the type to deserialize each YAML document into. This must implementserde::de::DeserializeOwned.
Lifetimes
'a: the lifetime of the returned iterator. It is tied to the lifetime of the providedreadervalue because the iterator borrows internal state that references the reader. In practice, this means the iterator cannot outlive the reader it was created from.
Limits and budget
- All parsing limits configured via
Options::budget(such as maximum events, nodes, nesting depth, total scalar bytes) are enforced while streaming. A hard input-byte cap is also enforced viaBudget::max_reader_input_bytes(256 MiB by default), set this to None if you need a streamer to exist for arbitrary long time. - Alias replay limits from
Options::alias_limitsare also enforced to mitigate alias bombs.
use serde::Deserialize;
#[derive(Debug, Deserialize, PartialEq)]
struct Simple { id: usize }
let yaml = b"id: 1\n---\nid: 2\n";
let mut reader = std::io::Cursor::new(&yaml[..]);
// Type `T` is inferred from the collection target (Vec<Simple>).
let values: Vec<Simple> = serde_saphyr::read(&mut reader)
.map(|r| r.unwrap())
.collect();
assert_eq!(values.len(), 2);
assert_eq!(values[0].id, 1);Specifying only T with turbofish and letting R be inferred using _:
use serde::Deserialize;
#[derive(Debug, Deserialize, PartialEq)]
struct Simple { id: usize }
let yaml = b"id: 10\n---\nid: 20\n";
let mut reader = std::io::Cursor::new(&yaml[..]);
// First turbofish parameter is R (reader type) which we let the compiler infer via `_`.
let iter = serde_saphyr::read_with_options::<_, Simple>(&mut reader, serde_saphyr::Options::default());
let ids: Vec<usize> = iter.map(|res| res.unwrap().id).collect();
assert_eq!(ids, vec![10, 20]);- Each
next()yields eitherOk(T)for a successfully deserialized document orErr(Error)if parsing fails or a budget/alias limit is exceeded. After an error, the iterator ends. - Empty/null-like documents are skipped and produce no items.