Skip to content

Commit

Permalink
Add missing Ord and ReserveItems impls
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Hoffmann <[email protected]>
  • Loading branch information
antiguru committed Jun 17, 2024
1 parent 889b7a4 commit db17a43
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions src/impls/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ where
}
}

impl<T, TR> ReserveItems<Option<T>> for OptionRegion<TR>
where
TR: Region + ReserveItems<T>,
{
#[inline]
fn reserve_items<I>(&mut self, items: I)
where
I: Iterator<Item = Option<T>> + 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<T>> for OptionRegion<TR>
where
TR: Region + ReserveItems<&'a T>,
Expand All @@ -148,6 +164,8 @@ mod tests {
fn test_reserve() {
let mut r = <OptionRegion<MirrorRegion<u8>>>::default();
ReserveItems::reserve_items(&mut r, [Some(0), None].iter());

ReserveItems::reserve_items(&mut r, [Some(0), None].into_iter());
}

#[test]
Expand Down
17 changes: 17 additions & 0 deletions src/impls/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,21 @@ where
}
}

impl<T, TC, E, EC> ReserveItems<Result<T, E>> for ResultRegion<TC, EC>
where
TC: Region + ReserveItems<T>,
EC: Region + ReserveItems<E>,
{
#[inline]
fn reserve_items<I>(&mut self, items: I)
where
I: Iterator<Item = Result<T, E>> + 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<T, E>> for ResultRegion<TC, EC>
where
TC: Region + ReserveItems<&'a T>,
Expand Down Expand Up @@ -174,6 +189,8 @@ mod tests {
fn test_reserve() {
let mut r = <ResultRegion<MirrorRegion<u8>, MirrorRegion<u8>>>::default();
ReserveItems::reserve_items(&mut r, [Ok(0), Err(1)].iter());

ReserveItems::reserve_items(&mut r, [Ok(0), Err(1)].into_iter());
}

#[test]
Expand Down
97 changes: 91 additions & 6 deletions src/impls/slice.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! A region that stores slices.
use std::cmp::Ordering;
use std::fmt::{Debug, Formatter};
use std::ops::{Deref, Range};

Expand Down Expand Up @@ -131,12 +132,6 @@ pub struct ReadSlice<'a, C: Region, O: OffsetContainer<C::Index> = Vec<<C as Reg
Result<ReadSliceInner<'a, C, O>, &'a [C::Owned]>,
);

struct ReadSliceInner<'a, C: Region, O: OffsetContainer<C::Index> = Vec<<C as Region>::Index>> {
region: &'a SliceRegion<C, O>,
start: usize,
end: usize,
}

impl<C: Region, O: OffsetContainer<C::Index>> ReadSlice<'_, C, O> {
/// Read the n-th item from the underlying region.
///
Expand Down Expand Up @@ -177,6 +172,45 @@ impl<C: Region, O: OffsetContainer<C::Index>> ReadSlice<'_, C, O> {
self.into_iter()
}
}

impl<R: Region, O: OffsetContainer<R::Index>> PartialEq for ReadSlice<'_, R, O>
where
for<'a> R::ReadItem<'a>: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.iter().eq(*other)
}
}

impl<R: Region, O: OffsetContainer<R::Index>> Eq for ReadSlice<'_, R, O> where
for<'a> R::ReadItem<'a>: Eq
{
}

impl<R: Region, O: OffsetContainer<R::Index>> PartialOrd for ReadSlice<'_, R, O>
where
for<'a> R::ReadItem<'a>: PartialOrd,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.iter().partial_cmp(*other)
}
}

impl<R: Region, O: OffsetContainer<R::Index>> 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<C::Index> = Vec<<C as Region>::Index>> {
region: &'a SliceRegion<C, O>,
start: usize,
end: usize,
}

impl<C: Region, O: OffsetContainer<C::Index>> ReadSliceInner<'_, C, O> {
/// Read the n-th item from the underlying region.
///
Expand Down Expand Up @@ -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 = <SliceRegion<MirrorRegion<u8>>>::default();
let index = r.push([1; 4]);

assert_eq!(

Check failure on line 606 in src/impls/slice.rs

View workflow job for this annotation

GitHub Actions / cargo test on ubuntu, rust 1.77

binary operation `==` cannot be applied to type `impls::slice::ReadSlice<'_, _, _>`

Check failure on line 606 in src/impls/slice.rs

View workflow job for this annotation

GitHub Actions / cargo test on macos, rust 1.77

binary operation `==` cannot be applied to type `impls::slice::ReadSlice<'_, _, _>`
<ReadSlice<_, _> as IntoOwned>::borrow_as(&vec![1; 4]),
r.index(index)
);
assert_ne!(

Check failure on line 610 in src/impls/slice.rs

View workflow job for this annotation

GitHub Actions / cargo test on ubuntu, rust 1.77

binary operation `==` cannot be applied to type `impls::slice::ReadSlice<'_, _, _>`

Check failure on line 610 in src/impls/slice.rs

View workflow job for this annotation

GitHub Actions / cargo test on macos, rust 1.77

binary operation `==` cannot be applied to type `impls::slice::ReadSlice<'_, _, _>`
<ReadSlice<_, _> as IntoOwned>::borrow_as(&vec![0; 4]),
r.index(index)
);
assert_ne!(

Check failure on line 614 in src/impls/slice.rs

View workflow job for this annotation

GitHub Actions / cargo test on ubuntu, rust 1.77

binary operation `==` cannot be applied to type `impls::slice::ReadSlice<'_, _, _>`

Check failure on line 614 in src/impls/slice.rs

View workflow job for this annotation

GitHub Actions / cargo test on macos, rust 1.77

binary operation `==` cannot be applied to type `impls::slice::ReadSlice<'_, _, _>`
<ReadSlice<_, _> as IntoOwned>::borrow_as(&vec![1; 5]),
r.index(index)
);
}

