Lines Matching refs:bytes
10 /// reading arbitrary bytes into something that contains a `bool` is not okay.
12 /// It's okay for the type to have padding, as initializing those bytes has no effect.
39 /// Converts a slice of bytes to a reference to `Self`.
41 /// Succeeds if the reference is properly aligned, and the size of `bytes` is equal to that of
45 fn from_bytes(bytes: &[u8]) -> Option<&Self>
49 let slice_ptr = bytes.as_ptr().cast::<Self>();
53 if bytes.len() == size && slice_ptr.is_aligned() {
61 /// Converts the beginning of `bytes` to a reference to `Self`.
63 /// This method is similar to [`Self::from_bytes`], with the difference that `bytes` does not
65 /// `bytes`, and the remainder returned alongside `Self`.
66 fn from_bytes_prefix(bytes: &[u8]) -> Option<(&Self, &[u8])>
70 if bytes.len() < size_of::<Self>() {
73 // PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at` cannot
76 let (prefix, remainder) = bytes.split_at(size_of::<Self>());
82 /// Converts a mutable slice of bytes to a reference to `Self`.
84 /// Succeeds if the reference is properly aligned, and the size of `bytes` is equal to that of
88 fn from_bytes_mut(bytes: &mut [u8]) -> Option<&mut Self>
92 let slice_ptr = bytes.as_mut_ptr().cast::<Self>();
96 if bytes.len() == size && slice_ptr.is_aligned() {
104 /// Converts the beginning of `bytes` to a mutable reference to `Self`.
106 /// This method is similar to [`Self::from_bytes_mut`], with the difference that `bytes` does
108 /// of `bytes`, and the remainder returned alongside `Self`.
109 fn from_bytes_mut_prefix(bytes: &mut [u8]) -> Option<(&mut Self, &mut [u8])>
113 if bytes.len() < size_of::<Self>() {
116 // PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at_mut` cannot
119 let (prefix, remainder) = bytes.split_at_mut(size_of::<Self>());
125 /// Creates an owned instance of `Self` by copying `bytes`.
129 fn from_bytes_copy(bytes: &[u8]) -> Option<Self>
133 if bytes.len() == size_of::<Self>() {
134 // SAFETY: we just verified that `bytes` has the same size as `Self`, and per the
137 Some(unsafe { core::ptr::read_unaligned(bytes.as_ptr().cast::<Self>()) })
143 /// Creates an owned instance of `Self` from the beginning of `bytes`.
145 /// This method is similar to [`Self::from_bytes_copy`], with the difference that `bytes` does
147 /// of `bytes`, and the remainder returned alongside `Self`.
148 fn from_bytes_copy_prefix(bytes: &[u8]) -> Option<(Self, &[u8])>
152 if bytes.len() < size_of::<Self>() {
155 // PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at` cannot
158 let (prefix, remainder) = bytes.split_at(size_of::<Self>());
187 /// Types that can be viewed as an immutable slice of initialized bytes.
190 /// means that it should not have any padding, as padding bytes are uninitialized. Reading
200 /// Values of this type may not contain any uninitialized bytes. This type must not have interior
203 /// Returns `self` as a slice of bytes.
205 // CAST: `Self` implements `AsBytes` thus all bytes of `self` are initialized.
209 // SAFETY: `data` is non-null and valid for reads of `len * sizeof::<u8>()` bytes.
213 /// Returns `self` as a mutable slice of bytes.
224 // bytes.