Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing Ord and ReserveItems impls #39

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.78
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!(
<ReadSlice<_, _> as IntoOwned>::borrow_as(&vec![1; 4]),
r.index(index)
);
assert_ne!(
<ReadSlice<_, _> as IntoOwned>::borrow_as(&vec![0; 4]),
r.index(index)
);
assert_ne!(
<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))
);
assert_eq!(
Ordering::Equal,
<ReadSlice<_, _> as IntoOwned>::borrow_as(&vec![1; 4]).cmp(&r.index(index))
);
assert_eq!(
Ordering::Greater,
<ReadSlice<_, _> as IntoOwned>::borrow_as(&vec![2; 4]).cmp(&r.index(index))
);

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

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