File tree 3 files changed +16
-0
lines changed
3 files changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -140,6 +140,18 @@ impl Layout {
140
140
unsafe { Layout :: from_size_align_unchecked ( size, align) }
141
141
}
142
142
143
+ /// Creates a `NonNull` that is dangling, but well-aligned for this Layout.
144
+ ///
145
+ /// Note that the pointer value may potentially represent a valid pointer to
146
+ /// a `T`, which means this must not be used as a "not yet initialized"
147
+ /// sentinel value. Types that lazily allocate must track initialization by
148
+ /// some other means.
149
+ #[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
150
+ pub const fn dangling ( & self ) -> NonNull < u8 > {
151
+ // align is non-zero and a power of two
152
+ unsafe { NonNull :: new_unchecked ( self . align ( ) as * mut u8 ) }
153
+ }
154
+
143
155
/// Creates a layout describing the record that can hold a value
144
156
/// of the same layout as `self`, but that also is aligned to
145
157
/// alignment `align` (measured in bytes).
Original file line number Diff line number Diff line change 1
1
use core:: alloc:: Layout ;
2
+ use core:: ptr:: NonNull ;
2
3
3
4
#[ test]
4
5
fn const_unchecked_layout ( ) {
5
6
const SIZE : usize = 0x2000 ;
6
7
const ALIGN : usize = 0x1000 ;
7
8
const LAYOUT : Layout = unsafe { Layout :: from_size_align_unchecked ( SIZE , ALIGN ) } ;
9
+ const DANGLING : NonNull < u8 > = LAYOUT . dangling ( ) ;
8
10
assert_eq ! ( LAYOUT . size( ) , SIZE ) ;
9
11
assert_eq ! ( LAYOUT . align( ) , ALIGN ) ;
12
+ assert_eq ! ( Some ( DANGLING ) , NonNull :: new( ALIGN as * mut u8 ) ) ;
10
13
}
Original file line number Diff line number Diff line change
1
+ #![ feature( alloc_layout_extra) ]
1
2
#![ feature( bool_to_option) ]
2
3
#![ feature( bound_cloned) ]
3
4
#![ feature( box_syntax) ]
You can’t perform that action at this time.
0 commit comments