use super::OrderSet; use core::ops::{self, Bound, Index}; use indexmap::set::Slice; // We can't have `impl> Index` because that conflicts with `Index`. // Instead, we repeat the implementations for all the core range types. macro_rules! impl_index { ($($range:ty),*) => {$( impl Index<$range> for OrderSet { type Output = Slice; fn index(&self, range: $range) -> &Self::Output { &self.inner[range] } } )*} } impl_index!( ops::Range, ops::RangeFrom, ops::RangeFull, ops::RangeInclusive, ops::RangeTo, ops::RangeToInclusive, (Bound, Bound) ); #[cfg(all(test, feature = "std"))] mod tests { use super::*; use alloc::vec::Vec; #[test] fn slice_index() { fn check(vec_slice: &[i32], set_slice: &Slice, sub_slice: &Slice) { assert_eq!(set_slice as *const _, sub_slice as *const _); itertools::assert_equal(vec_slice, set_slice); } let vec: Vec = (0..10).map(|i| i * i).collect(); let set: OrderSet = vec.iter().cloned().collect(); let slice = set.as_slice(); // RangeFull check(&vec[..], &set[..], &slice[..]); for i in 0usize..10 { // Index assert_eq!(vec[i], set[i]); assert_eq!(vec[i], slice[i]); // RangeFrom check(&vec[i..], &set[i..], &slice[i..]); // RangeTo check(&vec[..i], &set[..i], &slice[..i]); // RangeToInclusive check(&vec[..=i], &set[..=i], &slice[..=i]); // (Bound, Bound) let bounds = (Bound::Excluded(i), Bound::Unbounded); check(&vec[i + 1..], &set[bounds], &slice[bounds]); for j in i..=10 { // Range check(&vec[i..j], &set[i..j], &slice[i..j]); } for j in i..10 { // RangeInclusive check(&vec[i..=j], &set[i..=j], &slice[i..=j]); } } } }