@@ -136,29 +136,35 @@ impl<T> Unalign<T> {
136
136
/// not properly aligned.
137
137
///
138
138
/// If `self` does not satisfy `mem::align_of::<T>()`, then it is unsound to
139
- /// return a reference to the wrapped `T`, and `try_deref` returns `None `.
139
+ /// return a reference to the wrapped `T`, and `try_deref` returns `Err `.
140
140
///
141
141
/// If `T: Unaligned`, then `Unalign<T>` implements [`Deref`], and callers
142
142
/// may prefer [`Deref::deref`], which is infallible.
143
143
#[ inline( always) ]
144
- pub fn try_deref ( & self ) -> Option < & T > {
144
+ pub fn try_deref ( & self ) -> Result < & T , AlignmentError < & Self , T > > {
145
145
let inner = Ptr :: from_ref ( self ) . transparent_wrapper_into_inner ( ) ;
146
- inner. bikeshed_try_into_aligned ( ) . map ( Ptr :: as_ref)
146
+ match inner. bikeshed_try_into_aligned ( ) {
147
+ Ok ( aligned) => Ok ( aligned. as_ref ( ) ) ,
148
+ Err ( err) => Err ( err. map_src ( |src| src. into_unalign ( ) . as_ref ( ) ) ) ,
149
+ }
147
150
}
148
151
149
152
/// Attempts to return a mutable reference to the wrapped `T`, failing if
150
153
/// `self` is not properly aligned.
151
154
///
152
155
/// If `self` does not satisfy `mem::align_of::<T>()`, then it is unsound to
153
156
/// return a reference to the wrapped `T`, and `try_deref_mut` returns
154
- /// `None `.
157
+ /// `Err `.
155
158
///
156
159
/// If `T: Unaligned`, then `Unalign<T>` implements [`DerefMut`], and
157
160
/// callers may prefer [`DerefMut::deref_mut`], which is infallible.
158
161
#[ inline( always) ]
159
- pub fn try_deref_mut ( & mut self ) -> Option < & mut T > {
162
+ pub fn try_deref_mut ( & mut self ) -> Result < & mut T , AlignmentError < & mut Self , T > > {
160
163
let inner = Ptr :: from_mut ( self ) . transparent_wrapper_into_inner ( ) ;
161
- inner. bikeshed_try_into_aligned ( ) . map ( Ptr :: as_mut)
164
+ match inner. bikeshed_try_into_aligned ( ) {
165
+ Ok ( aligned) => Ok ( aligned. as_mut ( ) ) ,
166
+ Err ( err) => Err ( err. map_src ( |src| src. into_unalign ( ) . as_mut ( ) ) ) ,
167
+ }
162
168
}
163
169
164
170
/// Returns a reference to the wrapped `T` without checking alignment.
@@ -423,8 +429,8 @@ mod tests {
423
429
424
430
// Test methods that depend on alignment (when alignment is satisfied).
425
431
let mut u: Align < _ , AU64 > = Align :: new ( Unalign :: new ( AU64 ( 123 ) ) ) ;
426
- assert_eq ! ( u. t. try_deref( ) , Some ( & AU64 ( 123 ) ) ) ;
427
- assert_eq ! ( u. t. try_deref_mut( ) , Some ( & mut AU64 ( 123 ) ) ) ;
432
+ assert_eq ! ( u. t. try_deref( ) . unwrap ( ) , & AU64 ( 123 ) ) ;
433
+ assert_eq ! ( u. t. try_deref_mut( ) . unwrap ( ) , & mut AU64 ( 123 ) ) ;
428
434
// SAFETY: The `Align<_, AU64>` guarantees proper alignment.
429
435
assert_eq ! ( unsafe { u. t. deref_unchecked( ) } , & AU64 ( 123 ) ) ;
430
436
// SAFETY: The `Align<_, AU64>` guarantees proper alignment.
@@ -435,13 +441,13 @@ mod tests {
435
441
// Test methods that depend on alignment (when alignment is not
436
442
// satisfied).
437
443
let mut u: ForceUnalign < _ , AU64 > = ForceUnalign :: new ( Unalign :: new ( AU64 ( 123 ) ) ) ;
438
- assert_eq ! ( u. t. try_deref( ) , None ) ;
439
- assert_eq ! ( u. t. try_deref_mut( ) , None ) ;
444
+ assert ! ( matches! ( u. t. try_deref( ) , Err ( AlignmentError { .. } ) ) ) ;
445
+ assert ! ( matches! ( u. t. try_deref_mut( ) , Err ( AlignmentError { .. } ) ) ) ;
440
446
441
447
// Test methods that depend on `T: Unaligned`.
442
448
let mut u = Unalign :: new ( 123u8 ) ;
443
- assert_eq ! ( u. try_deref( ) , Some ( & 123 ) ) ;
444
- assert_eq ! ( u. try_deref_mut( ) , Some ( & mut 123 ) ) ;
449
+ assert_eq ! ( u. try_deref( ) , Ok ( & 123 ) ) ;
450
+ assert_eq ! ( u. try_deref_mut( ) , Ok ( & mut 123 ) ) ;
445
451
assert_eq ! ( u. deref( ) , & 123 ) ;
446
452
assert_eq ! ( u. deref_mut( ) , & mut 123 ) ;
447
453
* u = 21 ;
0 commit comments