@@ -44,11 +44,12 @@ pub unsafe trait ByteValued: Copy + Default + Send + Sync {
44
44
return None ;
45
45
}
46
46
47
- // Safe because the ByteValued trait asserts any data is valid for this type, and we ensured
48
- // the size of the pointer's buffer is the correct size. The `align_to` method ensures that
49
- // we don't have any unaligned references. This aliases a pointer, but because the pointer
50
- // is from a const slice reference, there are no mutable aliases. Finally, the reference
51
- // returned can not outlive data because they have equal implicit lifetime constraints.
47
+ // SAFETY: Safe because the ByteValued trait asserts any data is valid for this type, and
48
+ // we ensured the size of the pointer's buffer is the correct size. The `align_to` method
49
+ // ensures that we don't have any unaligned references. This aliases a pointer, but because
50
+ // the pointer is from a const slice reference, there are no mutable aliases. Finally, the
51
+ // reference returned can not outlive data because they have equal implicit lifetime
52
+ // constraints.
52
53
match unsafe { data. align_to :: < Self > ( ) } {
53
54
( [ ] , [ mid] , [ ] ) => Some ( mid) ,
54
55
_ => None ,
@@ -69,12 +70,12 @@ pub unsafe trait ByteValued: Copy + Default + Send + Sync {
69
70
return None ;
70
71
}
71
72
72
- // Safe because the ByteValued trait asserts any data is valid for this type, and we ensured
73
- // the size of the pointer's buffer is the correct size. The `align_to` method ensures that
74
- // we don't have any unaligned references. This aliases a pointer, but because the pointer
75
- // is from a mut slice reference, we borrow the passed in mutable reference. Finally, the
76
- // reference returned can not outlive data because they have equal implicit lifetime
77
- // constraints.
73
+ // SAFETY: Safe because the ByteValued trait asserts any data is valid for this type, and
74
+ // we ensured the size of the pointer's buffer is the correct size. The `align_to` method
75
+ // ensures that we don't have any unaligned references. This aliases a pointer, but because
76
+ // the pointer is from a mut slice reference, we borrow the passed in mutable reference.
77
+ // Finally, the reference returned can not outlive data because they have equal implicit
78
+ // lifetime constraints.
78
79
match unsafe { data. align_to_mut :: < Self > ( ) } {
79
80
( [ ] , [ mid] , [ ] ) => Some ( mid) ,
80
81
_ => None ,
@@ -87,9 +88,9 @@ pub unsafe trait ByteValued: Copy + Default + Send + Sync {
87
88
/// The value of bytes in the returned slice will depend on the representation of the type in
88
89
/// memory, and may change in an unstable fashion.
89
90
fn as_slice ( & self ) -> & [ u8 ] {
90
- // Safe because the entire size of self is accessible as bytes because the trait guarantees
91
- // it. The lifetime of the returned slice is the same as the passed reference, so that no
92
- // dangling pointers will result from this pointer alias.
91
+ // SAFETY: Safe because the entire size of self is accessible as bytes because the trait
92
+ // guarantees it. The lifetime of the returned slice is the same as the passed reference,
93
+ // so that no dangling pointers will result from this pointer alias.
93
94
unsafe { from_raw_parts ( self as * const Self as * const u8 , size_of :: < Self > ( ) ) }
94
95
}
95
96
@@ -99,12 +100,12 @@ pub unsafe trait ByteValued: Copy + Default + Send + Sync {
99
100
/// immediately reflected in `self`. The value of bytes in the returned slice will depend on
100
101
/// the representation of the type in memory, and may change in an unstable fashion.
101
102
fn as_mut_slice ( & mut self ) -> & mut [ u8 ] {
102
- // Safe because the entire size of self is accessible as bytes because the trait guarantees
103
- // it. The trait also guarantees that any combination of bytes is valid for this type, so
104
- // modifying them in the form of a byte slice is valid. The lifetime of the returned slice
105
- // is the same as the passed reference, so that no dangling pointers will result from this
106
- // pointer alias. Although this does alias a mutable pointer, we do so by exclusively
107
- // borrowing the given mutable reference.
103
+ // SAFETY: Safe because the entire size of self is accessible as bytes because the trait
104
+ // guarantees it. The trait also guarantees that any combination of bytes is valid for this
105
+ // type, so modifying them in the form of a byte slice is valid. The lifetime of the
106
+ // returned slice is the same as the passed reference, so that no dangling pointers will
107
+ // result from this pointer alias. Although this does alias a mutable pointer, we do so by
108
+ // exclusively borrowing the given mutable reference.
108
109
unsafe { from_raw_parts_mut ( self as * mut Self as * mut u8 , size_of :: < Self > ( ) ) }
109
110
}
110
111
@@ -118,24 +119,25 @@ pub unsafe trait ByteValued: Copy + Default + Send + Sync {
118
119
/// that all accesses to `self` use volatile accesses (because there can
119
120
/// be no other accesses).
120
121
fn as_bytes ( & mut self ) -> VolatileSlice {
121
- unsafe {
122
- // This is safe because the lifetime is the same as self
123
- VolatileSlice :: new ( self as * mut Self as usize as * mut _ , size_of :: < Self > ( ) )
124
- }
122
+ // SAFETY: This is safe because the lifetime is the same as self
123
+ unsafe { VolatileSlice :: new ( self as * mut Self as usize as * mut _ , size_of :: < Self > ( ) ) }
125
124
}
126
125
}
127
126
128
- // All intrinsic types and arrays of intrinsic types are ByteValued. They are just numbers.
129
127
macro_rules! byte_valued_array {
130
128
( $T: ty, $( $N: expr) +) => {
131
129
$(
130
+ // SAFETY: All intrinsic types and arrays of intrinsic types are ByteValued.
131
+ // They are just numbers.
132
132
unsafe impl ByteValued for [ $T; $N] { }
133
133
) +
134
134
}
135
135
}
136
136
137
137
macro_rules! byte_valued_type {
138
138
( $T: ty) => {
139
+ // SAFETY: Safe as long T is POD.
140
+ // We are using this macro to generated the implementation for integer types below.
139
141
unsafe impl ByteValued for $T { }
140
142
byte_valued_array! {
141
143
$T,
@@ -329,6 +331,7 @@ pub trait Bytes<A> {
329
331
330
332
#[ cfg( test) ]
331
333
pub ( crate ) mod tests {
334
+ #![ allow( clippy:: undocumented_unsafe_blocks) ]
332
335
use super :: * ;
333
336
334
337
use std:: fmt:: Debug ;
0 commit comments