diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2b8abfd..3b754b1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,7 +15,7 @@ jobs: - windows toolchain: - stable - - 1.65 + - 1.77 name: cargo test on ${{ matrix.os }}, rust ${{ matrix.toolchain }} runs-on: ${{ matrix.os }}-latest steps: diff --git a/src/impls/option.rs b/src/impls/option.rs index b43ae73..608e003 100644 --- a/src/impls/option.rs +++ b/src/impls/option.rs @@ -125,6 +125,22 @@ where } } +impl ReserveItems> for OptionRegion +where + TR: Region + ReserveItems, +{ + #[inline] + fn reserve_items(&mut self, items: I) + where + I: Iterator> + Clone, + { + // Clippy is confused about using `flatten` here, which we cannot use because + // the iterator isn't `Clone`. + #[allow(clippy::filter_map_identity)] + self.inner.reserve_items(items.filter_map(|r| r)); + } +} + impl<'a, T: 'a, TR> ReserveItems<&'a Option> for OptionRegion where TR: Region + ReserveItems<&'a T>, @@ -148,6 +164,8 @@ mod tests { fn test_reserve() { let mut r = >>::default(); ReserveItems::reserve_items(&mut r, [Some(0), None].iter()); + + ReserveItems::reserve_items(&mut r, [Some(0), None].into_iter()); } #[test] diff --git a/src/impls/result.rs b/src/impls/result.rs index b305d3e..05301c5 100644 --- a/src/impls/result.rs +++ b/src/impls/result.rs @@ -147,6 +147,21 @@ where } } +impl ReserveItems> for ResultRegion +where + TC: Region + ReserveItems, + EC: Region + ReserveItems, +{ + #[inline] + fn reserve_items(&mut self, items: I) + where + I: Iterator> + Clone, + { + self.oks.reserve_items(items.clone().filter_map(|r| r.ok())); + self.errs.reserve_items(items.filter_map(|r| r.err())); + } +} + impl<'a, T: 'a, TC, E: 'a, EC> ReserveItems<&'a Result> for ResultRegion where TC: Region + ReserveItems<&'a T>, @@ -174,6 +189,8 @@ mod tests { fn test_reserve() { let mut r = , MirrorRegion>>::default(); ReserveItems::reserve_items(&mut r, [Ok(0), Err(1)].iter()); + + ReserveItems::reserve_items(&mut r, [Ok(0), Err(1)].into_iter()); } #[test] diff --git a/src/impls/slice.rs b/src/impls/slice.rs index f129137..8d38b6d 100644 --- a/src/impls/slice.rs +++ b/src/impls/slice.rs @@ -1,5 +1,6 @@ //! A region that stores slices. +use std::cmp::Ordering; use std::fmt::{Debug, Formatter}; use std::ops::{Deref, Range}; @@ -131,12 +132,6 @@ pub struct ReadSlice<'a, C: Region, O: OffsetContainer = Vec<, &'a [C::Owned]>, ); -struct ReadSliceInner<'a, C: Region, O: OffsetContainer = Vec<::Index>> { - region: &'a SliceRegion, - start: usize, - end: usize, -} - impl> ReadSlice<'_, C, O> { /// Read the n-th item from the underlying region. /// @@ -177,6 +172,45 @@ impl> ReadSlice<'_, C, O> { self.into_iter() } } + +impl> PartialEq for ReadSlice<'_, R, O> +where + for<'a> R::ReadItem<'a>: PartialEq, +{ + fn eq(&self, other: &Self) -> bool { + self.iter().eq(*other) + } +} + +impl> Eq for ReadSlice<'_, R, O> where + for<'a> R::ReadItem<'a>: Eq +{ +} + +impl> PartialOrd for ReadSlice<'_, R, O> +where + for<'a> R::ReadItem<'a>: PartialOrd, +{ + fn partial_cmp(&self, other: &Self) -> Option { + self.iter().partial_cmp(*other) + } +} + +impl> Ord for ReadSlice<'_, R, O> +where + for<'a> R::ReadItem<'a>: Ord, +{ + fn cmp(&self, other: &Self) -> Ordering { + self.iter().cmp(*other) + } +} + +struct ReadSliceInner<'a, C: Region, O: OffsetContainer = Vec<::Index>> { + region: &'a SliceRegion, + start: usize, + end: usize, +} + impl> ReadSliceInner<'_, C, O> { /// Read the n-th item from the underlying region. /// @@ -564,6 +598,57 @@ mod tests { assert_eq!("[1, 1, 1, 1]", format!("{:?}", r.index(index).clone())); } + #[test] + fn test_read_slice_eq() { + let mut r = >>::default(); + let index = r.push([1; 4]); + + assert_eq!( + as IntoOwned>::borrow_as(&vec![1; 4]), + r.index(index) + ); + assert_ne!( + as IntoOwned>::borrow_as(&vec![0; 4]), + r.index(index) + ); + assert_ne!( + as IntoOwned>::borrow_as(&vec![1; 5]), + r.index(index) + ); + } + + #[test] + fn test_read_slice_cmp() { + let mut r = >>::default(); + let index = r.push([1; 4]); + + assert_eq!( + Ordering::Less, + as IntoOwned>::borrow_as(&vec![0; 4]).cmp(&r.index(index)) + ); + assert_eq!( + Ordering::Equal, + as IntoOwned>::borrow_as(&vec![1; 4]).cmp(&r.index(index)) + ); + assert_eq!( + Ordering::Greater, + as IntoOwned>::borrow_as(&vec![2; 4]).cmp(&r.index(index)) + ); + + assert_eq!( + Ordering::Less, + as IntoOwned>::borrow_as(&vec![1; 3]).cmp(&r.index(index)) + ); + assert_eq!( + Ordering::Equal, + as IntoOwned>::borrow_as(&vec![1; 4]).cmp(&r.index(index)) + ); + assert_eq!( + Ordering::Greater, + as IntoOwned>::borrow_as(&vec![1; 5]).cmp(&r.index(index)) + ); + } + #[test] fn test_reserve_ref_slice() { let mut r = >>::default();