| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 |
|
| 12 | use core::{
|
| 13 | cell::{Cell, UnsafeCell},
|
| 14 | mem::MaybeUninit as CoreMaybeUninit,
|
| 15 | ptr::NonNull,
|
| 16 | };
|
| 17 |
|
| 18 | use super::*;
|
| 19 | use crate::pointer::cast::{CastSizedExact, CastUnsized};
|
| 20 |
|
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 31 | const _: () = unsafe {
|
| 32 | unsafe_impl!((): Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
|
| 33 | assert_unaligned!(());
|
| 34 | };
|
| 35 |
|
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 67 | const _: () = unsafe {
|
| 68 | unsafe_impl!(u8: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
|
| 69 | unsafe_impl!(i8: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
|
| 70 | assert_unaligned!(u8, i8);
|
| 71 | unsafe_impl!(u16: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 72 | unsafe_impl!(i16: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 73 | unsafe_impl!(u32: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 74 | unsafe_impl!(i32: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 75 | unsafe_impl!(u64: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 76 | unsafe_impl!(i64: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 77 | unsafe_impl!(u128: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 78 | unsafe_impl!(i128: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 79 | unsafe_impl!(usize: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 80 | unsafe_impl!(isize: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 81 | unsafe_impl!(f32: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 82 | unsafe_impl!(f64: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 83 | #[cfg(feature = "float-nightly")]
|
| 84 | unsafe_impl!(#[cfg_attr(doc_cfg, doc(cfg(feature = "float-nightly")))] f16: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 85 | #[cfg(feature = "float-nightly")]
|
| 86 | unsafe_impl!(#[cfg_attr(doc_cfg, doc(cfg(feature = "float-nightly")))] f128: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 87 | };
|
| 88 |
|
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 101 | const _: () = unsafe { unsafe_impl!(bool: Immutable, FromZeros, IntoBytes, Unaligned) };
|
| 102 | assert_unaligned!(bool);
|
| 103 |
|
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | const _: () = unsafe {
|
| 113 | unsafe_impl!(=> TryFromBytes for bool; |byte| {
|
| 114 | let byte = byte.transmute_with::<u8, invariant::Valid, CastSizedExact, BecauseImmutable>();
|
| 115 | *byte.unaligned_as_ref() < 2
|
| 116 | })
|
| 117 | };
|
| 118 |
|
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 131 | const _: () = unsafe { unsafe_impl!(char: Immutable, FromZeros, IntoBytes) };
|
| 132 |
|
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | const _: () = unsafe {
|
| 142 | unsafe_impl!(=> TryFromBytes for char; |c| {
|
| 143 | let c = c.transmute_with::<Unalign<u32>, invariant::Valid, CastSizedExact, BecauseImmutable>();
|
| 144 | let c = c.read().into_inner();
|
| 145 | char::from_u32(c).is_some()
|
| 146 | });
|
| 147 | };
|
| 148 |
|
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 165 | const _: () = unsafe { unsafe_impl!(str: Immutable, FromZeros, IntoBytes, Unaligned) };
|
| 166 |
|
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | const _: () = unsafe {
|
| 175 | unsafe_impl!(=> TryFromBytes for str; |c| {
|
| 176 | let c = c.transmute_with::<[u8], invariant::Valid, CastUnsized, BecauseImmutable>();
|
| 177 | let c = c.unaligned_as_ref();
|
| 178 | core::str::from_utf8(c).is_ok()
|
| 179 | })
|
| 180 | };
|
| 181 |
|
| 182 | macro_rules! unsafe_impl_try_from_bytes_for_nonzero {
|
| 183 | ($($nonzero:ident[$prim:ty]),*) => {
|
| 184 | $(
|
| 185 | unsafe_impl!(=> TryFromBytes for $nonzero; |n| {
|
| 186 | let n = n.transmute_with::<Unalign<$prim>, invariant::Valid, CastSizedExact, BecauseImmutable>();
|
| 187 | $nonzero::new(n.read().into_inner()).is_some()
|
| 188 | });
|
| 189 | )*
|
| 190 | }
|
| 191 | }
|
| 192 |
|
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 226 | const _: () = unsafe {
|
| 227 | unsafe_impl!(NonZeroU8: Immutable, IntoBytes, Unaligned);
|
| 228 | unsafe_impl!(NonZeroI8: Immutable, IntoBytes, Unaligned);
|
| 229 | assert_unaligned!(NonZeroU8, NonZeroI8);
|
| 230 | unsafe_impl!(NonZeroU16: Immutable, IntoBytes);
|
| 231 | unsafe_impl!(NonZeroI16: Immutable, IntoBytes);
|
| 232 | unsafe_impl!(NonZeroU32: Immutable, IntoBytes);
|
| 233 | unsafe_impl!(NonZeroI32: Immutable, IntoBytes);
|
| 234 | unsafe_impl!(NonZeroU64: Immutable, IntoBytes);
|
| 235 | unsafe_impl!(NonZeroI64: Immutable, IntoBytes);
|
| 236 | unsafe_impl!(NonZeroU128: Immutable, IntoBytes);
|
| 237 | unsafe_impl!(NonZeroI128: Immutable, IntoBytes);
|
| 238 | unsafe_impl!(NonZeroUsize: Immutable, IntoBytes);
|
| 239 | unsafe_impl!(NonZeroIsize: Immutable, IntoBytes);
|
| 240 | unsafe_impl_try_from_bytes_for_nonzero!(
|
| 241 | NonZeroU8[u8],
|
| 242 | NonZeroI8[i8],
|
| 243 | NonZeroU16[u16],
|
| 244 | NonZeroI16[i16],
|
| 245 | NonZeroU32[u32],
|
| 246 | NonZeroI32[i32],
|
| 247 | NonZeroU64[u64],
|
| 248 | NonZeroI64[i64],
|
| 249 | NonZeroU128[u128],
|
| 250 | NonZeroI128[i128],
|
| 251 | NonZeroUsize[usize],
|
| 252 | NonZeroIsize[isize]
|
| 253 | );
|
| 254 | };
|
| 255 |
|
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 281 | const _: () = unsafe {
|
| 282 | unsafe_impl!(Option<NonZeroU8>: TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
|
| 283 | unsafe_impl!(Option<NonZeroI8>: TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
|
| 284 | assert_unaligned!(Option<NonZeroU8>, Option<NonZeroI8>);
|
| 285 | unsafe_impl!(Option<NonZeroU16>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 286 | unsafe_impl!(Option<NonZeroI16>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 287 | unsafe_impl!(Option<NonZeroU32>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 288 | unsafe_impl!(Option<NonZeroI32>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 289 | unsafe_impl!(Option<NonZeroU64>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 290 | unsafe_impl!(Option<NonZeroI64>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 291 | unsafe_impl!(Option<NonZeroU128>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 292 | unsafe_impl!(Option<NonZeroI128>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 293 | unsafe_impl!(Option<NonZeroUsize>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 294 | unsafe_impl!(Option<NonZeroIsize>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
|
| 295 | };
|
| 296 |
|
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | #[cfg(feature = "alloc")]
|
| 303 | const _: () = unsafe {
|
| 304 | unsafe_impl!(
|
| 305 | #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
|
| 306 | T: Sized => Immutable for Box<T>
|
| 307 | )
|
| 308 | };
|
| 309 |
|
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 332 | const _: () = unsafe {
|
| 333 | #[cfg(feature = "alloc")]
|
| 334 | unsafe_impl!(
|
| 335 | #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
|
| 336 | T => TryFromBytes for Option<Box<T>>; |c| pointer::is_zeroed(c)
|
| 337 | );
|
| 338 | #[cfg(feature = "alloc")]
|
| 339 | unsafe_impl!(
|
| 340 | #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
|
| 341 | T => FromZeros for Option<Box<T>>
|
| 342 | );
|
| 343 | unsafe_impl!(
|
| 344 | T => TryFromBytes for Option<&'_ T>; |c| pointer::is_zeroed(c)
|
| 345 | );
|
| 346 | unsafe_impl!(T => FromZeros for Option<&'_ T>);
|
| 347 | unsafe_impl!(
|
| 348 | T => TryFromBytes for Option<&'_ mut T>; |c| pointer::is_zeroed(c)
|
| 349 | );
|
| 350 | unsafe_impl!(T => FromZeros for Option<&'_ mut T>);
|
| 351 | unsafe_impl!(
|
| 352 | T => TryFromBytes for Option<NonNull<T>>; |c| pointer::is_zeroed(c)
|
| 353 | );
|
| 354 | unsafe_impl!(T => FromZeros for Option<NonNull<T>>);
|
| 355 | unsafe_impl_for_power_set!(A, B, C, D, E, F, G, H, I, J, K, L -> M => FromZeros for opt_fn!(...));
|
| 356 | unsafe_impl_for_power_set!(
|
| 357 | A, B, C, D, E, F, G, H, I, J, K, L -> M => TryFromBytes for opt_fn!(...);
|
| 358 | |c| pointer::is_zeroed(c)
|
| 359 | );
|
| 360 | unsafe_impl_for_power_set!(A, B, C, D, E, F, G, H, I, J, K, L -> M => FromZeros for opt_unsafe_fn!(...));
|
| 361 | unsafe_impl_for_power_set!(
|
| 362 | A, B, C, D, E, F, G, H, I, J, K, L -> M => TryFromBytes for opt_unsafe_fn!(...);
|
| 363 | |c| pointer::is_zeroed(c)
|
| 364 | );
|
| 365 | unsafe_impl_for_power_set!(A, B, C, D, E, F, G, H, I, J, K, L -> M => FromZeros for opt_extern_c_fn!(...));
|
| 366 | unsafe_impl_for_power_set!(
|
| 367 | A, B, C, D, E, F, G, H, I, J, K, L -> M => TryFromBytes for opt_extern_c_fn!(...);
|
| 368 | |c| pointer::is_zeroed(c)
|
| 369 | );
|
| 370 | unsafe_impl_for_power_set!(A, B, C, D, E, F, G, H, I, J, K, L -> M => FromZeros for opt_unsafe_extern_c_fn!(...));
|
| 371 | unsafe_impl_for_power_set!(
|
| 372 | A, B, C, D, E, F, G, H, I, J, K, L -> M => TryFromBytes for opt_unsafe_extern_c_fn!(...);
|
| 373 | |c| pointer::is_zeroed(c)
|
| 374 | );
|
| 375 | };
|
| 376 |
|
| 377 | |
| 378 | |
| 379 | |
| 380 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 381 | const _: () = unsafe {
|
| 382 | unsafe_impl_for_power_set!(A, B, C, D, E, F, G, H, I, J, K, L -> M => Immutable for opt_fn!(...));
|
| 383 | unsafe_impl_for_power_set!(A, B, C, D, E, F, G, H, I, J, K, L -> M => Immutable for opt_unsafe_fn!(...));
|
| 384 | unsafe_impl_for_power_set!(A, B, C, D, E, F, G, H, I, J, K, L -> M => Immutable for opt_extern_c_fn!(...));
|
| 385 | unsafe_impl_for_power_set!(A, B, C, D, E, F, G, H, I, J, K, L -> M => Immutable for opt_unsafe_extern_c_fn!(...));
|
| 386 | };
|
| 387 |
|
| 388 | #[cfg(all(
|
| 389 | not(no_zerocopy_target_has_atomics_1_60_0),
|
| 390 | any(
|
| 391 | target_has_atomic = "8",
|
| 392 | target_has_atomic = "16",
|
| 393 | target_has_atomic = "32",
|
| 394 | target_has_atomic = "64",
|
| 395 | target_has_atomic = "ptr"
|
| 396 | )
|
| 397 | ))]
|
| 398 | #[cfg_attr(doc_cfg, doc(cfg(rust = "1.60.0")))]
|
| 399 | mod atomics {
|
| 400 | use super::*;
|
| 401 |
|
| 402 | macro_rules! impl_traits_for_atomics {
|
| 403 | ($($atomics:tt [$primitives:ty]),* $(,)?) => {
|
| 404 | $(
|
| 405 | impl_known_layout!($atomics);
|
| 406 | impl_for_transmute_from!(=> FromZeros for $atomics [$primitives]);
|
| 407 | impl_for_transmute_from!(=> FromBytes for $atomics [$primitives]);
|
| 408 | impl_for_transmute_from!(=> TryFromBytes for $atomics [$primitives]);
|
| 409 | impl_for_transmute_from!(=> IntoBytes for $atomics [$primitives]);
|
| 410 | )*
|
| 411 | };
|
| 412 | }
|
| 413 |
|
| 414 | /// Implements `TransmuteFrom` for `$atomic`, `$prim`, and
|
| 415 | /// `UnsafeCell<$prim>`.
|
| 416 | ///
|
| 417 | /// # Safety
|
| 418 | ///
|
| 419 | /// `$atomic` must have the same size and bit validity as `$prim`.
|
| 420 | macro_rules! unsafe_impl_transmute_from_for_atomic {
|
| 421 | ($($($tyvar:ident)? => $atomic:ty [$prim:ty]),*) => {{
|
| 422 | crate::util::macros::__unsafe();
|
| 423 |
|
| 424 | use crate::pointer::{SizeEq, TransmuteFrom, invariant::Valid};
|
| 425 |
|
| 426 | $(
|
| 427 | |
| 428 | |
| 429 | unsafe impl<$($tyvar)?> TransmuteFrom<$atomic, Valid, Valid> for $prim {}
|
| 430 | |
| 431 | |
| 432 | unsafe impl<$($tyvar)?> TransmuteFrom<$prim, Valid, Valid> for $atomic {}
|
| 433 |
|
| 434 | impl<$($tyvar)?> SizeEq<ReadOnly<$atomic>> for ReadOnly<$prim> {
|
| 435 | type CastFrom = $crate::pointer::cast::CastSizedExact;
|
| 436 | }
|
| 437 |
|
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | unsafe impl<$($tyvar)?> TransmuteFrom<$atomic, Valid, Valid> for core::cell::UnsafeCell<$prim> {}
|
| 448 | |
| 449 | unsafe impl<$($tyvar)?> TransmuteFrom<core::cell::UnsafeCell<$prim>, Valid, Valid> for $atomic {}
|
| 450 | )*
|
| 451 | }};
|
| 452 | }
|
| 453 |
|
| 454 | #[cfg(target_has_atomic = "8")]
|
| 455 | #[cfg_attr(doc_cfg, doc(cfg(target_has_atomic = "8")))]
|
| 456 | mod atomic_8 {
|
| 457 | use core::sync::atomic::{AtomicBool, AtomicI8, AtomicU8};
|
| 458 |
|
| 459 | use super::*;
|
| 460 |
|
| 461 | impl_traits_for_atomics!(AtomicU8[u8], AtomicI8[i8]);
|
| 462 |
|
| 463 | impl_known_layout!(AtomicBool);
|
| 464 | impl_for_transmute_from!(=> FromZeros for AtomicBool [bool]);
|
| 465 | impl_for_transmute_from!(=> TryFromBytes for AtomicBool [bool]);
|
| 466 | impl_for_transmute_from!(=> IntoBytes for AtomicBool [bool]);
|
| 467 |
|
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 487 | const _: () = unsafe {
|
| 488 | unsafe_impl!(AtomicBool: Unaligned);
|
| 489 | unsafe_impl!(AtomicU8: Unaligned);
|
| 490 | unsafe_impl!(AtomicI8: Unaligned);
|
| 491 | assert_unaligned!(AtomicBool, AtomicU8, AtomicI8);
|
| 492 | };
|
| 493 |
|
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 511 | const _: () = unsafe {
|
| 512 | unsafe_impl_transmute_from_for_atomic!(
|
| 513 | => AtomicU8 [u8],
|
| 514 | => AtomicI8 [i8],
|
| 515 | => AtomicBool [bool]
|
| 516 | )
|
| 517 | };
|
| 518 | }
|
| 519 |
|
| 520 | #[cfg(target_has_atomic = "16")]
|
| 521 | #[cfg_attr(doc_cfg, doc(cfg(target_has_atomic = "16")))]
|
| 522 | mod atomic_16 {
|
| 523 | use core::sync::atomic::{AtomicI16, AtomicU16};
|
| 524 |
|
| 525 | use super::*;
|
| 526 |
|
| 527 | impl_traits_for_atomics!(AtomicU16[u16], AtomicI16[i16]);
|
| 528 |
|
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 542 | const _: () = unsafe {
|
| 543 | unsafe_impl_transmute_from_for_atomic!(=> AtomicU16 [u16], => AtomicI16 [i16])
|
| 544 | };
|
| 545 | }
|
| 546 |
|
| 547 | #[cfg(target_has_atomic = "32")]
|
| 548 | #[cfg_attr(doc_cfg, doc(cfg(target_has_atomic = "32")))]
|
| 549 | mod atomic_32 {
|
| 550 | use core::sync::atomic::{AtomicI32, AtomicU32};
|
| 551 |
|
| 552 | use super::*;
|
| 553 |
|
| 554 | impl_traits_for_atomics!(AtomicU32[u32], AtomicI32[i32]);
|
| 555 |
|
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 569 | const _: () = unsafe {
|
| 570 | unsafe_impl_transmute_from_for_atomic!(=> AtomicU32 [u32], => AtomicI32 [i32])
|
| 571 | };
|
| 572 | }
|
| 573 |
|
| 574 | #[cfg(target_has_atomic = "64")]
|
| 575 | #[cfg_attr(doc_cfg, doc(cfg(target_has_atomic = "64")))]
|
| 576 | mod atomic_64 {
|
| 577 | use core::sync::atomic::{AtomicI64, AtomicU64};
|
| 578 |
|
| 579 | use super::*;
|
| 580 |
|
| 581 | impl_traits_for_atomics!(AtomicU64[u64], AtomicI64[i64]);
|
| 582 |
|
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 596 | const _: () = unsafe {
|
| 597 | unsafe_impl_transmute_from_for_atomic!(=> AtomicU64 [u64], => AtomicI64 [i64])
|
| 598 | };
|
| 599 | }
|
| 600 |
|
| 601 | #[cfg(target_has_atomic = "ptr")]
|
| 602 | #[cfg_attr(doc_cfg, doc(cfg(target_has_atomic = "ptr")))]
|
| 603 | mod atomic_ptr {
|
| 604 | use core::sync::atomic::{AtomicIsize, AtomicPtr, AtomicUsize};
|
| 605 |
|
| 606 | use super::*;
|
| 607 |
|
| 608 | impl_traits_for_atomics!(AtomicUsize[usize], AtomicIsize[isize]);
|
| 609 |
|
| 610 | |
| 611 | |
| 612 | impl_known_layout!(T => AtomicPtr<T>);
|
| 613 | impl_for_transmute_from!(T => TryFromBytes for AtomicPtr<T> [*mut T]);
|
| 614 | impl_for_transmute_from!(T => FromZeros for AtomicPtr<T> [*mut T]);
|
| 615 |
|
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 629 | const _: () = unsafe {
|
| 630 | unsafe_impl_transmute_from_for_atomic!(=> AtomicUsize [usize], => AtomicIsize [isize])
|
| 631 | };
|
| 632 |
|
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 638 | const _: () = unsafe { unsafe_impl_transmute_from_for_atomic!(T => AtomicPtr<T> [*mut T]) };
|
| 639 | }
|
| 640 | }
|
| 641 |
|
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 653 | const _: () = unsafe {
|
| 654 | unsafe_impl!(T: ?Sized => Immutable for PhantomData<T>);
|
| 655 | unsafe_impl!(T: ?Sized => TryFromBytes for PhantomData<T>);
|
| 656 | unsafe_impl!(T: ?Sized => FromZeros for PhantomData<T>);
|
| 657 | unsafe_impl!(T: ?Sized => FromBytes for PhantomData<T>);
|
| 658 | unsafe_impl!(T: ?Sized => IntoBytes for PhantomData<T>);
|
| 659 | unsafe_impl!(T: ?Sized => Unaligned for PhantomData<T>);
|
| 660 | assert_unaligned!(PhantomData<()>, PhantomData<u8>, PhantomData<u64>);
|
| 661 | };
|
| 662 |
|
| 663 | impl_for_transmute_from!(T: TryFromBytes => TryFromBytes for Wrapping<T>[T]);
|
| 664 | impl_for_transmute_from!(T: FromZeros => FromZeros for Wrapping<T>[T]);
|
| 665 | impl_for_transmute_from!(T: FromBytes => FromBytes for Wrapping<T>[T]);
|
| 666 | impl_for_transmute_from!(T: IntoBytes => IntoBytes for Wrapping<T>[T]);
|
| 667 | assert_unaligned!(Wrapping<()>, Wrapping<u8>);
|
| 668 |
|
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | const _: () = unsafe { unsafe_impl!(T: Immutable => Immutable for Wrapping<T>) };
|
| 680 |
|
| 681 | |
| 682 | |
| 683 | const _: () = unsafe { unsafe_impl!(T: Unaligned => Unaligned for Wrapping<T>) };
|
| 684 |
|
| 685 | |
| 686 | |
| 687 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 688 | const _: () = unsafe {
|
| 689 | unsafe_impl!(T => TryFromBytes for CoreMaybeUninit<T>);
|
| 690 | unsafe_impl!(T => FromZeros for CoreMaybeUninit<T>);
|
| 691 | unsafe_impl!(T => FromBytes for CoreMaybeUninit<T>);
|
| 692 | };
|
| 693 |
|
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | const _: () = unsafe { unsafe_impl!(T: Immutable => Immutable for CoreMaybeUninit<T>) };
|
| 712 |
|
| 713 | |
| 714 | |
| 715 | const _: () = unsafe { unsafe_impl!(T: Unaligned => Unaligned for CoreMaybeUninit<T>) };
|
| 716 | assert_unaligned!(CoreMaybeUninit<()>, CoreMaybeUninit<u8>);
|
| 717 |
|
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | const _: () = unsafe { unsafe_impl!(T: ?Sized + Immutable => Immutable for ManuallyDrop<T>) };
|
| 736 |
|
| 737 | impl_for_transmute_from!(T: ?Sized + TryFromBytes => TryFromBytes for ManuallyDrop<T>[T]);
|
| 738 | impl_for_transmute_from!(T: ?Sized + FromZeros => FromZeros for ManuallyDrop<T>[T]);
|
| 739 | impl_for_transmute_from!(T: ?Sized + FromBytes => FromBytes for ManuallyDrop<T>[T]);
|
| 740 | impl_for_transmute_from!(T: ?Sized + IntoBytes => IntoBytes for ManuallyDrop<T>[T]);
|
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | const _: () = unsafe { unsafe_impl!(T: ?Sized + Unaligned => Unaligned for ManuallyDrop<T>) };
|
| 749 | assert_unaligned!(ManuallyDrop<()>, ManuallyDrop<u8>);
|
| 750 |
|
| 751 | const _: () = {
|
| 752 | #[allow(
|
| 753 | non_camel_case_types,
|
| 754 | missing_copy_implementations,
|
| 755 | missing_debug_implementations,
|
| 756 | missing_docs
|
| 757 | )]
|
| 758 | pub enum value {}
|
| 759 |
|
| 760 | |
| 761 | unsafe impl<T: ?Sized> HasTag for ManuallyDrop<T> {
|
| 762 | #[inline]
|
| 763 | fn only_derive_is_allowed_to_implement_this_trait()
|
| 764 | where
|
| 765 | Self: Sized,
|
| 766 | {
|
| 767 | }
|
| 768 |
|
| 769 | type Tag = ();
|
| 770 |
|
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | type ProjectToTag = crate::pointer::cast::CastToUnit;
|
| 782 | }
|
| 783 |
|
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | unsafe impl<T: ?Sized>
|
| 800 | HasField<value, { crate::STRUCT_VARIANT_ID }, { crate::ident_id!(value) }>
|
| 801 | for ManuallyDrop<T>
|
| 802 | {
|
| 803 | #[inline]
|
| 804 | fn only_derive_is_allowed_to_implement_this_trait()
|
| 805 | where
|
| 806 | Self: Sized,
|
| 807 | {
|
| 808 | }
|
| 809 |
|
| 810 | type Type = T;
|
| 811 |
|
| 812 | #[inline(always)]
|
| 813 | fn project(slf: PtrInner<'_, Self>) -> *mut T {
|
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | #[allow(clippy::as_conversions)]
|
| 822 | return slf.as_ptr() as *mut T;
|
| 823 | }
|
| 824 | }
|
| 825 | };
|
| 826 |
|
| 827 | impl_for_transmute_from!(T: ?Sized + TryFromBytes => TryFromBytes for Cell<T>[T]);
|
| 828 | impl_for_transmute_from!(T: ?Sized + FromZeros => FromZeros for Cell<T>[T]);
|
| 829 | impl_for_transmute_from!(T: ?Sized + FromBytes => FromBytes for Cell<T>[T]);
|
| 830 | impl_for_transmute_from!(T: ?Sized + IntoBytes => IntoBytes for Cell<T>[T]);
|
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | const _: () = unsafe { unsafe_impl!(T: ?Sized + Unaligned => Unaligned for Cell<T>) };
|
| 838 |
|
| 839 | impl_for_transmute_from!(T: ?Sized + FromZeros => FromZeros for UnsafeCell<T>[T]);
|
| 840 | impl_for_transmute_from!(T: ?Sized + FromBytes => FromBytes for UnsafeCell<T>[T]);
|
| 841 | impl_for_transmute_from!(T: ?Sized + IntoBytes => IntoBytes for UnsafeCell<T>[T]);
|
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | |
| 849 | const _: () = unsafe { unsafe_impl!(T: ?Sized + Unaligned => Unaligned for UnsafeCell<T>) };
|
| 850 | assert_unaligned!(UnsafeCell<()>, UnsafeCell<u8>);
|
| 851 |
|
| 852 | |
| 853 | unsafe impl<T: TryFromBytes + ?Sized> TryFromBytes for UnsafeCell<T> {
|
| 854 | #[allow(clippy::missing_inline_in_public_items)]
|
| 855 | fn only_derive_is_allowed_to_implement_this_trait()
|
| 856 | where
|
| 857 | Self: Sized,
|
| 858 | {
|
| 859 | }
|
| 860 |
|
| 861 | #[inline(always)]
|
| 862 | fn is_bit_valid<A>(candidate: Maybe<'_, Self, A>) -> bool
|
| 863 | where
|
| 864 | A: invariant::Alignment,
|
| 865 | {
|
| 866 | T::is_bit_valid(candidate.transmute::<_, _, BecauseImmutable>())
|
| 867 | }
|
| 868 | }
|
| 869 |
|
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 892 | const _: () = unsafe {
|
| 893 | unsafe_impl!(const N: usize, T: Immutable => Immutable for [T; N]);
|
| 894 | unsafe_impl!(const N: usize, T: TryFromBytes => TryFromBytes for [T; N]; |c| {
|
| 895 | let c: Ptr<'_, [ReadOnly<T>; N], _> = c.cast::<_, crate::pointer::cast::CastSized, _>();
|
| 896 | let c: Ptr<'_, [ReadOnly<T>], _> = c.as_slice();
|
| 897 | let c: Ptr<'_, ReadOnly<[T]>, _> = c.cast::<_, crate::pointer::cast::CastUnsized, _>();
|
| 898 |
|
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | <[T] as TryFromBytes>::is_bit_valid(c)
|
| 904 | });
|
| 905 | unsafe_impl!(const N: usize, T: FromZeros => FromZeros for [T; N]);
|
| 906 | unsafe_impl!(const N: usize, T: FromBytes => FromBytes for [T; N]);
|
| 907 | unsafe_impl!(const N: usize, T: IntoBytes => IntoBytes for [T; N]);
|
| 908 | unsafe_impl!(const N: usize, T: Unaligned => Unaligned for [T; N]);
|
| 909 | assert_unaligned!([(); 0], [(); 1], [u8; 0], [u8; 1]);
|
| 910 | unsafe_impl!(T: Immutable => Immutable for [T]);
|
| 911 | unsafe_impl!(T: TryFromBytes => TryFromBytes for [T]; |c| {
|
| 912 | let c: Ptr<'_, [ReadOnly<T>], _> = c.cast::<_, crate::pointer::cast::CastUnsized, _>();
|
| 913 |
|
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | |
| 929 | |
| 930 | |
| 931 | |
| 932 | |
| 933 | |
| 934 | c.iter().all(<T as TryFromBytes>::is_bit_valid)
|
| 935 | });
|
| 936 | unsafe_impl!(T: FromZeros => FromZeros for [T]);
|
| 937 | unsafe_impl!(T: FromBytes => FromBytes for [T]);
|
| 938 | unsafe_impl!(T: IntoBytes => IntoBytes for [T]);
|
| 939 | unsafe_impl!(T: Unaligned => Unaligned for [T]);
|
| 940 | };
|
| 941 |
|
| 942 | |
| 943 | |
| 944 | |
| 945 | |
| 946 | |
| 947 | |
| 948 | |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | |
| 960 | |
| 961 | |
| 962 | |
| 963 | |
| 964 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 965 | const _: () = unsafe {
|
| 966 | unsafe_impl!(T: ?Sized => Immutable for *const T);
|
| 967 | unsafe_impl!(T: ?Sized => Immutable for *mut T);
|
| 968 | unsafe_impl!(T => TryFromBytes for *const T; |c| pointer::is_zeroed(c));
|
| 969 | unsafe_impl!(T => FromZeros for *const T);
|
| 970 | unsafe_impl!(T => TryFromBytes for *mut T; |c| pointer::is_zeroed(c));
|
| 971 | unsafe_impl!(T => FromZeros for *mut T);
|
| 972 | };
|
| 973 |
|
| 974 | |
| 975 | |
| 976 | const _: () = unsafe { unsafe_impl!(T: ?Sized => Immutable for NonNull<T>) };
|
| 977 |
|
| 978 | |
| 979 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 980 | const _: () = unsafe {
|
| 981 | unsafe_impl!(T: ?Sized => Immutable for &'_ T);
|
| 982 | unsafe_impl!(T: ?Sized => Immutable for &'_ mut T);
|
| 983 | };
|
| 984 |
|
| 985 | |
| 986 | |
| 987 | |
| 988 | |
| 989 | |
| 990 | const _: () = unsafe { unsafe_impl!(T: Immutable => Immutable for Option<T>) };
|
| 991 |
|
| 992 | mod tuples {
|
| 993 | use super::*;
|
| 994 |
|
| 995 | |
| 996 | |
| 997 | |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | macro_rules! impl_tuple {
|
| 1002 | |
| 1003 | ($($T:ident $I:tt),+ $(,)?) => {
|
| 1004 | crate::util::macros::__unsafe();
|
| 1005 | impl_tuple!(@all [] [$($T $I)+]);
|
| 1006 | };
|
| 1007 |
|
| 1008 | // Build up the set of tuple types (i.e., `(A,)`, `(A, B)`, `(A, B, C)`,
|
| 1009 | |
| 1010 | |
| 1011 | (@all [$($head_T:ident $head_I:tt)*] [$next_T:ident $next_I:tt $($tail:tt)*]) => {
|
| 1012 | |
| 1013 | unsafe_impl!($($head_T: Immutable,)* $next_T: Immutable => Immutable for ($($head_T,)* $next_T,));
|
| 1014 |
|
| 1015 | |
| 1016 | unsafe_impl!($($head_T: TryFromBytes,)* $next_T: TryFromBytes => TryFromBytes for ($($head_T,)* $next_T,); |c| {
|
| 1017 | let mut c = c;
|
| 1018 | $(TryFromBytes::is_bit_valid(into_inner!(c.reborrow().project::<_, { crate::STRUCT_VARIANT_ID }, { crate::ident_id!($head_I) }>())) &&)*
|
| 1019 | TryFromBytes::is_bit_valid(into_inner!(c.reborrow().project::<_, { crate::STRUCT_VARIANT_ID }, { crate::ident_id!($next_I) }>()))
|
| 1020 | });
|
| 1021 |
|
| 1022 | |
| 1023 | unsafe_impl!($($head_T: FromZeros,)* $next_T: FromZeros => FromZeros for ($($head_T,)* $next_T,));
|
| 1024 |
|
| 1025 | |
| 1026 | unsafe_impl!($($head_T: FromBytes,)* $next_T: FromBytes => FromBytes for ($($head_T,)* $next_T,));
|
| 1027 |
|
| 1028 | |
| 1029 | unsafe impl<$($head_T,)* $next_T> crate::HasTag for ($($head_T,)* $next_T,) {
|
| 1030 | #[inline]
|
| 1031 | fn only_derive_is_allowed_to_implement_this_trait()
|
| 1032 | where
|
| 1033 | Self: Sized
|
| 1034 | {}
|
| 1035 |
|
| 1036 | type Tag = ();
|
| 1037 |
|
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | |
| 1044 | |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | type ProjectToTag = crate::pointer::cast::CastToUnit;
|
| 1049 | }
|
| 1050 |
|
| 1051 | |
| 1052 | impl_tuple!(@variants
|
| 1053 | [$($head_T $head_I)* $next_T $next_I]
|
| 1054 | []
|
| 1055 | [$($head_T $head_I)* $next_T $next_I]
|
| 1056 | );
|
| 1057 |
|
| 1058 | |
| 1059 | impl_tuple!(@all [$($head_T $head_I)* $next_T $next_I] [$($tail)*]);
|
| 1060 | };
|
| 1061 | (@all [$($head_T:ident $head_I:tt)*] []) => {};
|
| 1062 |
|
| 1063 | |
| 1064 | (@variants
|
| 1065 | |
| 1066 | [$($AllT:ident $AllI:tt)+]
|
| 1067 | |
| 1068 | [$($BeforeT:ident)*]
|
| 1069 | |
| 1070 | [$CurrT:ident $CurrI:tt $($AfterT:ident $AfterI:tt)*]
|
| 1071 | ) => {
|
| 1072 | |
| 1073 | |
| 1074 | |
| 1075 | |
| 1076 | |
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | unsafe impl<$($AllT),+> crate::HasField<
|
| 1081 | (),
|
| 1082 | { crate::STRUCT_VARIANT_ID },
|
| 1083 | { crate::ident_id!($CurrI)}
|
| 1084 | > for ($($AllT,)+) {
|
| 1085 | #[inline]
|
| 1086 | fn only_derive_is_allowed_to_implement_this_trait()
|
| 1087 | where
|
| 1088 | Self: Sized
|
| 1089 | {}
|
| 1090 |
|
| 1091 | type Type = $CurrT;
|
| 1092 |
|
| 1093 | #[inline(always)]
|
| 1094 | fn project(slf: crate::PtrInner<'_, Self>) -> *mut Self::Type {
|
| 1095 | let slf = slf.as_non_null().as_ptr();
|
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | unsafe { core::ptr::addr_of_mut!((*slf).$CurrI) }
|
| 1102 | }
|
| 1103 | }
|
| 1104 |
|
| 1105 | |
| 1106 | unsafe impl<Aliasing, Alignment, $($AllT),+> crate::ProjectField<
|
| 1107 | (),
|
| 1108 | (Aliasing, Alignment, crate::invariant::Uninit),
|
| 1109 | { crate::STRUCT_VARIANT_ID },
|
| 1110 | { crate::ident_id!($CurrI)}
|
| 1111 | > for ($($AllT,)+)
|
| 1112 | where
|
| 1113 | Aliasing: crate::invariant::Aliasing,
|
| 1114 | Alignment: crate::invariant::Alignment,
|
| 1115 | {
|
| 1116 | #[inline]
|
| 1117 | fn only_derive_is_allowed_to_implement_this_trait()
|
| 1118 | where
|
| 1119 | Self: Sized
|
| 1120 | {}
|
| 1121 |
|
| 1122 | |
| 1123 | |
| 1124 | |
| 1125 | type Invariants = (Aliasing, Alignment, crate::invariant::Uninit);
|
| 1126 |
|
| 1127 | |
| 1128 | type Error = core::convert::Infallible;
|
| 1129 | }
|
| 1130 |
|
| 1131 | |
| 1132 | unsafe impl<Aliasing, Alignment, $($AllT),+> crate::ProjectField<
|
| 1133 | (),
|
| 1134 | (Aliasing, Alignment, crate::invariant::Initialized),
|
| 1135 | { crate::STRUCT_VARIANT_ID },
|
| 1136 | { crate::ident_id!($CurrI)}
|
| 1137 | > for ($($AllT,)+)
|
| 1138 | where
|
| 1139 | Aliasing: crate::invariant::Aliasing,
|
| 1140 | Alignment: crate::invariant::Alignment,
|
| 1141 | {
|
| 1142 | #[inline]
|
| 1143 | fn only_derive_is_allowed_to_implement_this_trait()
|
| 1144 | where
|
| 1145 | Self: Sized
|
| 1146 | {}
|
| 1147 |
|
| 1148 | |
| 1149 | |
| 1150 | |
| 1151 | type Invariants = (Aliasing, Alignment, crate::invariant::Initialized);
|
| 1152 |
|
| 1153 | |
| 1154 | type Error = core::convert::Infallible;
|
| 1155 | }
|
| 1156 |
|
| 1157 | |
| 1158 | unsafe impl<Aliasing, Alignment, $($AllT),+> crate::ProjectField<
|
| 1159 | (),
|
| 1160 | (Aliasing, Alignment, crate::invariant::Valid),
|
| 1161 | { crate::STRUCT_VARIANT_ID },
|
| 1162 | { crate::ident_id!($CurrI)}
|
| 1163 | > for ($($AllT,)+)
|
| 1164 | where
|
| 1165 | Aliasing: crate::invariant::Aliasing,
|
| 1166 | Alignment: crate::invariant::Alignment,
|
| 1167 | {
|
| 1168 | #[inline]
|
| 1169 | fn only_derive_is_allowed_to_implement_this_trait()
|
| 1170 | where
|
| 1171 | Self: Sized
|
| 1172 | {}
|
| 1173 |
|
| 1174 | |
| 1175 | |
| 1176 | |
| 1177 | type Invariants = (Aliasing, Alignment, crate::invariant::Valid);
|
| 1178 |
|
| 1179 | |
| 1180 | type Error = core::convert::Infallible;
|
| 1181 | }
|
| 1182 |
|
| 1183 | |
| 1184 | impl_tuple!(@variants [$($AllT $AllI)+] [$($BeforeT)* $CurrT] [$($AfterT $AfterI)*]);
|
| 1185 | };
|
| 1186 | (@variants [$($AllT:ident $AllI:tt)+] [$($BeforeT:ident)*] []) => {};
|
| 1187 | }
|
| 1188 |
|
| 1189 | |
| 1190 | |
| 1191 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 1192 | const _: () = unsafe {
|
| 1193 | impl_tuple! {
|
| 1194 | A 0,
|
| 1195 | B 1,
|
| 1196 | C 2,
|
| 1197 | D 3,
|
| 1198 | E 4,
|
| 1199 | F 5,
|
| 1200 | G 6,
|
| 1201 | H 7,
|
| 1202 | I 8,
|
| 1203 | J 9,
|
| 1204 | K 10,
|
| 1205 | L 11,
|
| 1206 | M 12,
|
| 1207 | N 13,
|
| 1208 | O 14,
|
| 1209 | P 15,
|
| 1210 | Q 16,
|
| 1211 | R 17,
|
| 1212 | S 18,
|
| 1213 | T 19,
|
| 1214 | U 20,
|
| 1215 | V 21,
|
| 1216 | W 22,
|
| 1217 | X 23,
|
| 1218 | Y 24,
|
| 1219 | Z 25,
|
| 1220 | };
|
| 1221 | };
|
| 1222 | }
|
| 1223 |
|
| 1224 | |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 | |
| 1229 | |
| 1230 | |
| 1231 | |
| 1232 | |
| 1233 | |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | |
| 1238 | |
| 1239 | |
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | |
| 1244 | |
| 1245 | |
| 1246 | |
| 1247 | |
| 1248 | |
| 1249 | |
| 1250 | |
| 1251 | |
| 1252 | |
| 1253 | |
| 1254 | |
| 1255 | |
| 1256 | |
| 1257 | |
| 1258 | |
| 1259 | |
| 1260 | |
| 1261 | |
| 1262 | |
| 1263 | |
| 1264 | |
| 1265 | |
| 1266 | |
| 1267 | |
| 1268 | |
| 1269 | |
| 1270 | |
| 1271 | |
| 1272 | |
| 1273 | |
| 1274 | |
| 1275 | |
| 1276 | |
| 1277 | |
| 1278 | |
| 1279 | |
| 1280 | |
| 1281 | |
| 1282 | |
| 1283 | |
| 1284 | |
| 1285 | |
| 1286 | |
| 1287 | |
| 1288 | |
| 1289 | #[cfg(feature = "simd")]
|
| 1290 | #[cfg_attr(doc_cfg, doc(cfg(feature = "simd")))]
|
| 1291 | mod simd {
|
| 1292 | |
| 1293 | |
| 1294 | |
| 1295 | |
| 1296 | |
| 1297 | |
| 1298 | |
| 1299 | #[allow(unused_macros)] |
| 1300 | |
| 1301 | |
| 1302 | macro_rules! simd_arch_mod {
|
| 1303 | ($(#[cfg $cfg:tt])* $(#[cfg_attr $cfg_attr:tt])? $arch:ident, $mod:ident, $($typ:ident),*) => {
|
| 1304 | $(#[cfg $cfg])*
|
| 1305 | #[cfg_attr(doc_cfg, doc(cfg $($cfg)*))]
|
| 1306 | $(#[cfg_attr $cfg_attr])?
|
| 1307 | mod $mod {
|
| 1308 | use core::arch::$arch::{$($typ),*};
|
| 1309 |
|
| 1310 | use crate::*;
|
| 1311 | impl_known_layout!($($typ),*);
|
| 1312 | |
| 1313 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 1314 | const _: () = unsafe {
|
| 1315 | $( unsafe_impl!($typ: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes); )*
|
| 1316 | };
|
| 1317 | }
|
| 1318 | };
|
| 1319 | }
|
| 1320 |
|
| 1321 | #[rustfmt::skip]
|
| 1322 | const _: () = {
|
| 1323 | simd_arch_mod!(
|
| 1324 | #[cfg(target_arch = "x86")]
|
| 1325 | x86, x86, __m128, __m128d, __m128i, __m256, __m256d, __m256i
|
| 1326 | );
|
| 1327 | #[cfg(not(no_zerocopy_simd_x86_avx12_1_89_0))]
|
| 1328 | simd_arch_mod!(
|
| 1329 | #[cfg(target_arch = "x86")]
|
| 1330 | #[cfg_attr(doc_cfg, doc(cfg(rust = "1.89.0")))]
|
| 1331 | x86, x86_nightly, __m512bh, __m512, __m512d, __m512i
|
| 1332 | );
|
| 1333 | simd_arch_mod!(
|
| 1334 | #[cfg(target_arch = "x86_64")]
|
| 1335 | x86_64, x86_64, __m128, __m128d, __m128i, __m256, __m256d, __m256i
|
| 1336 | );
|
| 1337 | #[cfg(not(no_zerocopy_simd_x86_avx12_1_89_0))]
|
| 1338 | simd_arch_mod!(
|
| 1339 | #[cfg(target_arch = "x86_64")]
|
| 1340 | #[cfg_attr(doc_cfg, doc(cfg(rust = "1.89.0")))]
|
| 1341 | x86_64, x86_64_nightly, __m512bh, __m512, __m512d, __m512i
|
| 1342 | );
|
| 1343 | simd_arch_mod!(
|
| 1344 | #[cfg(target_arch = "wasm32")]
|
| 1345 | wasm32, wasm32, v128
|
| 1346 | );
|
| 1347 | simd_arch_mod!(
|
| 1348 | #[cfg(all(feature = "simd-nightly", target_arch = "powerpc"))]
|
| 1349 | powerpc, powerpc, vector_bool_long, vector_double, vector_signed_long, vector_unsigned_long
|
| 1350 | );
|
| 1351 | simd_arch_mod!(
|
| 1352 | #[cfg(all(feature = "simd-nightly", target_arch = "powerpc64"))]
|
| 1353 | powerpc64, powerpc64, vector_bool_long, vector_double, vector_signed_long, vector_unsigned_long
|
| 1354 | );
|
| 1355 | |
| 1356 | |
| 1357 | |
| 1358 | |
| 1359 | #[cfg(not(no_zerocopy_aarch64_simd_1_59_0))]
|
| 1360 | simd_arch_mod!(
|
| 1361 | #[cfg(all(
|
| 1362 | target_arch = "aarch64",
|
| 1363 | any(
|
| 1364 | target_endian = "little",
|
| 1365 | not(no_zerocopy_aarch64_simd_be_1_87_0)
|
| 1366 | )
|
| 1367 | ))]
|
| 1368 | #[cfg_attr(
|
| 1369 | doc_cfg,
|
| 1370 | doc(cfg(all(target_arch = "aarch64", any(
|
| 1371 | all(rust = "1.59.0", target_endian = "little"),
|
| 1372 | rust = "1.87.0",
|
| 1373 | ))))
|
| 1374 | )]
|
| 1375 | aarch64, aarch64, float32x2_t, float32x4_t, float64x1_t, float64x2_t, int8x8_t, int8x8x2_t,
|
| 1376 | int8x8x3_t, int8x8x4_t, int8x16_t, int8x16x2_t, int8x16x3_t, int8x16x4_t, int16x4_t,
|
| 1377 | int16x8_t, int32x2_t, int32x4_t, int64x1_t, int64x2_t, poly8x8_t, poly8x8x2_t, poly8x8x3_t,
|
| 1378 | poly8x8x4_t, poly8x16_t, poly8x16x2_t, poly8x16x3_t, poly8x16x4_t, poly16x4_t, poly16x8_t,
|
| 1379 | poly64x1_t, poly64x2_t, uint8x8_t, uint8x8x2_t, uint8x8x3_t, uint8x8x4_t, uint8x16_t,
|
| 1380 | uint8x16x2_t, uint8x16x3_t, uint8x16x4_t, uint16x4_t, uint16x4x2_t, uint16x4x3_t,
|
| 1381 | uint16x4x4_t, uint16x8_t, uint32x2_t, uint32x4_t, uint64x1_t, uint64x2_t
|
| 1382 | );
|
| 1383 | };
|
| 1384 | }
|
| 1385 |
|
| 1386 | #[cfg(test)]
|
| 1387 | mod tests {
|
| 1388 | use super::*;
|
| 1389 |
|
| 1390 | #[test]
|
| 1391 | fn test_impls() {
|
| 1392 | |
| 1393 | |
| 1394 | |
| 1395 | |
| 1396 | |
| 1397 | |
| 1398 | |
| 1399 | |
| 1400 | trait TryFromBytesTestable {
|
| 1401 | fn with_passing_test_cases<F: Fn(Box<ReadOnly<Self>>)>(f: F);
|
| 1402 | fn with_failing_test_cases<F: Fn(&mut [u8])>(f: F);
|
| 1403 | }
|
| 1404 |
|
| 1405 | impl<T: FromBytes> TryFromBytesTestable for T {
|
| 1406 | fn with_passing_test_cases<F: Fn(Box<ReadOnly<Self>>)>(f: F) {
|
| 1407 | |
| 1408 | f(ReadOnly::<Self>::new_box_zeroed().unwrap());
|
| 1409 |
|
| 1410 | let ffs = {
|
| 1411 | let mut t = ReadOnly::new(Self::new_zeroed());
|
| 1412 | let ptr: *mut T = ReadOnly::as_mut(&mut t);
|
| 1413 | |
| 1414 | unsafe { ptr::write_bytes(ptr.cast::<u8>(), 0xFF, mem::size_of::<T>()) };
|
| 1415 | t
|
| 1416 | };
|
| 1417 |
|
| 1418 | |
| 1419 | f(Box::new(ffs));
|
| 1420 | }
|
| 1421 |
|
| 1422 | fn with_failing_test_cases<F: Fn(&mut [u8])>(_f: F) {}
|
| 1423 | }
|
| 1424 |
|
| 1425 | macro_rules! impl_try_from_bytes_testable_for_null_pointer_optimization {
|
| 1426 | ($($tys:ty),*) => {
|
| 1427 | $(
|
| 1428 | impl TryFromBytesTestable for Option<$tys> {
|
| 1429 | fn with_passing_test_cases<F: Fn(Box<ReadOnly<Self>>)>(f: F) {
|
| 1430 | |
| 1431 | f(Box::new(ReadOnly::new(None)));
|
| 1432 | }
|
| 1433 |
|
| 1434 | fn with_failing_test_cases<F: Fn(&mut [u8])>(f: F) {
|
| 1435 | for pos in 0..mem::size_of::<Self>() {
|
| 1436 | let mut bytes = [0u8; mem::size_of::<Self>()];
|
| 1437 | bytes[pos] = 0x01;
|
| 1438 | f(&mut bytes[..]);
|
| 1439 | }
|
| 1440 | }
|
| 1441 | }
|
| 1442 | )*
|
| 1443 | };
|
| 1444 | }
|
| 1445 |
|
| 1446 | |
| 1447 | macro_rules! impl_try_from_bytes_testable {
|
| 1448 | |
| 1449 | (=> @success $($success_case:expr),* $(, @failure $($failure_case:expr),*)?) => {};
|
| 1450 | |
| 1451 | ($ty:ty $(,$tys:ty)* => @success $($success_case:expr),* $(, @failure $($failure_case:expr),*)?) => {
|
| 1452 | impl TryFromBytesTestable for $ty {
|
| 1453 | impl_try_from_bytes_testable!(
|
| 1454 | @methods @success $($success_case),*
|
| 1455 | $(, @failure $($failure_case),*)?
|
| 1456 | );
|
| 1457 | }
|
| 1458 | impl_try_from_bytes_testable!($($tys),* => @success $($success_case),* $(, @failure $($failure_case),*)?);
|
| 1459 | };
|
| 1460 | |
| 1461 | ($($($ty:ty),* => @success $($success_case:expr), * $(, @failure $($failure_case:expr),*)?;)*) => {
|
| 1462 | $(
|
| 1463 | impl_try_from_bytes_testable!($($ty),* => @success $($success_case),* $(, @failure $($failure_case),*)*);
|
| 1464 | )*
|
| 1465 | };
|
| 1466 | |
| 1467 | |
| 1468 | (@methods @success $($success_case:expr),* $(, @failure $($failure_case:expr),*)?) => {
|
| 1469 | fn with_passing_test_cases<F: Fn(Box<ReadOnly<Self>>)>(_f: F) {
|
| 1470 | $(
|
| 1471 | let bx = Box::<Self>::from($success_case);
|
| 1472 | let ro: Box<ReadOnly<_>> = {
|
| 1473 | let raw = Box::into_raw(bx);
|
| 1474 | |
| 1475 | |
| 1476 | #[allow(clippy::as_conversions)]
|
| 1477 | unsafe { Box::from_raw(raw as *mut _) }
|
| 1478 | };
|
| 1479 | _f(ro);
|
| 1480 | )*
|
| 1481 | }
|
| 1482 |
|
| 1483 | fn with_failing_test_cases<F: Fn(&mut [u8])>(_f: F) {
|
| 1484 | $($(
|
| 1485 | let mut case = $failure_case;
|
| 1486 | _f(case.as_mut_bytes());
|
| 1487 | )*)?
|
| 1488 | }
|
| 1489 | };
|
| 1490 | }
|
| 1491 |
|
| 1492 | impl_try_from_bytes_testable_for_null_pointer_optimization!(
|
| 1493 | Box<UnsafeCell<NotZerocopy>>,
|
| 1494 | &'static UnsafeCell<NotZerocopy>,
|
| 1495 | &'static mut UnsafeCell<NotZerocopy>,
|
| 1496 | NonNull<UnsafeCell<NotZerocopy>>,
|
| 1497 | fn(),
|
| 1498 | FnManyArgs,
|
| 1499 | extern "C" fn(),
|
| 1500 | ECFnManyArgs
|
| 1501 | );
|
| 1502 |
|
| 1503 | macro_rules! bx {
|
| 1504 | ($e:expr) => {
|
| 1505 | Box::new($e)
|
| 1506 | };
|
| 1507 | }
|
| 1508 |
|
| 1509 | |
| 1510 | |
| 1511 | impl_try_from_bytes_testable!(
|
| 1512 | bool => @success true, false,
|
| 1513 | @failure 2u8, 3u8, 0xFFu8;
|
| 1514 | char => @success '\u{0}', '\u{D7FF}', '\u{E000}', '\u{10FFFF}',
|
| 1515 | @failure 0xD800u32, 0xDFFFu32, 0x110000u32;
|
| 1516 | str => @success "", "hello", "❤️🧡💛💚💙💜",
|
| 1517 | @failure [0, 159, 146, 150];
|
| 1518 | [u8] => @success vec![].into_boxed_slice(), vec![0, 1, 2].into_boxed_slice();
|
| 1519 | NonZeroU8, NonZeroI8, NonZeroU16, NonZeroI16, NonZeroU32,
|
| 1520 | NonZeroI32, NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128,
|
| 1521 | NonZeroUsize, NonZeroIsize
|
| 1522 | => @success Self::new(1).unwrap(),
|
| 1523 | |
| 1524 | |
| 1525 | |
| 1526 | |
| 1527 | @failure Option::<Self>::None;
|
| 1528 | [bool; 0] => @success [];
|
| 1529 | [bool; 1]
|
| 1530 | => @success [true], [false],
|
| 1531 | @failure [2u8], [3u8], [0xFFu8];
|
| 1532 | [bool]
|
| 1533 | => @success vec![true, false].into_boxed_slice(), vec![false, true].into_boxed_slice(),
|
| 1534 | @failure [2u8], [3u8], [0xFFu8], [0u8, 1u8, 2u8];
|
| 1535 | Unalign<bool>
|
| 1536 | => @success Unalign::new(false), Unalign::new(true),
|
| 1537 | @failure 2u8, 0xFFu8;
|
| 1538 | ManuallyDrop<bool>
|
| 1539 | => @success ManuallyDrop::new(false), ManuallyDrop::new(true),
|
| 1540 | @failure 2u8, 0xFFu8;
|
| 1541 | ManuallyDrop<[u8]>
|
| 1542 | => @success bx!(ManuallyDrop::new([])), bx!(ManuallyDrop::new([0u8])), bx!(ManuallyDrop::new([0u8, 1u8]));
|
| 1543 | ManuallyDrop<[bool]>
|
| 1544 | => @success bx!(ManuallyDrop::new([])), bx!(ManuallyDrop::new([false])), bx!(ManuallyDrop::new([false, true])),
|
| 1545 | @failure [2u8], [3u8], [0xFFu8], [0u8, 1u8, 2u8];
|
| 1546 | ManuallyDrop<[UnsafeCell<u8>]>
|
| 1547 | => @success bx!(ManuallyDrop::new([UnsafeCell::new(0)])), bx!(ManuallyDrop::new([UnsafeCell::new(0), UnsafeCell::new(1)]));
|
| 1548 | ManuallyDrop<[UnsafeCell<bool>]>
|
| 1549 | => @success bx!(ManuallyDrop::new([UnsafeCell::new(false)])), bx!(ManuallyDrop::new([UnsafeCell::new(false), UnsafeCell::new(true)])),
|
| 1550 | @failure [2u8], [3u8], [0xFFu8], [0u8, 1u8, 2u8];
|
| 1551 | Wrapping<bool>
|
| 1552 | => @success Wrapping(false), Wrapping(true),
|
| 1553 | @failure 2u8, 0xFFu8;
|
| 1554 | *const NotZerocopy
|
| 1555 | => @success ptr::null::<NotZerocopy>(),
|
| 1556 | @failure [0x01; mem::size_of::<*const NotZerocopy>()];
|
| 1557 | *mut NotZerocopy
|
| 1558 | => @success ptr::null_mut::<NotZerocopy>(),
|
| 1559 | @failure [0x01; mem::size_of::<*mut NotZerocopy>()];
|
| 1560 | );
|
| 1561 |
|
| 1562 | |
| 1563 | |
| 1564 | |
| 1565 | |
| 1566 | |
| 1567 | |
| 1568 | |
| 1569 | |
| 1570 | |
| 1571 | |
| 1572 | mod autoref_trick {
|
| 1573 | use super::*;
|
| 1574 |
|
| 1575 | pub(super) struct AutorefWrapper<T: ?Sized>(pub(super) PhantomData<T>);
|
| 1576 |
|
| 1577 | pub(super) trait TestIsBitValidShared<T: ?Sized> {
|
| 1578 | #[allow(clippy::needless_lifetimes)]
|
| 1579 | fn test_is_bit_valid_shared<'ptr>(&self, candidate: Maybe<'ptr, T>)
|
| 1580 | -> Option<bool>;
|
| 1581 | }
|
| 1582 |
|
| 1583 | impl<T: TryFromBytes + Immutable + ?Sized> TestIsBitValidShared<T> for AutorefWrapper<T> {
|
| 1584 | #[allow(clippy::needless_lifetimes)]
|
| 1585 | fn test_is_bit_valid_shared<'ptr>(
|
| 1586 | &self,
|
| 1587 | candidate: Maybe<'ptr, T>,
|
| 1588 | ) -> Option<bool> {
|
| 1589 | Some(T::is_bit_valid(candidate))
|
| 1590 | }
|
| 1591 | }
|
| 1592 |
|
| 1593 | pub(super) trait TestTryFromRef<T: ?Sized> {
|
| 1594 | #[allow(clippy::needless_lifetimes)]
|
| 1595 | fn test_try_from_ref<'bytes>(
|
| 1596 | &self,
|
| 1597 | bytes: &'bytes [u8],
|
| 1598 | ) -> Option<Option<&'bytes T>>;
|
| 1599 | }
|
| 1600 |
|
| 1601 | impl<T: TryFromBytes + Immutable + KnownLayout + ?Sized> TestTryFromRef<T> for AutorefWrapper<T> {
|
| 1602 | #[allow(clippy::needless_lifetimes)]
|
| 1603 | fn test_try_from_ref<'bytes>(
|
| 1604 | &self,
|
| 1605 | bytes: &'bytes [u8],
|
| 1606 | ) -> Option<Option<&'bytes T>> {
|
| 1607 | Some(T::try_ref_from_bytes(bytes).ok())
|
| 1608 | }
|
| 1609 | }
|
| 1610 |
|
| 1611 | pub(super) trait TestTryFromMut<T: ?Sized> {
|
| 1612 | #[allow(clippy::needless_lifetimes)]
|
| 1613 | fn test_try_from_mut<'bytes>(
|
| 1614 | &self,
|
| 1615 | bytes: &'bytes mut [u8],
|
| 1616 | ) -> Option<Option<&'bytes mut T>>;
|
| 1617 | }
|
| 1618 |
|
| 1619 | impl<T: TryFromBytes + IntoBytes + KnownLayout + ?Sized> TestTryFromMut<T> for AutorefWrapper<T> {
|
| 1620 | #[allow(clippy::needless_lifetimes)]
|
| 1621 | fn test_try_from_mut<'bytes>(
|
| 1622 | &self,
|
| 1623 | bytes: &'bytes mut [u8],
|
| 1624 | ) -> Option<Option<&'bytes mut T>> {
|
| 1625 | Some(T::try_mut_from_bytes(bytes).ok())
|
| 1626 | }
|
| 1627 | }
|
| 1628 |
|
| 1629 | pub(super) trait TestTryReadFrom<T> {
|
| 1630 | fn test_try_read_from(&self, bytes: &[u8]) -> Option<Option<T>>;
|
| 1631 | }
|
| 1632 |
|
| 1633 | impl<T: TryFromBytes> TestTryReadFrom<T> for AutorefWrapper<T> {
|
| 1634 | fn test_try_read_from(&self, bytes: &[u8]) -> Option<Option<T>> {
|
| 1635 | Some(T::try_read_from_bytes(bytes).ok())
|
| 1636 | }
|
| 1637 | }
|
| 1638 |
|
| 1639 | pub(super) trait TestAsBytes<T: ?Sized> {
|
| 1640 | #[allow(clippy::needless_lifetimes)]
|
| 1641 | fn test_as_bytes<'slf, 't>(&'slf self, t: &'t ReadOnly<T>) -> Option<&'t [u8]>;
|
| 1642 | }
|
| 1643 |
|
| 1644 | impl<T: IntoBytes + Immutable + ?Sized> TestAsBytes<T> for AutorefWrapper<T> {
|
| 1645 | #[allow(clippy::needless_lifetimes)]
|
| 1646 | fn test_as_bytes<'slf, 't>(&'slf self, t: &'t ReadOnly<T>) -> Option<&'t [u8]> {
|
| 1647 | Some(t.as_bytes())
|
| 1648 | }
|
| 1649 | }
|
| 1650 | }
|
| 1651 |
|
| 1652 | use autoref_trick::*;
|
| 1653 |
|
| 1654 | |
| 1655 | |
| 1656 | |
| 1657 | |
| 1658 | |
| 1659 | |
| 1660 | |
| 1661 | |
| 1662 | |
| 1663 | |
| 1664 | |
| 1665 | |
| 1666 | |
| 1667 | |
| 1668 | |
| 1669 | macro_rules! assert_on_allowlist {
|
| 1670 | ($fn_name:ident($ty:ty) $(: $($tys:ty),*)?) => {{
|
| 1671 | use core::any::TypeId;
|
| 1672 |
|
| 1673 | let allowlist: &[TypeId] = &[ $($(TypeId::of::<$tys>()),*)? ];
|
| 1674 | let allowlist_names: &[&str] = &[ $($(stringify!($tys)),*)? ];
|
| 1675 |
|
| 1676 | let id = TypeId::of::<$ty>();
|
| 1677 | assert!(allowlist.contains(&id), "{} is not on allowlist for {}: {:?}", stringify!($ty), stringify!($fn_name), allowlist_names);
|
| 1678 | }};
|
| 1679 | }
|
| 1680 |
|
| 1681 | // Asserts that `$ty` implements any `$trait` and doesn't implement any
|
| 1682 | // `!$trait`. Note that all `$trait`s must come before any `!$trait`s.
|
| 1683 | //
|
| 1684 | // For `T: TryFromBytes`, uses `TryFromBytesTestable` to test success
|
| 1685 | // and failure cases.
|
| 1686 | macro_rules! assert_impls {
|
| 1687 | ($ty:ty: TryFromBytes) => {
|
| 1688 | // "Default" implementations that match the "real"
|
| 1689 | // implementations defined in the `autoref_trick` module above.
|
| 1690 | #[allow(unused, non_local_definitions)]
|
| 1691 | impl AutorefWrapper<$ty> {
|
| 1692 | #[allow(clippy::needless_lifetimes)]
|
| 1693 | fn test_is_bit_valid_shared<'ptr>(
|
| 1694 | &mut self,
|
| 1695 | candidate: Maybe<'ptr, $ty>,
|
| 1696 | ) -> Option<bool> {
|
| 1697 | assert_on_allowlist!(
|
| 1698 | test_is_bit_valid_shared($ty):
|
| 1699 | ManuallyDrop<UnsafeCell<()>>,
|
| 1700 | ManuallyDrop<[UnsafeCell<u8>]>,
|
| 1701 | ManuallyDrop<[UnsafeCell<bool>]>,
|
| 1702 | CoreMaybeUninit<NotZerocopy>,
|
| 1703 | CoreMaybeUninit<UnsafeCell<()>>,
|
| 1704 | Wrapping<UnsafeCell<()>>
|
| 1705 | );
|
| 1706 |
|
| 1707 | None
|
| 1708 | }
|
| 1709 |
|
| 1710 | #[allow(clippy::needless_lifetimes)]
|
| 1711 | fn test_try_from_ref<'bytes>(&mut self, _bytes: &'bytes [u8]) -> Option<Option<&'bytes $ty>> {
|
| 1712 | assert_on_allowlist!(
|
| 1713 | test_try_from_ref($ty):
|
| 1714 | ManuallyDrop<[UnsafeCell<bool>]>
|
| 1715 | );
|
| 1716 |
|
| 1717 | None
|
| 1718 | }
|
| 1719 |
|
| 1720 | #[allow(clippy::needless_lifetimes)]
|
| 1721 | fn test_try_from_mut<'bytes>(&mut self, _bytes: &'bytes mut [u8]) -> Option<Option<&'bytes mut $ty>> {
|
| 1722 | assert_on_allowlist!(
|
| 1723 | test_try_from_mut($ty):
|
| 1724 | Option<Box<UnsafeCell<NotZerocopy>>>,
|
| 1725 | Option<&'static UnsafeCell<NotZerocopy>>,
|
| 1726 | Option<&'static mut UnsafeCell<NotZerocopy>>,
|
| 1727 | Option<NonNull<UnsafeCell<NotZerocopy>>>,
|
| 1728 | Option<fn()>,
|
| 1729 | Option<FnManyArgs>,
|
| 1730 | Option<extern "C" fn()>,
|
| 1731 | Option<ECFnManyArgs>,
|
| 1732 | *const NotZerocopy,
|
| 1733 | *mut NotZerocopy
|
| 1734 | );
|
| 1735 |
|
| 1736 | None
|
| 1737 | }
|
| 1738 |
|
| 1739 | fn test_try_read_from(&mut self, _bytes: &[u8]) -> Option<Option<&$ty>> {
|
| 1740 | assert_on_allowlist!(
|
| 1741 | test_try_read_from($ty):
|
| 1742 | str,
|
| 1743 | ManuallyDrop<[u8]>,
|
| 1744 | ManuallyDrop<[bool]>,
|
| 1745 | ManuallyDrop<[UnsafeCell<bool>]>,
|
| 1746 | [u8],
|
| 1747 | [bool]
|
| 1748 | );
|
| 1749 |
|
| 1750 | None
|
| 1751 | }
|
| 1752 |
|
| 1753 | fn test_as_bytes(&mut self, _t: &ReadOnly<$ty>) -> Option<&[u8]> {
|
| 1754 | assert_on_allowlist!(
|
| 1755 | test_as_bytes($ty):
|
| 1756 | Option<&'static UnsafeCell<NotZerocopy>>,
|
| 1757 | Option<&'static mut UnsafeCell<NotZerocopy>>,
|
| 1758 | Option<NonNull<UnsafeCell<NotZerocopy>>>,
|
| 1759 | Option<Box<UnsafeCell<NotZerocopy>>>,
|
| 1760 | Option<fn()>,
|
| 1761 | Option<FnManyArgs>,
|
| 1762 | Option<extern "C" fn()>,
|
| 1763 | Option<ECFnManyArgs>,
|
| 1764 | CoreMaybeUninit<u8>,
|
| 1765 | CoreMaybeUninit<NotZerocopy>,
|
| 1766 | CoreMaybeUninit<UnsafeCell<()>>,
|
| 1767 | ManuallyDrop<UnsafeCell<()>>,
|
| 1768 | ManuallyDrop<[UnsafeCell<u8>]>,
|
| 1769 | ManuallyDrop<[UnsafeCell<bool>]>,
|
| 1770 | Wrapping<UnsafeCell<()>>,
|
| 1771 | *const NotZerocopy,
|
| 1772 | *mut NotZerocopy
|
| 1773 | );
|
| 1774 |
|
| 1775 | None
|
| 1776 | }
|
| 1777 | }
|
| 1778 |
|
| 1779 | <$ty as TryFromBytesTestable>::with_passing_test_cases(|mut val| {
|
| 1780 | // FIXME(#494): These tests only get exercised for types
|
| 1781 | // which are `IntoBytes`. Once we implement #494, we should
|
| 1782 | // be able to support non-`IntoBytes` types by zeroing
|
| 1783 | // padding.
|
| 1784 |
|
| 1785 | // We define `w` and `ww` since, in the case of the inherent
|
| 1786 | // methods, Rust thinks they're both borrowed mutably at the
|
| 1787 | // same time (given how we use them below). If we just
|
| 1788 | // defined a single `w` and used it for multiple operations,
|
| 1789 | // this would conflict.
|
| 1790 | //
|
| 1791 | // We `#[allow(unused_mut]` for the cases where the "real"
|
| 1792 | // impls are used, which take `&self`.
|
| 1793 | #[allow(unused_mut)]
|
| 1794 | let (mut w, mut ww) = (AutorefWrapper::<$ty>(PhantomData), AutorefWrapper::<$ty>(PhantomData));
|
| 1795 |
|
| 1796 | let c = Ptr::from_ref(&*val);
|
| 1797 | let c = c.forget_aligned();
|
| 1798 | // SAFETY: FIXME(#899): This is unsound. `$ty` is not
|
| 1799 | // necessarily `IntoBytes`, but that's the corner we've
|
| 1800 | // backed ourselves into by using `Ptr::from_ref`.
|
| 1801 | let c = unsafe { c.assume_initialized() };
|
| 1802 | let res = w.test_is_bit_valid_shared(c);
|
| 1803 | if let Some(res) = res {
|
| 1804 | assert!(res, "{}::is_bit_valid (shared `Ptr`): got false, expected true", stringify!($ty));
|
| 1805 | }
|
| 1806 |
|
| 1807 | let c = Ptr::from_mut(&mut *val);
|
| 1808 | let c = c.forget_aligned();
|
| 1809 | // SAFETY: FIXME(#899): This is unsound. `$ty` is not
|
| 1810 | // necessarily `IntoBytes`, but that's the corner we've
|
| 1811 | // backed ourselves into by using `Ptr::from_ref`.
|
| 1812 | let mut c = unsafe { c.assume_initialized() };
|
| 1813 | let res = <$ty as TryFromBytes>::is_bit_valid(c.reborrow_shared());
|
| 1814 | assert!(res, "{}::is_bit_valid (exclusive `Ptr`): got false, expected true", stringify!($ty));
|
| 1815 |
|
| 1816 | // `bytes` is `Some(val.as_bytes())` if `$ty: IntoBytes +
|
| 1817 | // Immutable` and `None` otherwise.
|
| 1818 | let bytes = w.test_as_bytes(&*val);
|
| 1819 |
|
| 1820 | // The inner closure returns
|
| 1821 | // `Some($ty::try_ref_from_bytes(bytes))` if `$ty:
|
| 1822 | // Immutable` and `None` otherwise.
|
| 1823 | let res = bytes.and_then(|bytes| ww.test_try_from_ref(bytes));
|
| 1824 | if let Some(res) = res {
|
| 1825 | assert!(res.is_some(), "{}::try_ref_from_bytes: got `None`, expected `Some`", stringify!($ty));
|
| 1826 | }
|
| 1827 |
|
| 1828 | if let Some(bytes) = bytes {
|
| 1829 | // We need to get a mutable byte slice, and so we clone
|
| 1830 | // into a `Vec`. However, we also need these bytes to
|
| 1831 | // satisfy `$ty`'s alignment requirement, which isn't
|
| 1832 | // guaranteed for `Vec<u8>`. In order to get around
|
| 1833 | // this, we create a `Vec` which is twice as long as we
|
| 1834 | // need. There is guaranteed to be an aligned byte range
|
| 1835 | // of size `size_of_val(val)` within that range.
|
| 1836 | let val = &*val;
|
| 1837 | let size = mem::size_of_val(val);
|
| 1838 | let align = mem::align_of_val(val);
|
| 1839 |
|
| 1840 | let mut vec = bytes.to_vec();
|
| 1841 | vec.extend(bytes);
|
| 1842 | let slc = vec.as_slice();
|
| 1843 | let offset = slc.as_ptr().align_offset(align);
|
| 1844 | let bytes_mut = &mut vec.as_mut_slice()[offset..offset+size];
|
| 1845 | bytes_mut.copy_from_slice(bytes);
|
| 1846 |
|
| 1847 | let res = ww.test_try_from_mut(bytes_mut);
|
| 1848 | if let Some(res) = res {
|
| 1849 | assert!(res.is_some(), "{}::try_mut_from_bytes: got `None`, expected `Some`", stringify!($ty));
|
| 1850 | }
|
| 1851 | }
|
| 1852 |
|
| 1853 | let res = bytes.and_then(|bytes| ww.test_try_read_from(bytes));
|
| 1854 | if let Some(res) = res {
|
| 1855 | assert!(res.is_some(), "{}::try_read_from_bytes: got `None`, expected `Some`", stringify!($ty));
|
| 1856 | }
|
| 1857 | });
|
| 1858 | #[allow(clippy::as_conversions)]
|
| 1859 | <$ty as TryFromBytesTestable>::with_failing_test_cases(|c| {
|
| 1860 | #[allow(unused_mut)] // For cases where the "real" impls are used, which take `&self`.
|
| 1861 | let mut w = AutorefWrapper::<$ty>(PhantomData);
|
| 1862 |
|
| 1863 | // This is `Some($ty::try_ref_from_bytes(c))` if `$ty:
|
| 1864 | // Immutable` and `None` otherwise.
|
| 1865 | let res = w.test_try_from_ref(c);
|
| 1866 | if let Some(res) = res {
|
| 1867 | assert!(res.is_none(), "{}::try_ref_from_bytes({:?}): got Some, expected None", stringify!($ty), c);
|
| 1868 | }
|
| 1869 |
|
| 1870 | let res = w.test_try_from_mut(c);
|
| 1871 | if let Some(res) = res {
|
| 1872 | assert!(res.is_none(), "{}::try_mut_from_bytes({:?}): got Some, expected None", stringify!($ty), c);
|
| 1873 | }
|
| 1874 |
|
| 1875 |
|
| 1876 | let res = w.test_try_read_from(c);
|
| 1877 | if let Some(res) = res {
|
| 1878 | assert!(res.is_none(), "{}::try_read_from_bytes({:?}): got Some, expected None", stringify!($ty), c);
|
| 1879 | }
|
| 1880 | });
|
| 1881 |
|
| 1882 | #[allow(dead_code)]
|
| 1883 | const _: () = { static_assertions::assert_impl_all!($ty: TryFromBytes); };
|
| 1884 | };
|
| 1885 | ($ty:ty: $trait:ident) => {
|
| 1886 | #[allow(dead_code)]
|
| 1887 | const _: () = { static_assertions::assert_impl_all!($ty: $trait); };
|
| 1888 | };
|
| 1889 | ($ty:ty: !$trait:ident) => {
|
| 1890 | #[allow(dead_code)]
|
| 1891 | const _: () = { static_assertions::assert_not_impl_any!($ty: $trait); };
|
| 1892 | };
|
| 1893 | ($ty:ty: $($trait:ident),* $(,)? $(!$negative_trait:ident),*) => {
|
| 1894 | $(
|
| 1895 | assert_impls!($ty: $trait);
|
| 1896 | )*
|
| 1897 |
|
| 1898 | $(
|
| 1899 | assert_impls!($ty: !$negative_trait);
|
| 1900 | )*
|
| 1901 | };
|
| 1902 | }
|
| 1903 |
|
| 1904 | // NOTE: The negative impl assertions here are not necessarily
|
| 1905 | // prescriptive. They merely serve as change detectors to make sure
|
| 1906 | // we're aware of what trait impls are getting added with a given
|
| 1907 | // change. Of course, some impls would be invalid (e.g., `bool:
|
| 1908 | // FromBytes`), and so this change detection is very important.
|
| 1909 |
|
| 1910 | assert_impls!(
|
| 1911 | (): KnownLayout,
|
| 1912 | Immutable,
|
| 1913 | TryFromBytes,
|
| 1914 | FromZeros,
|
| 1915 | FromBytes,
|
| 1916 | IntoBytes,
|
| 1917 | Unaligned
|
| 1918 | );
|
| 1919 | assert_impls!(
|
| 1920 | u8: KnownLayout,
|
| 1921 | Immutable,
|
| 1922 | TryFromBytes,
|
| 1923 | FromZeros,
|
| 1924 | FromBytes,
|
| 1925 | IntoBytes,
|
| 1926 | Unaligned
|
| 1927 | );
|
| 1928 | assert_impls!(
|
| 1929 | i8: KnownLayout,
|
| 1930 | Immutable,
|
| 1931 | TryFromBytes,
|
| 1932 | FromZeros,
|
| 1933 | FromBytes,
|
| 1934 | IntoBytes,
|
| 1935 | Unaligned
|
| 1936 | );
|
| 1937 | assert_impls!(
|
| 1938 | u16: KnownLayout,
|
| 1939 | Immutable,
|
| 1940 | TryFromBytes,
|
| 1941 | FromZeros,
|
| 1942 | FromBytes,
|
| 1943 | IntoBytes,
|
| 1944 | !Unaligned
|
| 1945 | );
|
| 1946 | assert_impls!(
|
| 1947 | i16: KnownLayout,
|
| 1948 | Immutable,
|
| 1949 | TryFromBytes,
|
| 1950 | FromZeros,
|
| 1951 | FromBytes,
|
| 1952 | IntoBytes,
|
| 1953 | !Unaligned
|
| 1954 | );
|
| 1955 | assert_impls!(
|
| 1956 | u32: KnownLayout,
|
| 1957 | Immutable,
|
| 1958 | TryFromBytes,
|
| 1959 | FromZeros,
|
| 1960 | FromBytes,
|
| 1961 | IntoBytes,
|
| 1962 | !Unaligned
|
| 1963 | );
|
| 1964 | assert_impls!(
|
| 1965 | i32: KnownLayout,
|
| 1966 | Immutable,
|
| 1967 | TryFromBytes,
|
| 1968 | FromZeros,
|
| 1969 | FromBytes,
|
| 1970 | IntoBytes,
|
| 1971 | !Unaligned
|
| 1972 | );
|
| 1973 | assert_impls!(
|
| 1974 | u64: KnownLayout,
|
| 1975 | Immutable,
|
| 1976 | TryFromBytes,
|
| 1977 | FromZeros,
|
| 1978 | FromBytes,
|
| 1979 | IntoBytes,
|
| 1980 | !Unaligned
|
| 1981 | );
|
| 1982 | assert_impls!(
|
| 1983 | i64: KnownLayout,
|
| 1984 | Immutable,
|
| 1985 | TryFromBytes,
|
| 1986 | FromZeros,
|
| 1987 | FromBytes,
|
| 1988 | IntoBytes,
|
| 1989 | !Unaligned
|
| 1990 | );
|
| 1991 | assert_impls!(
|
| 1992 | u128: KnownLayout,
|
| 1993 | Immutable,
|
| 1994 | TryFromBytes,
|
| 1995 | FromZeros,
|
| 1996 | FromBytes,
|
| 1997 | IntoBytes,
|
| 1998 | !Unaligned
|
| 1999 | );
|
| 2000 | assert_impls!(
|
| 2001 | i128: KnownLayout,
|
| 2002 | Immutable,
|
| 2003 | TryFromBytes,
|
| 2004 | FromZeros,
|
| 2005 | FromBytes,
|
| 2006 | IntoBytes,
|
| 2007 | !Unaligned
|
| 2008 | );
|
| 2009 | assert_impls!(
|
| 2010 | usize: KnownLayout,
|
| 2011 | Immutable,
|
| 2012 | TryFromBytes,
|
| 2013 | FromZeros,
|
| 2014 | FromBytes,
|
| 2015 | IntoBytes,
|
| 2016 | !Unaligned
|
| 2017 | );
|
| 2018 | assert_impls!(
|
| 2019 | isize: KnownLayout,
|
| 2020 | Immutable,
|
| 2021 | TryFromBytes,
|
| 2022 | FromZeros,
|
| 2023 | FromBytes,
|
| 2024 | IntoBytes,
|
| 2025 | !Unaligned
|
| 2026 | );
|
| 2027 | #[cfg(feature = "float-nightly")]
|
| 2028 | assert_impls!(
|
| 2029 | f16: KnownLayout,
|
| 2030 | Immutable,
|
| 2031 | TryFromBytes,
|
| 2032 | FromZeros,
|
| 2033 | FromBytes,
|
| 2034 | IntoBytes,
|
| 2035 | !Unaligned
|
| 2036 | );
|
| 2037 | assert_impls!(
|
| 2038 | f32: KnownLayout,
|
| 2039 | Immutable,
|
| 2040 | TryFromBytes,
|
| 2041 | FromZeros,
|
| 2042 | FromBytes,
|
| 2043 | IntoBytes,
|
| 2044 | !Unaligned
|
| 2045 | );
|
| 2046 | assert_impls!(
|
| 2047 | f64: KnownLayout,
|
| 2048 | Immutable,
|
| 2049 | TryFromBytes,
|
| 2050 | FromZeros,
|
| 2051 | FromBytes,
|
| 2052 | IntoBytes,
|
| 2053 | !Unaligned
|
| 2054 | );
|
| 2055 | #[cfg(feature = "float-nightly")]
|
| 2056 | assert_impls!(
|
| 2057 | f128: KnownLayout,
|
| 2058 | Immutable,
|
| 2059 | TryFromBytes,
|
| 2060 | FromZeros,
|
| 2061 | FromBytes,
|
| 2062 | IntoBytes,
|
| 2063 | !Unaligned
|
| 2064 | );
|
| 2065 | assert_impls!(
|
| 2066 | bool: KnownLayout,
|
| 2067 | Immutable,
|
| 2068 | TryFromBytes,
|
| 2069 | FromZeros,
|
| 2070 | IntoBytes,
|
| 2071 | Unaligned,
|
| 2072 | !FromBytes
|
| 2073 | );
|
| 2074 | assert_impls!(
|
| 2075 | char: KnownLayout,
|
| 2076 | Immutable,
|
| 2077 | TryFromBytes,
|
| 2078 | FromZeros,
|
| 2079 | IntoBytes,
|
| 2080 | !FromBytes,
|
| 2081 | !Unaligned
|
| 2082 | );
|
| 2083 | assert_impls!(
|
| 2084 | str: KnownLayout,
|
| 2085 | Immutable,
|
| 2086 | TryFromBytes,
|
| 2087 | FromZeros,
|
| 2088 | IntoBytes,
|
| 2089 | Unaligned,
|
| 2090 | !FromBytes
|
| 2091 | );
|
| 2092 |
|
| 2093 | assert_impls!(
|
| 2094 | NonZeroU8: KnownLayout,
|
| 2095 | Immutable,
|
| 2096 | TryFromBytes,
|
| 2097 | IntoBytes,
|
| 2098 | Unaligned,
|
| 2099 | !FromZeros,
|
| 2100 | !FromBytes
|
| 2101 | );
|
| 2102 | assert_impls!(
|
| 2103 | NonZeroI8: KnownLayout,
|
| 2104 | Immutable,
|
| 2105 | TryFromBytes,
|
| 2106 | IntoBytes,
|
| 2107 | Unaligned,
|
| 2108 | !FromZeros,
|
| 2109 | !FromBytes
|
| 2110 | );
|
| 2111 | assert_impls!(
|
| 2112 | NonZeroU16: KnownLayout,
|
| 2113 | Immutable,
|
| 2114 | TryFromBytes,
|
| 2115 | IntoBytes,
|
| 2116 | !FromBytes,
|
| 2117 | !Unaligned
|
| 2118 | );
|
| 2119 | assert_impls!(
|
| 2120 | NonZeroI16: KnownLayout,
|
| 2121 | Immutable,
|
| 2122 | TryFromBytes,
|
| 2123 | IntoBytes,
|
| 2124 | !FromBytes,
|
| 2125 | !Unaligned
|
| 2126 | );
|
| 2127 | assert_impls!(
|
| 2128 | NonZeroU32: KnownLayout,
|
| 2129 | Immutable,
|
| 2130 | TryFromBytes,
|
| 2131 | IntoBytes,
|
| 2132 | !FromBytes,
|
| 2133 | !Unaligned
|
| 2134 | );
|
| 2135 | assert_impls!(
|
| 2136 | NonZeroI32: KnownLayout,
|
| 2137 | Immutable,
|
| 2138 | TryFromBytes,
|
| 2139 | IntoBytes,
|
| 2140 | !FromBytes,
|
| 2141 | !Unaligned
|
| 2142 | );
|
| 2143 | assert_impls!(
|
| 2144 | NonZeroU64: KnownLayout,
|
| 2145 | Immutable,
|
| 2146 | TryFromBytes,
|
| 2147 | IntoBytes,
|
| 2148 | !FromBytes,
|
| 2149 | !Unaligned
|
| 2150 | );
|
| 2151 | assert_impls!(
|
| 2152 | NonZeroI64: KnownLayout,
|
| 2153 | Immutable,
|
| 2154 | TryFromBytes,
|
| 2155 | IntoBytes,
|
| 2156 | !FromBytes,
|
| 2157 | !Unaligned
|
| 2158 | );
|
| 2159 | assert_impls!(
|
| 2160 | NonZeroU128: KnownLayout,
|
| 2161 | Immutable,
|
| 2162 | TryFromBytes,
|
| 2163 | IntoBytes,
|
| 2164 | !FromBytes,
|
| 2165 | !Unaligned
|
| 2166 | );
|
| 2167 | assert_impls!(
|
| 2168 | NonZeroI128: KnownLayout,
|
| 2169 | Immutable,
|
| 2170 | TryFromBytes,
|
| 2171 | IntoBytes,
|
| 2172 | !FromBytes,
|
| 2173 | !Unaligned
|
| 2174 | );
|
| 2175 | assert_impls!(
|
| 2176 | NonZeroUsize: KnownLayout,
|
| 2177 | Immutable,
|
| 2178 | TryFromBytes,
|
| 2179 | IntoBytes,
|
| 2180 | !FromBytes,
|
| 2181 | !Unaligned
|
| 2182 | );
|
| 2183 | assert_impls!(
|
| 2184 | NonZeroIsize: KnownLayout,
|
| 2185 | Immutable,
|
| 2186 | TryFromBytes,
|
| 2187 | IntoBytes,
|
| 2188 | !FromBytes,
|
| 2189 | !Unaligned
|
| 2190 | );
|
| 2191 |
|
| 2192 | assert_impls!(Option<NonZeroU8>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
|
| 2193 | assert_impls!(Option<NonZeroI8>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
|
| 2194 | assert_impls!(Option<NonZeroU16>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
|
| 2195 | assert_impls!(Option<NonZeroI16>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
|
| 2196 | assert_impls!(Option<NonZeroU32>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
|
| 2197 | assert_impls!(Option<NonZeroI32>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
|
| 2198 | assert_impls!(Option<NonZeroU64>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
|
| 2199 | assert_impls!(Option<NonZeroI64>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
|
| 2200 | assert_impls!(Option<NonZeroU128>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
|
| 2201 | assert_impls!(Option<NonZeroI128>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
|
| 2202 | assert_impls!(Option<NonZeroUsize>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
|
| 2203 | assert_impls!(Option<NonZeroIsize>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
|
| 2204 |
|
| 2205 | // Implements none of the ZC traits.
|
| 2206 | struct NotZerocopy;
|
| 2207 |
|
| 2208 | #[rustfmt::skip]
|
| 2209 | type FnManyArgs = fn(
|
| 2210 | NotZerocopy, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8,
|
| 2211 | ) -> (NotZerocopy, NotZerocopy);
|
| 2212 |
|
| 2213 | // Allowed, because we're not actually using this type for FFI.
|
| 2214 | #[allow(improper_ctypes_definitions)]
|
| 2215 | #[rustfmt::skip]
|
| 2216 | type ECFnManyArgs = extern "C" fn(
|
| 2217 | NotZerocopy, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8,
|
| 2218 | ) -> (NotZerocopy, NotZerocopy);
|
| 2219 |
|
| 2220 | #[cfg(feature = "alloc")]
|
| 2221 | assert_impls!(Option<Box<UnsafeCell<NotZerocopy>>>: KnownLayout, Immutable, TryFromBytes, FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2222 | assert_impls!(Option<Box<[UnsafeCell<NotZerocopy>]>>: KnownLayout, !Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2223 | assert_impls!(Option<&'static UnsafeCell<NotZerocopy>>: KnownLayout, Immutable, TryFromBytes, FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2224 | assert_impls!(Option<&'static [UnsafeCell<NotZerocopy>]>: KnownLayout, Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2225 | assert_impls!(Option<&'static mut UnsafeCell<NotZerocopy>>: KnownLayout, Immutable, TryFromBytes, FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2226 | assert_impls!(Option<&'static mut [UnsafeCell<NotZerocopy>]>: KnownLayout, Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2227 | assert_impls!(Option<NonNull<UnsafeCell<NotZerocopy>>>: KnownLayout, TryFromBytes, FromZeros, Immutable, !FromBytes, !IntoBytes, !Unaligned);
|
| 2228 | assert_impls!(Option<NonNull<[UnsafeCell<NotZerocopy>]>>: KnownLayout, Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2229 | assert_impls!(Option<fn()>: KnownLayout, Immutable, TryFromBytes, FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2230 | assert_impls!(Option<FnManyArgs>: KnownLayout, Immutable, TryFromBytes, FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2231 | assert_impls!(Option<extern "C" fn()>: KnownLayout, Immutable, TryFromBytes, FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2232 | assert_impls!(Option<ECFnManyArgs>: KnownLayout, Immutable, TryFromBytes, FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2233 |
|
| 2234 | assert_impls!(PhantomData<NotZerocopy>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
|
| 2235 | assert_impls!(PhantomData<UnsafeCell<()>>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
|
| 2236 | assert_impls!(PhantomData<[u8]>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
|
| 2237 |
|
| 2238 | assert_impls!(ManuallyDrop<u8>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
|
| 2239 | // This test is important because it allows us to test our hand-rolled
|
| 2240 | // implementation of `<ManuallyDrop<T> as TryFromBytes>::is_bit_valid`.
|
| 2241 | assert_impls!(ManuallyDrop<bool>: KnownLayout, Immutable, TryFromBytes, FromZeros, IntoBytes, Unaligned, !FromBytes);
|
| 2242 | assert_impls!(ManuallyDrop<[u8]>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
|
| 2243 | // This test is important because it allows us to test our hand-rolled
|
| 2244 | // implementation of `<ManuallyDrop<T> as TryFromBytes>::is_bit_valid`.
|
| 2245 | assert_impls!(ManuallyDrop<[bool]>: KnownLayout, Immutable, TryFromBytes, FromZeros, IntoBytes, Unaligned, !FromBytes);
|
| 2246 | assert_impls!(ManuallyDrop<NotZerocopy>: !Immutable, !TryFromBytes, !KnownLayout, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2247 | assert_impls!(ManuallyDrop<[NotZerocopy]>: KnownLayout, !Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2248 | assert_impls!(ManuallyDrop<UnsafeCell<()>>: KnownLayout, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned, !Immutable);
|
| 2249 | assert_impls!(ManuallyDrop<[UnsafeCell<u8>]>: KnownLayout, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned, !Immutable);
|
| 2250 | assert_impls!(ManuallyDrop<[UnsafeCell<bool>]>: KnownLayout, TryFromBytes, FromZeros, IntoBytes, Unaligned, !Immutable, !FromBytes);
|
| 2251 |
|
| 2252 | assert_impls!(CoreMaybeUninit<u8>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, Unaligned, !IntoBytes);
|
| 2253 | assert_impls!(CoreMaybeUninit<NotZerocopy>: KnownLayout, TryFromBytes, FromZeros, FromBytes, !Immutable, !IntoBytes, !Unaligned);
|
| 2254 | assert_impls!(CoreMaybeUninit<UnsafeCell<()>>: KnownLayout, TryFromBytes, FromZeros, FromBytes, Unaligned, !Immutable, !IntoBytes);
|
| 2255 |
|
| 2256 | assert_impls!(Wrapping<u8>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
|
| 2257 | // This test is important because it allows us to test our hand-rolled
|
| 2258 | // implementation of `<Wrapping<T> as TryFromBytes>::is_bit_valid`.
|
| 2259 | assert_impls!(Wrapping<bool>: KnownLayout, Immutable, TryFromBytes, FromZeros, IntoBytes, Unaligned, !FromBytes);
|
| 2260 | assert_impls!(Wrapping<NotZerocopy>: KnownLayout, !Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2261 | assert_impls!(Wrapping<UnsafeCell<()>>: KnownLayout, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned, !Immutable);
|
| 2262 |
|
| 2263 | assert_impls!(Unalign<u8>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
|
| 2264 | // This test is important because it allows us to test our hand-rolled
|
| 2265 | // implementation of `<Unalign<T> as TryFromBytes>::is_bit_valid`.
|
| 2266 | assert_impls!(Unalign<bool>: KnownLayout, Immutable, TryFromBytes, FromZeros, IntoBytes, Unaligned, !FromBytes);
|
| 2267 | assert_impls!(Unalign<NotZerocopy>: KnownLayout, Unaligned, !Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes);
|
| 2268 |
|
| 2269 | assert_impls!(
|
| 2270 | [u8]: KnownLayout,
|
| 2271 | Immutable,
|
| 2272 | TryFromBytes,
|
| 2273 | FromZeros,
|
| 2274 | FromBytes,
|
| 2275 | IntoBytes,
|
| 2276 | Unaligned
|
| 2277 | );
|
| 2278 | assert_impls!(
|
| 2279 | [bool]: KnownLayout,
|
| 2280 | Immutable,
|
| 2281 | TryFromBytes,
|
| 2282 | FromZeros,
|
| 2283 | IntoBytes,
|
| 2284 | Unaligned,
|
| 2285 | !FromBytes
|
| 2286 | );
|
| 2287 | assert_impls!([NotZerocopy]: KnownLayout, !Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2288 | assert_impls!(
|
| 2289 | [u8; 0]: KnownLayout,
|
| 2290 | Immutable,
|
| 2291 | TryFromBytes,
|
| 2292 | FromZeros,
|
| 2293 | FromBytes,
|
| 2294 | IntoBytes,
|
| 2295 | Unaligned,
|
| 2296 | );
|
| 2297 | assert_impls!(
|
| 2298 | [NotZerocopy; 0]: KnownLayout,
|
| 2299 | !Immutable,
|
| 2300 | !TryFromBytes,
|
| 2301 | !FromZeros,
|
| 2302 | !FromBytes,
|
| 2303 | !IntoBytes,
|
| 2304 | !Unaligned
|
| 2305 | );
|
| 2306 | assert_impls!(
|
| 2307 | [u8; 1]: KnownLayout,
|
| 2308 | Immutable,
|
| 2309 | TryFromBytes,
|
| 2310 | FromZeros,
|
| 2311 | FromBytes,
|
| 2312 | IntoBytes,
|
| 2313 | Unaligned,
|
| 2314 | );
|
| 2315 | assert_impls!(
|
| 2316 | [NotZerocopy; 1]: KnownLayout,
|
| 2317 | !Immutable,
|
| 2318 | !TryFromBytes,
|
| 2319 | !FromZeros,
|
| 2320 | !FromBytes,
|
| 2321 | !IntoBytes,
|
| 2322 | !Unaligned
|
| 2323 | );
|
| 2324 |
|
| 2325 | assert_impls!(*const NotZerocopy: KnownLayout, Immutable, TryFromBytes, FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2326 | assert_impls!(*mut NotZerocopy: KnownLayout, Immutable, TryFromBytes, FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2327 | assert_impls!(*const [NotZerocopy]: KnownLayout, Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2328 | assert_impls!(*mut [NotZerocopy]: KnownLayout, Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2329 | assert_impls!(*const dyn Debug: KnownLayout, Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2330 | assert_impls!(*mut dyn Debug: KnownLayout, Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
|
| 2331 |
|
| 2332 | #[cfg(feature = "simd")]
|
| 2333 | {
|
| 2334 | #[allow(unused_macros)]
|
| 2335 | macro_rules! test_simd_arch_mod {
|
| 2336 | ($arch:ident, $($typ:ident),*) => {
|
| 2337 | {
|
| 2338 | use core::arch::$arch::{$($typ),*};
|
| 2339 | use crate::*;
|
| 2340 | $( assert_impls!($typ: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned); )*
|
| 2341 | }
|
| 2342 | };
|
| 2343 | }
|
| 2344 | #[cfg(target_arch = "x86")]
|
| 2345 | test_simd_arch_mod!(x86, __m128, __m128d, __m128i, __m256, __m256d, __m256i);
|
| 2346 |
|
| 2347 | #[cfg(all(not(no_zerocopy_simd_x86_avx12_1_89_0), target_arch = "x86"))]
|
| 2348 | test_simd_arch_mod!(x86, __m512bh, __m512, __m512d, __m512i);
|
| 2349 |
|
| 2350 | #[cfg(target_arch = "x86_64")]
|
| 2351 | test_simd_arch_mod!(x86_64, __m128, __m128d, __m128i, __m256, __m256d, __m256i);
|
| 2352 |
|
| 2353 | #[cfg(all(not(no_zerocopy_simd_x86_avx12_1_89_0), target_arch = "x86_64"))]
|
| 2354 | test_simd_arch_mod!(x86_64, __m512bh, __m512, __m512d, __m512i);
|
| 2355 |
|
| 2356 | #[cfg(target_arch = "wasm32")]
|
| 2357 | test_simd_arch_mod!(wasm32, v128);
|
| 2358 |
|
| 2359 | #[cfg(all(feature = "simd-nightly", target_arch = "powerpc"))]
|
| 2360 | test_simd_arch_mod!(
|
| 2361 | powerpc,
|
| 2362 | vector_bool_long,
|
| 2363 | vector_double,
|
| 2364 | vector_signed_long,
|
| 2365 | vector_unsigned_long
|
| 2366 | );
|
| 2367 |
|
| 2368 | #[cfg(all(feature = "simd-nightly", target_arch = "powerpc64"))]
|
| 2369 | test_simd_arch_mod!(
|
| 2370 | powerpc64,
|
| 2371 | vector_bool_long,
|
| 2372 | vector_double,
|
| 2373 | vector_signed_long,
|
| 2374 | vector_unsigned_long
|
| 2375 | );
|
| 2376 | #[cfg(all(target_arch = "aarch64", not(no_zerocopy_aarch64_simd_1_59_0)))]
|
| 2377 | #[rustfmt::skip]
|
| 2378 | test_simd_arch_mod!(
|
| 2379 | aarch64, float32x2_t, float32x4_t, float64x1_t, float64x2_t, int8x8_t, int8x8x2_t,
|
| 2380 | int8x8x3_t, int8x8x4_t, int8x16_t, int8x16x2_t, int8x16x3_t, int8x16x4_t, int16x4_t,
|
| 2381 | int16x8_t, int32x2_t, int32x4_t, int64x1_t, int64x2_t, poly8x8_t, poly8x8x2_t, poly8x8x3_t,
|
| 2382 | poly8x8x4_t, poly8x16_t, poly8x16x2_t, poly8x16x3_t, poly8x16x4_t, poly16x4_t, poly16x8_t,
|
| 2383 | poly64x1_t, poly64x2_t, uint8x8_t, uint8x8x2_t, uint8x8x3_t, uint8x8x4_t, uint8x16_t,
|
| 2384 | uint8x16x2_t, uint8x16x3_t, uint8x16x4_t, uint16x4_t, uint16x4x2_t, uint16x4x3_t,
|
| 2385 | uint16x4x4_t, uint16x8_t, uint32x2_t, uint32x4_t, uint64x1_t, uint64x2_t
|
| 2386 | );
|
| 2387 | }
|
| 2388 | }
|
| 2389 | }
|