Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Handle `NonzeroDuration` in config files too (#15253)

Alex Crichton68f6475eac03
diff --git a/crates/cli-flags/src/lib.rs b/crates/cli-flags/src/lib.rs
index ac0ebf803e..525623cc8e 100644
--- a/crates/cli-flags/src/lib.rs
+++ b/crates/cli-flags/src/lib.rs
@@ -1921,6 +1921,34 @@ mod tests {
"Mismatch for input '{collector_value}'. Parsed: {parsed_collector:?}, Expected: {expected:?}",
);
}
+
+ // Timeout: zero is rejected, as it is on the command line
+ for (timeout_value, expected) in [
+ (
+ "{ secs = 5, nanos = 0 }",
+ Some(std::time::Duration::from_secs(5)),
+ ),
+ (
+ "{ secs = 0, nanos = 1 }",
+ Some(std::time::Duration::from_nanos(1)),
+ ),
+ ("{ secs = 0, nanos = 0 }", None), // should fail
+ ] {
+ let toml = format!(
+ r#"
+ [wasm]
+ timeout = {timeout_value}
+ "#,
+ );
+ let parsed_timeout = toml::from_str::<CommonOptions>(&toml)
+ .ok()
+ .and_then(|common_options| common_options.wasm.timeout)
+ .map(|timeout| *timeout);
+ assert_eq!(
+ parsed_timeout, expected,
+ "Mismatch for input '{timeout_value}'. Parsed: {parsed_timeout:?}, Expected: {expected:?}",
+ );
+ }
}
}
diff --git a/crates/cli-flags/src/opt.rs b/crates/cli-flags/src/opt.rs
index b0aad8b45c..0676bd5d2d 100644
--- a/crates/cli-flags/src/opt.rs
+++ b/crates/cli-flags/src/opt.rs
@@ -521,7 +521,8 @@ impl WasmtimeOptionValue for bool {
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(
feature = "serde",
- derive(serde_derive::Serialize, serde_derive::Deserialize)
+ derive(serde_derive::Serialize, serde_derive::Deserialize),
+ serde(try_from = "Duration", into = "Duration")
)]
pub struct NonzeroDuration(Duration);
@@ -536,6 +537,20 @@ impl NonzeroDuration {
}
}
+impl TryFrom<Duration> for NonzeroDuration {
+ type Error = wasmtime::Error;
+
+ fn try_from(duration: Duration) -> Result<Self> {
+ Self::new(duration).ok_or_else(|| format_err!("duration must not be zero"))
+ }
+}
+
+impl From<NonzeroDuration> for Duration {
+ fn from(duration: NonzeroDuration) -> Duration {
+ duration.0
+ }
+}
+
impl std::ops::Deref for NonzeroDuration {
type Target = Duration;
@@ -549,7 +564,7 @@ impl WasmtimeOptionValue for NonzeroDuration {
fn parse(val: Option<&str>) -> Result<Self> {
let duration = <Duration as WasmtimeOptionValue>::parse(val)?;
- Self::new(duration).ok_or_else(|| format_err!("duration must not be zero"))
+ Self::try_from(duration)
}
fn display(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {