Skip to content

Commit

Permalink
Make it work on RangeBounds again
Browse files Browse the repository at this point in the history
  • Loading branch information
DRiKE committed Oct 30, 2023
1 parent 44c018f commit 4fc66a6
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! ref and allows to parse values from the octets.

use core::fmt;
use core::ops::Range;
use core::ops::{Bound, RangeBounds};
use crate::octets::Octets;

//------------ Parser --------------------------------------------------------
Expand Down Expand Up @@ -51,24 +51,51 @@ impl<'a, Octs: ?Sized> Parser<'a, Octs> {
/// # Panics
///
/// Panics if `range` is decreasing or out of bounds.
pub fn with_range(octets: &'a Octs, range: Range<usize>) -> Self
pub fn with_range<R>(octets: &'a Octs, range: R) -> Self
where
Octs: AsRef<[u8]>
Octs: AsRef<[u8]>,
R: RangeBounds<usize>
{
if range.end < range.start {
let octets_len = octets.as_ref().len();

let pos = match range.start_bound() {
Bound::Unbounded => 0,
Bound::Included(n) => {
if *n > octets_len - 1 {
panic!("range start is out of range for octets")
}
*n
}
Bound::Excluded(_) => unreachable!() // not a thing for the
// start_bound, right?
};

let len = match range.end_bound() {
Bound::Unbounded => octets_len,
Bound::Excluded(n) => {
if *n > octets_len {
panic!("range end is out of range for octets")
}
*n
}
Bound::Included(n) => {
if *n > octets_len - 1 {
panic!("range end is out of range for octets")
}
n + 1
}
};

if len < pos {
panic!(
"range starts at {} but ends at {}",
range.start, range.end
);
}

if range.end > octets.as_ref().len() {
panic!("range end is out of range for octets");
pos, len
)
}

Parser {
pos: range.start,
len: range.end,
pos,
len,
octets
}
}
Expand Down

0 comments on commit 4fc66a6

Please sign in to comment.