| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 |
|
| 11 | #![allow(missing_docs)]
|
| 12 |
|
| 13 | use core::{
|
| 14 | fmt::{Debug, Formatter},
|
| 15 | marker::PhantomData,
|
| 16 | };
|
| 17 |
|
| 18 | use crate::{
|
| 19 | pointer::{
|
| 20 | inner::PtrInner,
|
| 21 | invariant::*,
|
| 22 | transmute::{MutationCompatible, SizeEq, TransmuteFromPtr},
|
| 23 | },
|
| 24 | AlignmentError, CastError, CastType, KnownLayout, SizeError, TryFromBytes, ValidityError,
|
| 25 | };
|
| 26 |
|
| 27 | |
| 28 | mod def {
|
| 29 | #[cfg(doc)]
|
| 30 | use super::super::invariant;
|
| 31 | use super::*;
|
| 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 | pub struct Ptr<'a, T, I>
|
| 58 | where
|
| 59 | T: ?Sized,
|
| 60 | I: Invariants,
|
| 61 | {
|
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | ptr: PtrInner<'a, T>,
|
| 72 | _invariants: PhantomData<I>,
|
| 73 | }
|
| 74 |
|
| 75 | impl<'a, T, I> Ptr<'a, T, I>
|
| 76 | where
|
| 77 | T: 'a + ?Sized,
|
| 78 | I: Invariants,
|
| 79 | {
|
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | pub(crate) unsafe fn from_inner(ptr: PtrInner<'a, T>) -> Ptr<'a, T, I> {
|
| 93 | |
| 94 | |
| 95 | Self { ptr, _invariants: PhantomData }
|
| 96 | }
|
| 97 |
|
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | #[inline]
|
| 104 | #[must_use]
|
| 105 | pub fn as_inner(&self) -> PtrInner<'a, T> {
|
| 106 | self.ptr
|
| 107 | }
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | #[allow(unreachable_pub)] |
| 112 | pub use def::Ptr;
|
| 113 |
|
| 114 | |
| 115 | mod _external {
|
| 116 | use super::*;
|
| 117 |
|
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | impl<'a, T, I> Copy for Ptr<'a, T, I>
|
| 136 | where
|
| 137 | T: 'a + ?Sized,
|
| 138 | I: Invariants<Aliasing = Shared>,
|
| 139 | {
|
| 140 | }
|
| 141 |
|
| 142 | |
| 143 | impl<'a, T, I> Clone for Ptr<'a, T, I>
|
| 144 | where
|
| 145 | T: 'a + ?Sized,
|
| 146 | I: Invariants<Aliasing = Shared>,
|
| 147 | {
|
| 148 | #[inline]
|
| 149 | fn clone(&self) -> Self {
|
| 150 | *self
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | impl<'a, T, I> Debug for Ptr<'a, T, I>
|
| 155 | where
|
| 156 | T: 'a + ?Sized,
|
| 157 | I: Invariants,
|
| 158 | {
|
| 159 | #[inline]
|
| 160 | fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
|
| 161 | self.as_inner().as_non_null().fmt(f)
|
| 162 | }
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | |
| 167 | mod _conversions {
|
| 168 | use super::*;
|
| 169 | use crate::pointer::cast::{CastExact, CastSized, IdCast};
|
| 170 |
|
| 171 | |
| 172 | impl<'a, T> Ptr<'a, T, (Shared, Aligned, Valid)>
|
| 173 | where
|
| 174 | T: 'a + ?Sized,
|
| 175 | {
|
| 176 | |
| 177 | #[inline(always)]
|
| 178 | pub fn from_ref(ptr: &'a T) -> Self {
|
| 179 | let inner = PtrInner::from_ref(ptr);
|
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | unsafe { Self::from_inner(inner) }
|
| 195 | }
|
| 196 | }
|
| 197 |
|
| 198 | |
| 199 | impl<'a, T> Ptr<'a, T, (Exclusive, Aligned, Valid)>
|
| 200 | where
|
| 201 | T: 'a + ?Sized,
|
| 202 | {
|
| 203 | |
| 204 | #[inline(always)]
|
| 205 | pub fn from_mut(ptr: &'a mut T) -> Self {
|
| 206 | let inner = PtrInner::from_mut(ptr);
|
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | unsafe { Self::from_inner(inner) }
|
| 220 | }
|
| 221 | }
|
| 222 |
|
| 223 | |
| 224 | impl<'a, T, I> Ptr<'a, T, I>
|
| 225 | where
|
| 226 | T: 'a + ?Sized,
|
| 227 | I: Invariants<Alignment = Aligned, Validity = Valid>,
|
| 228 | I::Aliasing: Reference,
|
| 229 | {
|
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | #[allow(clippy::wrong_self_convention)]
|
| 236 | #[inline]
|
| 237 | #[must_use]
|
| 238 | pub fn as_ref(self) -> &'a T {
|
| 239 | let raw = self.as_inner().as_non_null();
|
| 240 | |
| 241 | |
| 242 | #[cfg(miri)]
|
| 243 | unsafe {
|
| 244 | crate::util::miri_promise_symbolic_alignment(
|
| 245 | raw.as_ptr().cast(),
|
| 246 | core::mem::align_of_val_raw(raw.as_ptr()),
|
| 247 | );
|
| 248 | }
|
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 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 | unsafe { raw.as_ref() }
|
| 278 | }
|
| 279 | }
|
| 280 |
|
| 281 | impl<'a, T, I> Ptr<'a, T, I>
|
| 282 | where
|
| 283 | T: 'a + ?Sized,
|
| 284 | I: Invariants,
|
| 285 | I::Aliasing: Reference,
|
| 286 | {
|
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | #[inline]
|
| 292 | #[must_use]
|
| 293 | #[allow(clippy::needless_lifetimes)] |
| 294 | pub fn reborrow<'b>(&'b mut self) -> Ptr<'b, T, I>
|
| 295 | where
|
| 296 | 'a: 'b,
|
| 297 | {
|
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | unsafe { Ptr::from_inner(self.as_inner()) }
|
| 330 | }
|
| 331 |
|
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | #[inline]
|
| 338 | #[must_use]
|
| 339 | #[allow(clippy::needless_lifetimes)] |
| 340 | pub fn reborrow_shared<'b>(&'b mut self) -> Ptr<'b, T, (Shared, I::Alignment, I::Validity)>
|
| 341 | where
|
| 342 | 'a: 'b,
|
| 343 | {
|
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | unsafe { Ptr::from_inner(self.as_inner()) }
|
| 376 | }
|
| 377 | }
|
| 378 |
|
| 379 | |
| 380 | impl<'a, T> Ptr<'a, T, (Exclusive, Aligned, Valid)>
|
| 381 | where
|
| 382 | T: 'a + ?Sized,
|
| 383 | {
|
| 384 | |
| 385 | #[allow(clippy::wrong_self_convention)]
|
| 386 | #[inline]
|
| 387 | #[must_use]
|
| 388 | pub fn as_mut(self) -> &'a mut T {
|
| 389 | let mut raw = self.as_inner().as_non_null();
|
| 390 | |
| 391 | |
| 392 | #[cfg(miri)]
|
| 393 | unsafe {
|
| 394 | crate::util::miri_promise_symbolic_alignment(
|
| 395 | raw.as_ptr().cast(),
|
| 396 | core::mem::align_of_val_raw(raw.as_ptr()),
|
| 397 | );
|
| 398 | }
|
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | unsafe { raw.as_mut() }
|
| 426 | }
|
| 427 | }
|
| 428 |
|
| 429 | |
| 430 | impl<'a, T: ?Sized, I> Ptr<'a, T, I>
|
| 431 | where
|
| 432 | I: Invariants,
|
| 433 | {
|
| 434 | #[must_use]
|
| 435 | #[inline(always)]
|
| 436 | pub fn transmute<U, V, R>(self) -> Ptr<'a, U, (I::Aliasing, Unaligned, V)>
|
| 437 | where
|
| 438 | V: Validity,
|
| 439 | U: TransmuteFromPtr<T, I::Aliasing, I::Validity, V, <U as SizeEq<T>>::CastFrom, R>
|
| 440 | + SizeEq<T>
|
| 441 | + ?Sized,
|
| 442 | {
|
| 443 | self.transmute_with::<U, V, <U as SizeEq<T>>::CastFrom, R>()
|
| 444 | }
|
| 445 |
|
| 446 | #[inline]
|
| 447 | #[must_use]
|
| 448 | pub fn transmute_with<U, V, C, R>(self) -> Ptr<'a, U, (I::Aliasing, Unaligned, V)>
|
| 449 | where
|
| 450 | V: Validity,
|
| 451 | U: TransmuteFromPtr<T, I::Aliasing, I::Validity, V, C, R> + ?Sized,
|
| 452 | C: CastExact<T, U>,
|
| 453 | {
|
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | unsafe { self.project_transmute_unchecked::<_, _, C>() }
|
| 468 | }
|
| 469 |
|
| 470 | #[inline]
|
| 471 | #[must_use]
|
| 472 | pub fn recall_validity<V, R>(self) -> Ptr<'a, T, (I::Aliasing, I::Alignment, V)>
|
| 473 | where
|
| 474 | V: Validity,
|
| 475 | T: TransmuteFromPtr<T, I::Aliasing, I::Validity, V, IdCast, R>,
|
| 476 | {
|
| 477 | let ptr = self.transmute_with::<T, V, IdCast, R>();
|
| 478 | |
| 479 | |
| 480 | |
| 481 | unsafe { ptr.assume_alignment::<I::Alignment>() }
|
| 482 | }
|
| 483 |
|
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | #[inline]
|
| 508 | #[must_use]
|
| 509 | pub unsafe fn project_transmute_unchecked<U: ?Sized, V, P>(
|
| 510 | self,
|
| 511 | ) -> Ptr<'a, U, (I::Aliasing, Unaligned, V)>
|
| 512 | where
|
| 513 | V: Validity,
|
| 514 | P: crate::pointer::cast::Project<T, U>,
|
| 515 | {
|
| 516 | let ptr = self.as_inner().project::<_, P>();
|
| 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 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | unsafe { Ptr::from_inner(ptr) }
|
| 547 | }
|
| 548 | }
|
| 549 |
|
| 550 | |
| 551 | impl<'a, T, I> Ptr<'a, T, I>
|
| 552 | where
|
| 553 | I: Invariants,
|
| 554 | {
|
| 555 | |
| 556 | |
| 557 | #[inline]
|
| 558 | #[must_use]
|
| 559 | pub fn into_unalign(
|
| 560 | self,
|
| 561 | ) -> Ptr<'a, crate::Unalign<T>, (I::Aliasing, Aligned, I::Validity)> {
|
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 |
|
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | let ptr = unsafe { self.project_transmute_unchecked::<_, _, CastSized>() };
|
| 585 | ptr.bikeshed_recall_aligned()
|
| 586 | }
|
| 587 | }
|
| 588 |
|
| 589 | impl<'a, T, I> Ptr<'a, T, I>
|
| 590 | where
|
| 591 | T: ?Sized,
|
| 592 | I: Invariants<Validity = Valid>,
|
| 593 | I::Aliasing: Reference,
|
| 594 | {
|
| 595 | |
| 596 | #[must_use]
|
| 597 | #[inline(always)]
|
| 598 | pub fn read<R>(self) -> T
|
| 599 | where
|
| 600 | T: Copy,
|
| 601 | T: Read<I::Aliasing, R>,
|
| 602 | {
|
| 603 | <I::Alignment as Alignment>::read(self)
|
| 604 | }
|
| 605 |
|
| 606 | |
| 607 | |
| 608 | |
| 609 | #[must_use]
|
| 610 | #[inline]
|
| 611 | pub fn unaligned_as_ref(self) -> &'a T
|
| 612 | where
|
| 613 | T: crate::Unaligned,
|
| 614 | {
|
| 615 | self.bikeshed_recall_aligned().as_ref()
|
| 616 | }
|
| 617 | }
|
| 618 | }
|
| 619 |
|
| 620 | |
| 621 | mod _transitions {
|
| 622 | use super::*;
|
| 623 | use crate::{
|
| 624 | pointer::{cast::IdCast, transmute::TryTransmuteFromPtr},
|
| 625 | ReadOnly,
|
| 626 | };
|
| 627 |
|
| 628 | impl<'a, T, I> Ptr<'a, T, I>
|
| 629 | where
|
| 630 | T: 'a + ?Sized,
|
| 631 | I: Invariants,
|
| 632 | {
|
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | unsafe fn assume_invariants<H: Invariants>(self) -> Ptr<'a, T, H> {
|
| 639 | |
| 640 | |
| 641 | |
| 642 | unsafe { Ptr::from_inner(self.as_inner()) }
|
| 643 | }
|
| 644 |
|
| 645 | |
| 646 | |
| 647 | #[inline]
|
| 648 | #[must_use]
|
| 649 | pub fn unify_invariants<
|
| 650 | H: Invariants<Aliasing = I::Aliasing, Alignment = I::Alignment, Validity = I::Validity>,
|
| 651 | >(
|
| 652 | self,
|
| 653 | ) -> Ptr<'a, T, H> {
|
| 654 | |
| 655 | |
| 656 | unsafe { self.assume_invariants::<H>() }
|
| 657 | }
|
| 658 |
|
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | #[inline]
|
| 667 | pub(crate) unsafe fn assume_alignment<A: Alignment>(
|
| 668 | self,
|
| 669 | ) -> Ptr<'a, T, (I::Aliasing, A, I::Validity)> {
|
| 670 | |
| 671 | |
| 672 | unsafe { self.assume_invariants() }
|
| 673 | }
|
| 674 |
|
| 675 | |
| 676 | |
| 677 | #[inline]
|
| 678 | pub fn try_into_aligned(
|
| 679 | self,
|
| 680 | ) -> Result<Ptr<'a, T, (I::Aliasing, Aligned, I::Validity)>, AlignmentError<Self, T>>
|
| 681 | where
|
| 682 | T: Sized,
|
| 683 | {
|
| 684 | if let Err(err) =
|
| 685 | crate::util::validate_aligned_to::<_, T>(self.as_inner().as_non_null())
|
| 686 | {
|
| 687 | return Err(err.with_src(self));
|
| 688 | }
|
| 689 |
|
| 690 | |
| 691 | Ok(unsafe { self.assume_alignment::<Aligned>() })
|
| 692 | }
|
| 693 |
|
| 694 | |
| 695 | #[inline]
|
| 696 | |
| 697 | |
| 698 | #[must_use]
|
| 699 | pub fn bikeshed_recall_aligned(self) -> Ptr<'a, T, (I::Aliasing, Aligned, I::Validity)>
|
| 700 | where
|
| 701 | T: crate::Unaligned,
|
| 702 | {
|
| 703 | |
| 704 | |
| 705 | unsafe { self.assume_alignment::<Aligned>() }
|
| 706 | }
|
| 707 |
|
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | #[must_use]
|
| 716 | #[inline]
|
| 717 | pub unsafe fn assume_validity<V: Validity>(
|
| 718 | self,
|
| 719 | ) -> Ptr<'a, T, (I::Aliasing, I::Alignment, V)> {
|
| 720 | |
| 721 | |
| 722 | unsafe { self.assume_invariants() }
|
| 723 | }
|
| 724 |
|
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | #[must_use]
|
| 732 | #[inline]
|
| 733 | pub unsafe fn assume_initialized(
|
| 734 | self,
|
| 735 | ) -> Ptr<'a, T, (I::Aliasing, I::Alignment, Initialized)> {
|
| 736 | |
| 737 | |
| 738 | unsafe { self.assume_validity::<Initialized>() }
|
| 739 | }
|
| 740 |
|
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | #[must_use]
|
| 748 | #[inline]
|
| 749 | pub unsafe fn assume_valid(self) -> Ptr<'a, T, (I::Aliasing, I::Alignment, Valid)> {
|
| 750 | |
| 751 | |
| 752 | unsafe { self.assume_validity::<Valid>() }
|
| 753 | }
|
| 754 |
|
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | #[inline]
|
| 768 | pub fn try_into_valid<R, S>(
|
| 769 | mut self,
|
| 770 | ) -> Result<Ptr<'a, T, (I::Aliasing, I::Alignment, Valid)>, ValidityError<Self, T>>
|
| 771 | where
|
| 772 | T: TryFromBytes
|
| 773 | + Read<I::Aliasing, R>
|
| 774 | + TryTransmuteFromPtr<T, I::Aliasing, I::Validity, Valid, IdCast, S>,
|
| 775 | ReadOnly<T>: Read<I::Aliasing, R>,
|
| 776 | I::Aliasing: Reference,
|
| 777 | I: Invariants<Validity = Initialized>,
|
| 778 | {
|
| 779 | |
| 780 | |
| 781 | |
| 782 | if T::is_bit_valid(self.reborrow().transmute::<_, _, _>().reborrow_shared()) {
|
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | Ok(unsafe { self.assume_valid() })
|
| 790 | } else {
|
| 791 | Err(ValidityError::new(self))
|
| 792 | }
|
| 793 | }
|
| 794 |
|
| 795 | |
| 796 | #[inline]
|
| 797 | #[must_use]
|
| 798 | pub fn forget_aligned(self) -> Ptr<'a, T, (I::Aliasing, Unaligned, I::Validity)> {
|
| 799 | |
| 800 | unsafe { self.assume_invariants() }
|
| 801 | }
|
| 802 | }
|
| 803 | }
|
| 804 |
|
| 805 | |
| 806 | #[cfg_attr(not(zerocopy_unstable_ptr), allow(unreachable_pub))]
|
| 807 | pub use _casts::TryWithError;
|
| 808 | mod _casts {
|
| 809 | use core::cell::UnsafeCell;
|
| 810 |
|
| 811 | use super::*;
|
| 812 | use crate::{
|
| 813 | pointer::cast::{AsBytesCast, Cast},
|
| 814 | HasTag, ProjectField,
|
| 815 | };
|
| 816 |
|
| 817 | impl<'a, T, I> Ptr<'a, T, I>
|
| 818 | where
|
| 819 | T: 'a + ?Sized,
|
| 820 | I: Invariants,
|
| 821 | {
|
| 822 | |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | #[inline]
|
| 833 | #[must_use]
|
| 834 | pub unsafe fn cast_unchecked<U, C: Cast<T, U>>(
|
| 835 | self,
|
| 836 | ) -> Ptr<'a, U, (I::Aliasing, Unaligned, I::Validity)>
|
| 837 | where
|
| 838 | U: 'a + CastableFrom<T, I::Validity, I::Validity> + ?Sized,
|
| 839 | {
|
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | unsafe { self.project_transmute_unchecked::<_, _, C>() }
|
| 855 | }
|
| 856 |
|
| 857 | |
| 858 | #[inline]
|
| 859 | #[must_use]
|
| 860 | pub fn cast<U, C, R>(self) -> Ptr<'a, U, (I::Aliasing, Unaligned, I::Validity)>
|
| 861 | where
|
| 862 | T: MutationCompatible<U, I::Aliasing, I::Validity, I::Validity, R>,
|
| 863 | U: 'a + ?Sized + CastableFrom<T, I::Validity, I::Validity>,
|
| 864 | C: Cast<T, U>,
|
| 865 | {
|
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | unsafe { self.cast_unchecked::<_, C>() }
|
| 875 | }
|
| 876 |
|
| 877 | #[inline(always)]
|
| 878 | pub fn project<F, const VARIANT_ID: i128, const FIELD_ID: i128>(
|
| 879 | mut self,
|
| 880 | ) -> Result<Ptr<'a, T::Type, T::Invariants>, T::Error>
|
| 881 | where
|
| 882 | T: ProjectField<F, I, VARIANT_ID, FIELD_ID>,
|
| 883 | I::Aliasing: Reference,
|
| 884 | {
|
| 885 | use crate::pointer::cast::Projection;
|
| 886 | match T::is_projectable(self.reborrow().project_tag()) {
|
| 887 | Ok(()) => {
|
| 888 | let inner = self.as_inner();
|
| 889 | let projected = inner.project::<_, Projection<F, VARIANT_ID, FIELD_ID>>();
|
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | Ok(unsafe { Ptr::from_inner(projected) })
|
| 904 | }
|
| 905 | Err(err) => Err(err),
|
| 906 | }
|
| 907 | }
|
| 908 |
|
| 909 | #[must_use]
|
| 910 | #[inline(always)]
|
| 911 | pub(crate) fn project_tag(self) -> Ptr<'a, T::Tag, I>
|
| 912 | where
|
| 913 | T: HasTag,
|
| 914 | {
|
| 915 | |
| 916 | |
| 917 | let tag = unsafe { self.project_transmute_unchecked::<_, _, T::ProjectToTag>() };
|
| 918 | |
| 919 | |
| 920 | let tag = unsafe { tag.assume_alignment() };
|
| 921 | tag.unify_invariants()
|
| 922 | }
|
| 923 |
|
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | |
| 929 | |
| 930 | |
| 931 | #[inline(always)]
|
| 932 | pub(crate) unsafe fn try_with_unchecked<U, J, E, F>(
|
| 933 | self,
|
| 934 | f: F,
|
| 935 | ) -> Result<Ptr<'a, U, J>, E::Mapped>
|
| 936 | where
|
| 937 | U: 'a + ?Sized,
|
| 938 | J: Invariants<Aliasing = I::Aliasing>,
|
| 939 | E: TryWithError<Self>,
|
| 940 | F: FnOnce(Ptr<'a, T, I>) -> Result<Ptr<'a, U, J>, E>,
|
| 941 | {
|
| 942 | let old_inner = self.as_inner();
|
| 943 | #[rustfmt::skip]
|
| 944 | let res = f(self).map_err(#[inline(always)] move |err: E| {
|
| 945 | err.map(#[inline(always)] |src| {
|
| 946 | drop(src);
|
| 947 |
|
| 948 | |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | |
| 960 | |
| 961 | |
| 962 | |
| 963 | |
| 964 | |
| 965 | |
| 966 | |
| 967 | |
| 968 | |
| 969 | |
| 970 | |
| 971 | |
| 972 | |
| 973 | unsafe { Ptr::from_inner(old_inner) }
|
| 974 | })
|
| 975 | });
|
| 976 | res
|
| 977 | }
|
| 978 |
|
| 979 | |
| 980 | |
| 981 | #[inline(always)]
|
| 982 | pub fn try_with<U, J, E, F>(self, f: F) -> Result<Ptr<'a, U, J>, E::Mapped>
|
| 983 | where
|
| 984 | U: 'a + ?Sized,
|
| 985 | J: Invariants<Aliasing = I::Aliasing>,
|
| 986 | E: TryWithError<Self>,
|
| 987 | F: FnOnce(Ptr<'a, T, I>) -> Result<Ptr<'a, U, J>, E>,
|
| 988 | I: Invariants<Aliasing = Shared>,
|
| 989 | {
|
| 990 | |
| 991 | |
| 992 | unsafe { self.try_with_unchecked(f) }
|
| 993 | }
|
| 994 | }
|
| 995 |
|
| 996 | |
| 997 | |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | pub unsafe trait TryWithError<MappedInner> {
|
| 1004 | type Inner;
|
| 1005 | type Mapped;
|
| 1006 | fn map<F: FnOnce(Self::Inner) -> MappedInner>(self, f: F) -> Self::Mapped;
|
| 1007 | }
|
| 1008 |
|
| 1009 | impl<'a, T, I> Ptr<'a, T, I>
|
| 1010 | where
|
| 1011 | T: 'a + KnownLayout + ?Sized,
|
| 1012 | I: Invariants,
|
| 1013 | {
|
| 1014 | |
| 1015 | #[allow(clippy::wrong_self_convention)]
|
| 1016 | #[must_use]
|
| 1017 | #[inline]
|
| 1018 | pub fn as_bytes<R>(self) -> Ptr<'a, [u8], (I::Aliasing, Aligned, Valid)>
|
| 1019 | where
|
| 1020 | [u8]: TransmuteFromPtr<T, I::Aliasing, I::Validity, Valid, AsBytesCast, R>,
|
| 1021 | {
|
| 1022 | self.transmute_with::<[u8], Valid, AsBytesCast, _>().bikeshed_recall_aligned()
|
| 1023 | }
|
| 1024 | }
|
| 1025 |
|
| 1026 | impl<'a, T, I, const N: usize> Ptr<'a, [T; N], I>
|
| 1027 | where
|
| 1028 | T: 'a,
|
| 1029 | I: Invariants,
|
| 1030 | {
|
| 1031 | |
| 1032 | #[allow(clippy::wrong_self_convention)]
|
| 1033 | #[inline]
|
| 1034 | #[must_use]
|
| 1035 | pub fn as_slice(self) -> Ptr<'a, [T], I> {
|
| 1036 | let slice = self.as_inner().as_slice();
|
| 1037 | |
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | |
| 1044 | |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | |
| 1055 | |
| 1056 | |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | |
| 1064 | |
| 1065 | unsafe { Ptr::from_inner(slice) }
|
| 1066 | }
|
| 1067 | }
|
| 1068 |
|
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | impl<'a, I> Ptr<'a, [u8], I>
|
| 1073 | where
|
| 1074 | I: Invariants<Validity = Valid>,
|
| 1075 | {
|
| 1076 | |
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | |
| 1081 | |
| 1082 | |
| 1083 | |
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | |
| 1094 | |
| 1095 | |
| 1096 | |
| 1097 | |
| 1098 | #[inline(always)]
|
| 1099 | pub fn try_cast_into<U, R>(
|
| 1100 | self,
|
| 1101 | cast_type: CastType,
|
| 1102 | meta: Option<U::PointerMetadata>,
|
| 1103 | ) -> Result<
|
| 1104 | (Ptr<'a, U, (I::Aliasing, Aligned, Initialized)>, Ptr<'a, [u8], I>),
|
| 1105 | CastError<Self, U>,
|
| 1106 | >
|
| 1107 | where
|
| 1108 | I::Aliasing: Reference,
|
| 1109 | U: 'a + ?Sized + KnownLayout + Read<I::Aliasing, R>,
|
| 1110 | {
|
| 1111 | let (inner, remainder) = self.as_inner().try_cast_into(cast_type, meta).map_err(
|
| 1112 | #[inline(always)]
|
| 1113 | |err| {
|
| 1114 | err.map_src(
|
| 1115 | #[inline(always)]
|
| 1116 | |inner|
|
| 1117 | |
| 1118 | |
| 1119 | |
| 1120 | |
| 1121 | unsafe { Ptr::from_inner(inner) },
|
| 1122 | )
|
| 1123 | },
|
| 1124 | )?;
|
| 1125 |
|
| 1126 | |
| 1127 | |
| 1128 | |
| 1129 | |
| 1130 | |
| 1131 | |
| 1132 | |
| 1133 | |
| 1134 | |
| 1135 | |
| 1136 | |
| 1137 | |
| 1138 | |
| 1139 | |
| 1140 | |
| 1141 | |
| 1142 | let res = unsafe { Ptr::from_inner(inner) };
|
| 1143 |
|
| 1144 | |
| 1145 | |
| 1146 | |
| 1147 | |
| 1148 | |
| 1149 | |
| 1150 | |
| 1151 | |
| 1152 | |
| 1153 | |
| 1154 | let remainder = unsafe { Ptr::from_inner(remainder) };
|
| 1155 |
|
| 1156 | Ok((res, remainder))
|
| 1157 | }
|
| 1158 |
|
| 1159 | |
| 1160 | |
| 1161 | |
| 1162 | |
| 1163 | |
| 1164 | |
| 1165 | |
| 1166 | |
| 1167 | |
| 1168 | |
| 1169 | #[allow(unused)]
|
| 1170 | #[inline(always)]
|
| 1171 | pub fn try_cast_into_no_leftover<U, R>(
|
| 1172 | self,
|
| 1173 | meta: Option<U::PointerMetadata>,
|
| 1174 | ) -> Result<Ptr<'a, U, (I::Aliasing, Aligned, Initialized)>, CastError<Self, U>>
|
| 1175 | where
|
| 1176 | I::Aliasing: Reference,
|
| 1177 | U: 'a + ?Sized + KnownLayout + Read<I::Aliasing, R>,
|
| 1178 | [u8]: Read<I::Aliasing, R>,
|
| 1179 | {
|
| 1180 | |
| 1181 | unsafe {
|
| 1182 | self.try_with_unchecked(
|
| 1183 | #[inline(always)]
|
| 1184 | |slf| match slf.try_cast_into(CastType::Prefix, meta) {
|
| 1185 | Ok((slf, remainder)) => {
|
| 1186 | if remainder.is_empty() {
|
| 1187 | Ok(slf)
|
| 1188 | } else {
|
| 1189 | Err(CastError::Size(SizeError::<_, U>::new(())))
|
| 1190 | }
|
| 1191 | }
|
| 1192 | Err(err) => Err(err.map_src(
|
| 1193 | #[inline(always)]
|
| 1194 | |_slf| (),
|
| 1195 | )),
|
| 1196 | },
|
| 1197 | )
|
| 1198 | }
|
| 1199 | }
|
| 1200 | }
|
| 1201 |
|
| 1202 | impl<'a, T, I> Ptr<'a, UnsafeCell<T>, I>
|
| 1203 | where
|
| 1204 | T: 'a + ?Sized,
|
| 1205 | I: Invariants<Aliasing = Exclusive>,
|
| 1206 | {
|
| 1207 | |
| 1208 | |
| 1209 | |
| 1210 | |
| 1211 | |
| 1212 | |
| 1213 | |
| 1214 | |
| 1215 | #[must_use]
|
| 1216 | #[inline(always)]
|
| 1217 | pub fn get_mut(self) -> Ptr<'a, T, I> {
|
| 1218 | |
| 1219 | |
| 1220 | |
| 1221 | define_cast!(unsafe { Cast<T: ?Sized> = UnsafeCell<T> => T });
|
| 1222 |
|
| 1223 | |
| 1224 | |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 | |
| 1229 | |
| 1230 | |
| 1231 | |
| 1232 | |
| 1233 | |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | |
| 1238 | |
| 1239 | |
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | |
| 1244 | let ptr = unsafe { self.project_transmute_unchecked::<_, _, Cast>() };
|
| 1245 |
|
| 1246 | |
| 1247 | |
| 1248 | |
| 1249 | |
| 1250 | |
| 1251 | |
| 1252 | |
| 1253 | |
| 1254 | |
| 1255 | let ptr = unsafe { ptr.assume_alignment::<I::Alignment>() };
|
| 1256 | ptr.unify_invariants()
|
| 1257 | }
|
| 1258 | }
|
| 1259 | }
|
| 1260 |
|
| 1261 | |
| 1262 | mod _project {
|
| 1263 | use super::*;
|
| 1264 |
|
| 1265 | impl<'a, T, I> Ptr<'a, [T], I>
|
| 1266 | where
|
| 1267 | T: 'a,
|
| 1268 | I: Invariants,
|
| 1269 | I::Aliasing: Reference,
|
| 1270 | {
|
| 1271 | |
| 1272 | #[inline]
|
| 1273 | pub fn iter(self) -> impl Iterator<Item = Ptr<'a, T, I>> {
|
| 1274 | |
| 1275 | |
| 1276 | |
| 1277 | |
| 1278 | |
| 1279 | |
| 1280 | |
| 1281 | |
| 1282 | |
| 1283 | |
| 1284 | |
| 1285 | |
| 1286 | |
| 1287 | |
| 1288 | |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | |
| 1293 | |
| 1294 | |
| 1295 | |
| 1296 | |
| 1297 | |
| 1298 | self.as_inner().iter().map(
|
| 1299 | #[inline(always)]
|
| 1300 | |elem| unsafe { Ptr::from_inner(elem) },
|
| 1301 | )
|
| 1302 | }
|
| 1303 | }
|
| 1304 |
|
| 1305 | #[allow(clippy::needless_lifetimes)]
|
| 1306 | impl<'a, T, I> Ptr<'a, T, I>
|
| 1307 | where
|
| 1308 | T: 'a + ?Sized + KnownLayout<PointerMetadata = usize>,
|
| 1309 | I: Invariants,
|
| 1310 | {
|
| 1311 | |
| 1312 | #[inline]
|
| 1313 | #[must_use]
|
| 1314 | pub fn len(&self) -> usize {
|
| 1315 | self.as_inner().meta().get()
|
| 1316 | }
|
| 1317 |
|
| 1318 | |
| 1319 | #[inline]
|
| 1320 | #[must_use]
|
| 1321 | pub fn is_empty(&self) -> bool {
|
| 1322 | self.len() == 0
|
| 1323 | }
|
| 1324 | }
|
| 1325 | }
|
| 1326 |
|
| 1327 | #[cfg(test)]
|
| 1328 | mod tests {
|
| 1329 | use core::mem::{self, MaybeUninit};
|
| 1330 |
|
| 1331 | use super::*;
|
| 1332 | #[allow(unused)] |
| 1333 | use crate::util::AsAddress;
|
| 1334 | use crate::{pointer::BecauseImmutable, util::testutil::AU64, FromBytes, Immutable};
|
| 1335 |
|
| 1336 | mod test_ptr_try_cast_into_soundness {
|
| 1337 | use super::*;
|
| 1338 |
|
| 1339 | |
| 1340 | |
| 1341 |
|
| 1342 | |
| 1343 | |
| 1344 | |
| 1345 | |
| 1346 | fn test<T, I, const N: usize>(metas: I)
|
| 1347 | where
|
| 1348 | T: ?Sized + KnownLayout + Immutable + FromBytes,
|
| 1349 | I: IntoIterator<Item = Option<T::PointerMetadata>> + Clone,
|
| 1350 | {
|
| 1351 | let mut bytes = [MaybeUninit::<u8>::uninit(); N];
|
| 1352 | let initialized = [MaybeUninit::new(0u8); N];
|
| 1353 | for start in 0..=bytes.len() {
|
| 1354 | for end in start..=bytes.len() {
|
| 1355 | |
| 1356 | |
| 1357 | |
| 1358 | |
| 1359 | |
| 1360 | |
| 1361 | bytes = [MaybeUninit::<u8>::uninit(); N];
|
| 1362 | let bytes = &mut bytes[start..end];
|
| 1363 | |
| 1364 | |
| 1365 | bytes.copy_from_slice(&initialized[start..end]);
|
| 1366 |
|
| 1367 | let bytes = {
|
| 1368 | let bytes: *const [MaybeUninit<u8>] = bytes;
|
| 1369 | #[allow(clippy::as_conversions)]
|
| 1370 | let bytes = bytes as *const [u8];
|
| 1371 | |
| 1372 | |
| 1373 | unsafe { &*bytes }
|
| 1374 | };
|
| 1375 |
|
| 1376 | |
| 1377 | unsafe fn validate_and_get_len<
|
| 1378 | T: ?Sized + KnownLayout + FromBytes + Immutable,
|
| 1379 | >(
|
| 1380 | slf: Ptr<'_, T, (Shared, Aligned, Initialized)>,
|
| 1381 | ) -> usize {
|
| 1382 | let t = slf.recall_validity().as_ref();
|
| 1383 |
|
| 1384 | let bytes = {
|
| 1385 | let len = mem::size_of_val(t);
|
| 1386 | let t: *const T = t;
|
| 1387 | |
| 1388 | |
| 1389 | |
| 1390 | |
| 1391 | |
| 1392 | |
| 1393 | |
| 1394 | |
| 1395 | |
| 1396 | |
| 1397 | |
| 1398 | unsafe { core::slice::from_raw_parts(t.cast::<u8>(), len) }
|
| 1399 | };
|
| 1400 |
|
| 1401 | |
| 1402 | |
| 1403 | |
| 1404 | |
| 1405 | |
| 1406 | assert_eq!(bytes, vec![0u8; bytes.len()]);
|
| 1407 |
|
| 1408 | mem::size_of_val(t)
|
| 1409 | }
|
| 1410 |
|
| 1411 | for meta in metas.clone().into_iter() {
|
| 1412 | for cast_type in [CastType::Prefix, CastType::Suffix] {
|
| 1413 | if let Ok((slf, remaining)) = Ptr::from_ref(bytes)
|
| 1414 | .try_cast_into::<T, BecauseImmutable>(cast_type, meta)
|
| 1415 | {
|
| 1416 | |
| 1417 | |
| 1418 | let len = unsafe { validate_and_get_len(slf) };
|
| 1419 | assert_eq!(remaining.len(), bytes.len() - len);
|
| 1420 | #[allow(unstable_name_collisions)]
|
| 1421 | let bytes_addr = bytes.as_ptr().addr();
|
| 1422 | #[allow(unstable_name_collisions)]
|
| 1423 | let remaining_addr = remaining.as_inner().as_ptr().addr();
|
| 1424 | match cast_type {
|
| 1425 | CastType::Prefix => {
|
| 1426 | assert_eq!(remaining_addr, bytes_addr + len)
|
| 1427 | }
|
| 1428 | CastType::Suffix => assert_eq!(remaining_addr, bytes_addr),
|
| 1429 | }
|
| 1430 |
|
| 1431 | if let Some(want) = meta {
|
| 1432 | let got =
|
| 1433 | KnownLayout::pointer_to_metadata(slf.as_inner().as_ptr());
|
| 1434 | assert_eq!(got, want);
|
| 1435 | }
|
| 1436 | }
|
| 1437 | }
|
| 1438 |
|
| 1439 | if let Ok(slf) = Ptr::from_ref(bytes)
|
| 1440 | .try_cast_into_no_leftover::<T, BecauseImmutable>(meta)
|
| 1441 | {
|
| 1442 | |
| 1443 | |
| 1444 | let len = unsafe { validate_and_get_len(slf) };
|
| 1445 | assert_eq!(len, bytes.len());
|
| 1446 |
|
| 1447 | if let Some(want) = meta {
|
| 1448 | let got = KnownLayout::pointer_to_metadata(slf.as_inner().as_ptr());
|
| 1449 | assert_eq!(got, want);
|
| 1450 | }
|
| 1451 | }
|
| 1452 | }
|
| 1453 | }
|
| 1454 | }
|
| 1455 | }
|
| 1456 |
|
| 1457 | #[derive(FromBytes, KnownLayout, Immutable)]
|
| 1458 | #[repr(C)]
|
| 1459 | struct SliceDst<T> {
|
| 1460 | a: u8,
|
| 1461 | trailing: [T],
|
| 1462 | }
|
| 1463 |
|
| 1464 | |
| 1465 | |
| 1466 | |
| 1467 | |
| 1468 | |
| 1469 | |
| 1470 | |
| 1471 | |
| 1472 | |
| 1473 | |
| 1474 | macro_rules! test {
|
| 1475 | ($test_name:ident: $ty:ty) => {
|
| 1476 | #[test]
|
| 1477 | #[allow(non_snake_case)]
|
| 1478 | fn $test_name() {
|
| 1479 | const S: usize = core::mem::size_of::<$ty>();
|
| 1480 | const N: usize = if S == 0 { 4 } else { S * 4 };
|
| 1481 | test::<$ty, _, N>([None]);
|
| 1482 |
|
| 1483 | |
| 1484 | |
| 1485 | |
| 1486 | if S == 0 {
|
| 1487 | test::<[$ty], _, N>([Some(0), Some(1), Some(2), Some(3)]);
|
| 1488 | test::<SliceDst<$ty>, _, N>([Some(0), Some(1), Some(2), Some(3)]);
|
| 1489 | } else {
|
| 1490 | test::<[$ty], _, N>([None, Some(0), Some(1), Some(2), Some(3)]);
|
| 1491 | test::<SliceDst<$ty>, _, N>([None, Some(0), Some(1), Some(2), Some(3)]);
|
| 1492 | }
|
| 1493 | }
|
| 1494 | };
|
| 1495 | ($ty:ident) => {
|
| 1496 | test!($ty: $ty);
|
| 1497 | };
|
| 1498 | ($($ty:ident),*) => { $(test!($ty);)* }
|
| 1499 | }
|
| 1500 |
|
| 1501 | test!(empty_tuple: ());
|
| 1502 | test!(u8, u16, u32, u64, usize, AU64);
|
| 1503 | test!(i8, i16, i32, i64, isize);
|
| 1504 | test!(f32, f64);
|
| 1505 | }
|
| 1506 |
|
| 1507 | #[test]
|
| 1508 | fn test_try_cast_into_explicit_count() {
|
| 1509 | macro_rules! test {
|
| 1510 | ($ty:ty, $bytes:expr, $elems:expr, $expect:expr) => {{
|
| 1511 | let bytes = [0u8; $bytes];
|
| 1512 | let ptr = Ptr::from_ref(&bytes[..]);
|
| 1513 | let res =
|
| 1514 | ptr.try_cast_into::<$ty, BecauseImmutable>(CastType::Prefix, Some($elems));
|
| 1515 | if let Some(expect) = $expect {
|
| 1516 | let (ptr, _) = res.unwrap();
|
| 1517 | assert_eq!(KnownLayout::pointer_to_metadata(ptr.as_inner().as_ptr()), expect);
|
| 1518 | } else {
|
| 1519 | let _ = res.unwrap_err();
|
| 1520 | }
|
| 1521 | }};
|
| 1522 | }
|
| 1523 |
|
| 1524 | #[derive(KnownLayout, Immutable)]
|
| 1525 | #[repr(C)]
|
| 1526 | struct ZstDst {
|
| 1527 | u: [u8; 8],
|
| 1528 | slc: [()],
|
| 1529 | }
|
| 1530 |
|
| 1531 | test!(ZstDst, 8, 0, Some(0));
|
| 1532 | test!(ZstDst, 7, 0, None);
|
| 1533 |
|
| 1534 | test!(ZstDst, 8, usize::MAX, Some(usize::MAX));
|
| 1535 | test!(ZstDst, 7, usize::MAX, None);
|
| 1536 |
|
| 1537 | #[derive(KnownLayout, Immutable)]
|
| 1538 | #[repr(C)]
|
| 1539 | struct Dst {
|
| 1540 | u: [u8; 8],
|
| 1541 | slc: [u8],
|
| 1542 | }
|
| 1543 |
|
| 1544 | test!(Dst, 8, 0, Some(0));
|
| 1545 | test!(Dst, 7, 0, None);
|
| 1546 |
|
| 1547 | test!(Dst, 9, 1, Some(1));
|
| 1548 | test!(Dst, 8, 1, None);
|
| 1549 |
|
| 1550 | |
| 1551 | |
| 1552 | |
| 1553 | test!(Dst, 8, usize::MAX - 8 + 1, None);
|
| 1554 | }
|
| 1555 |
|
| 1556 | #[test]
|
| 1557 | fn test_try_cast_into_no_leftover_restores_original_slice() {
|
| 1558 | let bytes = [0u8; 4];
|
| 1559 | let ptr = Ptr::from_ref(&bytes[..]);
|
| 1560 | let res = ptr.try_cast_into_no_leftover::<[u8; 2], BecauseImmutable>(None);
|
| 1561 | match res {
|
| 1562 | Ok(_) => panic!("should have failed due to leftover bytes"),
|
| 1563 | Err(CastError::Size(e)) => {
|
| 1564 | assert_eq!(e.into_src().len(), 4, "Should return original slice length");
|
| 1565 | }
|
| 1566 | Err(e) => panic!("wrong error type: {:?}", e),
|
| 1567 | }
|
| 1568 | }
|
| 1569 |
|
| 1570 | #[test]
|
| 1571 | fn test_iter_exclusive_yields_disjoint_ptrs() {
|
| 1572 | let mut arr = [0u8, 1, 2, 3];
|
| 1573 |
|
| 1574 | {
|
| 1575 | let mut iter = Ptr::from_mut(&mut arr[..]).iter();
|
| 1576 | let first = iter.next().unwrap().as_mut();
|
| 1577 | let second = iter.next().unwrap().as_mut();
|
| 1578 |
|
| 1579 | *first = 10;
|
| 1580 | *second = 20;
|
| 1581 | *first = 30;
|
| 1582 | }
|
| 1583 |
|
| 1584 | assert_eq!(arr, [30, 20, 2, 3]);
|
| 1585 | }
|
| 1586 | }
|