Skip to content

Commit

Permalink
More minor changes
Browse files Browse the repository at this point in the history
Require T in MMTKOption<T> to implemnt FromStr because we use
parse::<T>() anyway.

Move Default out of macro.

Update comments.
  • Loading branch information
wks committed Nov 30, 2024
1 parent b17d14e commit 3121ef4
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/util/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ fn always_valid<T>(_: &T) -> bool {
/// This type allows us to store some metadata for the option. To get the value of an option,
/// you can simply dereference it (for example, *options.threads).
#[derive(Clone)]
pub struct MMTKOption<T: Debug + Clone> {
pub struct MMTKOption<T: Debug + Clone + FromStr> {
/// The actual value for the option
value: T,
/// The validator to ensure the value is valid.
validator: fn(&T) -> bool,
}

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

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

fn deref(&self) -> &Self::Target {
Expand All @@ -177,7 +177,14 @@ macro_rules! options {
options!($(#[$outer])*$($name: $type [$validator] = $default),*);
];
($($(#[$outer:meta])*$name:ident: $type:ty [$validator:expr] = $default:expr),*) => [
/// MMTk command line options.
/// Options for an MMTk instance. It affects many aspects of the behavior of the MMTk
/// instance, including the number of GC worker threads, the GC plan to use, etc.
///
/// Options are set by the VM binding before creating an instance of MMTk. The VM binding
/// usually parses command line options, environment variables, configuration files, etc.,
/// to determine the options. MMTk also provides the [`Options::read_env_var_settings`]
/// method which reads environment variables of the form `MMTK_*` and set options. It can
/// be convenient in the early development stage of a VM binding.
#[derive(Clone)]
pub struct Options {
$($(#[$outer])*pub $name: MMTKOption<$type>),*
Expand Down Expand Up @@ -209,16 +216,16 @@ macro_rules! options {
}
}
}

impl Default for Options {
/// By default, `Options` instance is created with built-in default settings.
fn default() -> Self {
Self::new()
}
}
]
}

impl Default for Options {
/// By default, `Options` instance is created with built-in default settings.
fn default() -> Self {
Self::new()
}
}

impl Options {
/// Set an option by name and value as strings. Returns true if the option is successfully set;
/// false otherwise.
Expand All @@ -228,13 +235,12 @@ impl Options {
///
/// ```rust
/// let mut builder = MMTKBuilder::new();
///
/// // Set values directly
/// builder.options.threads.set(4);
/// builder.options.plan.set(PlanSelector::GenImmix);
///
/// // or convert strings to values.
/// builder.options.stress_factor.set(user_input.parse());
/// // All `T` in `MMTKOption<T>` implement `FromStr`.
/// builder.options.plan.set(user_input1.parse()?);
/// builder.options.thread_affinity.set(user_input2.parse()?);
/// ```
///
/// Only use this method if the option name is also provided as strings, e.g. from command line
Expand Down Expand Up @@ -780,8 +786,6 @@ mod gc_trigger_tests {
}
}

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

0 comments on commit 3121ef4

Please sign in to comment.