#![cfg(all(feature = "enabled", feature = "as_bytes"))]
#[ repr( C ) ]
#[ derive( Clone, Copy, Debug, PartialEq, bytemuck ::Pod, bytemuck ::Zeroable ) ]
struct Point
{
x: i32,
y: i32,
}
#[ test ]
fn test_tuple_scalar_as_bytes()
{
{
use asbytes ::AsBytes;
use core ::mem;
let scalar_tuple = (123u32,);
let bytes = scalar_tuple.as_bytes();
let expected_length = mem ::size_of :: < u32 >();
assert_eq!(bytes.len(), expected_length);
assert_eq!(scalar_tuple.byte_size(), expected_length);
assert_eq!(scalar_tuple.len(), 1);
assert_eq!(bytes, &123u32.to_le_bytes());
}
}
#[ test ]
fn test_tuple_struct_as_bytes()
{
{
use asbytes ::AsBytes;
use core ::mem;
let point = Point { x: 10, y: -20 };
let struct_tuple = (point,);
let bytes = struct_tuple.as_bytes();
let expected_length = mem ::size_of :: < Point >();
assert_eq!(bytes.len(), expected_length);
assert_eq!(struct_tuple.byte_size(), expected_length);
assert_eq!(struct_tuple.len(), 1);
assert_eq!(bytes, bytemuck ::bytes_of(&point));
}
}
#[ test ]
fn test_vec_as_bytes()
{
{
use asbytes ::AsBytes;
use core ::mem;
let v = vec![1u32, 2, 3, 4];
let bytes = v.as_bytes();
let expected_length = v.len() * mem ::size_of :: < u32 >();
assert_eq!(bytes.len(), expected_length);
assert_eq!(v.byte_size(), expected_length);
assert_eq!(v.len(), 4); }
}
#[ test ]
fn test_slice_as_bytes()
{
{
use asbytes ::exposed ::AsBytes; use core ::mem;
let slice: &[ u32] = &[ 10, 20, 30];
let bytes = slice.as_bytes();
let expected_length = core ::mem ::size_of_val(slice);
assert_eq!(bytes.len(), expected_length);
assert_eq!(slice.byte_size(), expected_length);
assert_eq!(slice.len(), 3); }
}
#[ test ]
fn test_array_as_bytes()
{
{
use asbytes ::own ::AsBytes; use core ::mem;
let arr: [u32; 3] = [100, 200, 300];
let bytes = arr.as_bytes();
let expected_length = arr.len() * mem ::size_of :: < u32 >();
assert_eq!(bytes.len(), expected_length);
assert_eq!(arr.byte_size(), expected_length);
assert_eq!(arr.len(), 3); }
}
#[ test ]
fn test_vec_struct_as_bytes()
{
{
use asbytes ::AsBytes;
use core ::mem;
let points = vec![Point { x: 1, y: 2 }, Point { x: 3, y: 4 }];
let bytes = points.as_bytes();
let expected_length = points.len() * mem ::size_of :: < Point >();
assert_eq!(bytes.len(), expected_length);
assert_eq!(points.byte_size(), expected_length);
assert_eq!(points.len(), 2);
assert_eq!(bytes, bytemuck ::cast_slice(&points[..]));
}
}