pub fn read<'a, R, T>(
reader: &'a mut R,
) -> Box<dyn Iterator<Item = Result<T, Error>> + 'a>where
R: Read + 'a,
T: DeserializeOwned + 'a,Expand description
Create an iterator over YAML documents from any std::io::Read using default options.
This is a convenience wrapper around read_with_options, equivalent to
read_with_options(reader, Options::default()).
- It streams the reader without loading the whole input into memory.
- Each item produced by the returned iterator is one deserialized YAML document of type
T. - Documents that are completely empty or null-like (e.g.,
"", ~, null) are skipped.
Generic parameters
R: the concrete reader type implementingstd::io::Read. You almost never need to write this explicitly; the compiler will infer it from thereaderyou pass. When using turbofish, write_to let the compiler inferR.T: the type to deserialize each YAML document into. Must implementserde::de::DeserializeOwned.
Lifetimes
'a: the lifetime of the returned iterator, tied to the lifetime of the providedreader. The iterator cannot outlive the reader it was created from.
Limits and budget
- Uses
Options::default(), which enables a YAML parsing budget by default. This enforces limits such as maximum events, nodes, nesting depth, total scalar bytes Maximal input size limit is turned to off, as input for the streaming reader may potentially be indefinite. To customize these, callread_with_optionsand setOptions::budget.max_reader_input_bytesin the providedOptions. - Alias replay limits are also enforced with their default values 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), `_` lets the compiler infer it.
let iter = serde_saphyr::read::<_, Simple>(&mut reader);
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 limit is exceeded. After an error, the iterator ends. - Empty/null-like documents are skipped and produce no items.
Note Some content of the next document is read before the current parsed document is emitted.
Hence, while streaming is good for safely parsing large files with multiple documents without
loading it into RAM in advance, it does not emit each document exactly
after --- is encountered.