range_split/mem.rs
1//! Utilities for working with ranges of collections in memory.
2
3use core::ops::{RangeTo, RangeToInclusive};
4
5/// Converts a range with an inclusive end bound into the equivalent
6/// range with the exclusive end bound.
7///
8/// This allows reusing the implementation of `TakeRange<RangeTo<usize>>`
9/// to implement `TakeRange<RangeToInclusive<usize>>`. The exclusive end
10/// bound value also corresponds directly to container lengths and
11/// zero-based offsets, taking away the need to increment the value and
12/// check for a possible integer overflow.
13///
14/// This conversion is valid for any valid range within a collection in memory,
15/// since the entire length of the collection must fit within a `usize` value.
16///
17/// # Panics
18///
19/// Panics if the inclusive end bound is `std::usize::MAX`.
20///
21#[inline]
22pub fn convert_inclusive_range(
23 range: RangeToInclusive<usize>,
24) -> RangeTo<usize> {
25 ..range.end.checked_add(1).expect("integer overflow")
26}