Skip to main content

from_bytes_multiple

Function from_bytes_multiple 

Source
pub fn from_bytes_multiple<'de, T, C>(bytes: &'de [u8]) -> Result<C, Error>
where T: Deserialize<'de>, C: Default + Extend<T>,
Expand description

Deserialize multiple YAML documents from a UTF-8 byte slice into a collection, supporting borrowed values. Empty, untagged null-like, and explicit !!null documents are skipped, as with from_str_multiple. Application-tagged roots are deserialized, even with null payloads.

This is equivalent to from_str_multiple, but validates that the bytes are UTF-8 before parsing. Invalid UTF-8 returns Error::InvalidUtf8Input. For UTF-16 streams, use read or read_with_options instead.

Any C: Default + Extend<T> is supported. Documents extend the collection in input order; the collection determines how ordering and duplicate values are handled. Specify both types when using turbofish syntax, for example ::<String, Vec<_>>. Use from_slice_multiple to return a Vec<T> with a single type argument.

This function supports both owned types (like String) and borrowed types (like &str). Borrowed values refer to the input byte slice and cannot outlive it. Strings requiring escape processing, folding, or other transformations cannot be borrowed as &str; see from_str_multiple for details.

use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Person<'a> {
    name: &'a str,
}

let bytes = b"name: Alice\n---\nname: Bob\n".to_vec();
let people: Vec<Person<'_>> = serde_saphyr::from_bytes_multiple(&bytes)?;
assert_eq!(people[0].name, "Alice");
assert_eq!(people[1].name, "Bob");