read

Function read 

Source
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 implementing std::io::Read. You almost never need to write this explicitly; the compiler will infer it from the reader you pass. When using turbofish, write _ to let the compiler infer R.
  • T: the type to deserialize each YAML document into. Must implement serde::de::DeserializeOwned.

Lifetimes

  • 'a: the lifetime of the returned iterator, tied to the lifetime of the provided reader. 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, call read_with_options and set Options::budget.max_reader_input_bytes in the provided Options.
  • 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 either Ok(T) for a successfully deserialized document or Err(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.