Skip to main content

qubit_argument/argument/constraint/
range_constraint.rs

1// =============================================================================
2//    Copyright (c) 2025 - 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! Numeric range constraints.
9
10use crate::argument::ArgumentBound;
11
12/// A numeric range with independently inclusive, exclusive, or absent bounds.
13///
14/// ```compile_fail
15/// #![deny(unused_must_use)]
16/// use qubit_argument::{ArgumentBound, RangeConstraint};
17///
18/// RangeConstraint::new(ArgumentBound::Unbounded, ArgumentBound::Unbounded);
19/// ```
20#[must_use]
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct RangeConstraint {
23    lower: ArgumentBound,
24    upper: ArgumentBound,
25}
26
27impl RangeConstraint {
28    /// Creates a range from its lower and upper bounds.
29    ///
30    /// The bounds are retained exactly as supplied and are not ordered or
31    /// otherwise validated.
32    #[inline]
33    pub fn new(lower: ArgumentBound, upper: ArgumentBound) -> Self {
34        Self { lower, upper }
35    }
36
37    /// Returns the lower bound of this range.
38    #[inline(always)]
39    pub fn lower(&self) -> &ArgumentBound {
40        &self.lower
41    }
42
43    /// Returns the upper bound of this range.
44    #[inline(always)]
45    pub fn upper(&self) -> &ArgumentBound {
46        &self.upper
47    }
48
49    /// Consumes this range and returns its lower and upper bounds.
50    ///
51    /// The first tuple element is the lower bound and the second is the upper
52    /// bound.
53    #[inline]
54    pub fn into_bounds(self) -> (ArgumentBound, ArgumentBound) {
55        let Self { lower, upper } = self;
56        (lower, upper)
57    }
58}