Skip to content

Commit

Permalink
Module::set_extra_value_no_overwrite
Browse files Browse the repository at this point in the history
Summary:
Make it easier to debug when attempting to use extra value twice:
- assert
- helpful error message

Used in the following diff D53946874.

Reviewed By: JakobDegen

Differential Revision: D53946875

fbshipit-source-id: 20b6563b705009edeceb9d197695c603dcc21437
  • Loading branch information
stepancheg authored and facebook-github-bot committed Feb 21, 2024
1 parent c3033f3 commit c6d2096
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions starlark/src/environment/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ use crate::values::Value;
enum ModuleError {
#[error("Retained memory profiling is not enabled")]
RetainedMemoryProfileNotEnabled,
#[error("Extra value already set to a value of type `{}`", .0)]
ExtraValueAlreadySet(&'static str),
}

/// The result of freezing a [`Module`], making it and its contained values immutable.
Expand Down Expand Up @@ -545,6 +547,15 @@ impl Module {
self.extra_value.set(Some(v));
}

/// Set extra value, but fail if it's already set.
pub fn set_extra_value_no_overwrite<'v>(&'v self, v: Value<'v>) -> anyhow::Result<()> {
if let Some(existing) = self.extra_value() {
return Err(ModuleError::ExtraValueAlreadySet(existing.get_type()).into());
}
self.set_extra_value(v);
Ok(())
}

/// Field that can be used for any purpose you want.
pub fn extra_value<'v>(&'v self) -> Option<Value<'v>> {
// Cast lifetime.
Expand Down

0 comments on commit c6d2096

Please sign in to comment.