Skip to content

Commit 3121ef4

Browse files
committed
More minor changes
Require T in MMTKOption<T> to implemnt FromStr because we use parse::<T>() anyway. Move Default out of macro. Update comments.
1 parent b17d14e commit 3121ef4

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/util/options.rs

+21-17
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,14 @@ fn always_valid<T>(_: &T) -> bool {
128128
/// This type allows us to store some metadata for the option. To get the value of an option,
129129
/// you can simply dereference it (for example, *options.threads).
130130
#[derive(Clone)]
131-
pub struct MMTKOption<T: Debug + Clone> {
131+
pub struct MMTKOption<T: Debug + Clone + FromStr> {
132132
/// The actual value for the option
133133
value: T,
134134
/// The validator to ensure the value is valid.
135135
validator: fn(&T) -> bool,
136136
}
137137

138-
impl<T: Debug + Clone> MMTKOption<T> {
138+
impl<T: Debug + Clone + FromStr> MMTKOption<T> {
139139
/// Create a new MMTKOption
140140
pub fn new(value: T, validator: fn(&T) -> bool) -> Self {
141141
// FIXME: We should enable the following check to make sure the initial value is valid.
@@ -164,7 +164,7 @@ impl<T: Debug + Clone> MMTKOption<T> {
164164
}
165165

166166
// Dereference an option to get its value.
167-
impl<T: Debug + Clone> std::ops::Deref for MMTKOption<T> {
167+
impl<T: Debug + Clone + FromStr> std::ops::Deref for MMTKOption<T> {
168168
type Target = T;
169169

170170
fn deref(&self) -> &Self::Target {
@@ -177,7 +177,14 @@ macro_rules! options {
177177
options!($(#[$outer])*$($name: $type [$validator] = $default),*);
178178
];
179179
($($(#[$outer:meta])*$name:ident: $type:ty [$validator:expr] = $default:expr),*) => [
180-
/// MMTk command line options.
180+
/// Options for an MMTk instance. It affects many aspects of the behavior of the MMTk
181+
/// instance, including the number of GC worker threads, the GC plan to use, etc.
182+
///
183+
/// Options are set by the VM binding before creating an instance of MMTk. The VM binding
184+
/// usually parses command line options, environment variables, configuration files, etc.,
185+
/// to determine the options. MMTk also provides the [`Options::read_env_var_settings`]
186+
/// method which reads environment variables of the form `MMTK_*` and set options. It can
187+
/// be convenient in the early development stage of a VM binding.
181188
#[derive(Clone)]
182189
pub struct Options {
183190
$($(#[$outer])*pub $name: MMTKOption<$type>),*
@@ -209,16 +216,16 @@ macro_rules! options {
209216
}
210217
}
211218
}
212-
213-
impl Default for Options {
214-
/// By default, `Options` instance is created with built-in default settings.
215-
fn default() -> Self {
216-
Self::new()
217-
}
218-
}
219219
]
220220
}
221221

222+
impl Default for Options {
223+
/// By default, `Options` instance is created with built-in default settings.
224+
fn default() -> Self {
225+
Self::new()
226+
}
227+
}
228+
222229
impl Options {
223230
/// Set an option by name and value as strings. Returns true if the option is successfully set;
224231
/// false otherwise.
@@ -228,13 +235,12 @@ impl Options {
228235
///
229236
/// ```rust
230237
/// let mut builder = MMTKBuilder::new();
231-
///
232-
/// // Set values directly
233238
/// builder.options.threads.set(4);
234239
/// builder.options.plan.set(PlanSelector::GenImmix);
235240
///
236-
/// // or convert strings to values.
237-
/// builder.options.stress_factor.set(user_input.parse());
241+
/// // All `T` in `MMTKOption<T>` implement `FromStr`.
242+
/// builder.options.plan.set(user_input1.parse()?);
243+
/// builder.options.thread_affinity.set(user_input2.parse()?);
238244
/// ```
239245
///
240246
/// Only use this method if the option name is also provided as strings, e.g. from command line
@@ -780,8 +786,6 @@ mod gc_trigger_tests {
780786
}
781787
}
782788

783-
// Currently we allow all the options to be set by env var for the sake of convenience.
784-
// At some point, we may disallow this and all the options can only be set by command line.
785789
options! {
786790
/// The GC plan to use.
787791
plan: PlanSelector [always_valid] = PlanSelector::GenImmix,

0 commit comments

Comments
 (0)