@@ -52,16 +52,14 @@ macro_rules! safety_comment {
52
52
/// $ty>` which satisfies the preconditions of
53
53
/// `TryFromBytes::<$ty>::is_bit_valid`, it must be guaranteed that the
54
54
/// memory referenced by that `Ptr` always contains a valid `$repr`.
55
- /// - The alignment of `$repr` is less than or equal to the alignment of
56
- /// `$ty`.
57
55
/// - The impl of `is_bit_valid` must only return `true` for its argument
58
56
/// `Ptr<$repr>` if the original `Ptr<$ty>` refers to a valid `$ty`.
59
57
macro_rules! unsafe_impl {
60
58
// Implement `$trait` for `$ty` with no bounds.
61
- ( $( #[ $attr: meta] ) * $ty: ty: $trait: ident $( ; |$candidate: ident: & $repr: ty| $is_bit_valid: expr) ?) => {
59
+ ( $( #[ $attr: meta] ) * $ty: ty: $trait: ident $( ; |$candidate: ident: MaybeAligned < $repr: ty> | $is_bit_valid: expr) ?) => {
62
60
$( #[ $attr] ) *
63
61
unsafe impl $trait for $ty {
64
- unsafe_impl!( @method $trait $( ; |$candidate: & $repr| $is_bit_valid) ?) ;
62
+ unsafe_impl!( @method $trait $( ; |$candidate: MaybeAligned < $repr> | $is_bit_valid) ?) ;
65
63
}
66
64
} ;
67
65
// Implement all `$traits` for `$ty` with no bounds.
@@ -97,93 +95,88 @@ macro_rules! unsafe_impl {
97
95
$( #[ $attr: meta] ) *
98
96
const $constname: ident : $constty: ident $( , ) ?
99
97
$( $tyvar: ident $( : $( ? $optbound: ident $( +) ?) * $( $bound: ident $( +) ?) * ) ?) ,*
100
- => $trait: ident for $ty: ty $( ; |$candidate: ident $( : & $ref_repr: ty) ? $( : Ptr <$ptr_repr: ty>) ?| $is_bit_valid: expr) ?
98
+ => $trait: ident for $ty: ty $( ; |$candidate: ident $( : MaybeAligned < $ref_repr: ty> ) ? $( : MaybeValid <$ptr_repr: ty>) ?| $is_bit_valid: expr) ?
101
99
) => {
102
100
unsafe_impl!(
103
101
@inner
104
102
$( #[ $attr] ) *
105
103
@const $constname: $constty,
106
104
$( $tyvar $( : $( ? $optbound +) * + $( $bound +) * ) ?, ) *
107
- => $trait for $ty $( ; |$candidate $( : & $ref_repr) ? $( : Ptr <$ptr_repr>) ?| $is_bit_valid) ?
105
+ => $trait for $ty $( ; |$candidate $( : MaybeAligned < $ref_repr> ) ? $( : MaybeValid <$ptr_repr>) ?| $is_bit_valid) ?
108
106
) ;
109
107
} ;
110
108
(
111
109
$( #[ $attr: meta] ) *
112
110
$( $tyvar: ident $( : $( ? $optbound: ident $( +) ?) * $( $bound: ident $( +) ?) * ) ?) ,*
113
- => $trait: ident for $ty: ty $( ; |$candidate: ident $( : & $ref_repr: ty) ? $( : Ptr <$ptr_repr: ty>) ?| $is_bit_valid: expr) ?
111
+ => $trait: ident for $ty: ty $( ; |$candidate: ident $( : MaybeAligned < $ref_repr: ty> ) ? $( : MaybeValid <$ptr_repr: ty>) ?| $is_bit_valid: expr) ?
114
112
) => {
115
113
unsafe_impl!(
116
114
@inner
117
115
$( #[ $attr] ) *
118
116
$( $tyvar $( : $( ? $optbound +) * + $( $bound +) * ) ?, ) *
119
- => $trait for $ty $( ; |$candidate $( : & $ref_repr) ? $( : Ptr <$ptr_repr>) ?| $is_bit_valid) ?
117
+ => $trait for $ty $( ; |$candidate $( : MaybeAligned < $ref_repr> ) ? $( : MaybeValid <$ptr_repr>) ?| $is_bit_valid) ?
120
118
) ;
121
119
} ;
122
120
(
123
121
@inner
124
122
$( #[ $attr: meta] ) *
125
123
$( @const $constname: ident : $constty: ident, ) *
126
124
$( $tyvar: ident $( : $( ? $optbound: ident +) * + $( $bound: ident +) * ) ?, ) *
127
- => $trait: ident for $ty: ty $( ; |$candidate: ident $( : & $ref_repr: ty) ? $( : Ptr <$ptr_repr: ty>) ?| $is_bit_valid: expr) ?
125
+ => $trait: ident for $ty: ty $( ; |$candidate: ident $( : MaybeAligned < $ref_repr: ty> ) ? $( : MaybeValid <$ptr_repr: ty>) ?| $is_bit_valid: expr) ?
128
126
) => {
129
127
$( #[ $attr] ) *
130
128
unsafe impl <$( const $constname: $constty, ) * $( $tyvar $( : $( ? $optbound +) * $( $bound +) * ) ?) ,* > $trait for $ty {
131
- unsafe_impl!( @method $trait $( ; |$candidate: $( & $ref_repr) ? $( Ptr <$ptr_repr>) ?| $is_bit_valid) ?) ;
129
+ unsafe_impl!( @method $trait $( ; |$candidate: $( MaybeAligned < $ref_repr> ) ? $( MaybeValid <$ptr_repr>) ?| $is_bit_valid) ?) ;
132
130
}
133
131
} ;
134
132
135
- ( @method TryFromBytes ; |$candidate: ident: & $repr: ty| $is_bit_valid: expr) => {
133
+ ( @method TryFromBytes ; |$candidate: ident: MaybeAligned < $repr: ty> | $is_bit_valid: expr) => {
136
134
#[ allow( clippy:: missing_inline_in_public_items) ]
137
135
fn only_derive_is_allowed_to_implement_this_trait( ) { }
138
136
139
137
#[ inline]
140
- unsafe fn is_bit_valid( candidate: Ptr <' _, Self >) -> bool {
138
+ fn is_bit_valid( candidate: MaybeValid <' _, Self >) -> bool {
141
139
// SAFETY:
142
140
// - The argument to `cast_unsized` is `|p| p as *mut _` as required
143
141
// by that method's safety precondition.
144
142
// - The caller has promised that the cast results in an object of
145
143
// equal or lesser size.
146
- // - The caller has promised that `$repr`'s alignment is less than
147
- // or equal to `Self`'s alignment.
148
144
#[ allow( clippy:: as_conversions) ]
149
145
let candidate = unsafe { candidate. cast_unsized:: <$repr, _>( |p| p as * mut _) } ;
150
- // SAFETY:
151
- // - The caller has promised that the referenced memory region will
152
- // contain a valid `$repr` for `'a`.
153
- // - The memory may not be referenced by any mutable references.
154
- // This is a precondition of `is_bit_valid`.
155
- // - The memory may not be mutated even via `UnsafeCell`s. This is a
156
- // precondition of `is_bit_valid`.
157
- // - There must not exist any references to the same memory region
158
- // which contain `UnsafeCell`s at byte ranges which are not
159
- // identical to the byte ranges at which `T` contains
160
- // `UnsafeCell`s. This is a precondition of `is_bit_valid`.
161
- let $candidate: & $repr = unsafe { candidate. as_ref( ) } ;
146
+
147
+ // SAFETY: The caller has promised that the referenced memory region
148
+ // will contain a valid `$repr`.
149
+ let candidate = unsafe { candidate. assume_valid( ) } ;
150
+
151
+ let $candidate = MaybeAligned :: from_ptr( candidate) ;
162
152
$is_bit_valid
163
153
}
164
154
} ;
165
- ( @method TryFromBytes ; |$candidate: ident: Ptr <$repr: ty>| $is_bit_valid: expr) => {
155
+ ( @method TryFromBytes ; |$candidate: ident: MaybeValid <$repr: ty>| $is_bit_valid: expr) => {
166
156
#[ allow( clippy:: missing_inline_in_public_items) ]
167
157
fn only_derive_is_allowed_to_implement_this_trait( ) { }
168
158
169
159
#[ inline]
170
- unsafe fn is_bit_valid( candidate: Ptr <' _, Self >) -> bool {
160
+ fn is_bit_valid( candidate: MaybeValid <' _, Self >) -> bool {
171
161
// SAFETY:
172
162
// - The argument to `cast_unsized` is `|p| p as *mut _` as required
173
163
// by that method's safety precondition.
174
164
// - The caller has promised that the cast results in an object of
175
165
// equal or lesser size.
176
- // - The caller has promised that `$repr`'s alignment is less than
177
- // or equal to `Self`'s alignment.
178
166
#[ allow( clippy:: as_conversions) ]
179
167
let $candidate = unsafe { candidate. cast_unsized:: <$repr, _>( |p| p as * mut _) } ;
168
+
169
+ // SAFETY: The caller has promised that `$repr` is as-initialized as
170
+ // `Self`.
171
+ let $candidate = unsafe { $candidate. assume_as_initialized( ) } ;
172
+
180
173
$is_bit_valid
181
174
}
182
175
} ;
183
176
( @method TryFromBytes ) => {
184
177
#[ allow( clippy:: missing_inline_in_public_items) ]
185
178
fn only_derive_is_allowed_to_implement_this_trait( ) { }
186
- #[ inline( always) ] unsafe fn is_bit_valid( _: Ptr <' _, Self >) -> bool { true }
179
+ #[ inline( always) ] fn is_bit_valid( _: MaybeValid <' _, Self >) -> bool { true }
187
180
} ;
188
181
( @method $trait: ident) => {
189
182
#[ allow( clippy:: missing_inline_in_public_items) ]
0 commit comments