| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | use super::*;
|
| 12 | use crate::pointer::{
|
| 13 | BecauseInvariantsEq, BecauseMutationCompatible, MutationCompatible, TransmuteFromPtr,
|
| 14 | };
|
| 15 |
|
| 16 | mod def {
|
| 17 | use core::marker::PhantomData;
|
| 18 |
|
| 19 | use crate::{
|
| 20 | ByteSlice, ByteSliceMut, CloneableByteSlice, CopyableByteSlice, IntoByteSlice,
|
| 21 | IntoByteSliceMut,
|
| 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 | pub struct Ref<B, T: ?Sized>(
|
| 63 | ) byte
|
| 64 | |
| 65 | |
| 66 | B,
|
| 67 | PhantomData<T>,
|
| 68 | );
|
| 69 |
|
| 70 | impl<B, T: ?Sized> Ref<B, T> {
|
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | pub(crate) unsafe fn new_unchecked(bytes: B) -> Ref<B, T> {
|
| 83 | |
| 84 | |
| 85 | Ref(bytes, PhantomData)
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | impl<B: ByteSlice, T: ?Sized> Ref<B, T> {
|
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | pub(crate) unsafe fn as_byte_slice(&self) -> &impl ByteSlice {
|
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | &self.0
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | impl<B: ByteSliceMut, T: ?Sized> Ref<B, T> {
|
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | pub(crate) unsafe fn as_byte_slice_mut(&mut self) -> &mut impl ByteSliceMut {
|
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | &mut self.0
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | impl<'a, B: IntoByteSlice<'a>, T: ?Sized> Ref<B, T> {
|
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | pub(crate) unsafe fn into_byte_slice(self) -> impl IntoByteSlice<'a> {
|
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | self.0
|
| 155 | }
|
| 156 | }
|
| 157 |
|
| 158 | impl<'a, B: IntoByteSliceMut<'a>, T: ?Sized> Ref<B, T> {
|
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | pub(crate) unsafe fn into_byte_slice_mut(self) -> impl IntoByteSliceMut<'a> {
|
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | self.0
|
| 178 | }
|
| 179 | }
|
| 180 |
|
| 181 | impl<B: CloneableByteSlice + Clone, T: ?Sized> Clone for Ref<B, T> {
|
| 182 | #[inline]
|
| 183 | fn clone(&self) -> Ref<B, T> {
|
| 184 | |
| 185 | |
| 186 | |
| 187 | Ref(self.0.clone(), PhantomData)
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | |
| 192 | |
| 193 | |
| 194 | impl<B: CopyableByteSlice + Copy, T: ?Sized> Copy for Ref<B, T> {}
|
| 195 | }
|
| 196 |
|
| 197 | #[allow(unreachable_pub)] |
| 198 | pub use def::Ref;
|
| 199 |
|
| 200 | use crate::pointer::{
|
| 201 | invariant::{Aligned, BecauseExclusive, Initialized, Unaligned, Valid},
|
| 202 | BecauseRead, PtrInner,
|
| 203 | };
|
| 204 |
|
| 205 | impl<B, T> Ref<B, T>
|
| 206 | where
|
| 207 | B: ByteSlice,
|
| 208 | {
|
| 209 | #[must_use = "has no side effects"]
|
| 210 | pub(crate) fn sized_from(bytes: B) -> Result<Ref<B, T>, CastError<B, T>> {
|
| 211 | if bytes.len() != mem::size_of::<T>() {
|
| 212 | return Err(SizeError::new(bytes).into());
|
| 213 | }
|
| 214 | if let Err(err) = util::validate_aligned_to::<_, T>(bytes.deref()) {
|
| 215 | return Err(err.with_src(bytes).into());
|
| 216 | }
|
| 217 |
|
| 218 | |
| 219 | Ok(unsafe { Ref::new_unchecked(bytes) })
|
| 220 | }
|
| 221 | }
|
| 222 |
|
| 223 | impl<B, T> Ref<B, T>
|
| 224 | where
|
| 225 | B: SplitByteSlice,
|
| 226 | {
|
| 227 | #[must_use = "has no side effects"]
|
| 228 | pub(crate) fn sized_from_prefix(bytes: B) -> Result<(Ref<B, T>, B), CastError<B, T>> {
|
| 229 | if bytes.len() < mem::size_of::<T>() {
|
| 230 | return Err(SizeError::new(bytes).into());
|
| 231 | }
|
| 232 | if let Err(err) = util::validate_aligned_to::<_, T>(bytes.deref()) {
|
| 233 | return Err(err.with_src(bytes).into());
|
| 234 | }
|
| 235 | let (bytes, suffix) = bytes.split_at(mem::size_of::<T>()).map_err(
|
| 236 | #[inline(always)]
|
| 237 | |b| SizeError::new(b).into(),
|
| 238 | )?;
|
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | let r = unsafe { Ref::new_unchecked(bytes) };
|
| 245 | Ok((r, suffix))
|
| 246 | }
|
| 247 |
|
| 248 | #[must_use = "has no side effects"]
|
| 249 | pub(crate) fn sized_from_suffix(bytes: B) -> Result<(B, Ref<B, T>), CastError<B, T>> {
|
| 250 | let bytes_len = bytes.len();
|
| 251 | let split_at = if let Some(split_at) = bytes_len.checked_sub(mem::size_of::<T>()) {
|
| 252 | split_at
|
| 253 | } else {
|
| 254 | return Err(SizeError::new(bytes).into());
|
| 255 | };
|
| 256 | let (prefix, bytes) = bytes.split_at(split_at).map_err(|b| SizeError::new(b).into())?;
|
| 257 | if let Err(err) = util::validate_aligned_to::<_, T>(bytes.deref()) {
|
| 258 | return Err(err.with_src(bytes).into());
|
| 259 | }
|
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | let r = unsafe { Ref::new_unchecked(bytes) };
|
| 267 | Ok((prefix, r))
|
| 268 | }
|
| 269 | }
|
| 270 |
|
| 271 | impl<B, T> Ref<B, T>
|
| 272 | where
|
| 273 | B: ByteSlice,
|
| 274 | T: KnownLayout + Immutable + ?Sized,
|
| 275 | {
|
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | #[must_use = "has no side effects"]
|
| 310 | #[inline]
|
| 311 | pub fn from_bytes(source: B) -> Result<Ref<B, T>, CastError<B, T>> {
|
| 312 | static_assert_dst_is_not_zst!(T);
|
| 313 | if let Err(e) =
|
| 314 | Ptr::from_ref(source.deref()).try_cast_into_no_leftover::<T, BecauseImmutable>(None)
|
| 315 | {
|
| 316 | return Err(e.with_src(()).with_src(source));
|
| 317 | }
|
| 318 | |
| 319 | Ok(unsafe { Ref::new_unchecked(source) })
|
| 320 | }
|
| 321 | }
|
| 322 |
|
| 323 | impl<B, T> Ref<B, T>
|
| 324 | where
|
| 325 | B: SplitByteSlice,
|
| 326 | T: KnownLayout + Immutable + ?Sized,
|
| 327 | {
|
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | #[must_use = "has no side effects"]
|
| 364 | #[inline]
|
| 365 | pub fn from_prefix(source: B) -> Result<(Ref<B, T>, B), CastError<B, T>> {
|
| 366 | static_assert_dst_is_not_zst!(T);
|
| 367 | let remainder = match Ptr::from_ref(source.deref())
|
| 368 | .try_cast_into::<T, BecauseImmutable>(CastType::Prefix, None)
|
| 369 | {
|
| 370 | Ok((_, remainder)) => remainder,
|
| 371 | Err(e) => {
|
| 372 | return Err(e.with_src(()).with_src(source));
|
| 373 | }
|
| 374 | };
|
| 375 |
|
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | #[allow(unstable_name_collisions)]
|
| 382 | let split_at = unsafe { source.len().unchecked_sub(remainder.len()) };
|
| 383 | let (bytes, suffix) = source.split_at(split_at).map_err(|b| SizeError::new(b).into())?;
|
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | let r = unsafe { Ref::new_unchecked(bytes) };
|
| 389 | Ok((r, suffix))
|
| 390 | }
|
| 391 |
|
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 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 | |
| 426 | |
| 427 | |
| 428 | #[must_use = "has no side effects"]
|
| 429 | #[inline]
|
| 430 | pub fn from_suffix(source: B) -> Result<(B, Ref<B, T>), CastError<B, T>> {
|
| 431 | static_assert_dst_is_not_zst!(T);
|
| 432 | let remainder = match Ptr::from_ref(source.deref())
|
| 433 | .try_cast_into::<T, BecauseImmutable>(CastType::Suffix, None)
|
| 434 | {
|
| 435 | Ok((_, remainder)) => remainder,
|
| 436 | Err(e) => {
|
| 437 | let e = e.with_src(());
|
| 438 | return Err(e.with_src(source));
|
| 439 | }
|
| 440 | };
|
| 441 |
|
| 442 | let split_at = remainder.len();
|
| 443 | let (prefix, bytes) = source.split_at(split_at).map_err(|b| SizeError::new(b).into())?;
|
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | let r = unsafe { Ref::new_unchecked(bytes) };
|
| 449 | Ok((prefix, r))
|
| 450 | }
|
| 451 | }
|
| 452 |
|
| 453 | impl<B, T> Ref<B, T>
|
| 454 | where
|
| 455 | B: ByteSlice,
|
| 456 | T: KnownLayout<PointerMetadata = usize> + Immutable + ?Sized,
|
| 457 | {
|
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | #[inline]
|
| 491 | pub fn from_bytes_with_elems(source: B, count: usize) -> Result<Ref<B, T>, CastError<B, T>> {
|
| 492 | static_assert_dst_is_not_zst!(T);
|
| 493 | let expected_len = match T::size_for_metadata(count) {
|
| 494 | Some(len) => len,
|
| 495 | None => return Err(SizeError::new(source).into()),
|
| 496 | };
|
| 497 | if source.len() != expected_len {
|
| 498 | return Err(SizeError::new(source).into());
|
| 499 | }
|
| 500 | Self::from_bytes(source)
|
| 501 | }
|
| 502 | }
|
| 503 |
|
| 504 | impl<B, T> Ref<B, T>
|
| 505 | where
|
| 506 | B: SplitByteSlice,
|
| 507 | T: KnownLayout<PointerMetadata = usize> + Immutable + ?Sized,
|
| 508 | {
|
| 509 | |
| 510 | |
| 511 | |
| 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 | #[inline]
|
| 542 | pub fn from_prefix_with_elems(
|
| 543 | source: B,
|
| 544 | count: usize,
|
| 545 | ) -> Result<(Ref<B, T>, B), CastError<B, T>> {
|
| 546 | static_assert_dst_is_not_zst!(T);
|
| 547 | let expected_len = match T::size_for_metadata(count) {
|
| 548 | Some(len) => len,
|
| 549 | None => return Err(SizeError::new(source).into()),
|
| 550 | };
|
| 551 | let (prefix, bytes) = source.split_at(expected_len).map_err(SizeError::new)?;
|
| 552 | Self::from_bytes(prefix).map(move |l| (l, bytes))
|
| 553 | }
|
| 554 |
|
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | #[inline]
|
| 588 | pub fn from_suffix_with_elems(
|
| 589 | source: B,
|
| 590 | count: usize,
|
| 591 | ) -> Result<(B, Ref<B, T>), CastError<B, T>> {
|
| 592 | static_assert_dst_is_not_zst!(T);
|
| 593 | let expected_len = match T::size_for_metadata(count) {
|
| 594 | Some(len) => len,
|
| 595 | None => return Err(SizeError::new(source).into()),
|
| 596 | };
|
| 597 | let split_at = if let Some(split_at) = source.len().checked_sub(expected_len) {
|
| 598 | split_at
|
| 599 | } else {
|
| 600 | return Err(SizeError::new(source).into());
|
| 601 | };
|
| 602 | |
| 603 | |
| 604 | let (bytes, suffix) = unsafe { source.split_at_unchecked(split_at) };
|
| 605 | Self::from_bytes(suffix).map(move |l| (bytes, l))
|
| 606 | }
|
| 607 | }
|
| 608 |
|
| 609 | impl<'a, B, T> Ref<B, T>
|
| 610 | where
|
| 611 | B: 'a + IntoByteSlice<'a>,
|
| 612 | T: FromBytes + KnownLayout + Immutable + ?Sized,
|
| 613 | {
|
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | #[must_use = "has no side effects"]
|
| 622 | #[inline(always)]
|
| 623 | pub fn into_ref(r: Self) -> &'a T {
|
| 624 | |
| 625 | static_assert_dst_is_not_zst!(T);
|
| 626 |
|
| 627 | |
| 628 | |
| 629 | let b = unsafe { r.into_byte_slice() };
|
| 630 | let b = b.into_byte_slice();
|
| 631 |
|
| 632 | if let crate::layout::SizeInfo::Sized { .. } = T::LAYOUT.size_info {
|
| 633 | let ptr = Ptr::from_ref(b);
|
| 634 | |
| 635 | |
| 636 | let ptr = unsafe { cast_for_sized::<T, _, _, _>(ptr) };
|
| 637 |
|
| 638 | |
| 639 | |
| 640 | |
| 641 | let ptr = unsafe { ptr.assume_alignment::<Aligned>() };
|
| 642 | return ptr.as_ref();
|
| 643 | }
|
| 644 |
|
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | let ptr = Ptr::from_ref(b.into_byte_slice())
|
| 650 | .try_cast_into_no_leftover::<T, BecauseImmutable>(None)
|
| 651 | .expect("zerocopy internal error: into_ref should be infallible");
|
| 652 | let ptr = ptr.recall_validity();
|
| 653 | ptr.as_ref()
|
| 654 | }
|
| 655 | }
|
| 656 |
|
| 657 | impl<'a, B, T> Ref<B, T>
|
| 658 | where
|
| 659 | B: 'a + IntoByteSliceMut<'a>,
|
| 660 | T: FromBytes + IntoBytes + KnownLayout + ?Sized,
|
| 661 | {
|
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | #[must_use = "has no side effects"]
|
| 670 | #[inline(always)]
|
| 671 | pub fn into_mut(r: Self) -> &'a mut T {
|
| 672 | |
| 673 | static_assert_dst_is_not_zst!(T);
|
| 674 |
|
| 675 | |
| 676 | |
| 677 | let b = unsafe { r.into_byte_slice_mut() };
|
| 678 | let b = b.into_byte_slice_mut();
|
| 679 |
|
| 680 | if let crate::layout::SizeInfo::Sized { .. } = T::LAYOUT.size_info {
|
| 681 | let ptr = Ptr::from_mut(b);
|
| 682 | |
| 683 | |
| 684 | let ptr = unsafe {
|
| 685 | cast_for_sized::<
|
| 686 | T,
|
| 687 | _,
|
| 688 | (BecauseRead, BecauseExclusive),
|
| 689 | (BecauseMutationCompatible, BecauseInvariantsEq),
|
| 690 | >(ptr)
|
| 691 | };
|
| 692 |
|
| 693 | |
| 694 | |
| 695 | |
| 696 | let ptr = unsafe { ptr.assume_alignment::<Aligned>() };
|
| 697 | return ptr.as_mut();
|
| 698 | }
|
| 699 |
|
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | let ptr = Ptr::from_mut(b.into_byte_slice_mut())
|
| 705 | .try_cast_into_no_leftover::<T, BecauseExclusive>(None)
|
| 706 | .expect("zerocopy internal error: into_ref should be infallible");
|
| 707 | let ptr = ptr.recall_validity::<_, (_, (_, _))>();
|
| 708 | ptr.as_mut()
|
| 709 | }
|
| 710 | }
|
| 711 |
|
| 712 | impl<B, T> Ref<B, T>
|
| 713 | where
|
| 714 | B: ByteSlice,
|
| 715 | T: ?Sized,
|
| 716 | {
|
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | #[inline]
|
| 723 | pub fn bytes(r: &Self) -> &[u8] {
|
| 724 | |
| 725 | |
| 726 | unsafe { r.as_byte_slice().deref() }
|
| 727 | }
|
| 728 | }
|
| 729 |
|
| 730 | impl<B, T> Ref<B, T>
|
| 731 | where
|
| 732 | B: ByteSliceMut,
|
| 733 | T: ?Sized,
|
| 734 | {
|
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | #[inline]
|
| 741 | pub fn bytes_mut(r: &mut Self) -> &mut [u8] {
|
| 742 | |
| 743 | |
| 744 | unsafe { r.as_byte_slice_mut().deref_mut() }
|
| 745 | }
|
| 746 | }
|
| 747 |
|
| 748 | impl<B, T> Ref<B, T>
|
| 749 | where
|
| 750 | B: ByteSlice,
|
| 751 | T: FromBytes,
|
| 752 | {
|
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | #[must_use = "has no side effects"]
|
| 759 | #[inline]
|
| 760 | pub fn read(r: &Self) -> T {
|
| 761 | |
| 762 | |
| 763 | let b = unsafe { r.as_byte_slice() };
|
| 764 |
|
| 765 | |
| 766 | |
| 767 | |
| 768 | |
| 769 | unsafe { ptr::read(b.deref().as_ptr().cast::<T>()) }
|
| 770 | }
|
| 771 | }
|
| 772 |
|
| 773 | impl<B, T> Ref<B, T>
|
| 774 | where
|
| 775 | B: ByteSliceMut,
|
| 776 | T: IntoBytes,
|
| 777 | {
|
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | #[inline]
|
| 784 | pub fn write(r: &mut Self, t: T) {
|
| 785 | |
| 786 | |
| 787 | let b = unsafe { r.as_byte_slice_mut() };
|
| 788 |
|
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | unsafe { ptr::write(b.deref_mut().as_mut_ptr().cast::<T>(), t) }
|
| 795 | }
|
| 796 | }
|
| 797 |
|
| 798 | impl<B, T> Deref for Ref<B, T>
|
| 799 | where
|
| 800 | B: ByteSlice,
|
| 801 | T: FromBytes + KnownLayout + Immutable + ?Sized,
|
| 802 | {
|
| 803 | type Target = T;
|
| 804 | #[inline]
|
| 805 | fn deref(&self) -> &T {
|
| 806 | |
| 807 | static_assert_dst_is_not_zst!(T);
|
| 808 |
|
| 809 | |
| 810 | |
| 811 | let b = unsafe { self.as_byte_slice() };
|
| 812 | let b = b.deref();
|
| 813 |
|
| 814 | if let crate::layout::SizeInfo::Sized { .. } = T::LAYOUT.size_info {
|
| 815 | let ptr = Ptr::from_ref(b);
|
| 816 | |
| 817 | |
| 818 | let ptr = unsafe { cast_for_sized::<T, _, _, _>(ptr) };
|
| 819 |
|
| 820 | |
| 821 | |
| 822 | |
| 823 | let ptr = unsafe { ptr.assume_alignment::<Aligned>() };
|
| 824 | return ptr.as_ref();
|
| 825 | }
|
| 826 |
|
| 827 | |
| 828 | |
| 829 | |
| 830 | let ptr = Ptr::from_ref(b)
|
| 831 | .try_cast_into_no_leftover::<T, BecauseImmutable>(None)
|
| 832 | .expect("zerocopy internal error: Deref::deref should be infallible");
|
| 833 | let ptr = ptr.recall_validity();
|
| 834 | ptr.as_ref()
|
| 835 | }
|
| 836 | }
|
| 837 |
|
| 838 | impl<B, T> DerefMut for Ref<B, T>
|
| 839 | where
|
| 840 | B: ByteSliceMut,
|
| 841 | |
| 842 | |
| 843 | |
| 844 | T: FromBytes + IntoBytes + KnownLayout + Immutable + ?Sized,
|
| 845 | {
|
| 846 | #[inline]
|
| 847 | fn deref_mut(&mut self) -> &mut T {
|
| 848 | |
| 849 | static_assert_dst_is_not_zst!(T);
|
| 850 |
|
| 851 | |
| 852 | |
| 853 | let b = unsafe { self.as_byte_slice_mut() };
|
| 854 | let b = b.deref_mut();
|
| 855 |
|
| 856 | if let crate::layout::SizeInfo::Sized { .. } = T::LAYOUT.size_info {
|
| 857 | let ptr = Ptr::from_mut(b);
|
| 858 | |
| 859 | |
| 860 | let ptr = unsafe {
|
| 861 | cast_for_sized::<
|
| 862 | T,
|
| 863 | _,
|
| 864 | (BecauseRead, BecauseExclusive),
|
| 865 | (BecauseMutationCompatible, BecauseInvariantsEq),
|
| 866 | >(ptr)
|
| 867 | };
|
| 868 |
|
| 869 | |
| 870 | |
| 871 | |
| 872 | let ptr = unsafe { ptr.assume_alignment::<Aligned>() };
|
| 873 | return ptr.as_mut();
|
| 874 | }
|
| 875 |
|
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | let ptr = Ptr::from_mut(b)
|
| 881 | .try_cast_into_no_leftover::<T, BecauseExclusive>(None)
|
| 882 | .expect("zerocopy internal error: DerefMut::deref_mut should be infallible");
|
| 883 | let ptr = ptr.recall_validity::<_, (_, (_, BecauseExclusive))>();
|
| 884 | ptr.as_mut()
|
| 885 | }
|
| 886 | }
|
| 887 |
|
| 888 | impl<T, B> Display for Ref<B, T>
|
| 889 | where
|
| 890 | B: ByteSlice,
|
| 891 | T: FromBytes + Display + KnownLayout + Immutable + ?Sized,
|
| 892 | {
|
| 893 | #[inline]
|
| 894 | fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
|
| 895 | let inner: &T = self;
|
| 896 | inner.fmt(fmt)
|
| 897 | }
|
| 898 | }
|
| 899 |
|
| 900 | impl<T, B> Debug for Ref<B, T>
|
| 901 | where
|
| 902 | B: ByteSlice,
|
| 903 | T: FromBytes + Debug + KnownLayout + Immutable + ?Sized,
|
| 904 | {
|
| 905 | #[inline]
|
| 906 | fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
|
| 907 | let inner: &T = self;
|
| 908 | fmt.debug_tuple("Ref").field(&inner).finish()
|
| 909 | }
|
| 910 | }
|
| 911 |
|
| 912 | impl<T, B> Eq for Ref<B, T>
|
| 913 | where
|
| 914 | B: ByteSlice,
|
| 915 | T: FromBytes + Eq + KnownLayout + Immutable + ?Sized,
|
| 916 | {
|
| 917 | }
|
| 918 |
|
| 919 | impl<T, B> PartialEq for Ref<B, T>
|
| 920 | where
|
| 921 | B: ByteSlice,
|
| 922 | T: FromBytes + PartialEq + KnownLayout + Immutable + ?Sized,
|
| 923 | {
|
| 924 | #[inline]
|
| 925 | fn eq(&self, other: &Self) -> bool {
|
| 926 | self.deref().eq(other.deref())
|
| 927 | }
|
| 928 | }
|
| 929 |
|
| 930 | impl<T, B> Ord for Ref<B, T>
|
| 931 | where
|
| 932 | B: ByteSlice,
|
| 933 | T: FromBytes + Ord + KnownLayout + Immutable + ?Sized,
|
| 934 | {
|
| 935 | #[inline]
|
| 936 | fn cmp(&self, other: &Self) -> Ordering {
|
| 937 | let inner: &T = self;
|
| 938 | let other_inner: &T = other;
|
| 939 | inner.cmp(other_inner)
|
| 940 | }
|
| 941 | }
|
| 942 |
|
| 943 | impl<T, B> PartialOrd for Ref<B, T>
|
| 944 | where
|
| 945 | B: ByteSlice,
|
| 946 | T: FromBytes + PartialOrd + KnownLayout + Immutable + ?Sized,
|
| 947 | {
|
| 948 | #[inline]
|
| 949 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
| 950 | let inner: &T = self;
|
| 951 | let other_inner: &T = other;
|
| 952 | inner.partial_cmp(other_inner)
|
| 953 | }
|
| 954 | }
|
| 955 |
|
| 956 | |
| 957 | |
| 958 | |
| 959 | #[inline(always)]
|
| 960 | unsafe fn cast_for_sized<'a, T, A, R, S>(
|
| 961 | ptr: Ptr<'a, [u8], (A, Aligned, Valid)>,
|
| 962 | ) -> Ptr<'a, T, (A, Unaligned, Valid)>
|
| 963 | where
|
| 964 | T: FromBytes + KnownLayout + ?Sized,
|
| 965 | A: crate::invariant::Aliasing,
|
| 966 | [u8]: MutationCompatible<T, A, Initialized, Initialized, R>,
|
| 967 | T: TransmuteFromPtr<T, A, Initialized, Valid, crate::pointer::cast::IdCast, S>,
|
| 968 | {
|
| 969 | use crate::pointer::cast::{Cast, Project};
|
| 970 |
|
| 971 | enum CastForSized {}
|
| 972 |
|
| 973 | |
| 974 | |
| 975 | |
| 976 | |
| 977 | unsafe impl<T: ?Sized + KnownLayout> Project<[u8], T> for CastForSized {
|
| 978 | #[inline(always)]
|
| 979 | fn project(src: PtrInner<'_, [u8]>) -> *mut T {
|
| 980 | T::raw_from_ptr_len(
|
| 981 | src.as_non_null().cast(),
|
| 982 | <T::PointerMetadata as crate::PointerMetadata>::from_elem_count(0),
|
| 983 | )
|
| 984 | .as_ptr()
|
| 985 | }
|
| 986 | }
|
| 987 |
|
| 988 | |
| 989 | unsafe impl<T: ?Sized + KnownLayout> Cast<[u8], T> for CastForSized {}
|
| 990 |
|
| 991 | ptr.recall_validity::<Initialized, (_, (_, _))>()
|
| 992 | .cast::<_, CastForSized, _>()
|
| 993 | .recall_validity::<Valid, _>()
|
| 994 | }
|
| 995 |
|
| 996 | #[cfg(test)]
|
| 997 | #[allow(clippy::assertions_on_result_states)]
|
| 998 | mod tests {
|
| 999 | use core::convert::TryInto as _;
|
| 1000 |
|
| 1001 | use super::*;
|
| 1002 | use crate::util::testutil::*;
|
| 1003 |
|
| 1004 | #[test]
|
| 1005 | fn test_mut_slice_into_ref() {
|
| 1006 | |
| 1007 | |
| 1008 | let mut buf = [0u8];
|
| 1009 | let r = Ref::<&mut [u8], u8>::from_bytes(&mut buf).unwrap();
|
| 1010 | assert_eq!(Ref::into_ref(r), &0);
|
| 1011 | }
|
| 1012 |
|
| 1013 | #[test]
|
| 1014 | fn test_address() {
|
| 1015 | |
| 1016 | |
| 1017 |
|
| 1018 | let buf = [0];
|
| 1019 | let r = Ref::<_, u8>::from_bytes(&buf[..]).unwrap();
|
| 1020 | let buf_ptr = buf.as_ptr();
|
| 1021 | let deref_ptr: *const u8 = r.deref();
|
| 1022 | assert_eq!(buf_ptr, deref_ptr);
|
| 1023 |
|
| 1024 | let buf = [0];
|
| 1025 | let r = Ref::<_, [u8]>::from_bytes(&buf[..]).unwrap();
|
| 1026 | let buf_ptr = buf.as_ptr();
|
| 1027 | let deref_ptr = r.deref().as_ptr();
|
| 1028 | assert_eq!(buf_ptr, deref_ptr);
|
| 1029 | }
|
| 1030 |
|
| 1031 | |
| 1032 | |
| 1033 | |
| 1034 | |
| 1035 | fn test_new_helper(mut r: Ref<&mut [u8], AU64>) {
|
| 1036 | |
| 1037 | assert_eq!(*r, AU64(0));
|
| 1038 | assert_eq!(Ref::read(&r), AU64(0));
|
| 1039 |
|
| 1040 | |
| 1041 | |
| 1042 | const VAL1: AU64 = AU64(0xFF00FF00FF00FF00);
|
| 1043 | *r = VAL1;
|
| 1044 | assert_eq!(Ref::bytes(&r), &VAL1.to_bytes());
|
| 1045 | *r = AU64(0);
|
| 1046 | Ref::write(&mut r, VAL1);
|
| 1047 | assert_eq!(Ref::bytes(&r), &VAL1.to_bytes());
|
| 1048 |
|
| 1049 | |
| 1050 | |
| 1051 | const VAL2: AU64 = AU64(!VAL1.0); |
| 1052 | Ref::bytes_mut(&mut r).copy_from_slice(&VAL2.to_bytes()[..]);
|
| 1053 | assert_eq!(*r, VAL2);
|
| 1054 | assert_eq!(Ref::read(&r), VAL2);
|
| 1055 | }
|
| 1056 |
|
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | fn test_new_helper_slice(mut r: Ref<&mut [u8], [AU64]>, typed_len: usize) {
|
| 1061 | |
| 1062 | assert_eq!(&*r, vec![AU64(0); typed_len].as_slice());
|
| 1063 |
|
| 1064 | |
| 1065 | let untyped_len = typed_len * 8;
|
| 1066 | assert_eq!(Ref::bytes(&r).len(), untyped_len);
|
| 1067 | assert_eq!(Ref::bytes(&r).as_ptr(), r.as_ptr().cast::<u8>());
|
| 1068 |
|
| 1069 | |
| 1070 | |
| 1071 | const VAL1: AU64 = AU64(0xFF00FF00FF00FF00);
|
| 1072 | for typed in &mut *r {
|
| 1073 | *typed = VAL1;
|
| 1074 | }
|
| 1075 | assert_eq!(Ref::bytes(&r), VAL1.0.to_ne_bytes().repeat(typed_len).as_slice());
|
| 1076 |
|
| 1077 | |
| 1078 | |
| 1079 | const VAL2: AU64 = AU64(!VAL1.0); |
| 1080 | Ref::bytes_mut(&mut r).copy_from_slice(&VAL2.0.to_ne_bytes().repeat(typed_len));
|
| 1081 | assert!(r.iter().copied().all(|x| x == VAL2));
|
| 1082 | }
|
| 1083 |
|
| 1084 | #[test]
|
| 1085 | fn test_new_aligned_sized() {
|
| 1086 | |
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 |
|
| 1092 | |
| 1093 | let mut buf = Align::<[u8; 8], AU64>::default();
|
| 1094 | |
| 1095 | test_new_helper(Ref::<_, AU64>::from_bytes(&mut buf.t[..]).unwrap());
|
| 1096 | {
|
| 1097 | |
| 1098 | buf.set_default();
|
| 1099 | let (r, suffix) = Ref::<_, AU64>::from_prefix(&mut buf.t[..]).unwrap();
|
| 1100 | assert!(suffix.is_empty());
|
| 1101 | test_new_helper(r);
|
| 1102 | }
|
| 1103 | {
|
| 1104 | buf.set_default();
|
| 1105 | let (prefix, r) = Ref::<_, AU64>::from_suffix(&mut buf.t[..]).unwrap();
|
| 1106 | assert!(prefix.is_empty());
|
| 1107 | test_new_helper(r);
|
| 1108 | }
|
| 1109 |
|
| 1110 | |
| 1111 | |
| 1112 | |
| 1113 | |
| 1114 | |
| 1115 | let mut buf = Align::<[u8; 24], AU64>::default();
|
| 1116 | |
| 1117 | |
| 1118 | test_new_helper_slice(Ref::<_, [AU64]>::from_bytes(&mut buf.t[..]).unwrap(), 3);
|
| 1119 | buf.set_default();
|
| 1120 | let r = Ref::<_, [AU64]>::from_bytes_with_elems(&mut buf.t[..], 3).unwrap();
|
| 1121 | test_new_helper_slice(r, 3);
|
| 1122 |
|
| 1123 | let ascending: [u8; 24] = (0..24).collect::<Vec<_>>().try_into().unwrap();
|
| 1124 | |
| 1125 | let mut ascending_prefix = ascending;
|
| 1126 | ascending_prefix[16..].copy_from_slice(&[0, 0, 0, 0, 0, 0, 0, 0]);
|
| 1127 | |
| 1128 | let mut ascending_suffix = ascending;
|
| 1129 | ascending_suffix[..8].copy_from_slice(&[0, 0, 0, 0, 0, 0, 0, 0]);
|
| 1130 | {
|
| 1131 | buf.t = ascending_suffix;
|
| 1132 | let (r, suffix) = Ref::<_, [AU64]>::from_prefix_with_elems(&mut buf.t[..], 1).unwrap();
|
| 1133 | assert_eq!(suffix, &ascending[8..]);
|
| 1134 | test_new_helper_slice(r, 1);
|
| 1135 | }
|
| 1136 | {
|
| 1137 | buf.t = ascending_prefix;
|
| 1138 | let (prefix, r) = Ref::<_, [AU64]>::from_suffix_with_elems(&mut buf.t[..], 1).unwrap();
|
| 1139 | assert_eq!(prefix, &ascending[..16]);
|
| 1140 | test_new_helper_slice(r, 1);
|
| 1141 | }
|
| 1142 | }
|
| 1143 |
|
| 1144 | #[test]
|
| 1145 | fn test_new_oversized() {
|
| 1146 | |
| 1147 | |
| 1148 | |
| 1149 |
|
| 1150 | let mut buf = Align::<[u8; 16], AU64>::default();
|
| 1151 | {
|
| 1152 | |
| 1153 | |
| 1154 | let (r, suffix) = Ref::<_, AU64>::from_prefix(&mut buf.t[..]).unwrap();
|
| 1155 | assert_eq!(suffix.len(), 8);
|
| 1156 | test_new_helper(r);
|
| 1157 | }
|
| 1158 | {
|
| 1159 | buf.set_default();
|
| 1160 | |
| 1161 | let (prefix, r) = Ref::<_, AU64>::from_suffix(&mut buf.t[..]).unwrap();
|
| 1162 | assert_eq!(prefix.len(), 8);
|
| 1163 | test_new_helper(r);
|
| 1164 | }
|
| 1165 | }
|
| 1166 |
|
| 1167 | #[test]
|
| 1168 | #[allow(clippy::cognitive_complexity)]
|
| 1169 | fn test_new_error() {
|
| 1170 | |
| 1171 |
|
| 1172 | |
| 1173 | let buf = Align::<[u8; 16], AU64>::default();
|
| 1174 | |
| 1175 | assert!(Ref::<_, AU64>::from_bytes(&buf.t[..]).is_err());
|
| 1176 |
|
| 1177 | |
| 1178 |
|
| 1179 | |
| 1180 | let buf = Align::<[u8; 4], AU64>::default();
|
| 1181 | |
| 1182 | assert!(Ref::<_, AU64>::from_bytes(&buf.t[..]).is_err());
|
| 1183 | assert!(Ref::<_, AU64>::from_prefix(&buf.t[..]).is_err());
|
| 1184 | assert!(Ref::<_, AU64>::from_suffix(&buf.t[..]).is_err());
|
| 1185 |
|
| 1186 | |
| 1187 |
|
| 1188 | let buf = Align::<[u8; 12], AU64>::default();
|
| 1189 | |
| 1190 | assert!(Ref::<_, [AU64]>::from_bytes(&buf.t[..]).is_err());
|
| 1191 |
|
| 1192 | |
| 1193 | let buf = Align::<[u8; 12], AU64>::default();
|
| 1194 | |
| 1195 | |
| 1196 | |
| 1197 | |
| 1198 | |
| 1199 | |
| 1200 | let n = (usize::MAX / mem::size_of::<AU64>()) + 1;
|
| 1201 | assert!(Ref::<_, [AU64]>::from_bytes_with_elems(&buf.t[..], n).is_err());
|
| 1202 | assert!(Ref::<_, [AU64]>::from_bytes_with_elems(&buf.t[..], 2).is_err());
|
| 1203 | assert!(Ref::<_, [AU64]>::from_prefix_with_elems(&buf.t[..], n).is_err());
|
| 1204 | assert!(Ref::<_, [AU64]>::from_prefix_with_elems(&buf.t[..], 2).is_err());
|
| 1205 | assert!(Ref::<_, [AU64]>::from_suffix_with_elems(&buf.t[..], n).is_err());
|
| 1206 | assert!(Ref::<_, [AU64]>::from_suffix_with_elems(&buf.t[..], 2).is_err());
|
| 1207 |
|
| 1208 | |
| 1209 |
|
| 1210 | |
| 1211 | |
| 1212 | let buf = Align::<[u8; 13], AU64>::default();
|
| 1213 | |
| 1214 | |
| 1215 | assert!(Ref::<_, AU64>::from_bytes(&buf.t[1..]).is_err());
|
| 1216 | assert!(Ref::<_, AU64>::from_prefix(&buf.t[1..]).is_err());
|
| 1217 | assert!(Ref::<_, [AU64]>::from_bytes(&buf.t[1..]).is_err());
|
| 1218 | assert!(Ref::<_, [AU64]>::from_bytes_with_elems(&buf.t[1..], 1).is_err());
|
| 1219 | assert!(Ref::<_, [AU64]>::from_prefix_with_elems(&buf.t[1..], 1).is_err());
|
| 1220 | assert!(Ref::<_, [AU64]>::from_suffix_with_elems(&buf.t[1..], 1).is_err());
|
| 1221 | |
| 1222 | |
| 1223 | assert!(Ref::<_, AU64>::from_suffix(&buf.t[..]).is_err());
|
| 1224 |
|
| 1225 | |
| 1226 |
|
| 1227 | let buf = Align::<[u8; 16], AU64>::default();
|
| 1228 | let unreasonable_len = usize::MAX / mem::size_of::<AU64>() + 1;
|
| 1229 | assert!(Ref::<_, [AU64]>::from_prefix_with_elems(&buf.t[..], unreasonable_len).is_err());
|
| 1230 | assert!(Ref::<_, [AU64]>::from_suffix_with_elems(&buf.t[..], unreasonable_len).is_err());
|
| 1231 | }
|
| 1232 |
|
| 1233 | #[test]
|
| 1234 | #[allow(unstable_name_collisions)]
|
| 1235 | #[allow(clippy::as_conversions)]
|
| 1236 | fn test_into_ref_mut() {
|
| 1237 | #[allow(unused)]
|
| 1238 | use crate::util::AsAddress as _;
|
| 1239 |
|
| 1240 | let mut buf = Align::<[u8; 8], u64>::default();
|
| 1241 | let r = Ref::<_, u64>::from_bytes(&buf.t[..]).unwrap();
|
| 1242 | let rf = Ref::into_ref(r);
|
| 1243 | assert_eq!(rf, &0u64);
|
| 1244 | let buf_addr = (&buf.t as *const [u8; 8]).addr();
|
| 1245 | assert_eq!((rf as *const u64).addr(), buf_addr);
|
| 1246 |
|
| 1247 | let r = Ref::<_, u64>::from_bytes(&mut buf.t[..]).unwrap();
|
| 1248 | let rf = Ref::into_mut(r);
|
| 1249 | assert_eq!(rf, &mut 0u64);
|
| 1250 | assert_eq!((rf as *mut u64).addr(), buf_addr);
|
| 1251 |
|
| 1252 | *rf = u64::MAX;
|
| 1253 | assert_eq!(buf.t, [0xFF; 8]);
|
| 1254 | }
|
| 1255 |
|
| 1256 | #[test]
|
| 1257 | fn test_display_debug() {
|
| 1258 | let buf = Align::<[u8; 8], u64>::default();
|
| 1259 | let r = Ref::<_, u64>::from_bytes(&buf.t[..]).unwrap();
|
| 1260 | assert_eq!(format!("{}", r), "0");
|
| 1261 | assert_eq!(format!("{:?}", r), "Ref(0)");
|
| 1262 |
|
| 1263 | let buf = Align::<[u8; 8], u64>::default();
|
| 1264 | let r = Ref::<_, [u64]>::from_bytes(&buf.t[..]).unwrap();
|
| 1265 | assert_eq!(format!("{:?}", r), "Ref([0])");
|
| 1266 | }
|
| 1267 |
|
| 1268 | #[test]
|
| 1269 | fn test_eq() {
|
| 1270 | let buf1 = 0_u64;
|
| 1271 | let r1 = Ref::<_, u64>::from_bytes(buf1.as_bytes()).unwrap();
|
| 1272 | let buf2 = 0_u64;
|
| 1273 | let r2 = Ref::<_, u64>::from_bytes(buf2.as_bytes()).unwrap();
|
| 1274 | assert_eq!(r1, r2);
|
| 1275 | }
|
| 1276 |
|
| 1277 | #[test]
|
| 1278 | fn test_ne() {
|
| 1279 | let buf1 = 0_u64;
|
| 1280 | let r1 = Ref::<_, u64>::from_bytes(buf1.as_bytes()).unwrap();
|
| 1281 | let buf2 = 1_u64;
|
| 1282 | let r2 = Ref::<_, u64>::from_bytes(buf2.as_bytes()).unwrap();
|
| 1283 | assert_ne!(r1, r2);
|
| 1284 | }
|
| 1285 |
|
| 1286 | #[test]
|
| 1287 | fn test_ord() {
|
| 1288 | let buf1 = 0_u64;
|
| 1289 | let r1 = Ref::<_, u64>::from_bytes(buf1.as_bytes()).unwrap();
|
| 1290 | let buf2 = 1_u64;
|
| 1291 | let r2 = Ref::<_, u64>::from_bytes(buf2.as_bytes()).unwrap();
|
| 1292 | assert!(r1 < r2);
|
| 1293 | assert_eq!(PartialOrd::partial_cmp(&r1, &r2), Some(Ordering::Less));
|
| 1294 | assert_eq!(Ord::cmp(&r1, &r2), Ordering::Less);
|
| 1295 | }
|
| 1296 | }
|
| 1297 |
|
| 1298 | #[cfg(all(test, __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS))]
|
| 1299 | mod benches {
|
| 1300 | use test::{self, Bencher};
|
| 1301 |
|
| 1302 | use super::*;
|
| 1303 | use crate::util::testutil::*;
|
| 1304 |
|
| 1305 | #[bench]
|
| 1306 | fn bench_from_bytes_sized(b: &mut Bencher) {
|
| 1307 | let buf = Align::<[u8; 8], AU64>::default();
|
| 1308 | |
| 1309 | let bytes = &buf.t[..];
|
| 1310 | b.iter(|| test::black_box(Ref::<_, AU64>::from_bytes(test::black_box(bytes)).unwrap()));
|
| 1311 | }
|
| 1312 |
|
| 1313 | #[bench]
|
| 1314 | fn bench_into_ref_sized(b: &mut Bencher) {
|
| 1315 | let buf = Align::<[u8; 8], AU64>::default();
|
| 1316 | let bytes = &buf.t[..];
|
| 1317 | let r = Ref::<_, AU64>::from_bytes(bytes).unwrap();
|
| 1318 | b.iter(|| test::black_box(Ref::into_ref(test::black_box(r))));
|
| 1319 | }
|
| 1320 |
|
| 1321 | #[bench]
|
| 1322 | fn bench_into_mut_sized(b: &mut Bencher) {
|
| 1323 | let mut buf = Align::<[u8; 8], AU64>::default();
|
| 1324 | let buf = &mut buf.t[..];
|
| 1325 | let _ = Ref::<_, AU64>::from_bytes(&mut *buf).unwrap();
|
| 1326 | b.iter(move || {
|
| 1327 | |
| 1328 | |
| 1329 | let r = unsafe { Ref::<&mut [u8], AU64>::new_unchecked(&mut *buf) };
|
| 1330 | test::black_box(Ref::into_mut(test::black_box(r)));
|
| 1331 | });
|
| 1332 | }
|
| 1333 |
|
| 1334 | #[bench]
|
| 1335 | fn bench_deref_sized(b: &mut Bencher) {
|
| 1336 | let buf = Align::<[u8; 8], AU64>::default();
|
| 1337 | let bytes = &buf.t[..];
|
| 1338 | let r = Ref::<_, AU64>::from_bytes(bytes).unwrap();
|
| 1339 | b.iter(|| {
|
| 1340 | let temp = test::black_box(r);
|
| 1341 | test::black_box(temp.deref());
|
| 1342 | });
|
| 1343 | }
|
| 1344 |
|
| 1345 | #[bench]
|
| 1346 | fn bench_deref_mut_sized(b: &mut Bencher) {
|
| 1347 | let mut buf = Align::<[u8; 8], AU64>::default();
|
| 1348 | let buf = &mut buf.t[..];
|
| 1349 | let _ = Ref::<_, AU64>::from_bytes(&mut *buf).unwrap();
|
| 1350 | b.iter(|| {
|
| 1351 | |
| 1352 | |
| 1353 | let r = unsafe { Ref::<&mut [u8], AU64>::new_unchecked(&mut *buf) };
|
| 1354 | let mut temp = test::black_box(r);
|
| 1355 | test::black_box(temp.deref_mut());
|
| 1356 | });
|
| 1357 | }
|
| 1358 | }
|