| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 |
|
| 11 | #[macro_use]
|
| 12 | pub(crate) mod macros;
|
| 13 |
|
| 14 | #[doc(hidden)]
|
| 15 | pub mod macro_util;
|
| 16 |
|
| 17 | use core::{
|
| 18 | marker::PhantomData,
|
| 19 | mem::{self, ManuallyDrop},
|
| 20 | num::NonZeroUsize,
|
| 21 | ptr::NonNull,
|
| 22 | };
|
| 23 |
|
| 24 | use super::*;
|
| 25 | use crate::pointer::{
|
| 26 | invariant::{Exclusive, Shared, Valid},
|
| 27 | SizeEq, TransmuteFromPtr,
|
| 28 | };
|
| 29 |
|
| 30 | |
| 31 | |
| 32 | pub(crate) struct SendSyncPhantomData<T: ?Sized>(PhantomData<T>);
|
| 33 |
|
| 34 | |
| 35 | |
| 36 | unsafe impl<T: ?Sized> Send for SendSyncPhantomData<T> {}
|
| 37 | |
| 38 | |
| 39 | unsafe impl<T: ?Sized> Sync for SendSyncPhantomData<T> {}
|
| 40 |
|
| 41 | impl<T: ?Sized> Default for SendSyncPhantomData<T> {
|
| 42 | fn default() -> SendSyncPhantomData<T> {
|
| 43 | SendSyncPhantomData(PhantomData)
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | impl<T: ?Sized> PartialEq for SendSyncPhantomData<T> {
|
| 48 | fn eq(&self, _other: &Self) -> bool {
|
| 49 | true
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | impl<T: ?Sized> Eq for SendSyncPhantomData<T> {}
|
| 54 |
|
| 55 | impl<T: ?Sized> Clone for SendSyncPhantomData<T> {
|
| 56 | fn clone(&self) -> Self {
|
| 57 | SendSyncPhantomData(PhantomData)
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | #[cfg(miri)]
|
| 62 | extern "Rust" {
|
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | pub(crate) fn miri_promise_symbolic_alignment(ptr: *const (), align: usize);
|
| 72 | }
|
| 73 |
|
| 74 | pub(crate) trait AsAddress {
|
| 75 | fn addr(self) -> usize;
|
| 76 | }
|
| 77 |
|
| 78 | impl<T: ?Sized> AsAddress for &T {
|
| 79 | #[inline(always)]
|
| 80 | fn addr(self) -> usize {
|
| 81 | let ptr: *const T = self;
|
| 82 | AsAddress::addr(ptr)
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | impl<T: ?Sized> AsAddress for &mut T {
|
| 87 | #[inline(always)]
|
| 88 | fn addr(self) -> usize {
|
| 89 | let ptr: *const T = self;
|
| 90 | AsAddress::addr(ptr)
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | impl<T: ?Sized> AsAddress for NonNull<T> {
|
| 95 | #[inline(always)]
|
| 96 | fn addr(self) -> usize {
|
| 97 | AsAddress::addr(self.as_ptr())
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | impl<T: ?Sized> AsAddress for *const T {
|
| 102 | #[inline(always)]
|
| 103 | fn addr(self) -> usize {
|
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | #[allow(clippy::as_conversions)]
|
| 109 | #[cfg_attr(
|
| 110 | __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS,
|
| 111 | allow(lossy_provenance_casts)
|
| 112 | )]
|
| 113 | return self.cast::<()>() as usize;
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | impl<T: ?Sized> AsAddress for *mut T {
|
| 118 | #[inline(always)]
|
| 119 | fn addr(self) -> usize {
|
| 120 | let ptr: *const T = self;
|
| 121 | AsAddress::addr(ptr)
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | |
| 126 | #[inline(always)]
|
| 127 | pub(crate) fn validate_aligned_to<T: AsAddress, U>(t: T) -> Result<(), AlignmentError<(), U>> {
|
| 128 | |
| 129 | |
| 130 | #[allow(clippy::arithmetic_side_effects)]
|
| 131 | let remainder = t.addr() % mem::align_of::<U>();
|
| 132 | if remainder == 0 {
|
| 133 | Ok(())
|
| 134 | } else {
|
| 135 | |
| 136 | |
| 137 | Err(unsafe { AlignmentError::new_unchecked(()) })
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | #[cfg_attr(
|
| 146 | kani,
|
| 147 | kani::requires(len <= DstLayout::MAX_SIZE),
|
| 148 | kani::requires(align.is_power_of_two()),
|
| 149 | kani::ensures(|&p| (len + p) % align.get() == 0),
|
| 150 | // Ensures that we add the minimum required padding.
|
| 151 | kani::ensures(|&p| p < align.get()),
|
| 152 | )]
|
| 153 | #[cfg_attr(not(zerocopy_inline_always), inline)]
|
| 154 | #[cfg_attr(zerocopy_inline_always, inline(always))]
|
| 155 | pub(crate) const fn padding_needed_for(len: usize, align: NonZeroUsize) -> usize {
|
| 156 | #[cfg(kani)]
|
| 157 | #[kani::proof_for_contract(padding_needed_for)]
|
| 158 | fn proof() {
|
| 159 | padding_needed_for(kani::any(), kani::any());
|
| 160 | }
|
| 161 |
|
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | #[allow(clippy::arithmetic_side_effects)]
|
| 168 | let mask = align.get() - 1;
|
| 169 |
|
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 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 | !(len.wrapping_sub(1)) & mask
|
| 215 | }
|
| 216 |
|
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | #[inline(always)]
|
| 225 | #[cfg_attr(
|
| 226 | kani,
|
| 227 | kani::requires(align.is_power_of_two()),
|
| 228 | kani::ensures(|&m| m <= n && m % align.get() == 0),
|
| 229 | // Guarantees that `m` is the *largest* value such that `m % align == 0`.
|
| 230 | kani::ensures(|&m| {
|
| 231 | // If this `checked_add` fails, then the next multiple would wrap
|
| 232 | // around, which trivially satisfies the "largest value" requirement.
|
| 233 | m.checked_add(align.get()).map(|next_mul| next_mul > n).unwrap_or(true)
|
| 234 | })
|
| 235 | )]
|
| 236 | pub(crate) const fn round_down_to_next_multiple_of_alignment(
|
| 237 | n: usize,
|
| 238 | align: NonZeroUsize,
|
| 239 | ) -> usize {
|
| 240 | #[cfg(kani)]
|
| 241 | #[kani::proof_for_contract(round_down_to_next_multiple_of_alignment)]
|
| 242 | fn proof() {
|
| 243 | round_down_to_next_multiple_of_alignment(kani::any(), kani::any());
|
| 244 | }
|
| 245 |
|
| 246 | let align = align.get();
|
| 247 | #[cfg(not(no_zerocopy_panic_in_const_and_vec_try_reserve_1_57_0))]
|
| 248 | debug_assert!(align.is_power_of_two());
|
| 249 |
|
| 250 | |
| 251 | #[allow(clippy::arithmetic_side_effects)]
|
| 252 | let mask = !(align - 1);
|
| 253 | n & mask
|
| 254 | }
|
| 255 |
|
| 256 | #[cfg_attr(not(zerocopy_inline_always), inline)]
|
| 257 | #[cfg_attr(zerocopy_inline_always, inline(always))]
|
| 258 | pub(crate) const fn max(a: NonZeroUsize, b: NonZeroUsize) -> NonZeroUsize {
|
| 259 | if a.get() < b.get() {
|
| 260 | b
|
| 261 | } else {
|
| 262 | a
|
| 263 | }
|
| 264 | }
|
| 265 |
|
| 266 | #[cfg_attr(not(zerocopy_inline_always), inline)]
|
| 267 | #[cfg_attr(zerocopy_inline_always, inline(always))]
|
| 268 | pub(crate) const fn min(a: NonZeroUsize, b: NonZeroUsize) -> NonZeroUsize {
|
| 269 | if a.get() > b.get() {
|
| 270 | b
|
| 271 | } else {
|
| 272 | a
|
| 273 | }
|
| 274 | }
|
| 275 |
|
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | #[inline(always)]
|
| 282 | pub(crate) unsafe fn copy_unchecked(src: &[u8], dst: &mut [u8]) {
|
| 283 | debug_assert!(src.len() <= dst.len());
|
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | unsafe {
|
| 295 | core::ptr::copy_nonoverlapping(src.as_ptr(), dst.as_mut_ptr(), src.len());
|
| 296 | };
|
| 297 | }
|
| 298 |
|
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | #[inline(always)]
|
| 305 | pub(crate) const unsafe fn transmute_unchecked<Src, Dst>(src: Src) -> Dst {
|
| 306 | static_assert!(Src, Dst => core::mem::size_of::<Src>() == core::mem::size_of::<Dst>());
|
| 307 |
|
| 308 | #[repr(C)]
|
| 309 | union Transmute<Src, Dst> {
|
| 310 | src: ManuallyDrop<Src>,
|
| 311 | dst: ManuallyDrop<Dst>,
|
| 312 | }
|
| 313 |
|
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | unsafe { ManuallyDrop::into_inner(Transmute { src: ManuallyDrop::new(src) }.dst) }
|
| 334 | }
|
| 335 |
|
| 336 | |
| 337 | |
| 338 | |
| 339 | pub(crate) unsafe fn transmute_ref<Src, Dst, R>(src: &Src) -> &Dst
|
| 340 | where
|
| 341 | Src: ?Sized,
|
| 342 | Dst: SizeEq<Src>
|
| 343 | + TransmuteFromPtr<Src, Shared, Valid, Valid, <Dst as SizeEq<Src>>::CastFrom, R>
|
| 344 | + ?Sized,
|
| 345 | {
|
| 346 | let dst = Ptr::from_ref(src).transmute();
|
| 347 | |
| 348 | |
| 349 | let dst = unsafe { dst.assume_alignment() };
|
| 350 | dst.as_ref()
|
| 351 | }
|
| 352 |
|
| 353 | |
| 354 | |
| 355 | |
| 356 | pub(crate) unsafe fn transmute_mut<Src, Dst, R>(src: &mut Src) -> &mut Dst
|
| 357 | where
|
| 358 | Src: ?Sized,
|
| 359 | Dst: SizeEq<Src>
|
| 360 | + TransmuteFromPtr<Src, Exclusive, Valid, Valid, <Dst as SizeEq<Src>>::CastFrom, R>
|
| 361 | + ?Sized,
|
| 362 | {
|
| 363 | let dst = Ptr::from_mut(src).transmute();
|
| 364 | |
| 365 | |
| 366 | let dst = unsafe { dst.assume_alignment() };
|
| 367 | dst.as_mut()
|
| 368 | }
|
| 369 |
|
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | #[must_use = "has no side effects (other than allocation)"]
|
| 384 | #[cfg(feature = "alloc")]
|
| 385 | #[inline]
|
| 386 | pub(crate) unsafe fn new_box<T>(
|
| 387 | meta: T::PointerMetadata,
|
| 388 | allocate: unsafe fn(core::alloc::Layout) -> *mut u8,
|
| 389 | ) -> Result<alloc::boxed::Box<T>, AllocError>
|
| 390 | where
|
| 391 | T: ?Sized + crate::KnownLayout,
|
| 392 | {
|
| 393 | let align = T::LAYOUT.align.get();
|
| 394 | if !T::is_valid_metadata(meta) {
|
| 395 | return Err(AllocError);
|
| 396 | }
|
| 397 | let size = match T::size_for_metadata(meta) {
|
| 398 | Some(size) => size,
|
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | None => return Err(AllocError),
|
| 405 | };
|
| 406 | let ptr = if size != 0 {
|
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | let layout: Layout = unsafe { Layout::from_size_align_unchecked(size, align) };
|
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | let ptr = unsafe { allocate(layout) };
|
| 426 | match NonNull::new(ptr) {
|
| 427 | Some(ptr) => ptr,
|
| 428 | None => return Err(AllocError),
|
| 429 | }
|
| 430 | } else {
|
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | #[allow(unknown_lints)]
|
| 442 | #[allow(clippy::useless_transmute, integer_to_ptr_transmutes)]
|
| 443 | let dangling = unsafe { mem::transmute::<usize, *mut u8>(align) };
|
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | unsafe { NonNull::new_unchecked(dangling) }
|
| 457 | };
|
| 458 |
|
| 459 | let ptr = T::raw_from_ptr_len(ptr, meta);
|
| 460 |
|
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | #[allow(clippy::undocumented_unsafe_blocks)]
|
| 467 | Ok(unsafe { alloc::boxed::Box::from_raw(ptr.as_ptr()) })
|
| 468 | }
|
| 469 |
|
| 470 | mod len_of {
|
| 471 | use super::*;
|
| 472 |
|
| 473 | |
| 474 | pub struct MetadataOf<T: ?Sized + KnownLayout> {
|
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | meta: T::PointerMetadata,
|
| 480 | _p: PhantomData<T>,
|
| 481 | }
|
| 482 |
|
| 483 | impl<T: ?Sized + KnownLayout> Copy for MetadataOf<T> {}
|
| 484 | impl<T: ?Sized + KnownLayout> Clone for MetadataOf<T> {
|
| 485 | #[inline]
|
| 486 | fn clone(&self) -> Self {
|
| 487 | *self
|
| 488 | }
|
| 489 | }
|
| 490 |
|
| 491 | impl<T: ?Sized + KnownLayout> core::fmt::Debug for MetadataOf<T>
|
| 492 | where
|
| 493 | T::PointerMetadata: core::fmt::Debug,
|
| 494 | {
|
| 495 | #[inline]
|
| 496 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
| 497 | f.debug_struct("MetadataOf").field("meta", &self.meta).finish()
|
| 498 | }
|
| 499 | }
|
| 500 |
|
| 501 | impl<T: ?Sized> MetadataOf<T>
|
| 502 | where
|
| 503 | T: KnownLayout,
|
| 504 | {
|
| 505 | |
| 506 | #[inline(always)]
|
| 507 | pub(crate) fn new_in_bounds(t: &T, meta: usize) -> Option<Self>
|
| 508 | where
|
| 509 | T: KnownLayout<PointerMetadata = usize>,
|
| 510 | {
|
| 511 | if meta <= Ptr::from_ref(t).len() {
|
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | Some(unsafe { Self::new_unchecked(meta) })
|
| 543 | } else {
|
| 544 | None
|
| 545 | }
|
| 546 | }
|
| 547 |
|
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | pub(crate) unsafe fn new_unchecked(meta: T::PointerMetadata) -> Self {
|
| 553 | |
| 554 | |
| 555 | Self { meta, _p: PhantomData }
|
| 556 | }
|
| 557 |
|
| 558 | pub(crate) fn get(&self) -> T::PointerMetadata
|
| 559 | where
|
| 560 | T::PointerMetadata: Copy,
|
| 561 | {
|
| 562 | self.meta
|
| 563 | }
|
| 564 |
|
| 565 | #[inline]
|
| 566 | pub(crate) fn padding_needed_for(&self) -> usize
|
| 567 | where
|
| 568 | T: KnownLayout<PointerMetadata = usize>,
|
| 569 | {
|
| 570 | let trailing_slice_layout = crate::trailing_slice_layout::<T>();
|
| 571 |
|
| 572 | |
| 573 | #[allow(
|
| 574 | unstable_name_collisions,
|
| 575 | clippy::incompatible_msrv,
|
| 576 | clippy::multiple_unsafe_ops_per_block
|
| 577 | )]
|
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | let unpadded_size = unsafe {
|
| 583 | let trailing_size = self.meta.unchecked_mul(trailing_slice_layout.elem_size);
|
| 584 | trailing_size.unchecked_add(trailing_slice_layout.offset)
|
| 585 | };
|
| 586 |
|
| 587 | util::padding_needed_for(unpadded_size, T::LAYOUT.align)
|
| 588 | }
|
| 589 |
|
| 590 | #[inline(always)]
|
| 591 | pub(crate) fn validate_cast_and_convert_metadata(
|
| 592 | addr: usize,
|
| 593 | bytes_len: MetadataOf<[u8]>,
|
| 594 | cast_type: CastType,
|
| 595 | meta: Option<T::PointerMetadata>,
|
| 596 | ) -> Result<(MetadataOf<T>, MetadataOf<[u8]>), MetadataCastError> {
|
| 597 | let layout = match meta {
|
| 598 | None => T::LAYOUT,
|
| 599 | |
| 600 | |
| 601 | Some(meta) => {
|
| 602 | if !T::is_valid_metadata(meta) {
|
| 603 | return Err(MetadataCastError::Size);
|
| 604 | }
|
| 605 | let size = match T::size_for_metadata(meta) {
|
| 606 | Some(size) => size,
|
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | None => return Err(MetadataCastError::Size),
|
| 613 | };
|
| 614 | DstLayout {
|
| 615 | align: T::LAYOUT.align,
|
| 616 | size_info: crate::SizeInfo::Sized { size },
|
| 617 | statically_shallow_unpadded: false,
|
| 618 | }
|
| 619 | }
|
| 620 | };
|
| 621 | |
| 622 | |
| 623 | |
| 624 | let (elems, split_at) =
|
| 625 | layout.validate_cast_and_convert_metadata(addr, bytes_len.get(), cast_type)?;
|
| 626 | let elems = T::PointerMetadata::from_elem_count(elems);
|
| 627 |
|
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | let elems = meta.unwrap_or(elems);
|
| 654 |
|
| 655 | |
| 656 | let elems = unsafe { MetadataOf::new_unchecked(elems) };
|
| 657 |
|
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | let split_at = unsafe { MetadataOf::<[u8]>::new_unchecked(split_at) };
|
| 671 | Ok((elems, split_at))
|
| 672 | }
|
| 673 | }
|
| 674 | }
|
| 675 |
|
| 676 | pub use len_of::MetadataOf;
|
| 677 |
|
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | pub(crate) mod polyfills {
|
| 690 | use core::ptr::{self, NonNull};
|
| 691 |
|
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | #[allow(unused)]
|
| 701 | pub(crate) trait NonNullExt<T> {
|
| 702 | fn slice_from_raw_parts(data: Self, len: usize) -> NonNull<[T]>;
|
| 703 | }
|
| 704 |
|
| 705 | impl<T> NonNullExt<T> for NonNull<T> {
|
| 706 | |
| 707 | |
| 708 | |
| 709 | #[cfg_attr(
|
| 710 | all(coverage_nightly, __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS),
|
| 711 | coverage(off)
|
| 712 | )]
|
| 713 | #[inline(always)]
|
| 714 | fn slice_from_raw_parts(data: Self, len: usize) -> NonNull<[T]> {
|
| 715 | let ptr = ptr::slice_from_raw_parts_mut(data.as_ptr(), len);
|
| 716 | |
| 717 | unsafe { NonNull::new_unchecked(ptr) }
|
| 718 | }
|
| 719 | }
|
| 720 |
|
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | #[allow(unused)]
|
| 730 | pub(crate) trait NumExt {
|
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | unsafe fn unchecked_add(self, rhs: Self) -> Self;
|
| 737 |
|
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | unsafe fn unchecked_sub(self, rhs: Self) -> Self;
|
| 744 |
|
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | unsafe fn unchecked_mul(self, rhs: Self) -> Self;
|
| 751 | }
|
| 752 |
|
| 753 | |
| 754 | |
| 755 | |
| 756 | impl NumExt for usize {
|
| 757 | #[cfg_attr(
|
| 758 | all(coverage_nightly, __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS),
|
| 759 | coverage(off)
|
| 760 | )]
|
| 761 | #[inline(always)]
|
| 762 | unsafe fn unchecked_add(self, rhs: usize) -> usize {
|
| 763 | match self.checked_add(rhs) {
|
| 764 | Some(x) => x,
|
| 765 | None => {
|
| 766 | |
| 767 | |
| 768 | unsafe { core::hint::unreachable_unchecked() }
|
| 769 | }
|
| 770 | }
|
| 771 | }
|
| 772 |
|
| 773 | #[cfg_attr(
|
| 774 | all(coverage_nightly, __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS),
|
| 775 | coverage(off)
|
| 776 | )]
|
| 777 | #[inline(always)]
|
| 778 | unsafe fn unchecked_sub(self, rhs: usize) -> usize {
|
| 779 | match self.checked_sub(rhs) {
|
| 780 | Some(x) => x,
|
| 781 | None => {
|
| 782 | |
| 783 | |
| 784 | unsafe { core::hint::unreachable_unchecked() }
|
| 785 | }
|
| 786 | }
|
| 787 | }
|
| 788 |
|
| 789 | #[cfg_attr(
|
| 790 | all(coverage_nightly, __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS),
|
| 791 | coverage(off)
|
| 792 | )]
|
| 793 | #[inline(always)]
|
| 794 | unsafe fn unchecked_mul(self, rhs: usize) -> usize {
|
| 795 | match self.checked_mul(rhs) {
|
| 796 | Some(x) => x,
|
| 797 | None => {
|
| 798 | |
| 799 | |
| 800 | unsafe { core::hint::unreachable_unchecked() }
|
| 801 | }
|
| 802 | }
|
| 803 | }
|
| 804 | }
|
| 805 | }
|
| 806 |
|
| 807 | #[cfg(test)]
|
| 808 | pub(crate) mod testutil {
|
| 809 | use crate::*;
|
| 810 |
|
| 811 | |
| 812 | #[derive(Default)]
|
| 813 | pub(crate) struct Align<T, A> {
|
| 814 | pub(crate) t: T,
|
| 815 | _a: [A; 0],
|
| 816 | }
|
| 817 |
|
| 818 | impl<T: Default, A> Align<T, A> {
|
| 819 | pub(crate) fn set_default(&mut self) {
|
| 820 | self.t = T::default();
|
| 821 | }
|
| 822 | }
|
| 823 |
|
| 824 | impl<T, A> Align<T, A> {
|
| 825 | pub(crate) const fn new(t: T) -> Align<T, A> {
|
| 826 | Align { t, _a: [] }
|
| 827 | }
|
| 828 | }
|
| 829 |
|
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | #[repr(C)]
|
| 835 | pub(crate) struct ForceUnalign<T: Unaligned, A> {
|
| 836 | |
| 837 | |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | _u: u8,
|
| 844 | pub(crate) t: T,
|
| 845 | _a: [A; 0],
|
| 846 | }
|
| 847 |
|
| 848 | impl<T: Unaligned, A> ForceUnalign<T, A> {
|
| 849 | pub(crate) fn new(t: T) -> ForceUnalign<T, A> {
|
| 850 | ForceUnalign { _u: 0, t, _a: [] }
|
| 851 | }
|
| 852 | }
|
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | #[derive(
|
| 858 | KnownLayout,
|
| 859 | Immutable,
|
| 860 | FromBytes,
|
| 861 | IntoBytes,
|
| 862 | Eq,
|
| 863 | PartialEq,
|
| 864 | Ord,
|
| 865 | PartialOrd,
|
| 866 | Default,
|
| 867 | Debug,
|
| 868 | Copy,
|
| 869 | Clone,
|
| 870 | )]
|
| 871 | #[repr(C, align(8))]
|
| 872 | pub(crate) struct AU64(pub(crate) u64);
|
| 873 |
|
| 874 | impl AU64 {
|
| 875 | |
| 876 | pub(crate) fn to_bytes(self) -> [u8; 8] {
|
| 877 | crate::transmute!(self)
|
| 878 | }
|
| 879 | }
|
| 880 |
|
| 881 | impl Display for AU64 {
|
| 882 | #[cfg_attr(
|
| 883 | all(coverage_nightly, __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS),
|
| 884 | coverage(off)
|
| 885 | )]
|
| 886 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
| 887 | Display::fmt(&self.0, f)
|
| 888 | }
|
| 889 | }
|
| 890 | }
|
| 891 |
|
| 892 | #[cfg(test)]
|
| 893 | mod tests {
|
| 894 | use super::*;
|
| 895 |
|
| 896 | #[test]
|
| 897 | fn test_round_down_to_next_multiple_of_alignment() {
|
| 898 | fn alt_impl(n: usize, align: NonZeroUsize) -> usize {
|
| 899 | let mul = n / align.get();
|
| 900 | mul * align.get()
|
| 901 | }
|
| 902 |
|
| 903 | for align in [1, 2, 4, 8, 16] {
|
| 904 | for n in 0..256 {
|
| 905 | let align = NonZeroUsize::new(align).unwrap();
|
| 906 | let want = alt_impl(n, align);
|
| 907 | let got = round_down_to_next_multiple_of_alignment(n, align);
|
| 908 | assert_eq!(got, want, "round_down_to_next_multiple_of_alignment({}, {})", n, align);
|
| 909 | }
|
| 910 | }
|
| 911 | }
|
| 912 |
|
| 913 | #[rustversion::since(1.57.0)]
|
| 914 | #[test]
|
| 915 | #[should_panic]
|
| 916 | fn test_round_down_to_next_multiple_of_alignment_zerocopy_panic_in_const_and_vec_try_reserve() {
|
| 917 | round_down_to_next_multiple_of_alignment(0, NonZeroUsize::new(3).unwrap());
|
| 918 | }
|
| 919 | #[test]
|
| 920 | fn test_send_sync_phantom_data() {
|
| 921 | let x = SendSyncPhantomData::<u8>::default();
|
| 922 | let y = x.clone();
|
| 923 | assert!(x == y);
|
| 924 | assert!(x == SendSyncPhantomData::<u8>::default());
|
| 925 | }
|
| 926 |
|
| 927 | #[test]
|
| 928 | #[allow(clippy::as_conversions)]
|
| 929 | fn test_as_address() {
|
| 930 | let x = 0u8;
|
| 931 | let r = &x;
|
| 932 | let mut x_mut = 0u8;
|
| 933 | let rm = &mut x_mut;
|
| 934 | let p = r as *const u8;
|
| 935 | let pm = rm as *mut u8;
|
| 936 | let nn = NonNull::new(p as *mut u8).unwrap();
|
| 937 |
|
| 938 | assert_eq!(AsAddress::addr(r), p as usize);
|
| 939 | assert_eq!(AsAddress::addr(rm), pm as usize);
|
| 940 | assert_eq!(AsAddress::addr(p), p as usize);
|
| 941 | assert_eq!(AsAddress::addr(pm), pm as usize);
|
| 942 | assert_eq!(AsAddress::addr(nn), p as usize);
|
| 943 | }
|
| 944 | }
|