| 1 |
#![allow(dead_code)] |
| 2 |
#![deny(clippy::allow_attributes)] |
| 3 |
|
| 4 |
use bytemuck::{ |
| 5 |
checked::CheckedCastError, AnyBitPattern, CheckedBitPattern, Contiguous, |
| 6 |
NoUninit, Pod, TransparentWrapper, Zeroable, |
| 7 |
}; |
| 8 |
use std::marker::{PhantomData, PhantomPinned}; |
| 9 |
|
| 10 |
#[derive(Copy, Clone, Pod, Zeroable)] |
| 11 |
#[repr(C)] |
| 12 |
struct Test { |
| 13 |
a: u16, |
| 14 |
b: u16, |
| 15 |
} |
| 16 |
|
| 17 |
#[derive(Pod, Zeroable)] |
| 18 |
#[repr(C, packed)] |
| 19 |
struct GenericPackedStruct<T: Pod> { |
| 20 |
a: u32, |
| 21 |
b: T, |
| 22 |
c: u32, |
| 23 |
} |
| 24 |
|
| 25 |
impl<T: Pod> Clone for GenericPackedStruct<T> { |
| 26 |
fn clone(&self) -> Self { |
| 27 |
*self |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
impl<T: Pod> Copy for GenericPackedStruct<T> {} |
| 32 |
|
| 33 |
#[derive(Pod, Zeroable)] |
| 34 |
#[repr(C, packed(1))] |
| 35 |
struct GenericPackedStructExplicitPackedAlignment<T: Pod> { |
| 36 |
a: u32, |
| 37 |
b: T, |
| 38 |
c: u32, |
| 39 |
} |
| 40 |
|
| 41 |
impl<T: Pod> Clone for GenericPackedStructExplicitPackedAlignment<T> { |
| 42 |
fn clone(&self) -> Self { |
| 43 |
*self |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
impl<T: Pod> Copy for GenericPackedStructExplicitPackedAlignment<T> {} |
| 48 |
|
| 49 |
#[derive(Zeroable)] |
| 50 |
struct ZeroGeneric<T: bytemuck::Zeroable> { |
| 51 |
a: T, |
| 52 |
} |
| 53 |
|
| 54 |
#[derive(Zeroable)] |
| 55 |
#[repr(u8)] |
| 56 |
enum ZeroEnum { |
| 57 |
A = 0, |
| 58 |
B = 1, |
| 59 |
C = 2, |
| 60 |
} |
| 61 |
|
| 62 |
#[derive(Zeroable)] |
| 63 |
#[repr(u8)] |
| 64 |
enum BasicFieldfulZeroEnum { |
| 65 |
A(u8) = 0, |
| 66 |
B = 1, |
| 67 |
C(String) = 2, |
| 68 |
} |
| 69 |
|
| 70 |
#[derive(Zeroable)] |
| 71 |
#[repr(C)] |
| 72 |
enum ReprCFieldfulZeroEnum { |
| 73 |
A(u8), |
| 74 |
B(Box<[u8]>), |
| 75 |
C, |
| 76 |
} |
| 77 |
|
| 78 |
#[derive(Zeroable)] |
| 79 |
#[repr(C, i32)] |
| 80 |
enum ReprCIntFieldfulZeroEnum { |
| 81 |
B(String) = 1, |
| 82 |
A(u8, bool, char) = 0, |
| 83 |
C = 2, |
| 84 |
} |
| 85 |
|
| 86 |
#[derive(Zeroable)] |
| 87 |
#[repr(i32)] |
| 88 |
enum GenericFieldfulZeroEnum<T> { |
| 89 |
A(Box<T>) = 1, |
| 90 |
B(T, T) = 0, |
| 91 |
} |
| 92 |
|
| 93 |
#[derive(Zeroable)] |
| 94 |
#[repr(i32)] |
| 95 |
#[zeroable(bound = "")] |
| 96 |
enum GenericCustomBoundFieldfulZeroEnum<T> { |
| 97 |
A(Option<Box<T>>), |
| 98 |
B(String), |
| 99 |
} |
| 100 |
|
| 101 |
#[derive(TransparentWrapper)] |
| 102 |
#[repr(transparent)] |
| 103 |
struct TransparentSingle { |
| 104 |
a: u16, |
| 105 |
} |
| 106 |
|
| 107 |
#[derive(TransparentWrapper)] |
| 108 |
#[repr(transparent)] |
| 109 |
#[transparent(u16)] |
| 110 |
struct TransparentWithZeroSized<T> { |
| 111 |
a: u16, |
| 112 |
b: PhantomData<T>, |
| 113 |
} |
| 114 |
|
| 115 |
struct MyZst<T>(PhantomData<T>, [u8; 0], PhantomPinned); |
| 116 |
unsafe impl<T> Zeroable for MyZst<T> {} |
| 117 |
|
| 118 |
#[derive(TransparentWrapper)] |
| 119 |
#[repr(transparent)] |
| 120 |
#[transparent(u16)] |
| 121 |
struct TransparentTupleWithCustomZeroSized<T>(u16, MyZst<T>); |
| 122 |
|
| 123 |
#[repr(u8)] |
| 124 |
#[derive(Clone, Copy, Contiguous)] |
| 125 |
enum ContiguousWithValues { |
| 126 |
A = 0, |
| 127 |
B = 1, |
| 128 |
C = 2, |
| 129 |
D = 3, |
| 130 |
E = 4, |
| 131 |
} |
| 132 |
|
| 133 |
#[repr(i8)] |
| 134 |
#[derive(Clone, Copy, Contiguous)] |
| 135 |
enum ContiguousWithImplicitValues { |
| 136 |
A = -10, |
| 137 |
B, |
| 138 |
C, |
| 139 |
D, |
| 140 |
E, |
| 141 |
} |
| 142 |
|
| 143 |
#[derive(Copy, Clone, NoUninit)] |
| 144 |
#[repr(C)] |
| 145 |
struct NoUninitTest { |
| 146 |
a: u16, |
| 147 |
b: u16, |
| 148 |
} |
| 149 |
|
| 150 |
#[derive(Copy, Clone, AnyBitPattern)] |
| 151 |
#[repr(C)] |
| 152 |
union UnionTestAnyBitPattern { |
| 153 |
a: u8, |
| 154 |
b: u16, |
| 155 |
} |
| 156 |
|
| 157 |
#[repr(u8)] |
| 158 |
#[derive(Debug, Clone, Copy, NoUninit, CheckedBitPattern, PartialEq, Eq)] |
| 159 |
enum CheckedBitPatternEnumWithValues { |
| 160 |
A = 0, |
| 161 |
B = 1, |
| 162 |
C = 2, |
| 163 |
D = 3, |
| 164 |
E = 4, |
| 165 |
} |
| 166 |
|
| 167 |
#[repr(i8)] |
| 168 |
#[derive(Clone, Copy, NoUninit, CheckedBitPattern)] |
| 169 |
enum CheckedBitPatternEnumWithImplicitValues { |
| 170 |
A = -10, |
| 171 |
B, |
| 172 |
C, |
| 173 |
D, |
| 174 |
E, |
| 175 |
} |
| 176 |
|
| 177 |
#[repr(u8)] |
| 178 |
#[derive(Debug, Clone, Copy, NoUninit, CheckedBitPattern, PartialEq, Eq)] |
| 179 |
enum CheckedBitPatternEnumNonContiguous { |
| 180 |
A = 1, |
| 181 |
B = 8, |
| 182 |
C = 2, |
| 183 |
D = 3, |
| 184 |
E = 56, |
| 185 |
} |
| 186 |
|
| 187 |
#[repr(u8)] |
| 188 |
#[derive(Debug, Clone, Copy, NoUninit, CheckedBitPattern, PartialEq, Eq)] |
| 189 |
enum CheckedBitPatternEnumByteLit { |
| 190 |
A = b'A', |
| 191 |
B = b'B', |
| 192 |
C = b'C', |
| 193 |
D = b'D', |
| 194 |
E = b'E', |
| 195 |
} |
| 196 |
|
| 197 |
#[derive(Debug, Copy, Clone, NoUninit, CheckedBitPattern, PartialEq, Eq)] |
| 198 |
#[repr(C)] |
| 199 |
struct CheckedBitPatternStruct { |
| 200 |
a: u8, |
| 201 |
b: CheckedBitPatternEnumNonContiguous, |
| 202 |
} |
| 203 |
|
| 204 |
#[derive(Debug, Copy, Clone, NoUninit)] |
| 205 |
#[repr(C)] |
| 206 |
enum NoUninitEnum { |
| 207 |
A, |
| 208 |
B, |
| 209 |
} |
| 210 |
|
| 211 |
#[derive(Debug, Copy, Clone, NoUninit)] |
| 212 |
#[repr(C)] |
| 213 |
enum NoUninitEnumWithFields { |
| 214 |
A(u32, u32), |
| 215 |
B(u16, u16, u16, u16), |
| 216 |
} |
| 217 |
|
| 218 |
#[derive(Debug, Copy, Clone, NoUninit)] |
| 219 |
#[repr(C, u16)] |
| 220 |
enum NoUninitEnumWithFieldsAndCAndDiscriminant { |
| 221 |
A(u16, u16), |
| 222 |
B(u8, u8, u8, u8), |
| 223 |
} |
| 224 |
|
| 225 |
#[derive(Debug, Clone, Copy, NoUninit)] |
| 226 |
#[repr(u16)] |
| 227 |
enum NoUninitEnumWithFieldsAndDiscriminant { |
| 228 |
A(u16, u16), |
| 229 |
B(u8, u8, u8, u8), |
| 230 |
} |
| 231 |
|
| 232 |
#[derive(Copy, Clone, NoUninit)] |
| 233 |
#[repr(transparent)] |
| 234 |
struct NoUninitTransparentWrapper<T: NoUninit>(T); |
| 235 |
|
| 236 |
impl<T: NoUninit> NoUninitTransparentWrapper<T> { |
| 237 |
fn new(value: T) -> Self { |
| 238 |
Self(value) |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
#[derive(Debug, Copy, Clone, AnyBitPattern, PartialEq, Eq)] |
| 243 |
#[repr(C)] |
| 244 |
struct AnyBitPatternTest<A: AnyBitPattern, B: AnyBitPattern> { |
| 245 |
a: A, |
| 246 |
b: B, |
| 247 |
} |
| 248 |
|
| 249 |
#[derive(Clone, Copy, CheckedBitPattern)] |
| 250 |
#[repr(C, align(8))] |
| 251 |
struct CheckedBitPatternAlignedStruct { |
| 252 |
a: u16, |
| 253 |
} |
| 254 |
|
| 255 |
#[derive(Clone, Copy, CheckedBitPattern)] |
| 256 |
#[repr(C, packed)] |
| 257 |
struct CheckedBitPatternPackedStruct { |
| 258 |
a: u8, |
| 259 |
b: u16, |
| 260 |
} |
| 261 |
|
| 262 |
#[derive(Debug, Clone, Copy, CheckedBitPattern, PartialEq, Eq)] |
| 263 |
#[repr(C)] |
| 264 |
enum CheckedBitPatternCDefaultDiscriminantEnum { |
| 265 |
A, |
| 266 |
B, |
| 267 |
} |
| 268 |
|
| 269 |
#[derive(Debug, Clone, Copy, CheckedBitPattern, PartialEq, Eq)] |
| 270 |
#[repr(C)] |
| 271 |
enum CheckedBitPatternCDefaultDiscriminantEnumWithFields { |
| 272 |
A(u64), |
| 273 |
B { c: u64 }, |
| 274 |
} |
| 275 |
|
| 276 |
#[derive(Debug, Clone, Copy, CheckedBitPattern, PartialEq, Eq)] |
| 277 |
#[repr(C, u8)] |
| 278 |
enum CheckedBitPatternCEnumWithFields { |
| 279 |
A(u32), |
| 280 |
B { c: u32 }, |
| 281 |
} |
| 282 |
|
| 283 |
#[derive(Debug, Clone, Copy, CheckedBitPattern, PartialEq, Eq)] |
| 284 |
#[repr(u8)] |
| 285 |
enum CheckedBitPatternIntEnumWithFields { |
| 286 |
A(u8), |
| 287 |
B { c: u32 }, |
| 288 |
} |
| 289 |
|
| 290 |
#[derive(Debug, Clone, Copy, CheckedBitPattern, PartialEq, Eq)] |
| 291 |
#[repr(transparent)] |
| 292 |
enum CheckedBitPatternTransparentEnumWithFields { |
| 293 |
A { b: u32 }, |
| 294 |
} |
| 295 |
|
| 296 |
// size 24, align 8. |
| 297 |
// first byte always the u8 discriminant, then 7 bytes of padding until the |
| 298 |
// payload union since the align of the payload is the greatest of the align of |
| 299 |
// all the variants, which is 8 (from |
| 300 |
// CheckedBitPatternCDefaultDiscriminantEnumWithFields) |
| 301 |
#[derive(Debug, Clone, Copy, CheckedBitPattern, PartialEq, Eq)] |
| 302 |
#[repr(C, u8)] |
| 303 |
enum CheckedBitPatternEnumNested { |
| 304 |
A(CheckedBitPatternCEnumWithFields), |
| 305 |
B(CheckedBitPatternCDefaultDiscriminantEnumWithFields), |
| 306 |
} |
| 307 |
|
| 308 |
/// ```compile_fail |
| 309 |
/// use bytemuck::{Pod, Zeroable}; |
| 310 |
/// |
| 311 |
/// #[derive(Pod, Zeroable)] |
| 312 |
/// #[repr(transparent)] |
| 313 |
/// struct TransparentSingle<T>(T); |
| 314 |
/// |
| 315 |
/// struct NotPod(u32); |
| 316 |
/// |
| 317 |
/// let _: u32 = bytemuck::cast(TransparentSingle(NotPod(0u32))); |
| 318 |
/// ``` |
| 319 |
#[derive( |
| 320 |
Debug, Copy, Clone, PartialEq, Eq, Pod, Zeroable, TransparentWrapper, |
| 321 |
)] |
| 322 |
#[repr(transparent)] |
| 323 |
struct NewtypeWrapperTest<T>(T); |
| 324 |
|
| 325 |
#[derive(Debug, Clone, PartialEq, Eq, TransparentWrapper)] |
| 326 |
#[repr(transparent)] |
| 327 |
struct AlgebraicNewtypeWrapperTest<T>(Vec<T>); |
| 328 |
|
| 329 |
#[test] |
| 330 |
fn algebraic_newtype_corect() { |
| 331 |
let x: Vec<u32> = vec![1, 2, 3, 4]; |
| 332 |
let y: AlgebraicNewtypeWrapperTest<u32> = |
| 333 |
AlgebraicNewtypeWrapperTest::wrap(x.clone()); |
| 334 |
assert_eq!(y.0, x); |
| 335 |
} |
| 336 |
|
| 337 |
#[derive(Debug, Clone, PartialEq, Eq, TransparentWrapper)] |
| 338 |
#[repr(transparent)] |
| 339 |
#[transparent(Vec<T>)] |
| 340 |
struct AlgebraicNewtypeWrapperTestWithFields<T, U>(Vec<T>, PhantomData<U>); |
| 341 |
|
| 342 |
#[test] |
| 343 |
fn algebraic_newtype_fields_corect() { |
| 344 |
let x: Vec<u32> = vec![1, 2, 3, 4]; |
| 345 |
let y: AlgebraicNewtypeWrapperTestWithFields<u32, f32> = |
| 346 |
AlgebraicNewtypeWrapperTestWithFields::wrap(x.clone()); |
| 347 |
assert_eq!(y.0, x); |
| 348 |
} |
| 349 |
|
| 350 |
#[test] |
| 351 |
fn fails_cast_contiguous() { |
| 352 |
let can_cast = CheckedBitPatternEnumWithValues::is_valid_bit_pattern(&5); |
| 353 |
assert!(!can_cast); |
| 354 |
} |
| 355 |
|
| 356 |
#[test] |
| 357 |
fn passes_cast_contiguous() { |
| 358 |
let res = |
| 359 |
bytemuck::checked::from_bytes::<CheckedBitPatternEnumWithValues>(&[2u8]); |
| 360 |
assert_eq!(*res, CheckedBitPatternEnumWithValues::C); |
| 361 |
} |
| 362 |
|
| 363 |
#[test] |
| 364 |
fn fails_cast_noncontiguous() { |
| 365 |
let can_cast = CheckedBitPatternEnumNonContiguous::is_valid_bit_pattern(&4); |
| 366 |
assert!(!can_cast); |
| 367 |
} |
| 368 |
|
| 369 |
#[test] |
| 370 |
fn passes_cast_noncontiguous() { |
| 371 |
let res = |
| 372 |
bytemuck::checked::from_bytes::<CheckedBitPatternEnumNonContiguous>(&[ |
| 373 |
56u8, |
| 374 |
]); |
| 375 |
assert_eq!(*res, CheckedBitPatternEnumNonContiguous::E); |
| 376 |
} |
| 377 |
|
| 378 |
#[test] |
| 379 |
fn fails_cast_bytelit() { |
| 380 |
let can_cast = CheckedBitPatternEnumByteLit::is_valid_bit_pattern(&b'a'); |
| 381 |
assert!(!can_cast); |
| 382 |
} |
| 383 |
|
| 384 |
#[test] |
| 385 |
fn passes_cast_bytelit() { |
| 386 |
let res = |
| 387 |
bytemuck::checked::cast_slice::<u8, CheckedBitPatternEnumByteLit>(b"CAB"); |
| 388 |
assert_eq!( |
| 389 |
res, |
| 390 |
[ |
| 391 |
CheckedBitPatternEnumByteLit::C, |
| 392 |
CheckedBitPatternEnumByteLit::A, |
| 393 |
CheckedBitPatternEnumByteLit::B |
| 394 |
] |
| 395 |
); |
| 396 |
} |
| 397 |
|
| 398 |
#[test] |
| 399 |
fn fails_cast_struct() { |
| 400 |
let pod = [0u8, 24u8]; |
| 401 |
let res = bytemuck::checked::try_from_bytes::<CheckedBitPatternStruct>(&pod); |
| 402 |
assert!(res.is_err()); |
| 403 |
} |
| 404 |
|
| 405 |
#[test] |
| 406 |
fn passes_cast_struct() { |
| 407 |
let pod = [0u8, 8u8]; |
| 408 |
let res = bytemuck::checked::from_bytes::<CheckedBitPatternStruct>(&pod); |
| 409 |
assert_eq!( |
| 410 |
*res, |
| 411 |
CheckedBitPatternStruct { a: 0, b: CheckedBitPatternEnumNonContiguous::B } |
| 412 |
); |
| 413 |
} |
| 414 |
|
| 415 |
#[test] |
| 416 |
fn anybitpattern_implies_zeroable() { |
| 417 |
let test = AnyBitPatternTest::<isize, usize>::zeroed(); |
| 418 |
assert_eq!(test, AnyBitPatternTest { a: 0isize, b: 0usize }); |
| 419 |
} |
| 420 |
|
| 421 |
#[test] |
| 422 |
fn checkedbitpattern_try_pod_read_unaligned() { |
| 423 |
let pod = [0u8]; |
| 424 |
let res = bytemuck::checked::try_pod_read_unaligned::< |
| 425 |
CheckedBitPatternEnumWithValues, |
| 426 |
>(&pod); |
| 427 |
assert!(res.is_ok()); |
| 428 |
|
| 429 |
let pod = [5u8]; |
| 430 |
let res = bytemuck::checked::try_pod_read_unaligned::< |
| 431 |
CheckedBitPatternEnumWithValues, |
| 432 |
>(&pod); |
| 433 |
assert!(res.is_err()); |
| 434 |
} |
| 435 |
|
| 436 |
#[test] |
| 437 |
fn checkedbitpattern_aligned_struct() { |
| 438 |
let pod = [0u8; 8]; |
| 439 |
bytemuck::checked::pod_read_unaligned::<CheckedBitPatternAlignedStruct>(&pod); |
| 440 |
} |
| 441 |
|
| 442 |
#[test] |
| 443 |
fn checkedbitpattern_c_default_discriminant_enum_with_fields() { |
| 444 |
// these cfg's *actually* care about align_of::<u64>(), but that is highly |
| 445 |
// correlated with target pointer width. |
| 446 |
#[cfg(target_pointer_width = "64")] |
| 447 |
let pod = [ |
| 448 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcc, 0x55, 0x55, 0x55, |
| 449 |
0x55, 0x55, 0x55, 0xcc, |
| 450 |
]; |
| 451 |
#[cfg(target_pointer_width = "32")] |
| 452 |
let pod = |
| 453 |
[0x00, 0x00, 0x00, 0x00, 0xcc, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xcc]; |
| 454 |
let value = bytemuck::checked::pod_read_unaligned::< |
| 455 |
CheckedBitPatternCDefaultDiscriminantEnumWithFields, |
| 456 |
>(&pod); |
| 457 |
assert_eq!( |
| 458 |
value, |
| 459 |
CheckedBitPatternCDefaultDiscriminantEnumWithFields::A(0xcc555555555555cc) |
| 460 |
); |
| 461 |
|
| 462 |
#[cfg(all(target_pointer_width = "64", target_endian = "little"))] |
| 463 |
let pod = [ |
| 464 |
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcc, 0x55, 0x55, 0x55, |
| 465 |
0x55, 0x55, 0x55, 0xcc, |
| 466 |
]; |
| 467 |
#[cfg(all(target_pointer_width = "64", target_endian = "big"))] |
| 468 |
let pod = [ |
| 469 |
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xcc, 0x55, 0x55, 0x55, |
| 470 |
0x55, 0x55, 0x55, 0xcc, |
| 471 |
]; |
| 472 |
#[cfg(target_pointer_width = "32")] |
| 473 |
let pod = |
| 474 |
[0x01, 0x00, 0x00, 0x00, 0xcc, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xcc]; |
| 475 |
let value = bytemuck::checked::pod_read_unaligned::< |
| 476 |
CheckedBitPatternCDefaultDiscriminantEnumWithFields, |
| 477 |
>(&pod); |
| 478 |
assert_eq!( |
| 479 |
value, |
| 480 |
CheckedBitPatternCDefaultDiscriminantEnumWithFields::B { |
| 481 |
c: 0xcc555555555555cc |
| 482 |
} |
| 483 |
); |
| 484 |
} |
| 485 |
|
| 486 |
#[test] |
| 487 |
fn checkedbitpattern_c_enum_with_fields() { |
| 488 |
let pod = [0x00, 0x00, 0x00, 0x00, 0xcc, 0x55, 0x55, 0xcc]; |
| 489 |
let value = bytemuck::checked::pod_read_unaligned::< |
| 490 |
CheckedBitPatternCEnumWithFields, |
| 491 |
>(&pod); |
| 492 |
assert_eq!(value, CheckedBitPatternCEnumWithFields::A(0xcc5555cc)); |
| 493 |
|
| 494 |
let pod = [0x01, 0x00, 0x00, 0x00, 0xcc, 0x55, 0x55, 0xcc]; |
| 495 |
let value = bytemuck::checked::pod_read_unaligned::< |
| 496 |
CheckedBitPatternCEnumWithFields, |
| 497 |
>(&pod); |
| 498 |
assert_eq!(value, CheckedBitPatternCEnumWithFields::B { c: 0xcc5555cc }); |
| 499 |
} |
| 500 |
|
| 501 |
#[test] |
| 502 |
fn checkedbitpattern_int_enum_with_fields() { |
| 503 |
let pod = [0x00, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 504 |
let value = bytemuck::checked::pod_read_unaligned::< |
| 505 |
CheckedBitPatternIntEnumWithFields, |
| 506 |
>(&pod); |
| 507 |
assert_eq!(value, CheckedBitPatternIntEnumWithFields::A(0x55)); |
| 508 |
|
| 509 |
let pod = [0x01, 0x00, 0x00, 0x00, 0xcc, 0x55, 0x55, 0xcc]; |
| 510 |
let value = bytemuck::checked::pod_read_unaligned::< |
| 511 |
CheckedBitPatternIntEnumWithFields, |
| 512 |
>(&pod); |
| 513 |
assert_eq!(value, CheckedBitPatternIntEnumWithFields::B { c: 0xcc5555cc }); |
| 514 |
} |
| 515 |
|
| 516 |
#[test] |
| 517 |
fn checkedbitpattern_nested_enum_with_fields() { |
| 518 |
// these cfg's *actually* care about align_of::<u64>(), but that is highly |
| 519 |
// correlated with target pointer width. |
| 520 |
|
| 521 |
// total size 24 bytes on 64-bit. first byte always the u8 discriminant. |
| 522 |
#[cfg(target_pointer_width = "64")] |
| 523 |
#[repr(C, align(8))] |
| 524 |
struct Align8Bytes([u8; 24]); |
| 525 |
|
| 526 |
// total size 16 bytes on 64-bit. first byte always the u8 discriminant. |
| 527 |
#[cfg(target_pointer_width = "32")] |
| 528 |
#[repr(C, align(8))] |
| 529 |
struct Align8Bytes([u8; 16]); |
| 530 |
|
| 531 |
// first we'll check variantA, nested variant A |
| 532 |
#[cfg(target_pointer_width = "64")] |
| 533 |
let mut pod = Align8Bytes([ |
| 534 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 535 |
0x00, // byte 0 discriminant = 0 = variant A, bytes 1-7 irrelevant padding. |
| 536 |
0x00, 0x00, 0x00, 0x00, 0xcc, 0x55, 0x55, |
| 537 |
0xcc, // bytes 8-15 are the nested CheckedBitPatternCEnumWithFields, |
| 538 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // bytes 16-23 padding |
| 539 |
]); |
| 540 |
#[cfg(target_pointer_width = "32")] |
| 541 |
let mut pod = Align8Bytes([ |
| 542 |
0x00, 0x00, 0x00, |
| 543 |
0x00, // byte 0 discriminant = 0 = variant A, bytes 1-3 irrelevant padding. |
| 544 |
0x00, 0x00, 0x00, 0x00, 0xcc, 0x55, 0x55, |
| 545 |
0xcc, // bytes 4-11 are the nested CheckedBitPatternCEnumWithFields, |
| 546 |
0x00, 0x00, 0x00, 0x00, // bytes 12-15 padding |
| 547 |
]); |
| 548 |
let value = |
| 549 |
bytemuck::checked::from_bytes::<CheckedBitPatternEnumNested>(&pod.0); |
| 550 |
assert_eq!( |
| 551 |
value, |
| 552 |
&CheckedBitPatternEnumNested::A(CheckedBitPatternCEnumWithFields::A( |
| 553 |
0xcc5555cc |
| 554 |
)) |
| 555 |
); |
| 556 |
|
| 557 |
// next we'll check invalid first discriminant fails |
| 558 |
pod.0[0] = 0x02; // set the discriminant |
| 559 |
let result = |
| 560 |
bytemuck::checked::try_from_bytes::<CheckedBitPatternEnumNested>(&pod.0); |
| 561 |
assert_eq!(result, Err(CheckedCastError::InvalidBitPattern)); |
| 562 |
|
| 563 |
// next we'll check variant B, nested variant B |
| 564 |
#[cfg(all(target_pointer_width = "64", target_endian = "little"))] |
| 565 |
let pod = Align8Bytes([ |
| 566 |
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 567 |
0x00, // byte 0 discriminant = 1 = variant B, bytes 1-7 padding |
| 568 |
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 569 |
0x00, /* bytes 8-15 is C int size discriminant of |
| 570 |
* CheckedBitPatternCDefaultDiscrimimantEnumWithFields, 1 (LE byte |
| 571 |
* order) = variant B */ |
| 572 |
0xcc, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 573 |
0xcc, // bytes 16-23 is the data contained in nested variant B |
| 574 |
]); |
| 575 |
#[cfg(all(target_pointer_width = "64", target_endian = "big"))] |
| 576 |
let pod = Align8Bytes([ |
| 577 |
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 578 |
0x00, // byte 0 discriminant = 1 = variant B, bytes 1-7 padding |
| 579 |
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 580 |
0x00, /* bytes 8-15 is C int size discriminant of |
| 581 |
* CheckedBitPatternCDefaultDiscrimimantEnumWithFields, 1 (LE byte |
| 582 |
* order) = variant B */ |
| 583 |
0xcc, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 584 |
0xcc, // bytes 16-23 is the data contained in nested variant B |
| 585 |
]); |
| 586 |
#[cfg(target_pointer_width = "32")] |
| 587 |
let pod = Align8Bytes([ |
| 588 |
0x01, 0x00, 0x00, |
| 589 |
0x00, // byte 0 discriminant = 1 = variant B, bytes 1-3 padding |
| 590 |
0x01, 0x00, 0x00, |
| 591 |
0x00, /* bytes 4-11 is C int size discriminant of |
| 592 |
* CheckedBitPatternCDefaultDiscrimimantEnumWithFields, 1 (LE byte |
| 593 |
* order) = variant B */ |
| 594 |
0xcc, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 595 |
0xcc, // bytes 12-15 is the data contained in nested variant B |
| 596 |
]); |
| 597 |
let value = |
| 598 |
bytemuck::checked::from_bytes::<CheckedBitPatternEnumNested>(&pod.0); |
| 599 |
assert_eq!( |
| 600 |
value, |
| 601 |
&CheckedBitPatternEnumNested::B( |
| 602 |
CheckedBitPatternCDefaultDiscriminantEnumWithFields::B { |
| 603 |
c: 0xcc555555555555cc |
| 604 |
} |
| 605 |
) |
| 606 |
); |
| 607 |
|
| 608 |
// finally we'll check variant B, nested invalid discriminant |
| 609 |
#[cfg(target_pointer_width = "64")] |
| 610 |
let pod = Align8Bytes([ |
| 611 |
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 612 |
0x00, // 1 discriminant = variant B, bytes 1-7 padding |
| 613 |
0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 614 |
0x00, /* bytes 8-15 is C int size discriminant of |
| 615 |
* CheckedBitPatternCDefaultDiscrimimantEnumWithFields, 0x08 is |
| 616 |
* invalid */ |
| 617 |
0xcc, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 618 |
0xcc, // bytes 16-23 is the data contained in nested variant B |
| 619 |
]); |
| 620 |
#[cfg(target_pointer_width = "32")] |
| 621 |
let pod = Align8Bytes([ |
| 622 |
0x01, 0x00, 0x00, |
| 623 |
0x00, // 1 discriminant = variant B, bytes 1-7 padding |
| 624 |
0x08, 0x00, 0x00, |
| 625 |
0x00, /* bytes 4-11 is C int size discriminant of |
| 626 |
* CheckedBitPatternCDefaultDiscrimimantEnumWithFields, 0x08 is |
| 627 |
* invalid */ |
| 628 |
0xcc, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 629 |
0xcc, // bytes 12-15 is the data contained in nested variant B |
| 630 |
]); |
| 631 |
let result = |
| 632 |
bytemuck::checked::try_from_bytes::<CheckedBitPatternEnumNested>(&pod.0); |
| 633 |
assert_eq!(result, Err(CheckedCastError::InvalidBitPattern)); |
| 634 |
} |
| 635 |
#[test] |
| 636 |
fn checkedbitpattern_transparent_enum_with_fields() { |
| 637 |
let pod = [0xcc, 0x55, 0x55, 0xcc]; |
| 638 |
let value = bytemuck::checked::pod_read_unaligned::< |
| 639 |
CheckedBitPatternTransparentEnumWithFields, |
| 640 |
>(&pod); |
| 641 |
assert_eq!( |
| 642 |
value, |
| 643 |
CheckedBitPatternTransparentEnumWithFields::A { b: 0xcc5555cc } |
| 644 |
); |
| 645 |
} |
| 646 |
|
| 647 |
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] |
| 648 |
#[repr(C, align(16))] |
| 649 |
struct Issue127 {} |
| 650 |
|
| 651 |
use bytemuck as reexport_name; |
| 652 |
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, bytemuck::ByteEq)] |
| 653 |
#[bytemuck(crate = "reexport_name")] |
| 654 |
#[repr(C)] |
| 655 |
struct Issue93 {} |