Skip to content

Commit

Permalink
Improved size constraint (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicceboy authored Aug 15, 2023
1 parent e64391c commit a7a00c7
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/types/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,31 @@ impl TryFrom<Bounded<usize>> for Value {
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Size(Bounded<usize>);
pub struct Size(pub(crate) Bounded<usize>);

impl Size {
#[must_use]
/// Creates a varying range constraint.
pub const fn new(range: Bounded<usize>) -> Self {
Self(range)
}

#[must_use]
/// Creates a fixed size constraint.
pub const fn fixed(length: usize) -> Self {
Self(Bounded::Single(length))
}

#[must_use]
/// Returns whether the size is fixed.
pub fn is_fixed(&self) -> bool {
matches!(self.0, Bounded::Single(_))
}
/// Returns whether the size has a varying range.
#[must_use]
pub fn is_range(&self) -> bool {
matches!(self.0, Bounded::Range { .. })
}
}

impl core::ops::Deref for Size {
Expand Down

0 comments on commit a7a00c7

Please sign in to comment.