ringbuf/rb/utils.rs
1use core::{num::NonZeroUsize, ops::Range};
2
3/// Returns a pair of ranges between `start` and `end` indices in a ring buffer with specific `capacity`.
4///
5/// `start` and `end` may be arbitrary large, but must satisfy the following condition: `0 <= (start - end) % (2 * capacity) <= capacity`.
6/// Actual indices are taken modulo `capacity`.
7///
8/// The first range starts from `start`. If the first slice is empty then second slice is empty too.
9pub fn ranges(capacity: NonZeroUsize, start: usize, end: usize) -> (Range<usize>, Range<usize>) {
10 let (head_quo, head_rem) = (start / capacity, start % capacity);
11 let (tail_quo, tail_rem) = (end / capacity, end % capacity);
12
13 if (head_quo + tail_quo) % 2 == 0 {
14 (head_rem..tail_rem, 0..0)
15 } else {
16 (head_rem..capacity.get(), 0..tail_rem)
17 }
18}