From f75839e7c13f31708961999fe994c88976a8de34 Mon Sep 17 00:00:00 2001 From: Moritz Hoffmann Date: Mon, 17 Jun 2024 13:34:41 -0400 Subject: [PATCH] Add missing Ord and ReserveItems impls Signed-off-by: Moritz Hoffmann --- src/impls/option.rs | 15 +++++++ src/impls/result.rs | 17 ++++++++ src/impls/slice.rs | 97 ++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 123 insertions(+), 6 deletions(-) diff --git a/src/impls/option.rs b/src/impls/option.rs index b43ae73..3351036 100644 --- a/src/impls/option.rs +++ b/src/impls/option.rs @@ -125,6 +125,19 @@ where } } +impl ReserveItems> for OptionRegion +where + TR: Region + ReserveItems, +{ + #[inline] + fn reserve_items(&mut self, items: I) + where + I: Iterator> + Clone, + { + 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 +161,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..4fe7a98 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>: PartialEq +{ +} + +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();