Skip to content

Accept standard Rust ranges in ParameterBuilder::range() #675

Description

@azerupi

Declaring a bounded parameter currently requires building a ParameterRange struct by hand:

node.declare_parameter("speed")
    .default(0.5)
    .range(ParameterRange { lower: Some(0.0), upper: Some(1.0), step: None })
    .mandatory()?;

Most parameters have no step, so this is a lot of ceremony for a simple bound. Changing the builder to accept impl Into<T::Range> and adding a few conversion impl between Rust range types and ParameterRange would allow native Rust range syntax:

node.declare_parameter("speed").default(0.5).range(0.0..=1.0).mandatory()?;
node.declare_parameter("count").default(5).range(0..10).mandatory()?;
node.declare_parameter("offset").default(-3).range(..=0).mandatory()?;

We would need to add From impls into ParameterRange<T> for:

  • RangeInclusive<T>, RangeFrom<T>, RangeToInclusive<T>, RangeFull, generic over T: ParameterVariant + PartialOrd
  • Range<i64> and RangeTo<i64>, converting the exclusive end into the inclusive bound end - 1

Range<f64> and RangeTo<f64> should not be implemented. ParameterRange::upper is inclusive and no floating point value sits just below an exclusive bound, so .range(0.0..1.0) should remain a compile error rather than silently becoming an inclusive bound.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions