Skip to content

Commit ddb3a41

Browse files
CoAlloc: Move + rename macros DEFAULT_COOP_PREF + meta_num_slots* macros to ::alloc
1 parent 71b8f3d commit ddb3a41

File tree

1 file changed

+109
-30
lines changed

1 file changed

+109
-30
lines changed

library/alloc/src/macros.rs

Lines changed: 109 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// Creates a [`Vec`] containing the arguments.
22
///
3-
/// `vec!` allows `Vec`s to be defined with the same syntax as array expressions.
4-
/// There are two forms of this macro:
3+
/// `vec!` allows `Vec`s to be defined with the same syntax as array expressions. There are two
4+
/// forms of this macro:
55
///
66
/// - Create a [`Vec`] containing a given list of elements:
77
///
@@ -19,19 +19,17 @@
1919
/// assert_eq!(v, [1, 1, 1]);
2020
/// ```
2121
///
22-
/// Note that unlike array expressions this syntax supports all elements
23-
/// which implement [`Clone`] and the number of elements doesn't have to be
24-
/// a constant.
22+
/// Note that unlike array expressions this syntax supports all elements which implement [`Clone`]
23+
/// and the number of elements doesn't have to be a constant.
2524
///
26-
/// This will use `clone` to duplicate an expression, so one should be careful
27-
/// using this with types having a nonstandard `Clone` implementation. For
28-
/// example, `vec![Rc::new(1); 5]` will create a vector of five references
29-
/// to the same boxed integer value, not five references pointing to independently
30-
/// boxed integers.
25+
/// This will use `clone` to duplicate an expression, so one should be careful using this with types
26+
/// having a nonstandard `Clone` implementation. For example, `vec![Rc::new(1); 5]` will create a
27+
/// vector of five references to the same boxed integer value, not five references pointing to
28+
/// independently boxed integers.
3129
///
32-
/// Also, note that `vec![expr; 0]` is allowed, and produces an empty vector.
33-
/// This will still evaluate `expr`, however, and immediately drop the resulting value, so
34-
/// be mindful of side effects.
30+
/// Also, note that `vec![expr; 0]` is allowed, and produces an empty vector. This will still
31+
/// evaluate `expr`, however, and immediately drop the resulting value, so be mindful of side
32+
/// effects.
3533
///
3634
/// [`Vec`]: crate::vec::Vec
3735
#[cfg(all(not(no_global_oom_handling), not(test)))]
@@ -54,10 +52,9 @@ macro_rules! vec {
5452
);
5553
}
5654

