Skip to main content

deserialize

Function deserialize 

Source
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where D: Deserializer<'de>, T: FromIterator<u8>,
Available on crate feature serde only.
Expand description

Deserializes a 0x-prefixed hex string into a byte collection.

Available with serde. This is the default deserializer for #[serde(with = "faster_hex")]. The prefix must be exactly 0x; payload letters may use either case, including mixed case. Whitespace and separators are rejected. "0x" produces an empty collection.

Decoded bytes are collected into T, which implements FromIterator<u8>, for example Vec<u8> or VecDeque<u8>. Input text is borrowed when the format can lend it; transient, escaped or owned text may require storage. Use array for [u8; N] fields, or deserialize_bounded to limit decoded output. Allocation failure follows the allocator’s error handling.

§Errors

Propagates deserializer errors, including invalid input types or UTF-8. Once text is available, checks the prefix, even payload length, then characters, in that order. Character diagnostics report the first invalid byte and its byte position within the payload, excluding 0x. Collection starts only after successful decoding.

§Panics

A custom collector may panic, for example when its fixed capacity is exceeded. This adapter does not provide fallible collection.

§Examples

#[derive(serde::Deserialize)]
struct Record {
    #[serde(with = "faster_hex")]
    bytes: Vec<u8>,
}

let value: Record = serde_json::from_str(r#"{"bytes":"0x00aB"}"#)?;
assert_eq!(value.bytes, [0, 0xab]);