| 1 |
// Copyright (C) 2017,2018 Sebastian Dröge <[email protected]> |
| 2 |
// |
| 3 |
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT> |
| 4 |
|
| 5 |
#![cfg_attr(not(feature = "std"), no_std)] |
| 6 |
#![allow(clippy::missing_safety_doc)] |
| 7 |
|
| 8 |
//! Safely cast bytes slices from/to slices of built-in fundamental numeric types. |
| 9 |
//! |
| 10 |
//! The provided traits here allow safe casting between byte slices and slices of fundamental |
| 11 |
//! numeric types, like integers and floating point numbers. During casting, checks are performed |
| 12 |
//! to ensure that the output slice is safe to use: the input slice must be properly aligned for |
| 13 |
//! the output type and contain an integer number of values. |
| 14 |
//! |
| 15 |
//! Instead of working only on slices, the traits work on `AsRef<[T]>` in the immutable case and on |
| 16 |
//! `AsMut<[T]>` for the mutable case. As such, it is possible to directly work on e.g. `Vec<T>` |
| 17 |
//! and `Box<[T]>` too. |
| 18 |
//! |
| 19 |
//! The content of the output slice will be bitwise equivalent to the input slice, as such extra |
| 20 |
//! care has to be taken with regard to endianness. |
| 21 |
//! |
| 22 |
//! # Example with slices |
| 23 |
//! ``` |
| 24 |
//! # extern crate byte_slice_cast; |
| 25 |
//! # fn main() { |
| 26 |
//! use byte_slice_cast::*; |
| 27 |
//! |
| 28 |
//! let slice = [0x0102u16, 0x0304u16, 0x0506u16]; |
| 29 |
//! |
| 30 |
//! let converted_slice = slice.as_byte_slice(); |
| 31 |
//! |
| 32 |
//! if cfg!(target_endian = "big") { |
| 33 |
//! assert_eq!(converted_slice, &[1, 2, 3, 4, 5, 6]); |
| 34 |
//! } else { |
| 35 |
//! assert_eq!(converted_slice, &[2, 1, 4, 3, 6, 5]); |
| 36 |
//! } |
| 37 |
//! |
| 38 |
//! let converted_back_slice = converted_slice.as_slice_of::<u16>().unwrap(); |
| 39 |
//! |
| 40 |
//! assert_eq!(converted_back_slice, &slice); |
| 41 |
//! # } |
| 42 |
//! ``` |
| 43 |
//! |
| 44 |
//! # Example with mutable slices |
| 45 |
//! ``` |
| 46 |
//! # extern crate byte_slice_cast; |
| 47 |
//! # fn main() { |
| 48 |
//! use byte_slice_cast::*; |
| 49 |
//! |
| 50 |
//! let mut slice = [0u32; 1]; |
| 51 |
//! let mut converted_slice = slice.as_mut_byte_slice(); |
| 52 |
//! converted_slice.copy_from_slice(&[0x12, 0x34, 0x56, 0x78]); |
| 53 |
//! |
| 54 |
//! let mut converted_slice = converted_slice.as_mut_slice_of::<u16>().unwrap(); |
| 55 |
//! converted_slice[0] = 0xffff; |
| 56 |
//! |
| 57 |
//! if cfg!(target_endian = "big") { |
| 58 |
//! assert_eq!(&slice, &[0xffff5678]); |
| 59 |
//! } else { |
| 60 |
//! assert_eq!(&slice, &[0x7856ffff]); |
| 61 |
//! } |
| 62 |
//! |
| 63 |
//! # } |
| 64 |
//! ``` |
| 65 |
|
| 66 |
use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::{fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/, memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/, slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/}; |
| 67 |
|
| 68 |
#[cfg(feature = "std")] |
| 69 |
use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# as StdErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#; |
| 70 |
|
| 71 |
/// Possible errors during slice conversion. |
| 72 |
#[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)] |
| 73 |
pub enum Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error# { |
| 74 |
/// The input slice is not properly aligned for the |
| 75 |
/// output data type. E.g. for an `u32` output slice |
| 76 |
/// the memory must be 4-byte aligned. |
| 77 |
AlignmentMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch# { |
| 78 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_type.: &'static str, |
| 79 |
dst_minimum_alignmentrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_minimum_alignment.: usize, |
| 80 |
}, |
| 81 |
/// A non-integer number of values from the output |
| 82 |
/// type would be in the output slice. |
| 83 |
LengthMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch# { |
| 84 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type.: &'static str, |
| 85 |
src_slice_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#src_slice_size.: usize, |
| 86 |
dst_type_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type_size.: usize, |
| 87 |
}, |
| 88 |
} |
| 89 |
|
| 90 |
impl fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error# { |
| 91 |
fn fmtrust-analyzer cargo byte-slice-cast 1.2.2 impl#[Error][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Error#> { |
| 92 |
match self { |
| 93 |
Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::AlignmentMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch# { |
| 94 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_type., |
| 95 |
dst_minimum_alignmentrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_minimum_alignment., |
| 96 |
} => { |
| 97 |
writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!( |
| 98 |
f, |
| 99 |
"cannot cast a &[u8] into a &[{}]: the slice's address is not divisible by the minimum alignment ({}) of {}", |
| 100 |
dst_type, |
| 101 |
dst_minimum_alignment, |
| 102 |
dst_type |
| 103 |
)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 104 |
} |
| 105 |
Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::LengthMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch# { |
| 106 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type., |
| 107 |
src_slice_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#src_slice_size., |
| 108 |
dst_type_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type_size., |
| 109 |
} => { |
| 110 |
writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!( |
| 111 |
f, |
| 112 |
"cannot cast a &[u8] into a &[{}]: the size ({}) of the slice is not divisible by the size ({}) of {}", |
| 113 |
dst_type, |
| 114 |
src_slice_size, |
| 115 |
dst_type_size, |
| 116 |
dst_type |
| 117 |
)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
trait TypeNamerust-analyzer cargo byte-slice-cast 1.2.2 TypeName# { |
| 126 |
const TYPE_NAMErust-analyzer cargo byte-slice-cast 1.2.2 TypeName#TYPE_NAME.: &'static str; |
| 127 |
} |
| 128 |
|
| 129 |
#[cfg(feature = "std")] |
| 130 |
impl StdErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error# { |
| 131 |
fn descriptionrust-analyzer cargo byte-slice-cast 1.2.2 impl#[Error][Error]description().(&self) -> &str { |
| 132 |
use selfrust-analyzer cargo byte-slice-cast 1.2.2 crate/::Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::*; |
| 133 |
|
| 134 |
match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self { |
| 135 |
AlignmentMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch# { .. } => "Alignment Mismatch", |
| 136 |
LengthMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch# { .. } => "Length Mismatch", |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
fn check_alignmentrust-analyzer cargo byte-slice-cast 1.2.2 check_alignment().<T, U>(data: &T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#> |
| 142 |
where |
| 143 |
U: TypeNamerust-analyzer cargo byte-slice-cast 1.2.2 TypeName#, |
| 144 |
T: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]> + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 145 |
{ |
| 146 |
let alignment = memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<U>(); |
| 147 |
|
| 148 |
if (data.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_ptr().() as usize) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().alignment rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().0 { |
| 149 |
let err = Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::AlignmentMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch# { |
| 150 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_type.: U::TYPE_NAMErust-analyzer cargo byte-slice-cast 1.2.2 TypeName#TYPE_NAME., |
| 151 |
dst_minimum_alignmentrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_minimum_alignment.: alignment, |
| 152 |
}; |
| 153 |
return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err); |
| 154 |
} |
| 155 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(alignment) |
| 156 |
} |
| 157 |
|
| 158 |
fn check_lengthrust-analyzer cargo byte-slice-cast 1.2.2 check_length().<T, U>(data: &T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#> |
| 159 |
where |
| 160 |
U: TypeNamerust-analyzer cargo byte-slice-cast 1.2.2 TypeName#, |
| 161 |
T: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]> + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 162 |
{ |
| 163 |
let size_out = memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<U>(); |
| 164 |
if data.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().size_out rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().0 { |
| 165 |
let err = Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::LengthMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch# { |
| 166 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type.: U::TYPE_NAMErust-analyzer cargo byte-slice-cast 1.2.2 TypeName#TYPE_NAME., |
| 167 |
src_slice_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#src_slice_size.: data.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), |
| 168 |
dst_type_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type_size.: size_out, |
| 169 |
}; |
| 170 |
return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err); |
| 171 |
} |
| 172 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(size_out) |
| 173 |
} |
| 174 |
|
| 175 |
fn check_constraintsrust-analyzer cargo byte-slice-cast 1.2.2 check_constraints().<U>(data: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#> |
| 176 |
where |
| 177 |
U: TypeNamerust-analyzer cargo byte-slice-cast 1.2.2 TypeName#, |
| 178 |
{ |
| 179 |
if data.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() { |
| 180 |
return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(0); |
| 181 |
} |
| 182 |
|
| 183 |
check_alignmentrust-analyzer cargo byte-slice-cast 1.2.2 check_alignment().::<[u8], U>(data)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 184 |
let size_out = check_lengthrust-analyzer cargo byte-slice-cast 1.2.2 check_length().::<[u8], U>(data)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 185 |
|
| 186 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(data.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().size_out) |
| 187 |
} |
| 188 |
|
| 189 |
macro_rules! impl_traitrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait!( |
| 190 |
($to:ty) => { |
| 191 |
impl TypeName for $to { |
| 192 |
const TYPE_NAME: &'static str = stringify!($to); |
| 193 |
} |
| 194 |
|
| 195 |
unsafe impl FromByteSlice for $to { |
| 196 |
#[inline] |
| 197 |
fn from_byte_slice<T: AsRef<[u8]> + ?Sized>(slice: &T) -> Result<&[$to], Error> { |
| 198 |
let slice = slice.as_ref(); |
| 199 |
let len = check_constraints::<$to>(slice)?; |
| 200 |
|
| 201 |
// Need to handle the empty case separately as even an empty slices |
| 202 |
// must have a correctly aligned data pointer |
| 203 |
if len == 0 { |
| 204 |
Ok(&[]) |
| 205 |
} else { |
| 206 |
#[allow(clippy::cast_ptr_alignment)] |
| 207 |
unsafe { |
| 208 |
Ok(slice::from_raw_parts(slice.as_ptr() as *const $to, len)) |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
#[inline] |
| 214 |
fn from_mut_byte_slice<T: AsMut<[u8]> + ?Sized>(slice: &mut T) -> Result<&mut [$to], Error> { |
| 215 |
let slice = slice.as_mut(); |
| 216 |
let len = check_constraints::<$to>(slice)?; |
| 217 |
|
| 218 |
// Need to handle the empty case separately as even an empty slices |
| 219 |
// must have a correctly aligned data pointer |
| 220 |
if len == 0 { |
| 221 |
Ok(&mut []) |
| 222 |
} else { |
| 223 |
#[allow(clippy::cast_ptr_alignment)] |
| 224 |
unsafe { |
| 225 |
Ok(slice::from_raw_parts_mut(slice.as_mut_ptr() as *mut $to, len)) |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
unsafe impl ToByteSlice for $to { |
| 232 |
#[inline] |
| 233 |
fn to_byte_slice<T: AsRef<[$to]> + ?Sized>(slice: &T) -> &[u8] { |
| 234 |
let slice = slice.as_ref(); |
| 235 |
let len = slice.len() * mem::size_of::<$to>(); |
| 236 |
unsafe { |
| 237 |
slice::from_raw_parts(slice.as_ptr() as *const u8, len) |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
unsafe impl ToMutByteSlice for $to { |
| 243 |
#[inline] |
| 244 |
fn to_mut_byte_slice<T: AsMut<[$to]> + ?Sized>(slice: &mut T) -> &mut [u8] { |
| 245 |
let slice = slice.as_mut(); |
| 246 |
let len = slice.len() * mem::size_of::<$to>(); |
| 247 |
unsafe { |
| 248 |
slice::from_raw_parts_mut(slice.as_mut_ptr() as *mut u8, len) |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
}; |
| 253 |
); |
| 254 |
|
| 255 |
macro_rules! impl_trait_arrayrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait_array! ( |
| 256 |
($to:ty) => { |
| 257 |
impl<const N: usize> TypeName for [$to; N] { |
| 258 |
const TYPE_NAME: &'static str = stringify!([$to; N]); |
| 259 |
} |
| 260 |
|
| 261 |
unsafe impl<const N: usize> FromByteSlice for [$to; N] { |
| 262 |
#[inline] |
| 263 |
fn from_byte_slice<T: AsRef<[u8]> + ?Sized>(slice: &T) -> Result<&[[$to; N]], Error> { |
| 264 |
let slice = slice.as_ref(); |
| 265 |
let len = check_constraints::<[$to; N]>(slice)?; |
| 266 |
|
| 267 |
// Need to handle the empty case separately as even an empty slices |
| 268 |
// must have a correctly aligned data pointer |
| 269 |
if len == 0 { |
| 270 |
Ok(&[]) |
| 271 |
} else { |
| 272 |
#[allow(clippy::cast_ptr_alignment)] |
| 273 |
unsafe { |
| 274 |
Ok(slice::from_raw_parts(slice.as_ptr() as *const [$to; N], len)) |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
#[inline] |
| 280 |
fn from_mut_byte_slice<T: AsMut<[u8]> + ?Sized>(slice: &mut T) -> Result<&mut [[$to; N]], Error> { |
| 281 |
let slice = slice.as_mut(); |
| 282 |
let len = check_constraints::<[$to; N]>(slice)?; |
| 283 |
|
| 284 |
// Need to handle the empty case separately as even an empty slices |
| 285 |
// must have a correctly aligned data pointer |
| 286 |
if len == 0 { |
| 287 |
Ok(&mut []) |
| 288 |
} else { |
| 289 |
#[allow(clippy::cast_ptr_alignment)] |
| 290 |
unsafe { |
| 291 |
Ok(slice::from_raw_parts_mut(slice.as_mut_ptr() as *mut [$to; N], len)) |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
unsafe impl<const N: usize> ToByteSlice for [$to; N] { |
| 298 |
#[inline] |
| 299 |
fn to_byte_slice<T: AsRef<[[$to; N]]> + ?Sized>(slice: &T) -> &[u8] { |
| 300 |
let slice = slice.as_ref(); |
| 301 |
let len = slice.len() * mem::size_of::<[$to; N]>(); |
| 302 |
unsafe { |
| 303 |
slice::from_raw_parts(slice.as_ptr() as *const u8, len) |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
unsafe impl<const N: usize> ToMutByteSlice for [$to; N] { |
| 309 |
#[inline] |
| 310 |
fn to_mut_byte_slice<T: AsMut<[[$to; N]]> + ?Sized>(slice: &mut T) -> &mut [u8] { |
| 311 |
let slice = slice.as_mut(); |
| 312 |
let len = slice.len() * mem::size_of::<[$to; N]>(); |
| 313 |
unsafe { |
| 314 |
slice::from_raw_parts_mut(slice.as_mut_ptr() as *mut u8, len) |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
}; |
| 319 |
); |
| 320 |
|
| 321 |
/// Trait for converting from a byte slice to a slice of a fundamental, built-in numeric type. |
| 322 |
/// |
| 323 |
/// This trait is an implementation detail. Use the [`AsSliceOf`] and [`AsMutSliceOf`] traits. |
| 324 |
/// |
| 325 |
/// [`AsSliceOf`]: trait.AsSliceOf.html |
| 326 |
/// [`AsMutSliceOf`]: trait.AsMutSliceOf.html |
| 327 |
pub unsafe trait FromByteSlicerust-analyzer cargo byte-slice-cast 1.2.2 FromByteSlice# |
| 328 |
where |
| 329 |
Self: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 330 |
{ |
| 331 |
/// Convert from an immutable byte slice to a immutable slice of a fundamental, built-in |
| 332 |
/// numeric type |
| 333 |
fn from_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 FromByteSlice#from_byte_slice().<T: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]> + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(slice: &T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&[Self], Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#>; |
| 334 |
/// Convert from an mutable byte slice to a mutable slice of a fundamental, built-in numeric |
| 335 |
/// type |
| 336 |
fn from_mut_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 FromByteSlice#from_mut_byte_slice().<T: AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<[u8]> + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(slice: &mut T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&mut [Self], Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#>; |
| 337 |
} |
| 338 |
|
| 339 |
/// Trait for converting from an immutable slice of a fundamental, built-in numeric type to an |
| 340 |
/// immutable byte slice. |
| 341 |
/// |
| 342 |
/// This trait is an implementation detail. Use the [`AsByteSlice`] trait. |
| 343 |
/// |
| 344 |
/// [`AsByteSlice`]: trait.AsByteSlice.html |
| 345 |
pub unsafe trait ToByteSlicerust-analyzer cargo byte-slice-cast 1.2.2 ToByteSlice# |
| 346 |
where |
| 347 |
Self: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 348 |
{ |
| 349 |
/// Convert from an immutable slice of a fundamental, built-in numeric type to an immutable |
| 350 |
/// byte slice |
| 351 |
fn to_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 ToByteSlice#to_byte_slice().<T: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[Self]> + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(slice: &T) -> &[u8]; |
| 352 |
} |
| 353 |
|
| 354 |
/// Trait for converting from a mutable slice of a fundamental, built-in numeric type to a mutable |
| 355 |
/// byte slice. |
| 356 |
/// |
| 357 |
/// This trait is an implementation detail. Use the [`AsMutByteSlice`] trait. |
| 358 |
/// |
| 359 |
/// [`AsMutByteSlice`]: trait.AsMutByteSlice.html |
| 360 |
pub unsafe trait ToMutByteSlicerust-analyzer cargo byte-slice-cast 1.2.2 ToMutByteSlice# |
| 361 |
where |
| 362 |
Self: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 363 |
{ |
| 364 |
/// Convert from a mutable slice of a fundamental, built-in numeric type to a mutable byte |
| 365 |
/// slice |
| 366 |
fn to_mut_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 ToMutByteSlice#to_mut_byte_slice().<T: AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<[Self]> + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(slice: &mut T) -> &mut [u8]; |
| 367 |
} |
| 368 |
|
| 369 |
/// Trait for converting from a byte slice to a slice of a fundamental, built-in numeric type. |
| 370 |
/// |
| 371 |
/// # Example |
| 372 |
/// ```no_run |
| 373 |
/// # extern crate byte_slice_cast; |
| 374 |
/// # fn main() { |
| 375 |
/// use byte_slice_cast::*; |
| 376 |
/// |
| 377 |
/// let slice = [1u8, 2u8, 3u8, 4u8, 5u8, 6u8]; |
| 378 |
/// let converted_slice = slice.as_slice_of::<u16>().unwrap(); |
| 379 |
/// |
| 380 |
/// if cfg!(target_endian = "big") { |
| 381 |
/// assert_eq!(converted_slice, &[0x0102, 0x0304, 0x0506]); |
| 382 |
/// } else { |
| 383 |
/// assert_eq!(converted_slice, &[0x0201, 0x0403, 0x0605]); |
| 384 |
/// } |
| 385 |
/// # } |
| 386 |
/// ``` |
| 387 |
pub trait AsSliceOfrust-analyzer cargo byte-slice-cast 1.2.2 AsSliceOf# { |
| 388 |
fn as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 AsSliceOf#as_slice_of().<T: FromByteSlicerust-analyzer cargo byte-slice-cast 1.2.2 FromByteSlice#>(&self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&[T], Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#>; |
| 389 |
} |
| 390 |
|
| 391 |
impl<U: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]> + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#> AsSliceOfrust-analyzer cargo byte-slice-cast 1.2.2 AsSliceOf# for U { |
| 392 |
#[inline] |
| 393 |
fn as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().<T: FromByteSlicerust-analyzer cargo byte-slice-cast 1.2.2 FromByteSlice#>(&self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&[T], Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#> { |
| 394 |
FromByteSlicerust-analyzer cargo byte-slice-cast 1.2.2 FromByteSlice#::from_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 FromByteSlice#from_byte_slice().(self) |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
/// Trait for converting from a mutable byte slice to a mutable slice of a fundamental, built-in |
| 399 |
/// numeric type. |
| 400 |
/// |
| 401 |
/// # Example |
| 402 |
/// ```no_run |
| 403 |
/// # extern crate byte_slice_cast; |
| 404 |
/// # fn main() { |
| 405 |
/// use byte_slice_cast::*; |
| 406 |
/// |
| 407 |
/// let mut slice = [1u8, 2u8, 3u8, 4u8, 5u8, 6u8]; |
| 408 |
/// let converted_slice = slice.as_mut_slice_of::<u16>().unwrap(); |
| 409 |
/// |
| 410 |
/// if cfg!(target_endian = "big") { |
| 411 |
/// assert_eq!(converted_slice, &[0x0102, 0x0304, 0x0506]); |
| 412 |
/// } else { |
| 413 |
/// assert_eq!(converted_slice, &[0x0201, 0x0403, 0x0605]); |
| 414 |
/// } |
| 415 |
/// # } |
| 416 |
/// ``` |
| 417 |
pub trait AsMutSliceOfrust-analyzer cargo byte-slice-cast 1.2.2 AsMutSliceOf# { |
| 418 |
fn as_mut_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 AsMutSliceOf#as_mut_slice_of().<T: FromByteSlicerust-analyzer cargo byte-slice-cast 1.2.2 FromByteSlice#>(&mut self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&mut [T], Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#>; |
| 419 |
} |
| 420 |
|
| 421 |
impl<U: AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<[u8]> + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#> AsMutSliceOfrust-analyzer cargo byte-slice-cast 1.2.2 AsMutSliceOf# for U { |
| 422 |
#[inline] |
| 423 |
fn as_mut_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsMutSliceOf]as_mut_slice_of().<T: FromByteSlicerust-analyzer cargo byte-slice-cast 1.2.2 FromByteSlice#>(&mut self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&mut [T], Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#> { |
| 424 |
FromByteSlicerust-analyzer cargo byte-slice-cast 1.2.2 FromByteSlice#::from_mut_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 FromByteSlice#from_mut_byte_slice().(self) |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
/// Trait for converting from an immutable slice of a fundamental, built-in numeric type to an |
| 429 |
/// immutable byte slice. |
| 430 |
/// |
| 431 |
/// # Example |
| 432 |
/// ```no_run |
| 433 |
/// # extern crate byte_slice_cast; |
| 434 |
/// # fn main() { |
| 435 |
/// use byte_slice_cast::*; |
| 436 |
/// |
| 437 |
/// let slice: [u16; 3] = [0x0102, 0x0304, 0x0506]; |
| 438 |
/// let converted_slice = slice.as_byte_slice(); |
| 439 |
/// |
| 440 |
/// if cfg!(target_endian = "big") { |
| 441 |
/// assert_eq!(converted_slice, &[1u8, 2u8, 3u8, 4u8, 5u8, 6u8]); |
| 442 |
/// } else { |
| 443 |
/// assert_eq!(converted_slice, &[2u8, 1u8, 4u8, 3u8, 6u8, 5u8]); |
| 444 |
/// } |
| 445 |
/// # } |
| 446 |
/// ``` |
| 447 |
pub trait AsByteSlicerust-analyzer cargo byte-slice-cast 1.2.2 AsByteSlice#<T> { |
| 448 |
fn as_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 AsByteSlice#as_byte_slice().(&self) -> &[u8]; |
| 449 |
} |
| 450 |
|
| 451 |
impl<T: ToByteSlicerust-analyzer cargo byte-slice-cast 1.2.2 ToByteSlice#, U: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[T]> + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#> AsByteSlicerust-analyzer cargo byte-slice-cast 1.2.2 AsByteSlice#<T> for U { |
| 452 |
#[inline] |
| 453 |
fn as_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][`AsByteSlice<T>`]as_byte_slice().(&self) -> &[u8] { |
| 454 |
ToByteSlicerust-analyzer cargo byte-slice-cast 1.2.2 ToByteSlice#::to_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 ToByteSlice#to_byte_slice().(self) |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
/// Trait for converting from a mutable slice of a fundamental, built-in numeric type to a mutable |
| 459 |
/// byte slice. |
| 460 |
/// |
| 461 |
/// # Example |
| 462 |
/// ```no_run |
| 463 |
/// # extern crate byte_slice_cast; |
| 464 |
/// # fn main() { |
| 465 |
/// use byte_slice_cast::*; |
| 466 |
/// |
| 467 |
/// let mut slice: [u16; 3] = [0x0102, 0x0304, 0x0506]; |
| 468 |
/// let converted_slice = slice.as_mut_byte_slice(); |
| 469 |
/// |
| 470 |
/// if cfg!(target_endian = "big") { |
| 471 |
/// assert_eq!(converted_slice, &mut [1u8, 2u8, 3u8, 4u8, 5u8, 6u8]); |
| 472 |
/// } else { |
| 473 |
/// assert_eq!(converted_slice, &mut [2u8, 1u8, 4u8, 3u8, 6u8, 5u8]); |
| 474 |
/// } |
| 475 |
/// # } |
| 476 |
/// ``` |
| 477 |
pub trait AsMutByteSlicerust-analyzer cargo byte-slice-cast 1.2.2 AsMutByteSlice#<T> { |
| 478 |
fn as_mut_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 AsMutByteSlice#as_mut_byte_slice().(&mut self) -> &mut [u8]; |
| 479 |
} |
| 480 |
|
| 481 |
impl<T: ToMutByteSlicerust-analyzer cargo byte-slice-cast 1.2.2 ToMutByteSlice#, U: AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<[T]> + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#> AsMutByteSlicerust-analyzer cargo byte-slice-cast 1.2.2 AsMutByteSlice#<T> for U { |
| 482 |
#[inline] |
| 483 |
fn as_mut_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][`AsMutByteSlice<T>`]as_mut_byte_slice().(&mut self) -> &mut [u8] { |
| 484 |
ToMutByteSlicerust-analyzer cargo byte-slice-cast 1.2.2 ToMutByteSlice#::to_mut_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 ToMutByteSlice#to_mut_byte_slice().(self) |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
impl_traitrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait!!(u8); |
| 489 |
impl_traitrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait!!(u16); |
| 490 |
impl_traitrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait!!(u32); |
| 491 |
impl_traitrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait!!(u64); |
| 492 |
impl_traitrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait!!(u128); |
| 493 |
impl_traitrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait!!(i8); |
| 494 |
impl_traitrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait!!(i16); |
| 495 |
impl_traitrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait!!(i32); |
| 496 |
impl_traitrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait!!(i64); |
| 497 |
impl_traitrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait!!(i128); |
| 498 |
impl_traitrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait!!(f32); |
| 499 |
impl_traitrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait!!(f64); |
| 500 |
impl_traitrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait!!(usize); |
| 501 |
impl_traitrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait!!(isize); |
| 502 |
|
| 503 |
impl_trait_arrayrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait_array!!(u8); |
| 504 |
impl_trait_arrayrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait_array!!(u16); |
| 505 |
impl_trait_arrayrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait_array!!(u32); |
| 506 |
impl_trait_arrayrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait_array!!(u64); |
| 507 |
impl_trait_arrayrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait_array!!(u128); |
| 508 |
impl_trait_arrayrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait_array!!(i8); |
| 509 |
impl_trait_arrayrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait_array!!(i16); |
| 510 |
impl_trait_arrayrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait_array!!(i32); |
| 511 |
impl_trait_arrayrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait_array!!(i64); |
| 512 |
impl_trait_arrayrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait_array!!(i128); |
| 513 |
impl_trait_arrayrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait_array!!(f32); |
| 514 |
impl_trait_arrayrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait_array!!(f64); |
| 515 |
impl_trait_arrayrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait_array!!(usize); |
| 516 |
impl_trait_arrayrust-analyzer cargo byte-slice-cast 1.2.2 impl_trait_array!!(isize); |
| 517 |
|
| 518 |
impl TypeNamerust-analyzer cargo byte-slice-cast 1.2.2 TypeName# for () { |
| 519 |
const TYPE_NAMErust-analyzer cargo byte-slice-cast 1.2.2 impl#[`()`][TypeName]TYPE_NAME.: &'static str = "()"; |
| 520 |
} |
| 521 |
|
| 522 |
unsafe impl ToByteSlicerust-analyzer cargo byte-slice-cast 1.2.2 ToByteSlice# for () { |
| 523 |
#[inline] |
| 524 |
fn to_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[`()`][ToByteSlice]to_byte_slice().<T: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[()]> + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(_: &T) -> &[u8] { |
| 525 |
&[] |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
unsafe impl ToMutByteSlicerust-analyzer cargo byte-slice-cast 1.2.2 ToMutByteSlice# for () { |
| 530 |
#[inline] |
| 531 |
fn to_mut_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[`()`][ToMutByteSlice]to_mut_byte_slice().<T: AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<[()]> + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(_: &mut T) -> &mut [u8] { |
| 532 |
&mut [] |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
#[cfg(test)] |
| 537 |
mod testsrust-analyzer cargo byte-slice-cast 1.2.2 tests/ { |
| 538 |
use superrust-analyzer cargo byte-slice-cast 1.2.2 crate/::*; |
| 539 |
|
| 540 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 541 |
fn u8rust-analyzer cargo byte-slice-cast 1.2.2 tests/u8().() { |
| 542 |
let input: [u8; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; |
| 543 |
|
| 544 |
let output: &[u8] = input.as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<u8>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 545 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&input, output); |
| 546 |
|
| 547 |
let output2: &[u8] = input.as_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][`AsByteSlice<T>`]as_byte_slice().(); |
| 548 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&input, output2); |
| 549 |
} |
| 550 |
|
| 551 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 552 |
fn u16rust-analyzer cargo byte-slice-cast 1.2.2 tests/u16().() { |
| 553 |
let slice: [u16; 8] = [0, 1, 2, 3, 4, 5, 6, 7]; |
| 554 |
let bytes = slice.as_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][`AsByteSlice<T>`]as_byte_slice().(); |
| 555 |
|
| 556 |
if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_endian = "big") { |
| 557 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7]); |
| 558 |
} else { |
| 559 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0]); |
| 560 |
} |
| 561 |
|
| 562 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 563 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<u16>(), |
| 564 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::AlignmentMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch# { |
| 565 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_type.: "u16", |
| 566 |
dst_minimum_alignmentrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_minimum_alignment.: memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<u16>() |
| 567 |
}) |
| 568 |
); |
| 569 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 570 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..15]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<u16>(), |
| 571 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::LengthMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch# { |
| 572 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type.: "u16", |
| 573 |
src_slice_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#src_slice_size.: 15, |
| 574 |
dst_type_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type_size.: 2 |
| 575 |
}) |
| 576 |
); |
| 577 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes.as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<u16>(), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(slice.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`AsRef<[T]>`]as_ref().())); |
| 578 |
} |
| 579 |
|
| 580 |
#[cfg(feature = "std")] |
| 581 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 582 |
fn u16_error_stringrust-analyzer cargo byte-slice-cast 1.2.2 tests/u16_error_string().() { |
| 583 |
let slice: [u16; 8] = [0, 1, 2, 3, 4, 5, 6, 7]; |
| 584 |
let bytes = slice.as_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][`AsByteSlice<T>`]as_byte_slice().(); |
| 585 |
|
| 586 |
let error = (bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<u16>().unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 587 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 588 |
error, |
| 589 |
"cannot cast a &[u8] into a &[u16]: the slice's address is not divisible by the minimum alignment (2) of u16", |
| 590 |
); |
| 591 |
let error = (bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#15]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<u16>().unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 592 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 593 |
error, |
| 594 |
"cannot cast a &[u8] into a &[u16]: the size (15) of the slice is not divisible by the size (2) of u16" |
| 595 |
); |
| 596 |
} |
| 597 |
|
| 598 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 599 |
fn u32rust-analyzer cargo byte-slice-cast 1.2.2 tests/u32().() { |
| 600 |
let slice: [u32; 4] = [0, 1, 2, 3]; |
| 601 |
let bytes = slice.as_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][`AsByteSlice<T>`]as_byte_slice().(); |
| 602 |
|
| 603 |
if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_endian = "big") { |
| 604 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3]); |
| 605 |
} else { |
| 606 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]); |
| 607 |
} |
| 608 |
|
| 609 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 610 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<u32>(), |
| 611 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::AlignmentMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch# { |
| 612 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_type.: "u32", |
| 613 |
dst_minimum_alignmentrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_minimum_alignment.: memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<u32>() |
| 614 |
}) |
| 615 |
); |
| 616 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 617 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..15]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<u32>(), |
| 618 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::LengthMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch# { |
| 619 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type.: "u32", |
| 620 |
src_slice_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#src_slice_size.: 15, |
| 621 |
dst_type_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type_size.: 4 |
| 622 |
}) |
| 623 |
); |
| 624 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes.as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<u32>(), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(slice.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`AsRef<[T]>`]as_ref().())); |
| 625 |
} |
| 626 |
|
| 627 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 628 |
fn u64rust-analyzer cargo byte-slice-cast 1.2.2 tests/u64().() { |
| 629 |
let slice: [u64; 2] = [0, 1]; |
| 630 |
let bytes = slice.as_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][`AsByteSlice<T>`]as_byte_slice().(); |
| 631 |
|
| 632 |
if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_endian = "big") { |
| 633 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]); |
| 634 |
} else { |
| 635 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]); |
| 636 |
} |
| 637 |
|
| 638 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 639 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<u64>(), |
| 640 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::AlignmentMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch# { |
| 641 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_type.: "u64", |
| 642 |
dst_minimum_alignmentrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_minimum_alignment.: memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<u64>() |
| 643 |
}) |
| 644 |
); |
| 645 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 646 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..15]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<u64>(), |
| 647 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::LengthMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch# { |
| 648 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type.: "u64", |
| 649 |
src_slice_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#src_slice_size.: 15, |
| 650 |
dst_type_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type_size.: 8 |
| 651 |
}) |
| 652 |
); |
| 653 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes.as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<u64>(), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(slice.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`AsRef<[T]>`]as_ref().())); |
| 654 |
} |
| 655 |
|
| 656 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 657 |
#[allow(clippy::collapsible_if)] |
| 658 |
#[allow(clippy::collapsible_else_if)] |
| 659 |
fn usizerust-analyzer cargo byte-slice-cast 1.2.2 tests/usize().() { |
| 660 |
let slice: [usize; 2] = [0, 1]; |
| 661 |
let bytes = slice.as_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][`AsByteSlice<T>`]as_byte_slice().(); |
| 662 |
|
| 663 |
if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_endian = "big") { |
| 664 |
if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_pointer_width = "16") { |
| 665 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 0, 1]); |
| 666 |
} else if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_pointer_width = "32") { |
| 667 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 0, 0, 0, 0, 0, 1]); |
| 668 |
} else if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_pointer_width = "64") { |
| 669 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]); |
| 670 |
} else { |
| 671 |
panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("Unhandled target_endian/target_pointer_width configuration"); |
| 672 |
} |
| 673 |
} else { |
| 674 |
if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_pointer_width = "16") { |
| 675 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 1, 0]); |
| 676 |
} else if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_pointer_width = "32") { |
| 677 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 0, 0, 1, 0, 0, 0]); |
| 678 |
} else if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_pointer_width = "64") { |
| 679 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]); |
| 680 |
} else { |
| 681 |
panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("Unhandled target_endian/target_pointer_width configuration"); |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 686 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<usize>(), |
| 687 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::AlignmentMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch# { |
| 688 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_type.: "usize", |
| 689 |
dst_minimum_alignmentrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_minimum_alignment.: memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<usize>() |
| 690 |
}) |
| 691 |
); |
| 692 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 693 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..3]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<usize>(), |
| 694 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::LengthMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch# { |
| 695 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type.: "usize", |
| 696 |
src_slice_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#src_slice_size.: 3, |
| 697 |
dst_type_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type_size.: memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<usize>() |
| 698 |
}) |
| 699 |
); |
| 700 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes.as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<usize>(), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(slice.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`AsRef<[T]>`]as_ref().())); |
| 701 |
} |
| 702 |
|
| 703 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 704 |
fn f32rust-analyzer cargo byte-slice-cast 1.2.2 tests/f32().() { |
| 705 |
let slice: [f32; 4] = [2.0, 1.0, 0.5, 0.25]; |
| 706 |
let bytes = slice.as_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][`AsByteSlice<T>`]as_byte_slice().(); |
| 707 |
|
| 708 |
if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_endian = "big") { |
| 709 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 710 |
bytes, |
| 711 |
[ |
| 712 |
0x40, 0x00, 0x00, 0x00, 0x3f, 0x80, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3e, |
| 713 |
0x80, 0x00, 0x00 |
| 714 |
] |
| 715 |
); |
| 716 |
} else { |
| 717 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 718 |
bytes, |
| 719 |
[ |
| 720 |
0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, |
| 721 |
0x00, 0x80, 0x3e |
| 722 |
] |
| 723 |
); |
| 724 |
}; |
| 725 |
|
| 726 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 727 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<f32>(), |
| 728 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::AlignmentMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch# { |
| 729 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_type.: "f32", |
| 730 |
dst_minimum_alignmentrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_minimum_alignment.: memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<f32>() |
| 731 |
}) |
| 732 |
); |
| 733 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 734 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..15]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<f32>(), |
| 735 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::LengthMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch# { |
| 736 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type.: "f32", |
| 737 |
src_slice_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#src_slice_size.: 15, |
| 738 |
dst_type_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type_size.: 4 |
| 739 |
}) |
| 740 |
); |
| 741 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes.as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<f32>(), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(slice.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`AsRef<[T]>`]as_ref().())); |
| 742 |
} |
| 743 |
|
| 744 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 745 |
fn f64rust-analyzer cargo byte-slice-cast 1.2.2 tests/f64().() { |
| 746 |
let slice: [f64; 2] = [2.0, 0.5]; |
| 747 |
let bytes = slice.as_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][`AsByteSlice<T>`]as_byte_slice().(); |
| 748 |
|
| 749 |
if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_endian = "big") { |
| 750 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 751 |
bytes, |
| 752 |
[ |
| 753 |
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xe0, 0x00, 0x00, 0x00, |
| 754 |
0x00, 0x00, 0x00 |
| 755 |
] |
| 756 |
); |
| 757 |
} else { |
| 758 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 759 |
bytes, |
| 760 |
[ |
| 761 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 762 |
0x00, 0xe0, 0x3f |
| 763 |
] |
| 764 |
); |
| 765 |
}; |
| 766 |
|
| 767 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 768 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<f64>(), |
| 769 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::AlignmentMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch# { |
| 770 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_type.: "f64", |
| 771 |
dst_minimum_alignmentrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_minimum_alignment.: memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<f64>() |
| 772 |
}) |
| 773 |
); |
| 774 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 775 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..15]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<f64>(), |
| 776 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::LengthMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch# { |
| 777 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type.: "f64", |
| 778 |
src_slice_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#src_slice_size.: 15, |
| 779 |
dst_type_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type_size.: memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<f64>() |
| 780 |
}) |
| 781 |
); |
| 782 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes.as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<f64>(), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(slice.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`AsRef<[T]>`]as_ref().())); |
| 783 |
} |
| 784 |
|
| 785 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 786 |
fn u16_mutrust-analyzer cargo byte-slice-cast 1.2.2 tests/u16_mut().() { |
| 787 |
let mut slice: [u16; 8] = [0, 1, 2, 3, 4, 5, 6, 7]; |
| 788 |
let mut slice_2: [u16; 8] = [0, 1, 2, 3, 4, 5, 6, 7]; |
| 789 |
let bytes = slice_2.as_mut_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][`AsMutByteSlice<T>`]as_mut_byte_slice().(); |
| 790 |
|
| 791 |
if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_endian = "big") { |
| 792 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7]); |
| 793 |
} else { |
| 794 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0]); |
| 795 |
} |
| 796 |
|
| 797 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 798 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().1..]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().).as_mut_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsMutSliceOf]as_mut_slice_of().::<u16>(), |
| 799 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::AlignmentMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch# { |
| 800 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_type.: "u16", |
| 801 |
dst_minimum_alignmentrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_minimum_alignment.: memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<u16>() |
| 802 |
}) |
| 803 |
); |
| 804 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 805 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().0..15]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().).as_mut_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsMutSliceOf]as_mut_slice_of().::<u16>(), |
| 806 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::LengthMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch# { |
| 807 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type.: "u16", |
| 808 |
src_slice_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#src_slice_size.: 15, |
| 809 |
dst_type_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type_size.: 2 |
| 810 |
}) |
| 811 |
); |
| 812 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes.as_mut_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsMutSliceOf]as_mut_slice_of().::<u16>(), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(slice.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`AsMut<[T]>`]as_mut().())); |
| 813 |
} |
| 814 |
|
| 815 |
#[cfg(feature = "std")] |
| 816 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 817 |
fn u16_vecrust-analyzer cargo byte-slice-cast 1.2.2 tests/u16_vec().() { |
| 818 |
let vec: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u16> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0, 1, 2, 3, 4, 5, 6, 7]; |
| 819 |
let bytes = vec.as_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][`AsByteSlice<T>`]as_byte_slice().(); |
| 820 |
|
| 821 |
if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_endian = "big") { |
| 822 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7]); |
| 823 |
} else { |
| 824 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0]); |
| 825 |
} |
| 826 |
|
| 827 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 828 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<u16>(), |
| 829 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::AlignmentMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch# { |
| 830 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_type.: "u16", |
| 831 |
dst_minimum_alignmentrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_minimum_alignment.: memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<u16>() |
| 832 |
}) |
| 833 |
); |
| 834 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 835 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..15]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<u16>(), |
| 836 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::LengthMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch# { |
| 837 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type.: "u16", |
| 838 |
src_slice_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#src_slice_size.: 15, |
| 839 |
dst_type_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type_size.: 2 |
| 840 |
}) |
| 841 |
); |
| 842 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes.as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<u16>(), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(vec.as_refrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`AsRef<[T]>`]as_ref().())); |
| 843 |
} |
| 844 |
|
| 845 |
#[cfg(feature = "std")] |
| 846 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 847 |
fn u16_mut_vecrust-analyzer cargo byte-slice-cast 1.2.2 tests/u16_mut_vec().() { |
| 848 |
let mut vec: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u16> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0, 1, 2, 3, 4, 5, 6, 7]; |
| 849 |
let mut vec_clone = vec.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().(); |
| 850 |
let bytes = vec_clone.as_mut_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][`AsMutByteSlice<T>`]as_mut_byte_slice().(); |
| 851 |
|
| 852 |
if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_endian = "big") { |
| 853 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7]); |
| 854 |
} else { |
| 855 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0]); |
| 856 |
} |
| 857 |
|
| 858 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 859 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().1..]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().).as_mut_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsMutSliceOf]as_mut_slice_of().::<u16>(), |
| 860 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::AlignmentMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch# { |
| 861 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_type.: "u16", |
| 862 |
dst_minimum_alignmentrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_minimum_alignment.: memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<u16>() |
| 863 |
}) |
| 864 |
); |
| 865 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 866 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().0..15]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().).as_mut_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsMutSliceOf]as_mut_slice_of().::<u16>(), |
| 867 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::LengthMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch# { |
| 868 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type.: "u16", |
| 869 |
src_slice_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#src_slice_size.: 15, |
| 870 |
dst_type_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type_size.: 2 |
| 871 |
}) |
| 872 |
); |
| 873 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes.as_mut_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsMutSliceOf]as_mut_slice_of().::<u16>(), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(vec.as_mutrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`AsMut<[T]>`]as_mut().())); |
| 874 |
} |
| 875 |
|
| 876 |
#[cfg(feature = "std")] |
| 877 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 878 |
fn u16_box_slicerust-analyzer cargo byte-slice-cast 1.2.2 tests/u16_box_slice().() { |
| 879 |
let vec: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[u16]> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0, 1, 2, 3, 4, 5, 6, 7].into_boxed_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]into_boxed_slice().(); |
| 880 |
let bytes = vec.as_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][`AsByteSlice<T>`]as_byte_slice().(); |
| 881 |
|
| 882 |
if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_endian = "big") { |
| 883 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7]); |
| 884 |
} else { |
| 885 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0]); |
| 886 |
} |
| 887 |
|
| 888 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 889 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<u16>(), |
| 890 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::AlignmentMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch# { |
| 891 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_type.: "u16", |
| 892 |
dst_minimum_alignmentrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_minimum_alignment.: memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<u16>() |
| 893 |
}) |
| 894 |
); |
| 895 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 896 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..15]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<u16>(), |
| 897 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::LengthMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch# { |
| 898 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type.: "u16", |
| 899 |
src_slice_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#src_slice_size.: 15, |
| 900 |
dst_type_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type_size.: 2 |
| 901 |
}) |
| 902 |
); |
| 903 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes.as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<u16>(), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(vec.as_refrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][`AsRef<T>`]as_ref().())); |
| 904 |
} |
| 905 |
|
| 906 |
#[cfg(feature = "std")] |
| 907 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 908 |
fn u16_mut_box_slicerust-analyzer cargo byte-slice-cast 1.2.2 tests/u16_mut_box_slice().() { |
| 909 |
let mut vec: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[u16]> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0, 1, 2, 3, 4, 5, 6, 7].into_boxed_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]into_boxed_slice().(); |
| 910 |
let mut vec_clone: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[u16]> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0, 1, 2, 3, 4, 5, 6, 7].into_boxed_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]into_boxed_slice().(); |
| 911 |
let bytes = vec_clone.as_mut_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][`AsMutByteSlice<T>`]as_mut_byte_slice().(); |
| 912 |
|
| 913 |
if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_endian = "big") { |
| 914 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7]); |
| 915 |
} else { |
| 916 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0]); |
| 917 |
} |
| 918 |
|
| 919 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 920 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().1..]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().).as_mut_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsMutSliceOf]as_mut_slice_of().::<u16>(), |
| 921 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::AlignmentMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch# { |
| 922 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_type.: "u16", |
| 923 |
dst_minimum_alignmentrust-analyzer cargo byte-slice-cast 1.2.2 Error#AlignmentMismatch#dst_minimum_alignment.: memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<u16>() |
| 924 |
}) |
| 925 |
); |
| 926 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 927 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().0..15]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().).as_mut_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsMutSliceOf]as_mut_slice_of().::<u16>(), |
| 928 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo byte-slice-cast 1.2.2 Error#::LengthMismatchrust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch# { |
| 929 |
dst_typerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type.: "u16", |
| 930 |
src_slice_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#src_slice_size.: 15, |
| 931 |
dst_type_sizerust-analyzer cargo byte-slice-cast 1.2.2 Error#LengthMismatch#dst_type_size.: 2 |
| 932 |
}) |
| 933 |
); |
| 934 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes.as_mut_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsMutSliceOf]as_mut_slice_of().::<u16>(), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(vec.as_mutrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][`AsMut<T>`]as_mut().())); |
| 935 |
} |
| 936 |
|
| 937 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 938 |
fn u16_empty_to_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 tests/u16_empty_to_byte_slice().() { |
| 939 |
let slice: [u16; 0] = []; |
| 940 |
let bytes = slice.as_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][`AsByteSlice<T>`]as_byte_slice().(); |
| 941 |
|
| 942 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[]); |
| 943 |
} |
| 944 |
|
| 945 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 946 |
fn u16_empty_from_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 tests/u16_empty_from_byte_slice().() { |
| 947 |
let bytes: [u8; 0] = []; |
| 948 |
let slice = bytes.as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<u16>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 949 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(slice, &[]); |
| 950 |
} |
| 951 |
|
| 952 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 953 |
fn unitrust-analyzer cargo byte-slice-cast 1.2.2 tests/unit().() { |
| 954 |
let slice: [(); 4] = [(), (), (), ()]; |
| 955 |
let bytes = slice.as_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][`AsByteSlice<T>`]as_byte_slice().(); |
| 956 |
|
| 957 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, &[]); |
| 958 |
} |
| 959 |
|
| 960 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 961 |
fn u8_arrayrust-analyzer cargo byte-slice-cast 1.2.2 tests/u8_array().() { |
| 962 |
let input: &[[u8; 3]] = &[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12, 13, 14]][rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().; |
| 963 |
|
| 964 |
let bytes: &[u8] = input.as_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][`AsByteSlice<T>`]as_byte_slice().(); |
| 965 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); |
| 966 |
|
| 967 |
let output = bytes.as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<[u8; 3]>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 968 |
|
| 969 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(output, input); |
| 970 |
} |
| 971 |
|
| 972 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 973 |
fn u16_arrayrust-analyzer cargo byte-slice-cast 1.2.2 tests/u16_array().() { |
| 974 |
let input: &[[u16; 3]] = &[[0, 1, 2], [3, 4, 5]][rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().; |
| 975 |
|
| 976 |
let bytes: &[u8] = input.as_byte_slicerust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][`AsByteSlice<T>`]as_byte_slice().(); |
| 977 |
if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_endian = "big") { |
| 978 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, [0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5]); |
| 979 |
} else { |
| 980 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bytes, [0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0]); |
| 981 |
}; |
| 982 |
|
| •983 |
assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!( |
| 984 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<[u16; 3]>().is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() |
| 985 |
); |
| •986 |
assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!( |
| 987 |
(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..4]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<[u16; 3]>().is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() |
| 988 |
); |
| 989 |
|
| 990 |
let output = bytes.as_slice_ofrust-analyzer cargo byte-slice-cast 1.2.2 impl#[U][AsSliceOf]as_slice_of().::<[u16; 3]>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 991 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(output, input); |
| 992 |
} |
| 993 |
} |