nekolib/utils/
buf_range.rs1use std::fmt::Debug;
4use std::ops::Bound::{Excluded, Included, Unbounded};
5use std::ops::{Range, RangeBounds};
6
7pub fn bounds_within<R: RangeBounds<usize>>(r: R, len: usize) -> Range<usize> {
24 let e_ex = match r.end_bound() {
25 Included(&e) => e + 1,
26 Excluded(&e) => e,
27 Unbounded => len,
28 };
29 let s_in = match r.start_bound() {
30 Included(&s) => s,
31 Excluded(&s) => s + 1,
32 Unbounded => 0,
33 }
34 .min(e_ex);
35 s_in..e_ex
36}
37
38pub fn check_bounds(i: usize, len: usize) {
56 assert!(
57 i < len,
58 "index out of bounds: the len is {} but the index is {}",
59 len,
60 i
61 );
62}
63
64pub fn check_bounds_range(i: usize, range: impl RangeBounds<usize> + Debug) {
83 assert!(
84 range.contains(&i),
85 "index out of bounds: the range is {:?} but the index is {}",
86 range,
87 i
88 );
89}
90
91#[test]
92#[should_panic]
93fn test_panic_bound_large() { check_bounds_range(4, 0..=3); }
94
95#[test]
96#[should_panic]
97fn test_panic_bound_small() { check_bounds_range(0, 1..=3); }
98
99#[test]
100fn test_check() {
101 check_bounds_range(0, 0..=3);
102 check_bounds_range(3, 0..=3);
103 check_bounds_range(2, 0..3);
104}
105
106#[test]
107fn test_range() {
108 assert_eq!(bounds_within(0..3, 2), 0..3);
109 assert_eq!(bounds_within(0..3, 3), 0..3);
110 assert_eq!(bounds_within(0..3, 4), 0..3);
111
112 assert_eq!(bounds_within(0.., 2), 0..2);
113 assert_eq!(bounds_within(0.., 3), 0..3);
114 assert_eq!(bounds_within(0.., 4), 0..4);
115
116 assert_eq!(bounds_within((Excluded(2), Included(5)), 8), 3..6);
117 assert_eq!(bounds_within(.., 5), 0..5);
118}