1
- // ignore-tidy-undocumented-unsafe
2
-
3
1
use crate :: cmp;
4
2
use crate :: fmt;
5
3
use crate :: mem;
@@ -77,6 +75,8 @@ impl Layout {
77
75
return Err ( LayoutErr { private : ( ) } ) ;
78
76
}
79
77
78
+ // SAFETY: the conditions for `from_size_align_unchecked` have been
79
+ // checked above.
80
80
unsafe { Ok ( Layout :: from_size_align_unchecked ( size, align) ) }
81
81
}
82
82
@@ -115,7 +115,7 @@ impl Layout {
115
115
#[ inline]
116
116
pub const fn new < T > ( ) -> Self {
117
117
let ( size, align) = size_align :: < T > ( ) ;
118
- // Note that the align is guaranteed by rustc to be a power of two and
118
+ // SAFETY: the align is guaranteed by Rust to be a power of two and
119
119
// the size+align combo is guaranteed to fit in our address space. As a
120
120
// result use the unchecked constructor here to avoid inserting code
121
121
// that panics if it isn't optimized well enough.
@@ -129,8 +129,8 @@ impl Layout {
129
129
#[ inline]
130
130
pub fn for_value < T : ?Sized > ( t : & T ) -> Self {
131
131
let ( size, align) = ( mem:: size_of_val ( t) , mem:: align_of_val ( t) ) ;
132
- // See rationale in `new` for why this is using an unsafe variant below
133
132
debug_assert ! ( Layout :: from_size_align( size, align) . is_ok( ) ) ;
133
+ // SAFETY: see rationale in `new` for why this is using an unsafe variant below
134
134
unsafe { Layout :: from_size_align_unchecked ( size, align) }
135
135
}
136
136
@@ -143,7 +143,7 @@ impl Layout {
143
143
#[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
144
144
#[ inline]
145
145
pub const fn dangling ( & self ) -> NonNull < u8 > {
146
- // align is non-zero and a power of two
146
+ // SAFETY: align is guaranteed to be non-zero
147
147
unsafe { NonNull :: new_unchecked ( self . align ( ) as * mut u8 ) }
148
148
}
149
149
@@ -249,11 +249,9 @@ impl Layout {
249
249
let padded_size = self . size ( ) + self . padding_needed_for ( self . align ( ) ) ;
250
250
let alloc_size = padded_size. checked_mul ( n) . ok_or ( LayoutErr { private : ( ) } ) ?;
251
251
252
- unsafe {
253
- // self.align is already known to be valid and alloc_size has been
254
- // padded already.
255
- Ok ( ( Layout :: from_size_align_unchecked ( alloc_size, self . align ( ) ) , padded_size) )
256
- }
252
+ // SAFETY: self.align is already known to be valid and alloc_size has been
253
+ // padded already.
254
+ unsafe { Ok ( ( Layout :: from_size_align_unchecked ( alloc_size, self . align ( ) ) , padded_size) ) }
257
255
}
258
256
259
257
/// Creates a layout describing the record for `self` followed by
0 commit comments