57-
// HACK(japaric): with cfg(test) the inherent `[T]::into_vec` method, which is
58-
// required for this macro definition, is not available. Instead use the
59-
// `slice::into_vec` function which is only available with cfg(test)
60-
// NB see the slice::hack module in slice.rs for more information
55+
// HACK(japaric): with cfg(test) the inherent `[T]::into_vec` method, which is required for this
56+
// macro definition, is not available. Instead use the `slice::into_vec` function which is only
57+
// available with cfg(test) NB see the slice::hack module in slice.rs for more information
6158
#[cfg(all(not(no_global_oom_handling), test))]
6259
#[allow(unused_macro_rules)]
6360
macro_rules! vec {
@@ -75,19 +72,18 @@ macro_rules! vec {
7572

7673
/// Creates a `String` using interpolation of runtime expressions.
7774
///
78-
/// The first argument `format!` receives is a format string. This must be a string
79-
/// literal. The power of the formatting string is in the `{}`s contained.
75+
/// The first argument `format!` receives is a format string. This must be a string literal. The
76+
/// power of the formatting string is in the `{}`s contained.
8077
///
81-
/// Additional parameters passed to `format!` replace the `{}`s within the
82-
/// formatting string in the order given unless named or positional parameters
83-
/// are used; see [`std::fmt`] for more information.
78+
/// Additional parameters passed to `format!` replace the `{}`s within the formatting string in the
79+
/// order given unless named or positional parameters are used; see [`std::fmt`] for more
80+
/// information.
8481
///
85-
/// A common use for `format!` is concatenation and interpolation of strings.
86-
/// The same convention is used with [`print!`] and [`write!`] macros,
87-
/// depending on the intended destination of the string.
82+
/// A common use for `format!` is concatenation and interpolation of strings. The same convention is
83+
/// used with [`print!`] and [`write!`] macros, depending on the intended destination of the string.
8884
///
89-
/// To convert a single value to a string, use the [`to_string`] method. This
90-
/// will use the [`Display`] formatting trait.
85+
/// To convert a single value to a string, use the [`to_string`] method. This will use the
86+
/// [`Display`] formatting trait.
9187
///
9288
/// [`std::fmt`]: ../std/fmt/index.html
9389
/// [`print!`]: ../std/macro.print.html
@@ -97,9 +93,8 @@ macro_rules! vec {
9793
///
9894
/// # Panics
9995
///
100-
/// `format!` panics if a formatting trait implementation returns an error.
101-
/// This indicates an incorrect implementation
102-
/// since `fmt::Write for String` never returns an error itself.
96+
/// `format!` panics if a formatting trait implementation returns an error. This indicates an
97+
/// incorrect implementation since `fmt::Write for String` never returns an error itself.
10398
///
10499
/// # Examples
105100
///
@@ -129,3 +124,87 @@ macro_rules! __rust_force_expr {
129124
$e
130125
};
131126
}
127+
128+
/// Default coallocation "cooperation" (`COOP_PREF`) generic parameter.
129+
///
130+
/// NOT for public use. It's exported only so that library/proc_macro (and other internals) can use
131+
/// this.
132+
///
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.
135+
#[unstable(feature = "global_co_alloc_default", issue = "none")]
136+
#[macro_export]
137+
macro_rules! DEFAULT_COOP_PREF {
138+
() => {
139+
true
140+
};
141+
}
142+
// -\---> replace with something like: pub const DEFAULT_COOP_PREF: bool = true;
143+
144+
/// Return 0 or 1, indicating whether to use coallocation metadata (or not) with the given allocator
145+
/// type `alloc` and cooperation preference `coop_pref`.
146+
///
147+
/// NOT for public use. Param `coop_pref` - can override the allocator's default preference for
148+
/// cooperation, or can make the type not cooperative, regardless of whether allocator `A` is
149+
/// cooperative.
150+
// FIXME replace the macro with an (updated version of the below) `const` function). Only once
151+
// 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.
154+
#[unstable(feature = "global_co_alloc", issue = "none")]
155+
#[macro_export]
156+
macro_rules! meta_num_slots {
157+
($alloc:ty, $coop_pref:expr) => {
158+
if ($alloc::IS_CO_ALLOCATOR) && ($coop_pref) { 1 } else { 0 }
159+
};
160+
}
161+
// -\---> replace with something like:
162+
/*
163+
#[unstable(feature = "global_co_alloc", issue = "none")]
164+
pub const fn meta_num_slots<A: Allocator>(
165+
COOP_PREF: bool,
166+
) -> usize {
167+
if A::IS_CO_ALLOCATOR && COOP_PREF { 1 } else { 0 }
168+
}
169+
*/
170+
171+
/// Like `meta_num_slots`, but for the default coallocation preference (`DEFAULT_COOP_PREF`).
172+
///
173+
/// 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()!`).
175+
///
176+
/// NOT for public use.
177+
// 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.
180+
#[unstable(feature = "global_co_alloc", issue = "none")]
181+
#[macro_export]
182+
macro_rules! meta_num_slots_default {
183+
($alloc:ty) => {
184+
if ($alloc::IS_CO_ALLOCATOR) && (DEFAULT_COOP_PREF!()) { 1 } else { 0 }
185+
};
186+
}
187+
188+
/// NOT for public use.
189+
// See above.
190+
#[unstable(feature = "global_co_alloc", issue = "none")]
191+
#[macro_export]
192+
macro_rules! meta_num_slots_global {
193+
($coop_pref:expr) => {
194+
if ::alloc::alloc::Global::IS_CO_ALLOCATOR && ($coop_pref) { 1 } else { 0 }
195+
};
196+
}
197+
198+
/// Like `meta_num_slots`, but for Global allocator and default coallocation preference
199+
/// (`DEFAULT_COOP_PREF`).
200+
///
201+
/// NOT for public use.
202+
// @FIXME once generic_const_exprs is stable, replace this with a `const` function. Then move the
203+
// function to a submodule, for example alloc::co_alloc. See above.
204+
#[unstable(feature = "global_co_alloc", issue = "none")]
205+
#[macro_export]
206+
macro_rules! meta_num_slots_default_global {
207+
() => {
208+
if ::alloc::alloc::Global::IS_CO_ALLOCATOR && (DEFAULT_COOP_PREF!()) { 1 } else { 0 }
209+
};
210+
}

0 commit comments

Comments
 (0)