Skip to main content

from_slice_strict

Function from_slice_strict 

Source
pub fn from_slice_strict<T>(b: &[u8]) -> Result<T>
where T: for<'de> Deserialize<'de> + 'static,
Expand description

Strict deserialise from a byte slice — like from_slice but errors if the input contains keys the target type T does not declare.

Same semantics as from_str_strict. Use this when the caller already holds a &[u8] (e.g. data read from a buffer or returned by a bytes framework) and would otherwise pay the UTF-8 validation cost of converting to &str twice.

§Errors

  • The byte slice is not valid UTF-8.
  • Any key in the YAML document is not declared on T.
  • Any of the regular from_slice error paths.

§Examples

#[derive(Debug, serde::Deserialize)]
struct Config {
    port: u16,
}

let yaml: &[u8] = b"port: 8080\nporrt: 9090\n";
assert!(noyalib::from_slice::<Config>(yaml).is_ok());
assert!(noyalib::from_slice_strict::<Config>(yaml).is_err());