#[test]
fn test_read_slice_cmp() {
let mut r = <SliceRegion<MirrorRegion<u8>>>::default();
let index = r.push([1; 4]);

assert_eq!(
Ordering::Less,
<ReadSlice<_, _> as IntoOwned>::borrow_as(&vec![0; 4]).cmp(&r.index(index))

Check failure on line 627 in src/impls/slice.rs

View workflow job for this annotation

GitHub Actions / cargo test on ubuntu, rust 1.77

the method `cmp` exists for struct `ReadSlice<'_, _, _>`, but its trait bounds were not satisfied

Check failure on line 627 in src/impls/slice.rs

View workflow job for this annotation

GitHub Actions / cargo test on macos, rust 1.77

the method `cmp` exists for struct `ReadSlice<'_, _, _>`, but its trait bounds were not satisfied
);
assert_eq!(
Ordering::Equal,
<ReadSlice<_, _> as IntoOwned>::borrow_as(&vec![1; 4]).cmp(&r.index(index))

Check failure on line 631 in src/impls/slice.rs

View workflow job for this annotation

GitHub Actions / cargo test on ubuntu, rust 1.77

the method `cmp` exists for struct `ReadSlice<'_, _, _>`, but its trait bounds were not satisfied

Check failure on line 631 in src/impls/slice.rs

View workflow job for this annotation

GitHub Actions / cargo test on macos, rust 1.77

the method `cmp` exists for struct `ReadSlice<'_, _, _>`, but its trait bounds were not satisfied
);
assert_eq!(
Ordering::Greater,
<ReadSlice<_, _> as IntoOwned>::borrow_as(&vec![2; 4]).cmp(&r.index(index))

Check failure on line 635 in src/impls/slice.rs

View workflow job for this annotation

GitHub Actions / cargo test on ubuntu, rust 1.77

the method `cmp` exists for struct `ReadSlice<'_, _, _>`, but its trait bounds were not satisfied

Check failure on line 635 in src/impls/slice.rs

View workflow job for this annotation

GitHub Actions / cargo test on macos, rust 1.77

the method `cmp` exists for struct `ReadSlice<'_, _, _>`, but its trait bounds were not satisfied
);

assert_eq!(
Ordering::Less,
<ReadSlice<_, _> as IntoOwned>::borrow_as(&vec![1; 3]).cmp(&r.index(index))

Check failure on line 640 in src/impls/slice.rs

View workflow job for this annotation

GitHub Actions / cargo test on ubuntu, rust 1.77

the method `cmp` exists for struct `ReadSlice<'_, _, _>`, but its trait bounds were not satisfied

Check failure on line 640 in src/impls/slice.rs

View workflow job for this annotation

GitHub Actions / cargo test on macos, rust 1.77

the method `cmp` exists for struct `ReadSlice<'_, _, _>`, but its trait bounds were not satisfied
);
assert_eq!(
Ordering::Equal,
<ReadSlice<_, _> as IntoOwned>::borrow_as(&vec![1; 4]).cmp(&r.index(index))

Check failure on line 644 in src/impls/slice.rs

View workflow job for this annotation

GitHub Actions / cargo test on ubuntu, rust 1.77

the method `cmp` exists for struct `ReadSlice<'_, _, _>`, but its trait bounds were not satisfied

Check failure on line 644 in src/impls/slice.rs

View workflow job for this annotation

GitHub Actions / cargo test on macos, rust 1.77

the method `cmp` exists for struct `ReadSlice<'_, _, _>`, but its trait bounds were not satisfied
);
assert_eq!(
Ordering::Greater,
<ReadSlice<_, _> as IntoOwned>::borrow_as(&vec![1; 5]).cmp(&r.index(index))

Check failure on line 648 in src/impls/slice.rs

View workflow job for this annotation

GitHub Actions / cargo test on ubuntu, rust 1.77

the method `cmp` exists for struct `ReadSlice<'_, _, _>`, but its trait bounds were not satisfied

Check failure on line 648 in src/impls/slice.rs

View workflow job for this annotation

GitHub Actions / cargo test on macos, rust 1.77

the method `cmp` exists for struct `ReadSlice<'_, _, _>`, but its trait bounds were not satisfied
);
}

#[test]
fn test_reserve_ref_slice() {
let mut r = <SliceRegion<MirrorRegion<u8>>>::default();
Expand Down

0 comments on commit db17a43

Please sign in to comment.