File tree 4 files changed +13
-22
lines changed
4 files changed +13
-22
lines changed Original file line number Diff line number Diff line change @@ -116,6 +116,14 @@ impl Closure {
116
116
/// C1.call();
117
117
/// C2.call();
118
118
/// ```
119
+ ///
120
+ /// Don't call it at runtime:
121
+ ///
122
+ /// ```rust,should_panic
123
+ /// use r3_core::closure::Closure;
124
+ /// let x = [1, 2, 3];
125
+ /// Closure::from_fn_const(move || { let _x = x; });
126
+ /// ```
119
127
pub const fn from_fn_const < T : FnOnce ( ) + Copy + Send + ' static > ( func : T ) -> Self {
120
128
let size = size_of :: < T > ( ) ;
121
129
let align = align_of :: < T > ( ) ;
@@ -128,6 +136,10 @@ impl Closure {
128
136
Self :: from_raw_parts ( trampoline_zst :: < T > , ClosureEnv ( None ) )
129
137
} else {
130
138
let env = core:: intrinsics:: const_allocate ( size, align) ;
139
+ assert ! (
140
+ !env. guaranteed_eq( core:: ptr:: null_mut( ) ) ,
141
+ "heap allocation failed"
142
+ ) ;
131
143
env. cast :: < T > ( ) . write ( func) ;
132
144
Self :: from_raw_parts ( trampoline_indirect :: < T > , transmute ( env) )
133
145
}
Original file line number Diff line number Diff line change 13
13
#![ feature( const_raw_ptr_comparison) ]
14
14
#![ feature( const_ptr_offset_from) ]
15
15
#![ feature( maybe_uninit_slice) ]
16
- #![ feature( const_eval_select) ]
17
16
#![ feature( const_mut_refs) ]
18
17
#![ feature( const_slice_from_raw_parts) ]
19
18
#![ feature( const_option) ]
Original file line number Diff line number Diff line change @@ -462,17 +462,7 @@ unsafe impl const rlsf::FlexSource for ConstFlexSource {
462
462
463
463
assert ! ( min_size != 0 ) ;
464
464
465
- // FIXME: Directly calling `const_allocate` from here causes the
466
- // compiler to panic
467
- // Safety: `const_allocate_{in_const, at_rt}` behave observably
468
- // equivalent... if their results are ever observed.
469
- let ptr = unsafe {
470
- core:: intrinsics:: const_eval_select (
471
- ( size, BLOCK_SIZE ) ,
472
- const_allocate_in_const,
473
- const_allocate_at_rt,
474
- )
475
- } ;
465
+ let ptr = unsafe { core:: intrinsics:: const_allocate ( size, BLOCK_SIZE ) } ;
476
466
477
467
// FIXME: `NonNull::new` is not `const fn` yet
478
468
assert ! ( !ptr. guaranteed_eq( core:: ptr:: null_mut( ) ) ) ;
@@ -485,12 +475,3 @@ unsafe impl const rlsf::FlexSource for ConstFlexSource {
485
475
BLOCK_SIZE
486
476
}
487
477
}
488
-
489
- const fn const_allocate_in_const ( size : usize , align : usize ) -> * mut u8 {
490
- // Safety: Technically it's not `unsafe`
491
- unsafe { core:: intrinsics:: const_allocate ( size, align) }
492
- }
493
-
494
- fn const_allocate_at_rt ( _: usize , _: usize ) -> * mut u8 {
495
- loop { }
496
- }
Original file line number Diff line number Diff line change 10
10
#![ feature( generic_const_exprs) ]
11
11
#![ feature( const_refs_to_cell) ]
12
12
#![ feature( maybe_uninit_slice) ]
13
- #![ feature( const_eval_select) ]
14
13
#![ feature( const_option_ext) ]
15
14
#![ feature( const_ptr_as_ref) ]
16
15
#![ feature( const_ptr_offset) ]
You can’t perform that action at this time.
0 commit comments