@@ -125,37 +125,113 @@ macro_rules! __rust_force_expr {
125
125
} ;
126
126
}
127
127
128
- /// Default coallocation "cooperation" (`COOP_PREF`) generic parameter.
128
+ // ----- CoAlloc constant-like macros:
129
+ /// "Yes" as a type's preference for coallocation (in either user space, or `std` space).
129
130
///
130
- /// NOT for public use. It's exported only so that library/proc_macro (and other internals) can use
131
- /// this.
131
+ /// It may be overriden by the allocator. For example, if the allocator doesn't support
132
+ /// coallocation, then this value makes no difference .
132
133
///
133
- // FIXME replace with a `const` (or some kind of compile time preference) once a related ICE is
134
- // fixed. Then move the const to a submodule, for example alloc::co_alloc.
134
+ /// This constant and its type WILL CHANGE (once ``#![feature(generic_const_exprs)]` and
135
+ /// `#![feature(adt_const_params)]` are stable) to a dedicated struct/enum. Hence DO NOT hard
136
+ /// code/replace/mix this any other values/parameters.
137
+ #[ unstable( feature = "global_co_alloc_meta" , issue = "none" ) ]
138
+ #[ macro_export]
139
+ macro_rules! CO_ALLOC_PREF_META_YES {
140
+ ( ) => {
141
+ ( 1 as $crate:: co_alloc:: CoAllocMetaNumSlotsPref )
142
+ } ;
143
+ }
144
+
145
+ /// "No" as a type's preference for coallocation (in either user space, or `std` space).
146
+ ///
147
+ /// Any allocator is required to respect this. Even if the allocator does support coallocation, it
148
+ /// will not coallocate types that use this value.
149
+ ///
150
+ /// This constant and its type WILL CHANGE (once ``#![feature(generic_const_exprs)]` and
151
+ /// `#![feature(adt_const_params)]` are stable) to a dedicated struct/enum. Hence DO NOT hard
152
+ /// code/replace/mix this any other values/parameters.
153
+ #[ unstable( feature = "global_co_alloc_meta" , issue = "none" ) ]
154
+ #[ macro_export]
155
+ macro_rules! CO_ALLOC_PREF_META_NO {
156
+ ( ) => {
157
+ ( 0 as $crate:: co_alloc:: CoAllocMetaNumSlotsPref )
158
+ } ;
159
+ }
160
+
161
+ /// Default preference for coallocation (in either user space, or `std` space).
162
+ ///
163
+ /// Possible values are only: `CO_ALLOC_PREF_META_YES` and `CO_ALLOC_PREF_META_NO`. See their
164
+ /// documentation.
165
+ ///
166
+ /// This value and its type WILL CHANGE (once ``#![feature(generic_const_exprs)]` and
167
+ /// `#![feature(adt_const_params)]` are stable) to a dedicated struct/enum. Hence DO NOT hard
168
+ /// code/replace/mix this any other values/parameters.
169
+ ///
170
+ /// (@FIXME) This WILL BE BECOME OBSOLETE and it WILL BE REPLACED with a `const` (and/or some kind
171
+ /// of compile time preference) once a related ICE is fixed (@FIXME add the ICE link here). Then
172
+ /// consider moving such a `const` to a submodule, for example `::alloc::co_alloc`.
135
173
#[ unstable( feature = "global_co_alloc_default" , issue = "none" ) ]
136
174
#[ macro_export]
137
- macro_rules! CO_ALLOC_PREF_DEFAULT {
175
+ macro_rules! CO_ALLOC_PREF_META_DEFAULT {
138
176
( ) => {
139
- true
177
+ ( 0 as $crate:: co_alloc:: CoAllocMetaNumSlotsPref )
178
+ } ;
179
+ }
180
+
181
+ /// Default [::alloc::CoAllocPref] value/config, based on `CO_ALLOC_PREF_META_DEFAULT`.
182
+ #[ unstable( feature = "global_co_alloc_meta" , issue = "none" ) ]
183
+ #[ macro_export]
184
+ macro_rules! CO_ALLOC_PREF_DEFAULT {
185
+ ( ) => { $crate:: co_alloc_pref!( $crate:: CO_ALLOC_PREF_META_DEFAULT !( ) ) } ;
186
+ }
187
+
188
+ // ------ CoAlloc preference/config conversion macros:
189
+ /// Create a `CoAllocPref` value based on the given parameter(s). For now, only one parameter is
190
+ /// supported, and it's required: `meta_pref`.
191
+ ///
192
+ /// @param `meta_pref` is one of: `CO_ALLOC_PREF_META_YES, CO_ALLOC_PREF_META_NO,
193
+ /// CO_ALLOC_PREF_META_DEFAULT`.
194
+ ///
195
+ /// @return `CoAllocPref` value
196
+ #[ unstable( feature = "global_co_alloc_meta" , issue = "none" ) ]
197
+ #[ macro_export]
198
+ macro_rules! co_alloc_pref {
199
+ // ($meta_pref + (0 as CoAllocMetaNumSlotsPref)) ensures that $meta_pref is of type
200
+ // `CoAllocMetaNumSlotsPref`. Otherwise the casting of the result to `CoAllocPref` would not
201
+ // report the incorrect type of $meta_pref (if $meta_pref were some other integer, casting would
202
+ // compile, and we would not be notified).
203
+ ( $meta_pref: expr) => {
204
+ ( ( $meta_pref + ( 0 as $crate:: co_alloc:: CoAllocMetaNumSlotsPref ) ) as $crate:: co_alloc:: CoAllocPref )
140
205
} ;
141
206
}
142
- // -\---> replace with something like: pub const DEFAULT_COOP_PREF: bool = true;
143
207
144
208
/// Return 0 or 1, indicating whether to use coallocation metadata (or not) with the given allocator
145
- /// type `alloc` and cooperation preference `coop_pref `.
209
+ /// type `alloc` and cooperation preference `co_alloc_pref `.
146
210
///
147
- /// NOT for public use. Param `coop_pref ` - can override the allocator's default preference for
211
+ /// NOT for public use. Param `co_alloc_pref ` - can override the allocator's default preference for
148
212
/// cooperation, or can make the type not cooperative, regardless of whether allocator `A` is
149
213
/// cooperative.
214
+ ///
215
+ /// @param `alloc` Allocator (implementation) type. @param `co_alloc_pref` The heap-based type's
216
+ /// preference for coallocation, as an [::alloc::CoAllocPref] value.
217
+ ///
218
+ /// The type of second parameter `co_alloc_pref` WILL CHANGE. DO NOT hardcode/cast/mix that type.
219
+ /// Instead, use [::alloc::CoAllocPref].
220
+ ///
150
221
// FIXME replace the macro with an (updated version of the below) `const` function). Only once
151
222
// generic_const_exprs is stable (that is, when consumer crates don't need to declare
152
- // generic_const_exprs feature anymore). Then move the function to a submodule, for example
153
- // ::alloc::co_alloc.
223
+ // generic_const_exprs feature anymore). Then consider moving the function to a submodule, for
224
+ // example ::alloc::co_alloc.
154
225
#[ unstable( feature = "global_co_alloc" , issue = "none" ) ]
155
226
#[ macro_export]
156
227
macro_rules! meta_num_slots {
157
- ( $alloc: ty, $coop_pref: expr) => {
158
- if ( $alloc:: CO_ALLOC_META_NUM_SLOTS ) && ( $coop_pref) { 1 } else { 0 }
228
+ // This "validates" types of both params - to prevent mix ups.
229
+ ( $alloc: ty, $co_alloc_pref: expr) => {
230
+ (
231
+ ( ( <$alloc>:: CO_ALLOC_META_NUM_SLOTS + ( 0 as :: core:: alloc:: CoAllocatorMetaNumSlots ) )
232
+ as $crate:: co_alloc:: CoAllocPref )
233
+ * ( $co_alloc_pref + ( 0 as $crate:: co_alloc:: CoAllocPref ) )
234
+ as usize )
159
235
} ;
160
236
}
161
237
// -\---> replace with something like:
@@ -171,46 +247,46 @@ pub const fn meta_num_slots<A: Allocator>(
171
247
/// Like `meta_num_slots`, but for the default coallocation preference (`DEFAULT_COOP_PREF`).
172
248
///
173
249
/// Return 0 or 1, indicating whether to use coallocation metadata (or not) with the given allocator
174
- /// type `alloc` and the default cooperation preference (`DEFAULT_COOP_PREF()!`).
250
+ /// type `alloc` and the default coallocation preference (`DEFAULT_COOP_PREF()!`).
175
251
///
176
- /// NOT for public use.
177
252
// FIXME replace the macro with a `const` function. Only once generic_const_exprs is stable (that
178
- // is, when consumer crates don't need to declare generic_const_exprs feature anymore). Then move
179
- // the function to a submodule, for example ::alloc::co_alloc.
253
+ // is, when consumer crates don't need to declare generic_const_exprs feature anymore). Then
254
+ // consider moving the function to a submodule, for example ::alloc::co_alloc.
180
255
#[ unstable( feature = "global_co_alloc" , issue = "none" ) ]
181
256
#[ macro_export]
182
257
macro_rules! meta_num_slots_default {
183
258
// Can't generate if ... {1} else {0}
184
259
// because it's "overly complex generic constant".
185
260
( $alloc: ty) => {
186
- if ( <$alloc> :: CO_ALLOC_META_NUM_SLOTS ) && ( DEFAULT_COOP_PREF !( ) ) { 1 } else { 0 }
261
+ $crate :: meta_num_slots! ( $alloc , $crate :: CO_ALLOC_PREF_DEFAULT !( ) )
187
262
} ;
188
263
}
189
264
190
- /// NOT for public use.
191
- // See above.
265
+ /// Like `meta_num_slots`, but for the default coallocation preference (`DEFAULT_COOP_PREF`).
266
+ ///
267
+ /// Return 0 or 1, indicating whether to use coallocation metadata (or not) with the global allocator
268
+ /// type `alloc` and the given coallocation preference `co_alloc_`.
269
+ ///
270
+ // FIXME replace the macro with a `const` function. Only once generic_const_exprs is stable (that
271
+ // is, when consumer crates don't need to declare `generic_const_exprs` feature anymore). Then
272
+ // consider moving the function to a submodule, for example ::alloc::co_alloc. See above.
192
273
#[ unstable( feature = "global_co_alloc" , issue = "none" ) ]
193
274
#[ macro_export]
194
275
macro_rules! meta_num_slots_global {
195
- ( $coop_pref : expr) => {
196
- if :: alloc :: alloc:: Global :: CO_ALLOC_META_NUM_SLOTS && ( $coop_pref ) { 1 } else { 0 }
276
+ ( $co_alloc_pref : expr) => {
277
+ $crate :: meta_num_slots! ( $crate :: alloc:: Global , $co_alloc_pref )
197
278
} ;
198
279
}
199
280
200
- /// Like `meta_num_slots`, but for Global allocator and default coallocation preference
201
- /// (`DEFAULT_COOP_PREF `).
281
+ /// Like `meta_num_slots`, but for ` Global allocator and default coallocation preference
282
+ /// (`CO_ALLOC_PREF_DEFAULT `).
202
283
///
203
- /// NOT for public use.
204
- // @FIXME once generic_const_exprs is stable, replace this with a `const` function. Then move the
205
- // function to a submodule, for example alloc::co_alloc. See above.
284
+ // @FIXME once generic_const_exprs is stable, replace this with a `const` function. Then consider
285
+ // moving the function to a submodule, for example alloc::co_alloc. See above.
206
286
#[ unstable( feature = "global_co_alloc" , issue = "none" ) ]
207
287
#[ macro_export]
208
288
macro_rules! meta_num_slots_default_global {
209
289
( ) => {
210
- if :: alloc:: alloc:: Global :: CO_ALLOC_META_NUM_SLOTS && ( DEFAULT_COOP_PREF !( ) ) {
211
- 1
212
- } else {
213
- 0
214
- }
290
+ $crate:: meta_num_slots!( $crate:: alloc:: Global , $crate:: CO_ALLOC_PREF_DEFAULT !( ) )
215
291
} ;
216
292
}
0 commit comments