Skip to main content

from_bytes_mut

Function from_bytes_mut 

Source
pub const fn from_bytes_mut<D: ?Sized + Pun>(
    bytes: &mut [u8],
) -> Result<&mut D, Error>
Expand description

A mutable reference to bytes as a mutable reference to a D, if they are one.

Convenient sugar for try_pun_mut::<[u8], D>, with identical behaviour; see from_bytes for why it exists. As anything can be written through the result, D must not have padding: bytes would be de-initialized.

let mut bytes = [0u8, 0, 0, 1];
*puns::from_bytes_mut::<[bool; 2]>(&mut bytes[2..]).unwrap() = [true, false];
assert_eq!(bytes, [0, 0, 1, 0]);
assert!(puns::from_bytes_mut::<[bool; 2]>(&mut [2, 0]).is_err());
ⓘ
let _ = puns::from_bytes_mut::<(u8, u16)>(&mut [0; 4]);

§Errors

As try_pun_mut.