diff --git a/src/parameter.rs b/src/parameter.rs index 2e00cbd..fb57794 100644 --- a/src/parameter.rs +++ b/src/parameter.rs @@ -21,6 +21,7 @@ //! ``` use core::fmt::Debug; +use core::ops::RangeInclusive; use core::sync::atomic::{AtomicU64, Ordering}; use crate::distribution::{ @@ -187,6 +188,12 @@ impl FloatParam { } } +impl From> for FloatParam { + fn from(range: RangeInclusive) -> Self { + FloatParam::new(*range.start(), *range.end()) + } +} + impl Parameter for FloatParam { type Value = f64; @@ -306,6 +313,12 @@ impl IntParam { } } +impl From> for IntParam { + fn from(range: RangeInclusive) -> Self { + IntParam::new(*range.start(), *range.end()) + } +} + impl Parameter for IntParam { type Value = i64; @@ -992,4 +1005,34 @@ mod tests { let cloned = param.clone(); assert_eq!(param.id(), cloned.id()); } + + #[test] + fn float_param_from_range() { + let param = FloatParam::from(0.0..=1.0); + assert_eq!( + param.distribution(), + Distribution::Float(FloatDistribution { + low: 0.0, + high: 1.0, + log_scale: false, + step: None, + }) + ); + assert_eq!(param.label(), format!("{param:?}")); + } + + #[test] + fn int_param_from_range() { + let param = IntParam::from(1..=10); + assert_eq!( + param.distribution(), + Distribution::Int(IntDistribution { + low: 1, + high: 10, + log_scale: false, + step: None, + }) + ); + assert_eq!(param.label(), format!("{param:?}")); + } }