| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 |
|
| 11 | use core::{fmt, hash::Hash};
|
| 12 |
|
| 13 | use super::*;
|
| 14 | use crate::pointer::{invariant::Valid, SizeEq, TransmuteFrom};
|
| 15 |
|
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 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 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | #[allow(missing_debug_implementations)]
|
| 115 | #[derive(Default, Copy)]
|
| 116 | #[cfg_attr(any(feature = "derive", test), derive(Immutable, FromBytes, IntoBytes, Unaligned))]
|
| 117 | #[repr(C, packed)]
|
| 118 | pub struct Unalign<T>(T);
|
| 119 |
|
| 120 | |
| 121 | |
| 122 | |
| 123 | impl_known_layout!(T => Unalign<T>);
|
| 124 |
|
| 125 | |
| 126 | |
| 127 | #[cfg_attr(
|
| 128 | all(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS, any(feature = "derive", test)),
|
| 129 | expect(unused_unsafe)
|
| 130 | )]
|
| 131 | #[cfg_attr(
|
| 132 | all(
|
| 133 | not(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS),
|
| 134 | any(feature = "derive", test)
|
| 135 | ),
|
| 136 | allow(unused_unsafe)
|
| 137 | )]
|
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 149 | const _: () = unsafe {
|
| 150 | impl_or_verify!(T => Unaligned for Unalign<T>);
|
| 151 | impl_or_verify!(T: Immutable => Immutable for Unalign<T>);
|
| 152 | impl_or_verify!(
|
| 153 | T: TryFromBytes => TryFromBytes for Unalign<T>;
|
| 154 | |c| T::is_bit_valid(c.transmute::<_, _, BecauseImmutable>())
|
| 155 | );
|
| 156 | impl_or_verify!(T: FromZeros => FromZeros for Unalign<T>);
|
| 157 | impl_or_verify!(T: FromBytes => FromBytes for Unalign<T>);
|
| 158 | impl_or_verify!(T: IntoBytes => IntoBytes for Unalign<T>);
|
| 159 | };
|
| 160 |
|
| 161 | |
| 162 | |
| 163 | |
| 164 | impl<T: Copy> Clone for Unalign<T> {
|
| 165 | #[inline(always)]
|
| 166 | fn clone(&self) -> Unalign<T> {
|
| 167 | *self
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | impl<T> Unalign<T> {
|
| 172 | |
| 173 | #[inline(always)]
|
| 174 | pub const fn new(val: T) -> Unalign<T> {
|
| 175 | Unalign(val)
|
| 176 | }
|
| 177 |
|
| 178 | |
| 179 | #[inline(always)]
|
| 180 | pub const fn into_inner(self) -> T {
|
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | unsafe { crate::util::transmute_unchecked(self) }
|
| 191 | }
|
| 192 |
|
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | #[inline(always)]
|
| 202 | pub fn try_deref(&self) -> Result<&T, AlignmentError<&Self, T>> {
|
| 203 | let inner = Ptr::from_ref(self).transmute();
|
| 204 | match inner.try_into_aligned() {
|
| 205 | Ok(aligned) => Ok(aligned.as_ref()),
|
| 206 | Err(err) => Err(err.map_src(
|
| 207 | #[inline(always)]
|
| 208 | |src| src.into_unalign().as_ref(),
|
| 209 | )),
|
| 210 | }
|
| 211 | }
|
| 212 |
|
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | #[inline(always)]
|
| 222 | pub fn try_deref_mut(&mut self) -> Result<&mut T, AlignmentError<&mut Self, T>> {
|
| 223 | let inner = Ptr::from_mut(self).transmute::<_, _, (_, (_, _))>();
|
| 224 | match inner.try_into_aligned() {
|
| 225 | Ok(aligned) => Ok(aligned.as_mut()),
|
| 226 | Err(err) => Err(err.map_src(|src| src.into_unalign().as_mut())),
|
| 227 | }
|
| 228 | }
|
| 229 |
|
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | #[inline(always)]
|
| 239 | pub const unsafe fn deref_unchecked(&self) -> &T {
|
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | unsafe { mem::transmute(self) }
|
| 250 | }
|
| 251 |
|
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | #[inline(always)]
|
| 262 | pub unsafe fn deref_mut_unchecked(&mut self) -> &mut T {
|
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | unsafe { &mut *self.get_mut_ptr() }
|
| 269 | }
|
| 270 |
|
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | #[inline(always)]
|
| 292 | pub const fn get_ptr(&self) -> *const T {
|
| 293 | ptr::addr_of!(self.0)
|
| 294 | }
|
| 295 |
|
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | #[inline(always)]
|
| 312 | pub fn get_mut_ptr(&mut self) -> *mut T {
|
| 313 | ptr::addr_of_mut!(self.0)
|
| 314 | }
|
| 315 |
|
| 316 | |
| 317 | |
| 318 | #[inline(always)]
|
| 319 | pub fn set(&mut self, t: T) {
|
| 320 | *self = Unalign::new(t);
|
| 321 | }
|
| 322 |
|
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | #[inline]
|
| 339 | pub fn update<O, F: FnOnce(&mut T) -> O>(&mut self, f: F) -> O {
|
| 340 | if mem::align_of::<T>() == 1 {
|
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 |
|
| 349 | |
| 350 | |
| 351 | let t = unsafe { self.deref_mut_unchecked() };
|
| 352 | return f(t);
|
| 353 | }
|
| 354 |
|
| 355 | |
| 356 | |
| 357 | struct WriteBackOnDrop<T> {
|
| 358 | copy: ManuallyDrop<T>,
|
| 359 | slf: *mut Unalign<T>,
|
| 360 | }
|
| 361 |
|
| 362 | impl<T> Drop for WriteBackOnDrop<T> {
|
| 363 | fn drop(&mut self) {
|
| 364 | |
| 365 | |
| 366 | let copy = unsafe { ManuallyDrop::take(&mut self.copy) };
|
| 367 | |
| 368 | |
| 369 | |
| 370 | unsafe { ptr::write(self.slf, Unalign::new(copy)) };
|
| 371 | }
|
| 372 | }
|
| 373 |
|
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | let copy = unsafe { ptr::read(self) }.into_inner();
|
| 388 | let mut write_back = WriteBackOnDrop { copy: ManuallyDrop::new(copy), slf: self };
|
| 389 |
|
| 390 | let ret = f(&mut write_back.copy);
|
| 391 |
|
| 392 | drop(write_back);
|
| 393 | ret
|
| 394 | }
|
| 395 | }
|
| 396 |
|
| 397 | impl<T: Copy> Unalign<T> {
|
| 398 | |
| 399 | |
| 400 | #[inline(always)]
|
| 401 | pub fn get(&self) -> T {
|
| 402 | let Unalign(val) = *self;
|
| 403 | val
|
| 404 | }
|
| 405 | }
|
| 406 |
|
| 407 | impl<T: Unaligned> Deref for Unalign<T> {
|
| 408 | type Target = T;
|
| 409 |
|
| 410 | #[inline(always)]
|
| 411 | fn deref(&self) -> &T {
|
| 412 | Ptr::from_ref(self).transmute().bikeshed_recall_aligned().as_ref()
|
| 413 | }
|
| 414 | }
|
| 415 |
|
| 416 | impl<T: Unaligned> DerefMut for Unalign<T> {
|
| 417 | #[inline(always)]
|
| 418 | fn deref_mut(&mut self) -> &mut T {
|
| 419 | Ptr::from_mut(self).transmute::<_, _, (_, (_, _))>().bikeshed_recall_aligned().as_mut()
|
| 420 | }
|
| 421 | }
|
| 422 |
|
| 423 | impl<T: Unaligned + PartialOrd> PartialOrd<Unalign<T>> for Unalign<T> {
|
| 424 | #[inline(always)]
|
| 425 | fn partial_cmp(&self, other: &Unalign<T>) -> Option<Ordering> {
|
| 426 | PartialOrd::partial_cmp(self.deref(), other.deref())
|
| 427 | }
|
| 428 | }
|
| 429 |
|
| 430 | impl<T: Unaligned + Ord> Ord for Unalign<T> {
|
| 431 | #[inline(always)]
|
| 432 | fn cmp(&self, other: &Unalign<T>) -> Ordering {
|
| 433 | Ord::cmp(self.deref(), other.deref())
|
| 434 | }
|
| 435 | }
|
| 436 |
|
| 437 | impl<T: Unaligned + PartialEq> PartialEq<Unalign<T>> for Unalign<T> {
|
| 438 | #[inline(always)]
|
| 439 | fn eq(&self, other: &Unalign<T>) -> bool {
|
| 440 | PartialEq::eq(self.deref(), other.deref())
|
| 441 | }
|
| 442 | }
|
| 443 |
|
| 444 | impl<T: Unaligned + Eq> Eq for Unalign<T> {}
|
| 445 |
|
| 446 | impl<T: Unaligned + Hash> Hash for Unalign<T> {
|
| 447 | #[inline(always)]
|
| 448 | fn hash<H>(&self, state: &mut H)
|
| 449 | where
|
| 450 | H: Hasher,
|
| 451 | {
|
| 452 | self.deref().hash(state);
|
| 453 | }
|
| 454 | }
|
| 455 |
|
| 456 | impl<T: Unaligned + Debug> Debug for Unalign<T> {
|
| 457 | #[inline(always)]
|
| 458 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
| 459 | Debug::fmt(self.deref(), f)
|
| 460 | }
|
| 461 | }
|
| 462 |
|
| 463 | impl<T: Unaligned + Display> Display for Unalign<T> {
|
| 464 | #[inline(always)]
|
| 465 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
| 466 | Display::fmt(self.deref(), f)
|
| 467 | }
|
| 468 | }
|
| 469 |
|
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | #[repr(transparent)]
|
| 491 | #[doc(hidden)]
|
| 492 | pub struct MaybeUninit<T: ?Sized + KnownLayout>(
|
| 493 | |
| 494 | ) `T::MaybeUninit` has `T::LAYOUT` identical to `T`,
|
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | T::MaybeUninit,
|
| 501 | );
|
| 502 |
|
| 503 | #[doc(hidden)]
|
| 504 | impl<T: ?Sized + KnownLayout> MaybeUninit<T> {
|
| 505 | |
| 506 | #[inline(always)]
|
| 507 | pub fn new(val: T) -> Self
|
| 508 | where
|
| 509 | T: Sized,
|
| 510 | Self: Sized,
|
| 511 | {
|
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | unsafe { crate::util::transmute_unchecked(val) }
|
| 529 | }
|
| 530 |
|
| 531 | |
| 532 | #[must_use]
|
| 533 | #[inline(always)]
|
| 534 | pub fn uninit() -> Self
|
| 535 | where
|
| 536 | T: Sized,
|
| 537 | Self: Sized,
|
| 538 | {
|
| 539 | let uninit = CoreMaybeUninit::<T>::uninit();
|
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | unsafe { crate::util::transmute_unchecked(uninit) }
|
| 557 | }
|
| 558 |
|
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | #[cfg(feature = "alloc")]
|
| 569 | #[inline]
|
| 570 | pub fn new_boxed_uninit(meta: T::PointerMetadata) -> Result<Box<Self>, AllocError> {
|
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | unsafe { crate::util::new_box(meta, alloc::alloc::alloc) }
|
| 577 | }
|
| 578 |
|
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | #[inline(always)]
|
| 586 | pub unsafe fn assume_init(self) -> T
|
| 587 | where
|
| 588 | T: Sized,
|
| 589 | Self: Sized,
|
| 590 | {
|
| 591 | |
| 592 | unsafe { crate::util::transmute_unchecked(self) }
|
| 593 | }
|
| 594 | }
|
| 595 |
|
| 596 | impl<T: ?Sized + KnownLayout> fmt::Debug for MaybeUninit<T> {
|
| 597 | #[inline]
|
| 598 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 599 | f.pad(core::any::type_name::<Self>())
|
| 600 | }
|
| 601 | }
|
| 602 |
|
| 603 | #[allow(unreachable_pub)] |
| 604 | #[doc(hidden)]
|
| 605 | pub use read_only_def::*;
|
| 606 | mod read_only_def {
|
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | #[repr(transparent)]
|
| 618 | pub struct ReadOnly<T: ?Sized> {
|
| 619 | |
| 620 | |
| 621 | inner: T,
|
| 622 | }
|
| 623 |
|
| 624 | impl<T> ReadOnly<T> {
|
| 625 | |
| 626 | #[must_use]
|
| 627 | #[inline(always)]
|
| 628 | pub const fn new(t: T) -> ReadOnly<T> {
|
| 629 | ReadOnly { inner: t }
|
| 630 | }
|
| 631 |
|
| 632 | |
| 633 | #[must_use]
|
| 634 | #[inline(always)]
|
| 635 | pub fn into_inner(r: ReadOnly<T>) -> T {
|
| 636 | r.inner
|
| 637 | }
|
| 638 | }
|
| 639 |
|
| 640 | impl<T: ?Sized> ReadOnly<T> {
|
| 641 | #[inline(always)]
|
| 642 | pub(crate) fn as_mut(r: &mut ReadOnly<T>) -> &mut T {
|
| 643 | |
| 644 | |
| 645 | &mut r.inner
|
| 646 | }
|
| 647 |
|
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | pub(crate) const unsafe fn as_ref_unchecked(r: &ReadOnly<T>) -> &T {
|
| 653 | |
| 654 | &r.inner
|
| 655 | }
|
| 656 | }
|
| 657 | }
|
| 658 |
|
| 659 | |
| 660 | const _: () = unsafe {
|
| 661 | unsafe_impl_known_layout!(T: ?Sized + KnownLayout => #[repr(T)] ReadOnly<T>);
|
| 662 | };
|
| 663 |
|
| 664 | #[allow(clippy::multiple_unsafe_ops_per_block)]
|
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | const _: () = unsafe {
|
| 673 | unsafe_impl!(T: ?Sized + Unaligned => Unaligned for ReadOnly<T>);
|
| 674 | unsafe_impl!(
|
| 675 | T: ?Sized + TryFromBytes => TryFromBytes for ReadOnly<T>;
|
| 676 | |c| T::is_bit_valid(c.cast::<_, <ReadOnly<T> as SizeEq<ReadOnly<ReadOnly<T>>>>::CastFrom, _>())
|
| 677 | );
|
| 678 | unsafe_impl!(T: ?Sized + FromZeros => FromZeros for ReadOnly<T>);
|
| 679 | unsafe_impl!(T: ?Sized + FromBytes => FromBytes for ReadOnly<T>);
|
| 680 | unsafe_impl!(T: ?Sized + IntoBytes => IntoBytes for ReadOnly<T>);
|
| 681 | };
|
| 682 |
|
| 683 | |
| 684 | |
| 685 | const _: () = unsafe {
|
| 686 | unsafe_impl!(T: ?Sized => Immutable for ReadOnly<T>);
|
| 687 | };
|
| 688 |
|
| 689 | const _: () = {
|
| 690 | use crate::pointer::cast::CastExact;
|
| 691 |
|
| 692 | |
| 693 | define_cast!(unsafe { pub CastFromReadOnly<T: ?Sized> = ReadOnly<T> => T});
|
| 694 | |
| 695 | unsafe impl<T: ?Sized> CastExact<ReadOnly<T>, T> for CastFromReadOnly {}
|
| 696 | |
| 697 | define_cast!(unsafe { pub CastToReadOnly<T: ?Sized> = T => ReadOnly<T>});
|
| 698 | |
| 699 | unsafe impl<T: ?Sized> CastExact<T, ReadOnly<T>> for CastToReadOnly {}
|
| 700 |
|
| 701 | impl<T: ?Sized> SizeEq<ReadOnly<T>> for T {
|
| 702 | type CastFrom = CastFromReadOnly;
|
| 703 | }
|
| 704 |
|
| 705 | impl<T: ?Sized> SizeEq<T> for ReadOnly<T> {
|
| 706 | type CastFrom = CastToReadOnly;
|
| 707 | }
|
| 708 | };
|
| 709 |
|
| 710 | |
| 711 | |
| 712 | unsafe impl<T: ?Sized> TransmuteFrom<T, Valid, Valid> for ReadOnly<T> {}
|
| 713 |
|
| 714 | |
| 715 | |
| 716 | unsafe impl<T: ?Sized> TransmuteFrom<ReadOnly<T>, Valid, Valid> for T {}
|
| 717 |
|
| 718 | impl<'a, T: ?Sized + Immutable> From<&'a T> for &'a ReadOnly<T> {
|
| 719 | #[inline(always)]
|
| 720 | fn from(t: &'a T) -> &'a ReadOnly<T> {
|
| 721 | let ro = Ptr::from_ref(t).transmute::<_, _, (_, _)>();
|
| 722 | |
| 723 | |
| 724 | let ro = unsafe { ro.assume_alignment() };
|
| 725 | ro.as_ref()
|
| 726 | }
|
| 727 | }
|
| 728 |
|
| 729 | impl<T: ?Sized + Immutable> Deref for ReadOnly<T> {
|
| 730 | type Target = T;
|
| 731 |
|
| 732 | #[inline(always)]
|
| 733 | fn deref(&self) -> &Self::Target {
|
| 734 | |
| 735 | unsafe { ReadOnly::as_ref_unchecked(self) }
|
| 736 | }
|
| 737 | }
|
| 738 |
|
| 739 | impl<T: ?Sized + Immutable> DerefMut for ReadOnly<T> {
|
| 740 | #[inline(always)]
|
| 741 | fn deref_mut(&mut self) -> &mut Self::Target {
|
| 742 | ReadOnly::as_mut(self)
|
| 743 | }
|
| 744 | }
|
| 745 |
|
| 746 | impl<T: ?Sized + Immutable + Debug> Debug for ReadOnly<T> {
|
| 747 | #[inline(always)]
|
| 748 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
| 749 | self.deref().fmt(f)
|
| 750 | }
|
| 751 | }
|
| 752 |
|
| 753 | |
| 754 | unsafe impl<T: HasTag + ?Sized> HasTag for ReadOnly<T> {
|
| 755 | #[allow(clippy::missing_inline_in_public_items)]
|
| 756 | fn only_derive_is_allowed_to_implement_this_trait()
|
| 757 | where
|
| 758 | Self: Sized,
|
| 759 | {
|
| 760 | }
|
| 761 |
|
| 762 | type Tag = T::Tag;
|
| 763 |
|
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | |
| 769 | type ProjectToTag = crate::pointer::cast::TransitiveProject<
|
| 770 | T,
|
| 771 | <T as SizeEq<ReadOnly<T>>>::CastFrom,
|
| 772 | T::ProjectToTag,
|
| 773 | >;
|
| 774 | }
|
| 775 |
|
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | unsafe impl<T, Field, const VARIANT_ID: i128, const FIELD_ID: i128>
|
| 798 | HasField<Field, VARIANT_ID, FIELD_ID> for ReadOnly<T>
|
| 799 | where
|
| 800 | T: HasField<Field, VARIANT_ID, FIELD_ID> + ?Sized,
|
| 801 | {
|
| 802 | #[allow(clippy::missing_inline_in_public_items)]
|
| 803 | fn only_derive_is_allowed_to_implement_this_trait()
|
| 804 | where
|
| 805 | Self: Sized,
|
| 806 | {
|
| 807 | }
|
| 808 |
|
| 809 | type Type = ReadOnly<T::Type>;
|
| 810 |
|
| 811 | #[inline(always)]
|
| 812 | fn project(slf: PtrInner<'_, Self>) -> *mut ReadOnly<T::Type> {
|
| 813 | slf.project::<_, <T as SizeEq<ReadOnly<T>>>::CastFrom>()
|
| 814 | .project::<_, crate::pointer::cast::Projection<Field, VARIANT_ID, FIELD_ID>>()
|
| 815 | .project::<_, <ReadOnly<T::Type> as SizeEq<T::Type>>::CastFrom>()
|
| 816 | .as_non_null()
|
| 817 | .as_ptr()
|
| 818 | }
|
| 819 | }
|
| 820 |
|
| 821 | |
| 822 | |
| 823 | |
| 824 | |
| 825 | unsafe impl<T, Field, I, const VARIANT_ID: i128, const FIELD_ID: i128>
|
| 826 | ProjectField<Field, I, VARIANT_ID, FIELD_ID> for ReadOnly<T>
|
| 827 | where
|
| 828 | T: ProjectField<Field, I, VARIANT_ID, FIELD_ID> + ?Sized,
|
| 829 | I: invariant::Invariants,
|
| 830 | {
|
| 831 | #[allow(clippy::missing_inline_in_public_items)]
|
| 832 | fn only_derive_is_allowed_to_implement_this_trait()
|
| 833 | where
|
| 834 | Self: Sized,
|
| 835 | {
|
| 836 | }
|
| 837 |
|
| 838 | type Invariants = T::Invariants;
|
| 839 |
|
| 840 | type Error = T::Error;
|
| 841 |
|
| 842 | #[inline(always)]
|
| 843 | fn is_projectable<'a>(ptr: Ptr<'a, Self::Tag, I>) -> Result<(), Self::Error> {
|
| 844 | T::is_projectable(ptr)
|
| 845 | }
|
| 846 | }
|
| 847 |
|
| 848 | #[cfg(test)]
|
| 849 | mod tests {
|
| 850 | use core::panic::AssertUnwindSafe;
|
| 851 |
|
| 852 | use super::*;
|
| 853 | use crate::util::testutil::*;
|
| 854 |
|
| 855 | #[test]
|
| 856 | fn test_unalign() {
|
| 857 | |
| 858 | let mut u = Unalign::new(AU64(123));
|
| 859 | assert_eq!(u.get(), AU64(123));
|
| 860 | assert_eq!(u.into_inner(), AU64(123));
|
| 861 | assert_eq!(u.get_ptr(), <*const _>::cast::<AU64>(&u));
|
| 862 | assert_eq!(u.get_mut_ptr(), <*mut _>::cast::<AU64>(&mut u));
|
| 863 | u.set(AU64(321));
|
| 864 | assert_eq!(u.get(), AU64(321));
|
| 865 |
|
| 866 | |
| 867 | let mut u: Align<_, AU64> = Align::new(Unalign::new(AU64(123)));
|
| 868 | assert_eq!(u.t.try_deref().unwrap(), &AU64(123));
|
| 869 | assert_eq!(u.t.try_deref_mut().unwrap(), &mut AU64(123));
|
| 870 | |
| 871 | assert_eq!(unsafe { u.t.deref_unchecked() }, &AU64(123));
|
| 872 | |
| 873 | assert_eq!(unsafe { u.t.deref_mut_unchecked() }, &mut AU64(123));
|
| 874 | *u.t.try_deref_mut().unwrap() = AU64(321);
|
| 875 | assert_eq!(u.t.get(), AU64(321));
|
| 876 |
|
| 877 | |
| 878 | |
| 879 | let mut u: ForceUnalign<_, AU64> = ForceUnalign::new(Unalign::new(AU64(123)));
|
| 880 | assert!(matches!(u.t.try_deref(), Err(AlignmentError { .. })));
|
| 881 | assert!(matches!(u.t.try_deref_mut(), Err(AlignmentError { .. })));
|
| 882 |
|
| 883 | |
| 884 | let mut u = Unalign::new(123u8);
|
| 885 | assert_eq!(u.try_deref(), Ok(&123));
|
| 886 | assert_eq!(u.try_deref_mut(), Ok(&mut 123));
|
| 887 | assert_eq!(u.deref(), &123);
|
| 888 | assert_eq!(u.deref_mut(), &mut 123);
|
| 889 | *u = 21;
|
| 890 | assert_eq!(u.get(), 21);
|
| 891 |
|
| 892 | |
| 893 | const _UNALIGN: Unalign<u64> = Unalign::new(0);
|
| 894 | const _UNALIGN_PTR: *const u64 = _UNALIGN.get_ptr();
|
| 895 | const _U64: u64 = _UNALIGN.into_inner();
|
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | #[allow(dead_code)]
|
| 901 | const _: () = {
|
| 902 | let x: Align<_, AU64> = Align::new(Unalign::new(AU64(123)));
|
| 903 | |
| 904 | |
| 905 | |
| 906 | let au64 = unsafe { x.t.deref_unchecked() };
|
| 907 | match au64 {
|
| 908 | AU64(123) => {}
|
| 909 | _ => const_unreachable!(),
|
| 910 | }
|
| 911 | };
|
| 912 | }
|
| 913 |
|
| 914 | #[test]
|
| 915 | fn test_unalign_update() {
|
| 916 | let mut u = Unalign::new(AU64(123));
|
| 917 | u.update(|a| a.0 += 1);
|
| 918 | assert_eq!(u.get(), AU64(124));
|
| 919 |
|
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | let mut u = Unalign::new(Box::new(AU64(123)));
|
| 926 | let res = std::panic::catch_unwind(AssertUnwindSafe(|| {
|
| 927 | u.update(|a| {
|
| 928 | a.0 += 1;
|
| 929 | panic!();
|
| 930 | })
|
| 931 | }));
|
| 932 | assert!(res.is_err());
|
| 933 | assert_eq!(u.into_inner(), Box::new(AU64(124)));
|
| 934 |
|
| 935 | |
| 936 | let mut u = Unalign::new([0u8, 1]);
|
| 937 | u.update(|a| a[0] += 1);
|
| 938 | assert_eq!(u.get(), [1u8, 1]);
|
| 939 | }
|
| 940 |
|
| 941 | #[test]
|
| 942 | fn test_unalign_copy_clone() {
|
| 943 | |
| 944 | |
| 945 |
|
| 946 | |
| 947 | let u = ForceUnalign::<_, AU64>::new(Unalign::new(AU64(123)));
|
| 948 | #[allow(clippy::clone_on_copy)]
|
| 949 | let v = u.t.clone();
|
| 950 | let w = u.t;
|
| 951 | assert_eq!(u.t.get(), v.get());
|
| 952 | assert_eq!(u.t.get(), w.get());
|
| 953 | assert_eq!(v.get(), w.get());
|
| 954 | }
|
| 955 |
|
| 956 | #[test]
|
| 957 | fn test_unalign_trait_impls() {
|
| 958 | let zero = Unalign::new(0u8);
|
| 959 | let one = Unalign::new(1u8);
|
| 960 |
|
| 961 | assert!(zero < one);
|
| 962 | assert_eq!(PartialOrd::partial_cmp(&zero, &one), Some(Ordering::Less));
|
| 963 | assert_eq!(Ord::cmp(&zero, &one), Ordering::Less);
|
| 964 |
|
| 965 | assert_ne!(zero, one);
|
| 966 | assert_eq!(zero, zero);
|
| 967 | assert!(!PartialEq::eq(&zero, &one));
|
| 968 | assert!(PartialEq::eq(&zero, &zero));
|
| 969 |
|
| 970 | fn hash<T: Hash>(t: &T) -> u64 {
|
| 971 | let mut h = std::collections::hash_map::DefaultHasher::new();
|
| 972 | t.hash(&mut h);
|
| 973 | h.finish()
|
| 974 | }
|
| 975 |
|
| 976 | assert_eq!(hash(&zero), hash(&0u8));
|
| 977 | assert_eq!(hash(&one), hash(&1u8));
|
| 978 |
|
| 979 | assert_eq!(format!("{:?}", zero), format!("{:?}", 0u8));
|
| 980 | assert_eq!(format!("{:?}", one), format!("{:?}", 1u8));
|
| 981 | assert_eq!(format!("{}", zero), format!("{}", 0u8));
|
| 982 | assert_eq!(format!("{}", one), format!("{}", 1u8));
|
| 983 | }
|
| 984 |
|
| 985 | #[test]
|
| 986 | #[allow(clippy::as_conversions)]
|
| 987 | fn test_maybe_uninit() {
|
| 988 | |
| 989 | {
|
| 990 | let input = 42;
|
| 991 | let uninit = MaybeUninit::new(input);
|
| 992 | |
| 993 | let output = unsafe { uninit.assume_init() };
|
| 994 | assert_eq!(input, output);
|
| 995 | }
|
| 996 |
|
| 997 | |
| 998 | {
|
| 999 | let input = 42;
|
| 1000 | let uninit = MaybeUninit::new(&input);
|
| 1001 | |
| 1002 | let output = unsafe { uninit.assume_init() };
|
| 1003 | assert_eq!(&input as *const _, output as *const _);
|
| 1004 | assert_eq!(input, *output);
|
| 1005 | }
|
| 1006 |
|
| 1007 | |
| 1008 | {
|
| 1009 | let input = [1, 2, 3, 4];
|
| 1010 | let uninit = MaybeUninit::new(&input[..]);
|
| 1011 | |
| 1012 | let output = unsafe { uninit.assume_init() };
|
| 1013 | assert_eq!(&input[..] as *const _, output as *const _);
|
| 1014 | assert_eq!(input, *output);
|
| 1015 | }
|
| 1016 | }
|
| 1017 | #[test]
|
| 1018 | fn test_maybe_uninit_uninit() {
|
| 1019 | let _uninit = MaybeUninit::<u8>::uninit();
|
| 1020 | |
| 1021 | }
|
| 1022 |
|
| 1023 | #[test]
|
| 1024 | #[cfg(feature = "alloc")]
|
| 1025 | fn test_maybe_uninit_new_boxed_uninit() {
|
| 1026 | let _boxed = MaybeUninit::<u8>::new_boxed_uninit(()).unwrap();
|
| 1027 | }
|
| 1028 |
|
| 1029 | #[test]
|
| 1030 | fn test_maybe_uninit_debug() {
|
| 1031 | let uninit = MaybeUninit::<u8>::uninit();
|
| 1032 | assert!(format!("{:?}", uninit).contains("MaybeUninit"));
|
| 1033 | }
|
| 1034 | }
|