pub fn from_str_borrowing<'de, T>(s: &'de str) -> Result<T>where
T: Deserialize<'de>,Expand description
Deserialise a YAML document into a target type that may borrow
from the input slice (e.g. &'a str, Cow<'a, str>, structs
containing those).
Where from_str requires T: DeserializeOwned + 'static and
therefore can never satisfy Deserialize<'de> for &'de str, this
function pins the deserialiser’s lifetime to the input buffer’s
lifetime. Plain (unquoted) and unescaped quoted scalars are
surfaced via visit_borrowed_str, allowing zero-copy borrows.
Scalars that required transformation (escape decoding, line
folding, alias replay, tag resolution) fall back to owned
allocations — see crate::borrowed::TransformReason for the
catalogue of transform causes.
§Errors
Returns an error if s is not valid YAML, exceeds the default
security limits, or cannot be deserialised into T. When T
targets &'a str and the YAML scalar required transformation,
serde’s &str visitor surfaces an invalid type: string, expected a borrowed string error — switch the target to
Cow<'a, str> or String to accept either form.
§Examples
use noyalib::from_str_borrowing;
#[derive(serde::Deserialize)]
struct Person<'a> {
name: &'a str,
role: &'a str,
}
let yaml = "name: noyalib\nrole: parser\n";
let p: Person<'_> = from_str_borrowing(yaml).unwrap();
assert_eq!(p.name, "noyalib");
assert_eq!(p.role, "parser");