Repository navigation
Expand file tree
/
Copy pathref.rs
More file actions
1898 lines (1774 loc) · 71.7 KB
/
Copy pathref.rs
File metadata and controls
1898 lines (1774 loc) · 71.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
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
542
543
544
545
546
547
548
549
550
551
552
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) OR MIT
//
// Copyright 2024 The Fuchsia Authors
//
// Licensed under the 2-Clause BSD License <LICENSE-BSD or
// https://opensource.org/license/bsd-2-clause>, Apache License, Version 2.0
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
// This file may not be copied, modified, or distributed except according to
// those terms.
use super::*;
use crate::pointer::{
BecauseMutationCompatible, BecauseSharedCompatible, MutationCompatible, TransmuteFromPtr,
};
mod def {
use core::marker::PhantomData;
use crate::{
ByteSlice, ByteSliceMut, CloneableByteSlice, CopyableByteSlice, IntoByteSlice,
IntoByteSliceMut, KnownLayout, PointerMetadata,
};
/// A typed reference derived from a byte slice.
///
/// A `Ref<B, T>` is a reference to a `T` which is stored in a byte slice, `B`.
/// Unlike a native reference (`&T` or `&mut T`), `Ref<B, T>` has the same
/// mutability as the byte slice it was constructed from (`B`).
///
/// # Examples
///
/// `Ref` can be used to treat a sequence of bytes as a structured type, and
/// to read and write the fields of that type as if the byte slice reference
/// were simply a reference to that type.
///
/// ```rust
/// use zerocopy::*;
/// # use zerocopy_derive::*;
///
/// #[derive(FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)]
/// #[repr(C)]
/// struct UdpHeader {
/// src_port: [u8; 2],
/// dst_port: [u8; 2],
/// length: [u8; 2],
/// checksum: [u8; 2],
/// }
///
/// #[derive(FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)]
/// #[repr(C, packed)]
/// struct UdpPacket {
/// header: UdpHeader,
/// body: [u8],
/// }
///
/// impl UdpPacket {
/// pub fn parse<B: ByteSlice>(bytes: B) -> Option<Ref<B, UdpPacket>> {
/// Ref::from_bytes(bytes).ok()
/// }
/// }
/// ```
pub struct Ref<B, T: ?Sized> {
// INVARIANTS:
// - The referent (via `.deref`, `.deref_mut`, `.into`) byte slice is
// aligned to `T`'s alignment, and its size is valid for `T`.
// - If `T: KnownLayout`, let `metadata` be
// `T::PointerMetadata::from_elem_count` applied to `elem_count`.
// Then `T::size_for_metadata(metadata)` is `Some(referent.len())`.
bytes: B,
// Ignored when `T` does not implement `KnownLayout`.
elem_count: usize,
_marker: PhantomData<T>,
}
impl<B, T> Ref<B, T> {
/// Constructs a new `Ref` to a sized `T`.
///
/// # Safety
///
/// `bytes` dereferences (via [`deref`], [`deref_mut`], and [`into`]) to
/// a byte slice which is aligned to `T`'s alignment and whose size is
/// `size_of::<T>()`.
///
/// [`deref`]: core::ops::Deref::deref
/// [`deref_mut`]: core::ops::DerefMut::deref_mut
/// [`into`]: core::convert::Into::into
pub(super) unsafe fn new_sized_unchecked(bytes: B) -> Ref<B, T> {
// INVARIANTS: The caller has promised that `bytes`'s referent is
// validly aligned and has size `size_of::<T>()`. If `T:
// KnownLayout`, then, since `T` is sized, `T::PointerMetadata` is
// `()`. `from_elem_count` returns `()` for any element count, and
// `T::size_for_metadata(()) == Some(size_of::<T>())`. `size_of`
// reports this size in bytes [1].
//
// [1] Per https://doc.rust-lang.org/1.56.0/core/mem/fn.size_of.html:
//
// Returns the size of a type in bytes.
Ref { bytes, elem_count: 0, _marker: PhantomData }
}
}
impl<B, T: KnownLayout + ?Sized> Ref<B, T> {
/// Constructs a new `Ref`.
///
/// # Safety
///
/// `bytes` dereferences (via [`deref`], [`deref_mut`], and [`into`]) to
/// a byte slice which is aligned to `T`'s alignment and satisfies
/// `T::size_for_metadata(metadata) == Some(referent.len())`.
///
/// [`deref`]: core::ops::Deref::deref
/// [`deref_mut`]: core::ops::DerefMut::deref_mut
/// [`into`]: core::convert::Into::into
pub(super) unsafe fn new_unchecked(bytes: B, metadata: T::PointerMetadata) -> Ref<B, T> {
// INVARIANTS: The caller has promised that `bytes`'s referent is
// validly aligned and has exactly the size described by `metadata`.
// `KnownLayout::PointerMetadata` is `()` for sized types and
// `usize` for slice DSTs. For both permitted metadata types,
// `from_elem_count(metadata.to_elem_count()) == metadata`: `()`
// round-trips through zero, while `usize` round-trips through
// itself. Thus, storing the element count preserves the metadata
// whose size the caller validated. This agrees with Rust's
// language-level definition of slice pointer metadata [1].
//
// [1] Per https://doc.rust-lang.org/1.56.0/reference/dynamically-sized-types.html:
//
// Pointers to slices also store the number of elements of the
// slice.
Ref { bytes, elem_count: metadata.to_elem_count(), _marker: PhantomData }
}
/// Gets the pointer metadata represented by this `Ref`.
#[inline(always)]
pub(super) fn pointer_metadata(&self) -> T::PointerMetadata {
// INVARIANTS: `KnownLayout::PointerMetadata` is either `()` or
// `usize`. For `()`, `from_elem_count` ignores the element count;
// for `usize`, it returns the element count unchanged.
T::PointerMetadata::from_elem_count(self.elem_count)
}
}
impl<B: ByteSlice, T: ?Sized> Ref<B, T> {
/// Access the byte slice as a [`ByteSlice`].
///
/// # Safety
///
/// The caller promises not to call methods on the returned
/// [`ByteSlice`] other than `ByteSlice` methods (for example, via
/// `Any::downcast_ref`).
///
/// `as_byte_slice` promises to return a `ByteSlice` whose referent is
/// validly aligned for `T` and has a valid size for `T`. If `T:
/// KnownLayout`, then `T::size_for_metadata(self.pointer_metadata())`
/// returns `Some(referent.len())`.
pub(crate) unsafe fn as_byte_slice(&self) -> &impl ByteSlice {
// INVARIANTS: The caller promises not to call methods other than
// those on `ByteSlice`. Since `B: ByteSlice`, dereference stability
// guarantees that calling `ByteSlice` methods will not change the
// address or length of `self.bytes`'s referent.
//
// SAFETY: By invariant on `self`, the alignment, size, and metadata
// postconditions are upheld.
&self.bytes
}
}
impl<B: ByteSliceMut, T: ?Sized> Ref<B, T> {
/// Access the byte slice as a [`ByteSliceMut`].
///
/// # Safety
///
/// The caller promises not to call methods on the returned
/// [`ByteSliceMut`] other than `ByteSliceMut` methods (for example, via
/// `Any::downcast_mut`).
///
/// `as_byte_slice_mut` promises to return a `ByteSliceMut` whose
/// referent is validly aligned for `T` and has a valid size for `T`. If
/// `T: KnownLayout`, then
/// `T::size_for_metadata(self.pointer_metadata())` returns
/// `Some(referent.len())`.
pub(crate) unsafe fn as_byte_slice_mut(&mut self) -> &mut impl ByteSliceMut {
// INVARIANTS: The caller promises not to call methods other than
// those on `ByteSliceMut`. Since `B: ByteSlice`, dereference
// stability guarantees that calling `ByteSlice` methods will not
// change the address or length of `self.bytes`'s referent.
//
// SAFETY: By invariant on `self`, the alignment, size, and metadata
// postconditions are upheld.
&mut self.bytes
}
}
impl<'a, B: IntoByteSlice<'a>, T: ?Sized> Ref<B, T> {
/// Access the byte slice as an [`IntoByteSlice`].
///
/// # Safety
///
/// The caller promises not to call methods on the returned
/// [`IntoByteSlice`] other than `IntoByteSlice` methods (for example,
/// via `Any::downcast_ref`).
///
/// `into_byte_slice` promises to return an `IntoByteSlice` whose
/// referent is validly aligned for `T` and has a valid size for `T`. If
/// `T: KnownLayout`, then
/// `T::size_for_metadata(self.pointer_metadata())` returns
/// `Some(referent.len())`.
pub(crate) unsafe fn into_byte_slice(self) -> impl IntoByteSlice<'a> {
// INVARIANTS: The caller promises not to call methods other than
// those on `IntoByteSlice`. Since `B: ByteSlice`, dereference
// stability guarantees that calling `ByteSlice` methods will not
// change the address or length of `self.bytes`'s referent.
//
// SAFETY: By invariant on `self`, the alignment, size, and metadata
// postconditions are upheld.
self.bytes
}
}
impl<'a, B: IntoByteSliceMut<'a>, T: ?Sized> Ref<B, T> {
/// Access the byte slice as an [`IntoByteSliceMut`].
///
/// # Safety
///
/// The caller promises not to call methods on the returned
/// [`IntoByteSliceMut`] other than `IntoByteSliceMut` methods (for
/// example, via `Any::downcast_mut`).
///
/// `into_byte_slice_mut` promises to return an `IntoByteSliceMut` whose
/// referent is validly aligned for `T` and has a valid size for `T`. If
/// `T: KnownLayout`, then
/// `T::size_for_metadata(self.pointer_metadata())` returns
/// `Some(referent.len())`.
pub(crate) unsafe fn into_byte_slice_mut(self) -> impl IntoByteSliceMut<'a> {
// INVARIANTS: The caller promises not to call methods other than
// those on `IntoByteSliceMut`. Since `B: ByteSlice`, dereference
// stability guarantees that calling `ByteSlice` methods will not
// change the address or length of `self.bytes`'s referent.
//
// SAFETY: By invariant on `self`, the alignment, size, and metadata
// postconditions are upheld.
self.bytes
}
}
impl<B: CloneableByteSlice + Clone, T: ?Sized> Clone for Ref<B, T> {
#[inline]
fn clone(&self) -> Ref<B, T> {
// INVARIANTS: Since `B: CloneableByteSlice`, `self.bytes.clone()` has
// the same address and length as `self.bytes`. We copy
// `self.elem_count`, so the clone also has the same metadata. Since
// `self` upholds the field invariants, so does the clone.
Ref { bytes: self.bytes.clone(), elem_count: self.elem_count, _marker: PhantomData }
}
}
// INVARIANTS: Since `B: CopyableByteSlice`, the copied `Ref`'s `bytes` has the
// same address and length as the original `Ref`'s `bytes`. The copied
// `elem_count` has the same metadata. Since the original upholds the field
// invariants, so does the copy.
impl<B: CopyableByteSlice + Copy, T: ?Sized> Copy for Ref<B, T> {}
}
#[allow(unreachable_pub)] // This is a false positive on our MSRV toolchain.
pub use def::Ref;
use crate::pointer::{
invariant::{Aligned, BecauseExclusive, Initialized, Safe, Unaligned},
BecauseRead, PtrInner,
};
impl<B, T> Ref<B, T>
where
B: ByteSlice,
{
#[must_use = "has no side effects"]
pub(crate) fn sized_from(bytes: B) -> Result<Ref<B, T>, CastError<B, T>> {
if bytes.len() != mem::size_of::<T>() {
return Err(SizeError::new(bytes).into());
}
if let Err(err) = util::validate_aligned_to::<_, T>(bytes.deref()) {
return Err(err.with_src(bytes).into());
}
// SAFETY: `size_of` returns `T`'s size in bytes [1]. The equality check
// above therefore establishes that `bytes` has exactly that byte
// length, and `validate_aligned_to` establishes alignment.
//
// [1] Per https://doc.rust-lang.org/1.56.0/core/mem/fn.size_of.html:
//
// Returns the size of a type in bytes.
Ok(unsafe { Ref::new_sized_unchecked(bytes) })
}
}
impl<B, T> Ref<B, T>
where
B: SplitByteSlice,
{
#[must_use = "has no side effects"]
pub(crate) fn sized_from_prefix(bytes: B) -> Result<(Ref<B, T>, B), CastError<B, T>> {
if bytes.len() < mem::size_of::<T>() {
return Err(SizeError::new(bytes).into());
}
if let Err(err) = util::validate_aligned_to::<_, T>(bytes.deref()) {
return Err(err.with_src(bytes).into());
}
let (bytes, suffix) = bytes.split_at(mem::size_of::<T>()).map_err(
#[inline(always)]
|b| SizeError::new(b).into(),
)?;
// SAFETY: We just validated alignment and that `bytes` is at least as
// large as `T`. `bytes.split_at(mem::size_of::<T>())?` ensures that the
// new `bytes` is exactly the size of `T`, measured in bytes as specified
// for `size_of` [1]. By safety postcondition on
// `SplitByteSlice::split_at` we can rely on `split_at` to produce the
// correct `bytes` and `suffix`.
//
// [1] Per https://doc.rust-lang.org/1.56.0/core/mem/fn.size_of.html:
//
// Returns the size of a type in bytes.
let r = unsafe { Ref::new_sized_unchecked(bytes) };
Ok((r, suffix))
}
#[must_use = "has no side effects"]
pub(crate) fn sized_from_suffix(bytes: B) -> Result<(B, Ref<B, T>), CastError<B, T>> {
let bytes_len = bytes.len();
let split_at = if let Some(split_at) = bytes_len.checked_sub(mem::size_of::<T>()) {
split_at
} else {
return Err(SizeError::new(bytes).into());
};
let (prefix, bytes) = bytes.split_at(split_at).map_err(|b| SizeError::new(b).into())?;
if let Err(err) = util::validate_aligned_to::<_, T>(bytes.deref()) {
return Err(err.with_src(bytes).into());
}
// SAFETY: Since `split_at` is defined as `bytes_len - size_of::<T>()`,
// the `bytes` which results from `let (prefix, bytes) =
// bytes.split_at(split_at)?` has length `size_of::<T>()`, measured in
// bytes as specified for `size_of` [1]. After constructing `bytes`, we
// validate that it has the proper alignment. By safety postcondition on
// `SplitByteSlice::split_at` we can rely on `split_at` to produce the
// correct `prefix` and `bytes`.
//
// [1] Per https://doc.rust-lang.org/1.56.0/core/mem/fn.size_of.html:
//
// Returns the size of a type in bytes.
let r = unsafe { Ref::new_sized_unchecked(bytes) };
Ok((prefix, r))
}
}
impl<B, T> Ref<B, T>
where
B: ByteSlice,
T: KnownLayout + Immutable + ?Sized,
{
/// Constructs a `Ref` from a byte slice.
///
/// If the length of `source` is not a [valid size of `T`][valid-size], or
/// if `source` is not appropriately aligned for `T`, this returns `Err`. If
/// [`T: Unaligned`][t-unaligned], you can [infallibly discard the alignment
/// error][size-error-from].
///
/// `T` may be a sized type, a slice, or a [slice DST][slice-dst].
///
/// [valid-size]: crate::KnownLayout#what-is-a-valid-size
/// [t-unaligned]: crate::Unaligned
/// [size-error-from]: error/struct.SizeError.html#method.from-1
/// [slice-dst]: KnownLayout#dynamically-sized-types
///
/// # Compile-Time Assertions
///
/// This method cannot yet be used on unsized types whose dynamically-sized
/// component is zero-sized. Attempting to use this method on such types
/// results in a compile-time assertion error; e.g.:
///
/// ```compile_fail,E0080
/// use zerocopy::*;
/// # use zerocopy_derive::*;
///
/// #[derive(Immutable, KnownLayout)]
/// #[repr(C)]
/// struct ZSTy {
/// leading_sized: u16,
/// trailing_dst: [()],
/// }
///
/// let _ = Ref::<_, ZSTy>::from_bytes(&b"UU"[..]); // ⚠ Compile Error!
/// ```
///
#[doc = codegen_section!(
header = "h5",
bench = "ref_type_from_bytes",
format = "coco",
arity = 3,
[
open
@index 1
@title "Sized"
@variant "static_size"
],
[
@index 2
@title "Unsized"
@variant "dynamic_size"
],
[
@index 3
@title "Dynamically Padded"
@variant "dynamic_padding"
]
)]
#[must_use = "has no side effects"]
#[inline]
pub fn from_bytes(source: B) -> Result<Ref<B, T>, CastError<B, T>> {
static_assert_dst_is_not_zst!(T);
let metadata = match Ptr::from_ref(source.deref())
.try_cast_into_no_leftover::<T, BecauseImmutable>(None)
{
Ok(ptr) => T::pointer_to_metadata(ptr.as_inner().as_ptr()),
Err(e) => return Err(e.with_src(()).with_src(source)),
};
// SAFETY: By its safety postcondition, `try_cast_into_no_leftover`
// returned a `Ptr<T>` aligned as required for references [2], over the
// same byte range as `source.deref()`. `pointer_to_metadata` returns
// the metadata stored in that pointer, and `size_for_metadata` computes
// the corresponding dynamically-known byte size [1]. Thus,
// `T::size_for_metadata(metadata)` is `Some(source.len())`.
// `ByteSlice` and its applicable subtraits guarantee that the stored
// `source` continues to dereference or convert to that address and
// length. These facts establish both preconditions of `new_unchecked`.
//
// [1] Per https://doc.rust-lang.org/1.56.0/core/mem/fn.size_of_val.html:
//
// Returns the size of the pointed-to value in bytes.
//
// ... when `T` has no statically-known size ... `size_of_val` can
// be used to get the dynamically-known size.
//
// [2] Per https://doc.rust-lang.org/1.56.0/core/mem/fn.align_of_val.html:
//
// Returns the ABI-required minimum alignment of the type of the
// value that `val` points to.
//
// Every reference to a value of the type `T` must be a multiple of
// this number.
Ok(unsafe { Ref::new_unchecked(source, metadata) })
}
}
impl<B, T> Ref<B, T>
where
B: SplitByteSlice,
T: KnownLayout + Immutable + ?Sized,
{
/// Constructs a `Ref` from the prefix of a byte slice.
///
/// This method computes the [largest possible size of `T`][valid-size] that
/// can fit in the leading bytes of `source`, then attempts to return both a
/// `Ref` to those bytes, and a reference to the remaining bytes. If there
/// are insufficient bytes, or if `source` is not appropriately aligned,
/// this returns `Err`. If [`T: Unaligned`][t-unaligned], you can
/// [infallibly discard the alignment error][size-error-from].
///
/// `T` may be a sized type, a slice, or a [slice DST][slice-dst].
///
/// [valid-size]: crate::KnownLayout#what-is-a-valid-size
/// [t-unaligned]: crate::Unaligned
/// [size-error-from]: error/struct.SizeError.html#method.from-1
/// [slice-dst]: KnownLayout#dynamically-sized-types
///
/// # Compile-Time Assertions
///
/// This method cannot yet be used on unsized types whose dynamically-sized
/// component is zero-sized. Attempting to use this method on such types
/// results in a compile-time assertion error; e.g.:
///
/// ```compile_fail,E0080
/// use zerocopy::*;
/// # use zerocopy_derive::*;
///
/// #[derive(Immutable, KnownLayout)]
/// #[repr(C)]
/// struct ZSTy {
/// leading_sized: u16,
/// trailing_dst: [()],
/// }
///
/// let _ = Ref::<_, ZSTy>::from_prefix(&b"UU"[..]); // ⚠ Compile Error!
/// ```
///
#[doc = codegen_section!(
header = "h5",
bench = "ref_type_from_prefix",
format = "coco",
arity = 3,
[
open
@index 1
@title "Sized"
@variant "static_size"
],
[
@index 2
@title "Unsized"
@variant "dynamic_size"
],
[
@index 3
@title "Dynamically Padded"
@variant "dynamic_padding"
]
)]
#[must_use = "has no side effects"]
#[inline]
pub fn from_prefix(source: B) -> Result<(Ref<B, T>, B), CastError<B, T>> {
static_assert_dst_is_not_zst!(T);
let (metadata, remainder) = match Ptr::from_ref(source.deref())
.try_cast_into::<T, BecauseImmutable>(CastType::Prefix, None)
{
Ok((ptr, remainder)) => (T::pointer_to_metadata(ptr.as_inner().as_ptr()), remainder),
Err(e) => {
return Err(e.with_src(()).with_src(source));
}
};
// SAFETY: `remainder` is constructed as a subset of `source`, and so it
// cannot have a larger size than `source`. Both of their `len` methods
// measure bytes (`source` deref's to `[u8]`, and `remainder` is a
// `Ptr<[u8]>`), so `source.len() >= remainder.len()`. Thus, this cannot
// underflow.
#[allow(unstable_name_collisions)]
let split_at = unsafe { source.len().unchecked_sub(remainder.len()) };
let (bytes, suffix) = source.split_at(split_at).map_err(|b| SizeError::new(b).into())?;
// SAFETY: By its safety postcondition, `try_cast_into` returned an
// `Ptr<T>` aligned as required for references [2], and a remainder
// which are non-overlapping, cover `source.deref()`, and place the
// `Ptr<T>` at `source.deref()`'s address.
// Therefore, `split_at` is the typed pointer's byte length. By the
// safety postcondition of `SplitByteSlice::split_at`, `bytes` has that
// length and address. `pointer_to_metadata` returns the metadata stored
// in the typed pointer, and `size_for_metadata` computes the
// corresponding dynamically-known byte size [1]. Thus,
// `T::size_for_metadata(metadata)` is `Some(bytes.len())`. The
// `SplitByteSlice` and `ByteSlice` contracts preserve the established
// address and length, satisfying both preconditions of
// `new_unchecked`.
//
// [1] Per https://doc.rust-lang.org/1.56.0/core/mem/fn.size_of_val.html:
//
// Returns the size of the pointed-to value in bytes.
//
// ... when `T` has no statically-known size ... `size_of_val` can
// be used to get the dynamically-known size.
//
// [2] Per https://doc.rust-lang.org/1.56.0/core/mem/fn.align_of_val.html:
//
// Returns the ABI-required minimum alignment of the type of the
// value that `val` points to.
//
// Every reference to a value of the type `T` must be a multiple of
// this number.
let r = unsafe { Ref::new_unchecked(bytes, metadata) };
Ok((r, suffix))
}
/// Constructs a `Ref` from the suffix of a byte slice.
///
/// This method computes the [largest possible size of `T`][valid-size] that
/// can fit in the trailing bytes of `source`, then attempts to return both
/// a `Ref` to those bytes, and a reference to the preceding bytes. If there
/// are insufficient bytes, or if that suffix of `source` is not
/// appropriately aligned, this returns `Err`. If [`T:
/// Unaligned`][t-unaligned], you can [infallibly discard the alignment
/// error][size-error-from].
///
/// `T` may be a sized type, a slice, or a [slice DST][slice-dst].
///
/// [valid-size]: crate::KnownLayout#what-is-a-valid-size
/// [t-unaligned]: crate::Unaligned
/// [size-error-from]: error/struct.SizeError.html#method.from-1
/// [slice-dst]: KnownLayout#dynamically-sized-types
///
/// # Compile-Time Assertions
///
/// This method cannot yet be used on unsized types whose dynamically-sized
/// component is zero-sized. Attempting to use this method on such types
/// results in a compile-time assertion error; e.g.:
///
/// ```compile_fail,E0080
/// use zerocopy::*;
/// # use zerocopy_derive::*;
///
/// #[derive(Immutable, KnownLayout)]
/// #[repr(C)]
/// struct ZSTy {
/// leading_sized: u16,
/// trailing_dst: [()],
/// }
///
/// let _ = Ref::<_, ZSTy>::from_suffix(&b"UU"[..]); // ⚠ Compile Error!
/// ```
///
#[doc = codegen_section!(
header = "h5",
bench = "ref_type_from_suffix",
format = "coco",
arity = 3,
[
open
@index 1
@title "Sized"
@variant "static_size"
],
[
@index 2
@title "Unsized"
@variant "dynamic_size"
],
[
@index 3
@title "Dynamically Padded"
@variant "dynamic_padding"
]
)]
#[must_use = "has no side effects"]
#[inline]
pub fn from_suffix(source: B) -> Result<(B, Ref<B, T>), CastError<B, T>> {
static_assert_dst_is_not_zst!(T);
let (metadata, remainder) = match Ptr::from_ref(source.deref())
.try_cast_into::<T, BecauseImmutable>(CastType::Suffix, None)
{
Ok((ptr, remainder)) => (T::pointer_to_metadata(ptr.as_inner().as_ptr()), remainder),
Err(e) => {
let e = e.with_src(());
return Err(e.with_src(source));
}
};
let split_at = remainder.len();
let (prefix, bytes) = source.split_at(split_at).map_err(|b| SizeError::new(b).into())?;
// SAFETY: By its safety postcondition, `try_cast_into` returned an
// `Ptr<T>` aligned as required for references [2], and a remainder
// which are non-overlapping, cover `source.deref()`, and place the
// remainder at `source.deref()`'s address. More precisely,
// `PtrInner::try_cast_into` constructs the typed suffix from the
// `r_slice` produced by `split_at_unchecked(split_at)` and returns its
// `l_slice` as `remainder`. Thus, even when `T` is zero-sized, the typed
// pointer and the `bytes` produced by splitting `source` at
// `remainder.len()` have the same address and length. By the safety
// postcondition of `SplitByteSlice::split_at`, `bytes` retains that
// address and length. `pointer_to_metadata` returns the metadata stored
// in the typed pointer, and `size_for_metadata` computes the
// corresponding dynamically-known byte size [1]. Thus,
// `T::size_for_metadata(metadata)` is `Some(bytes.len())`. The
// `SplitByteSlice` and `ByteSlice` contracts preserve the established
// address and length, satisfying both preconditions of
// `new_unchecked`.
//
// [1] Per https://doc.rust-lang.org/1.56.0/core/mem/fn.size_of_val.html:
//
// Returns the size of the pointed-to value in bytes.
//
// ... when `T` has no statically-known size ... `size_of_val` can
// be used to get the dynamically-known size.
//
// [2] Per https://doc.rust-lang.org/1.56.0/core/mem/fn.align_of_val.html:
//
// Returns the ABI-required minimum alignment of the type of the
// value that `val` points to.
//
// Every reference to a value of the type `T` must be a multiple of
// this number.
let r = unsafe { Ref::new_unchecked(bytes, metadata) };
Ok((prefix, r))
}
}
impl<B, T> Ref<B, T>
where
B: ByteSlice,
T: KnownLayout<PointerMetadata = usize> + Immutable + ?Sized,
{
/// Constructs a `Ref` from the given bytes with DST length equal to `count`
/// without copying.
///
/// This method attempts to return a `Ref` to the prefix of `source`
/// interpreted as a `T` with `count` trailing elements, and a reference to
/// the remaining bytes. If the length of `source` is not equal to the size
/// of `Self` with `count` elements, or if `source` is not appropriately
/// aligned, this returns `Err`. If [`T: Unaligned`][t-unaligned], you can
/// [infallibly discard the alignment error][size-error-from].
///
/// [t-unaligned]: crate::Unaligned
/// [size-error-from]: error/struct.SizeError.html#method.from-1
///
/// # Compile-Time Assertions
///
/// This method cannot yet be used on unsized types whose dynamically-sized
/// component is zero-sized. Attempting to use this method on such types
/// results in a compile-time assertion error; e.g.:
///
/// ```compile_fail,E0080
/// use zerocopy::*;
/// # use zerocopy_derive::*;
///
/// #[derive(Immutable, KnownLayout)]
/// #[repr(C)]
/// struct ZSTy {
/// leading_sized: u16,
/// trailing_dst: [()],
/// }
///
/// let _ = Ref::<_, ZSTy>::from_bytes_with_elems(&b"UU"[..], 42); // ⚠ Compile Error!
/// ```
///
#[doc = codegen_section!(
header = "h5",
bench = "ref_type_from_bytes_with_elems",
format = "coco",
arity = 2,
[
open
@index 1
@title "Unsized"
@variant "dynamic_size"
],
[
@index 2
@title "Dynamically Padded"
@variant "dynamic_padding"
]
)]
#[inline]
pub fn from_bytes_with_elems(source: B, count: usize) -> Result<Ref<B, T>, CastError<B, T>> {
static_assert_dst_is_not_zst!(T);
let expected_len = match T::size_for_metadata(count) {
Some(len) => len,
None => return Err(SizeError::new(source).into()),
};
if source.len() != expected_len {
return Err(SizeError::new(source).into());
}
if let Err(e) = Ptr::from_ref(source.deref())
.try_cast_into_no_leftover::<T, BecauseImmutable>(Some(count))
{
return Err(e.with_src(()).with_src(source));
}
// SAFETY: `size_for_metadata(count)` returned `expected_len`, and the
// equality check established that this is `source.len()`. This is the
// dynamically-known byte size described by `size_of_val` [1]. By its
// safety postcondition, `try_cast_into_no_leftover(Some(count))` only
// succeeded after producing a `Ptr<T>` aligned as required for
// references [2], with that metadata over the same byte range as
// `source.deref()`. `ByteSlice` and its applicable subtraits guarantee
// that the stored `source` continues to dereference or convert to that
// address and length. These facts establish both preconditions of
// `new_unchecked`.
//
// [1] Per https://doc.rust-lang.org/1.56.0/core/mem/fn.size_of_val.html:
//
// Returns the size of the pointed-to value in bytes.
//
// ... when `T` has no statically-known size ... `size_of_val` can
// be used to get the dynamically-known size.
//
// [2] Per https://doc.rust-lang.org/1.56.0/core/mem/fn.align_of_val.html:
//
// Returns the ABI-required minimum alignment of the type of the
// value that `val` points to.
//
// Every reference to a value of the type `T` must be a multiple of
// this number.
Ok(unsafe { Ref::new_unchecked(source, count) })
}
}
impl<B, T> Ref<B, T>
where
B: SplitByteSlice,
T: KnownLayout<PointerMetadata = usize> + Immutable + ?Sized,
{
/// Constructs a `Ref` from the prefix of the given bytes with DST
/// length equal to `count` without copying.
///
/// This method attempts to return a `Ref` to the prefix of `source`
/// interpreted as a `T` with `count` trailing elements, and a reference to
/// the remaining bytes. If there are insufficient bytes, or if `source` is
/// not appropriately aligned, this returns `Err`. If [`T:
/// Unaligned`][t-unaligned], you can [infallibly discard the alignment
/// error][size-error-from].
///
/// [t-unaligned]: crate::Unaligned
/// [size-error-from]: error/struct.SizeError.html#method.from-1
///
/// # Compile-Time Assertions
///
/// This method cannot yet be used on unsized types whose dynamically-sized
/// component is zero-sized. Attempting to use this method on such types
/// results in a compile-time assertion error; e.g.:
///
/// ```compile_fail,E0080
/// use zerocopy::*;
/// # use zerocopy_derive::*;
///
/// #[derive(Immutable, KnownLayout)]
/// #[repr(C)]
/// struct ZSTy {
/// leading_sized: u16,
/// trailing_dst: [()],
/// }
///
/// let _ = Ref::<_, ZSTy>::from_prefix_with_elems(&b"UU"[..], 42); // ⚠ Compile Error!
/// ```
///
#[doc = codegen_section!(
header = "h5",
bench = "ref_type_from_prefix_with_elems",
format = "coco",
arity = 2,
[
open
@index 1
@title "Unsized"
@variant "dynamic_size"
],
[
@index 2
@title "Dynamically Padded"
@variant "dynamic_padding"
]
)]
#[inline]
pub fn from_prefix_with_elems(
source: B,
count: usize,
) -> Result<(Ref<B, T>, B), CastError<B, T>> {
static_assert_dst_is_not_zst!(T);
let expected_len = match T::size_for_metadata(count) {
Some(len) => len,
None => return Err(SizeError::new(source).into()),
};
let (prefix, bytes) = source.split_at(expected_len).map_err(SizeError::new)?;
Self::from_bytes_with_elems(prefix, count).map(move |l| (l, bytes))
}
/// Constructs a `Ref` from the suffix of the given bytes with DST length
/// equal to `count` without copying.
///
/// This method attempts to return a `Ref` to the suffix of `source`
/// interpreted as a `T` with `count` trailing elements, and a reference to
/// the preceding bytes. If there are insufficient bytes, or if that suffix
/// of `source` is not appropriately aligned, this returns `Err`. If [`T:
/// Unaligned`][t-unaligned], you can [infallibly discard the alignment
/// error][size-error-from].
///
/// [t-unaligned]: crate::Unaligned
/// [size-error-from]: error/struct.SizeError.html#method.from-1
///
/// # Compile-Time Assertions
///
/// This method cannot yet be used on unsized types whose dynamically-sized
/// component is zero-sized. Attempting to use this method on such types
/// results in a compile-time assertion error; e.g.:
///
/// ```compile_fail,E0080
/// use zerocopy::*;
/// # use zerocopy_derive::*;
///
/// #[derive(Immutable, KnownLayout)]
/// #[repr(C)]
/// struct ZSTy {
/// leading_sized: u16,
/// trailing_dst: [()],
/// }
///
/// let _ = Ref::<_, ZSTy>::from_suffix_with_elems(&b"UU"[..], 42); // ⚠ Compile Error!
/// ```
///
#[doc = codegen_section!(
header = "h5",
bench = "ref_type_from_suffix_with_elems",
format = "coco",
arity = 2,
[
open
@index 1
@title "Unsized"
@variant "dynamic_size"
],
[
@index 2
@title "Dynamically Padded"
@variant "dynamic_padding"
]
)]
#[inline]
pub fn from_suffix_with_elems(
source: B,
count: usize,
) -> Result<(B, Ref<B, T>), CastError<B, T>> {
static_assert_dst_is_not_zst!(T);
let expected_len = match T::size_for_metadata(count) {
Some(len) => len,
None => return Err(SizeError::new(source).into()),
};
let split_at = if let Some(split_at) = source.len().checked_sub(expected_len) {
split_at
} else {
return Err(SizeError::new(source).into());
};
// SAFETY: The preceding `source.len().checked_sub(expected_len)`
// guarantees that `split_at` is in-bounds.
let (bytes, suffix) = unsafe { source.split_at_unchecked(split_at) };
Self::from_bytes_with_elems(suffix, count).map(move |l| (bytes, l))
}
}
impl<'a, B, T> Ref<B, T>
where
B: 'a + IntoByteSlice<'a>,
T: FromBytes + KnownLayout + Immutable + ?Sized,
{
/// Converts this `Ref` into a reference.
///
/// `into_ref` consumes the `Ref`, and returns a reference to `T`.
///
/// Note: this is an associated function, which means that you have to call
/// it as `Ref::into_ref(r)` instead of `r.into_ref()`. This is so that
/// there is no conflict with a method on the inner type.
///
#[doc = codegen_section!(
header = "h5",
bench = "ref_type_into_ref",
format = "coco",
arity = 3,
[
open
@index 1
@title "Sized"
@variant "static_size"
],
[
@index 2
@title "Unsized"
@variant "dynamic_size"
],
[
@index 3
@title "Dynamically Padded"
@variant "dynamic_padding"
]
)]
#[must_use = "has no side effects"]
#[inline(always)]
pub fn into_ref(r: Self) -> &'a T {
// Presumably unreachable, since we've guarded each constructor of `Ref`.
static_assert_dst_is_not_zst!(T);
let metadata = r.pointer_metadata();
// SAFETY: We don't call any methods on `b` other than those provided by
// `IntoByteSlice`.
let b = unsafe { r.into_byte_slice() };
let b = b.into_byte_slice();
if let crate::layout::SizeInfo::Sized { .. } = T::LAYOUT.size_info {
let ptr = Ptr::from_ref(b);
// SAFETY: We just checked that `T: Sized`. By invariant on `r`,
// `b`'s size is equal to `size_of::<T>()`.
let ptr = unsafe { cast_for_sized::<T, _, _, _>(ptr) };
// SAFETY: None of the preceding transformations modifies the
// address of the pointer, and by invariant on `r`, we know that it
// is validly-aligned.
let ptr = unsafe { ptr.assume_alignment::<Aligned>() };
return ptr.as_ref();
}
// PANICS: By post-condition on `into_byte_slice`, `b` is aligned for
// `T`, and its size is exactly the size encoded by `metadata`. By
// post-condition, `b.into_byte_slice()` produces a byte slice with
// identical address and length to that produced by `b.deref()`.
let ptr = Ptr::from_ref(b.into_byte_slice())
.try_cast_into_no_leftover::<T, BecauseImmutable>(Some(metadata))
.expect("zerocopy internal error: into_ref should be infallible");
let ptr = ptr.recall_validity();
ptr.as_ref()
}
}
impl<'a, B, T> Ref<B, T>
where
B: 'a + IntoByteSliceMut<'a>,
T: FromBytes + IntoBytes + KnownLayout + ?Sized,
{
/// Converts this `Ref` into a mutable reference.
///
/// `into_mut` consumes the `Ref`, and returns a mutable reference to `T`.