1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use crate::std::{
fmt::{ self, Display, Formatter },
ops::{ Range, RangeBounds, Bound }
};
#[cfg(feature = "std")]
use crate::std::error::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BufferTooSmall;
impl Display for BufferTooSmall {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "Buffer is too small")
}
}
#[cfg(feature = "std")]
impl Error for BufferTooSmall {}
#[derive(Debug)]
#[non_exhaustive]
pub enum WillPanic {}
pub trait RangeBoundsExt<T> {
fn into_absolute(self, default_start: T, default_end: T) -> Option<Range<T>>;
}
impl<T> RangeBoundsExt<usize> for T where T: RangeBounds<usize> {
fn into_absolute(self, default_start: usize, default_end: usize) -> Option<Range<usize>> {
let start = match self.start_bound() {
Bound::Included(start) => *start,
Bound::Excluded(start) => start.checked_add(1)?,
Bound::Unbounded => default_start
};
let end = match self.end_bound() {
Bound::Excluded(end) => *end,
Bound::Included(end) => end.checked_add(1)?,
Bound::Unbounded => default_end
};
match start <= end {
true => Some(start..end),
false => None
}
}
}