Skip to content

Commit

Permalink
Rename Query to AndRemainder.
Browse files Browse the repository at this point in the history
This change gives `Query` a more general name and uses this type in the
output of the `Saturated` trait and related iterator APIs.
  • Loading branch information
olson-sean-k committed Jul 30, 2024
1 parent 1feb60a commit bfedfdf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
22 changes: 14 additions & 8 deletions src/array_vec1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::num::NonZeroUsize;
use core::ops::{Deref, DerefMut, RangeBounds};

use crate::array1::Array1;
use crate::iter1::{self, FromIterator1, IntoIterator1, Iterator1};
use crate::iter1::{self, AndRemainder, FromIterator1, IntoIterator1, Iterator1};
use crate::segment::range::{self, PositionalRange, Project, ProjectionExt as _};
use crate::segment::{self, Ranged, Segment, Segmentation, Segmented};
#[cfg(feature = "serde")]
Expand Down Expand Up @@ -50,10 +50,13 @@ where
{
type Remainder = I::IntoIter;

fn saturated(items: I) -> (Self, Self::Remainder) {
fn saturated(items: I) -> AndRemainder<Self, Self::Remainder> {
let mut remainder = items.into_iter();
let items: ArrayVec<_, N> = remainder.by_ref().take(N).collect();
(items, remainder)
AndRemainder {
output: items,
remainder,
}
}
}

Expand Down Expand Up @@ -471,12 +474,15 @@ where
{
type Remainder = I::IntoIter;

fn saturated(items: I) -> (Self, Self::Remainder) {
fn saturated(items: I) -> AndRemainder<Self, Self::Remainder> {
let mut remainder = items.into_iter1().into_iter();
// SAFETY:
let items =
unsafe { ArrayVec1::from_array_vec_unchecked(remainder.by_ref().take(N).collect()) };
(items, remainder)
AndRemainder {
output: items,
remainder,
}
}
}

Expand Down Expand Up @@ -843,15 +849,15 @@ mod tests {

#[test]
fn saturation() {
let (xs, remainder): (ArrayVec<_, 3>, _) = [0i32, 1, 2, 3].into_iter().saturate();
let (xs, remainder): (ArrayVec<_, 3>, _) = [0i32, 1, 2, 3].into_iter().saturate().into();
assert_eq!(xs.as_slice(), &[0, 1, 2]);
assert!(remainder.eq([3]));

let (xs, remainder): (ArrayVec<_, 4>, _) = [0i32, 1].into_iter1().saturate();
let (xs, remainder): (ArrayVec<_, 4>, _) = [0i32, 1].into_iter1().saturate().into();
assert_eq!(xs.as_slice(), &[0, 1]);
assert!(remainder.eq([]));

let (xs, remainder): (ArrayVec1<_, 3>, _) = [0i32, 1, 2, 3].into_iter1().saturate();
let (xs, remainder): (ArrayVec1<_, 3>, _) = [0i32, 1, 2, 3].into_iter1().saturate().into();
assert_eq!(xs.as_slice(), &[0, 1, 2]);
assert!(remainder.eq([3]));
}
Expand Down
28 changes: 14 additions & 14 deletions src/iter1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub trait IteratorExt: Iterator + Sized + ThenIterator1<Self> {
T::try_from_iter(self)
}

fn saturate<T>(self) -> (T, T::Remainder)
fn saturate<T>(self) -> AndRemainder<T, T::Remainder>
where
T: Saturated<Self>,
{
Expand Down Expand Up @@ -224,17 +224,17 @@ where
}

#[derive(Clone, Debug)]
pub struct Query<T, I> {
pub struct AndRemainder<T, I> {
pub output: T,
pub remainder: I,
}

impl<T, I> Query<T, I> {
impl<T, I> AndRemainder<T, I> {
pub fn with_output_and_then_remainder<F>(self, f: F) -> I
where
F: FnOnce(T),
{
let Query { output, remainder } = self;
let AndRemainder { output, remainder } = self;
f(output);
remainder
}
Expand All @@ -243,20 +243,20 @@ impl<T, I> Query<T, I> {
where
F: FnOnce(I),
{
let Query { output, remainder } = self;
let AndRemainder { output, remainder } = self;
f(remainder);
output
}
}

impl<T, I> From<Query<T, I>> for (T, I) {
fn from(query: Query<T, I>) -> Self {
let Query { output, remainder } = query;
impl<T, I> From<AndRemainder<T, I>> for (T, I) {
fn from(query: AndRemainder<T, I>) -> Self {
let AndRemainder { output, remainder } = query;
(output, remainder)
}
}

pub type Matched<T, I> = Query<Option<T>, I>;
pub type Matched<T, I> = AndRemainder<Option<T>, I>;

impl<T, I> Matched<T, I> {
pub fn matched(self) -> Option<T> {
Expand Down Expand Up @@ -292,7 +292,7 @@ impl<T, I> From<Matched<T, I>> for Option<T> {
}
}

pub type IsMatch<I> = Query<bool, I>;
pub type IsMatch<I> = AndRemainder<bool, I>;

impl<I> IsMatch<I> {
pub fn is_match(self) -> bool {
Expand Down Expand Up @@ -363,12 +363,12 @@ where
}

#[inline(always)]
fn maybe_empty<T, F>(mut self, f: F) -> Query<T, I>
fn maybe_empty<T, F>(mut self, f: F) -> AndRemainder<T, I>
where
F: FnOnce(&mut I) -> T,
{
let output = f(&mut self.items);
Query {
AndRemainder {
output,
remainder: self.items,
}
Expand Down Expand Up @@ -419,7 +419,7 @@ where
}

pub fn into_head_and_tail(self) -> (I::Item, I) {
let Query { output, remainder } = self.maybe_empty(|items| {
let AndRemainder { output, remainder } = self.maybe_empty(|items| {
// SAFETY:
unsafe { items.next().unwrap_maybe_unchecked() }
});
Expand Down Expand Up @@ -607,7 +607,7 @@ where
T::from_iter1(self)
}

pub fn saturate<T>(self) -> (T, T::Remainder)
pub fn saturate<T>(self) -> AndRemainder<T, T::Remainder>
where
T: Saturated<Self>,
{
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ use {
::serde_derive::{Deserialize, Serialize},
};

use crate::iter1::AndRemainder;
#[cfg(feature = "serde")]
use crate::serde::{EmptyError, Serde};

Expand Down Expand Up @@ -131,7 +132,7 @@ pub trait Vacancy {
pub trait Saturated<T>: Sized {
type Remainder;

fn saturated(items: T) -> (Self, Self::Remainder);
fn saturated(items: T) -> AndRemainder<Self, Self::Remainder>;
}

pub trait Saturate<T> {
Expand Down

0 comments on commit bfedfdf

Please sign in to comment.