RangeCfg

Struct RangeCfg 

Source
pub struct RangeCfg<T: Copy + PartialOrd> { /* private fields */ }
Expand description

Configuration for limiting the range of a value.

This is often used to configure length limits for variable-length types or collections.

§Examples

use commonware_codec::RangeCfg;

// Limit lengths to 0..=1024 (type inferred as usize)
let cfg = RangeCfg::new(0..=1024);
assert!(cfg.contains(&500));
assert!(!cfg.contains(&2000));

// Allow any length >= 1
let cfg_min = RangeCfg::from(1..);
assert!(cfg_min.contains(&1));
assert!(!cfg_min.contains(&0));

// Works with other integer types
let cfg_u8: RangeCfg<u8> = RangeCfg::new(0u8..=255u8);
assert!(cfg_u8.contains(&128));

let cfg_u32 = RangeCfg::new(0u32..1024u32);
assert!(cfg_u32.contains(&500));

Implementations§

Source§

impl<T: Copy + PartialOrd> RangeCfg<T>

Source

pub fn new(r: impl RangeBounds<T>) -> Self

Creates a new RangeCfg from any type implementing RangeBounds<T>.

§Examples
use commonware_codec::RangeCfg;

let cfg = RangeCfg::new(0..=1024);
assert!(cfg.contains(&500));
Source

pub fn exact(value: T) -> Self

Creates a RangeCfg that only accepts exactly value.

Source

pub fn contains(&self, value: &T) -> bool

Returns true if the value is within this range.

Trait Implementations§

Source§

impl<T: Clone + Copy + PartialOrd> Clone for RangeCfg<T>

Source§

fn clone(&self) -> RangeCfg<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Copy + PartialOrd> Debug for RangeCfg<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Copy + PartialOrd> From<Range<T>> for RangeCfg<T>

Source§

fn from(r: Range<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Copy + PartialOrd> From<RangeFrom<T>> for RangeCfg<T>

Source§

fn from(r: RangeFrom<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Copy + PartialOrd> From<RangeFull> for RangeCfg<T>

Source§

fn from(_: RangeFull) -> Self

Converts to this type from the input type.
Source§

impl<T: Copy + PartialOrd> From<RangeInclusive<T>> for RangeCfg<T>

Source§

fn from(r: RangeInclusive<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Copy + PartialOrd> From<RangeTo<T>> for RangeCfg<T>

Source§

fn from(r: RangeTo<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Copy + PartialOrd> From<RangeToInclusive<T>> for RangeCfg<T>

Source§

fn from(r: RangeToInclusive<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Hash + Copy + PartialOrd> Hash for RangeCfg<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: PartialEq + Copy + PartialOrd> PartialEq for RangeCfg<T>

Source§

fn eq(&self, other: &RangeCfg<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Copy + PartialOrd> RangeBounds<T> for RangeCfg<T>

Source§

fn start_bound(&self) -> Bound<&T>

Start index bound. Read more
Source§

fn end_bound(&self) -> Bound<&T>

End index bound. Read more
1.35.0 · Source§

fn contains<U>(&self, item: &U) -> bool
where T: PartialOrd<U>, U: PartialOrd<T> + ?Sized,

Returns true if item is contained in the range. Read more
Source§

fn is_empty(&self) -> bool
where T: PartialOrd,

🔬This is a nightly-only experimental API. (range_bounds_is_empty)
Returns true if the range contains no items. One-sided ranges (RangeFrom, etc) always return false. Read more
Source§

impl<T: Copy + Copy + PartialOrd> Copy for RangeCfg<T>

Source§

impl<T: Eq + Copy + PartialOrd> Eq for RangeCfg<T>

Source§

impl<T: Copy + PartialOrd> StructuralPartialEq for RangeCfg<T>

Auto Trait Implementations§

§

impl<T> Freeze for RangeCfg<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for RangeCfg<T>
where T: RefUnwindSafe,

§

impl<T> Send for RangeCfg<T>
where T: Send,

§

impl<T> Sync for RangeCfg<T>
where T: Sync,

§

impl<T> Unpin for RangeCfg<T>
where T: Unpin,

§

impl<T> UnwindSafe for RangeCfg<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.