1- // ignore-tidy-undocumented-unsafe
2-
31use crate :: cmp;
42use crate :: fmt;
53use crate :: mem;
@@ -77,6 +75,8 @@ impl Layout {
7775 return Err ( LayoutErr { private : ( ) } ) ;
7876 }
7977
78+ // SAFETY: the conditions for `from_size_align_unchecked` have been
79+ // checked above.
8080 unsafe { Ok ( Layout :: from_size_align_unchecked ( size, align) ) }
8181 }
8282
@@ -115,7 +115,7 @@ impl Layout {
115115 #[ inline]
116116 pub const fn new < T > ( ) -> Self {
117117 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
119119 // the size+align combo is guaranteed to fit in our address space. As a
120120 // result use the unchecked constructor here to avoid inserting code
121121 // that panics if it isn't optimized well enough.
@@ -129,8 +129,8 @@ impl Layout {
129129 #[ inline]
130130 pub fn for_value < T : ?Sized > ( t : & T ) -> Self {
131131 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
133132 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
134134 unsafe { Layout :: from_size_align_unchecked ( size, align) }
135135 }
136136
@@ -143,7 +143,7 @@ impl Layout {
143143 #[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
144144 #[ inline]
145145 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
147147 unsafe { NonNull :: new_unchecked ( self . align ( ) as * mut u8 ) }
148148 }
149149
@@ -249,11 +249,9 @@ impl Layout {
249249 let padded_size = self . size ( ) + self . padding_needed_for ( self . align ( ) ) ;
250250 let alloc_size = padded_size. checked_mul ( n) . ok_or ( LayoutErr { private : ( ) } ) ?;
251251
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) ) }
257255 }
258256
259257 /// Creates a layout describing the record for `self` followed by
0 commit comments