iter_seq/
bounds.rs

1use crate::size::{IsGreaterOrEqual, IsLessOrEqual};
2use crate::{Sequence, StaticSize, ToUInt, U};
3use sealed::sealed;
4use typenum::{Const, Unsigned};
5
6/// A `Sequence` that is guaranteed to yield *at least* `N` elements,
7/// but could yield more.
8///
9/// # Safety
10/// Implementing this trait is sound only if the sequence always produces
11/// at least `N` elements when no panics occur.
12#[sealed]
13pub unsafe trait LowerBound<N: Unsigned>: Sequence {}
14
15/// A `Sequence` that is guaranteed to yield *at most* `N` elements,
16/// but could yield less.
17///
18/// # Safety
19/// Implementing this trait is sound only if the sequence always produces at
20/// most `N` elements.
21#[sealed]
22pub unsafe trait UpperBound<N: Unsigned>: Sequence {}
23
24#[rustfmt::skip]
25#[sealed]
26pub trait WithLowerBound<const N: usize>: LowerBound<U<N>>
27where
28    Const<N>: ToUInt,
29{}
30
31#[rustfmt::skip]
32#[sealed]
33impl<S, const N: usize> WithLowerBound<N> for S
34where
35    S: LowerBound<U<N>>,
36    Const<N>: ToUInt,
37{}
38
39#[rustfmt::skip]
40#[sealed]
41pub trait WithUpperBound<const N: usize>: UpperBound<U<N>>
42where
43    Const<N>: ToUInt,
44{}
45
46#[rustfmt::skip]
47#[sealed]
48impl<S, const N: usize> WithUpperBound<N> for S
49where
50    S: UpperBound<U<N>>,
51    Const<N>: ToUInt,
52{}
53
54#[rustfmt::skip]
55#[sealed]
56unsafe impl<S: Sequence, N: Unsigned> LowerBound<N> for S
57where
58    S::MinSize: IsGreaterOrEqual<StaticSize<N>>,
59{}
60
61#[rustfmt::skip]
62#[sealed]
63unsafe impl<S: Sequence, N: Unsigned> UpperBound<N> for S
64where
65    S::MinSize: IsLessOrEqual<StaticSize<N>>,
66{}