Skip to content

Commit

Permalink
Deprecate GroupBy in favor of ChunkBy
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe-Cholet committed Jan 30, 2024
1 parent c508e5e commit edada59
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
26 changes: 15 additions & 11 deletions src/groupbylazy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::vec::{self, Vec};
use std::cell::{Cell, RefCell};

/// A trait to unify `FnMut` for `GroupBy` with the chunk key in `IntoChunks`
/// A trait to unify `FnMut` for `ChunkBy` with the chunk key in `IntoChunks`
trait KeyFunction<A> {
type Key;
fn call_mut(&mut self, arg: A) -> Self::Key;
Expand Down Expand Up @@ -282,10 +282,14 @@ where
}
}

/// `GroupBy` is the storage for the lazy grouping operation.
#[deprecated(note = "Use ChunkBy instead", since = "0.13.0")]
/// See [`ChunkBy`](Itertools::structs::ChunkBy).
pub type GroupBy<K, I, F> = ChunkBy<K, I, F>;

/// `ChunkBy` is the storage for the lazy grouping operation.
///
/// If the groups are consumed in their original order, or if each
/// group is dropped without keeping it around, then `GroupBy` uses
/// group is dropped without keeping it around, then `ChunkBy` uses
/// no allocations. It needs allocations only if several group iterators
/// are alive at the same time.
///
Expand All @@ -296,7 +300,7 @@ where
///
/// See [`.group_by()`](crate::Itertools::group_by) for more information.
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct GroupBy<K, I, F>
pub struct ChunkBy<K, I, F>
where
I: Iterator,
{
Expand All @@ -307,12 +311,12 @@ where
}

/// Create a new
pub fn new<K, J, F>(iter: J, f: F) -> GroupBy<K, J::IntoIter, F>
pub fn new<K, J, F>(iter: J, f: F) -> ChunkBy<K, J::IntoIter, F>
where
J: IntoIterator,
F: FnMut(&J::Item) -> K,
{
GroupBy {
ChunkBy {
inner: RefCell::new(GroupInner {
key: f,
iter: iter.into_iter(),
Expand All @@ -329,7 +333,7 @@ where
}
}

impl<K, I, F> GroupBy<K, I, F>
impl<K, I, F> ChunkBy<K, I, F>
where
I: Iterator,
{
Expand All @@ -348,7 +352,7 @@ where
}
}

impl<'a, K, I, F> IntoIterator for &'a GroupBy<K, I, F>
impl<'a, K, I, F> IntoIterator for &'a ChunkBy<K, I, F>
where
I: Iterator,
I::Item: 'a,
Expand Down Expand Up @@ -377,7 +381,7 @@ where
K: 'a,
F: 'a,
{
parent: &'a GroupBy<K, I, F>,
parent: &'a ChunkBy<K, I, F>,
}

impl<'a, K, I, F> Iterator for Groups<'a, K, I, F>
Expand Down Expand Up @@ -418,7 +422,7 @@ where
K: 'a,
F: 'a,
{
parent: &'a GroupBy<K, I, F>,
parent: &'a ChunkBy<K, I, F>,
index: usize,
first: Option<I::Item>,
}
Expand Down Expand Up @@ -476,7 +480,7 @@ where

/// `ChunkLazy` is the storage for a lazy chunking operation.
///
/// `IntoChunks` behaves just like `GroupBy`: it is iterable, and
/// `IntoChunks` behaves just like `ChunkBy`: it is iterable, and
/// it only buffers if several chunk iterators are alive at the same time.
///
/// This type implements [`IntoIterator`] (it is **not** an iterator
Expand Down
17 changes: 10 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,11 @@ pub mod structs {
pub use crate::exactly_one_err::ExactlyOneError;
pub use crate::flatten_ok::FlattenOk;
pub use crate::format::{Format, FormatWith};
#[allow(deprecated)]
#[cfg(feature = "use_alloc")]
pub use crate::groupbylazy::GroupBy;
#[cfg(feature = "use_alloc")]
pub use crate::groupbylazy::{Chunk, Chunks, Group, GroupBy, Groups, IntoChunks};
pub use crate::groupbylazy::{Chunk, ChunkBy, Chunks, Group, Groups, IntoChunks};
#[cfg(feature = "use_std")]
pub use crate::grouping_map::{GroupingMap, GroupingMapBy};
pub use crate::intersperse::{Intersperse, IntersperseWith};
Expand Down Expand Up @@ -575,10 +578,10 @@ pub trait Itertools: Iterator {
/// Consecutive elements that map to the same key (“runs”), are assigned
/// to the same group.
///
/// `GroupBy` is the storage for the lazy grouping operation.
/// `ChunkBy` is the storage for the lazy grouping operation.
///
/// If the groups are consumed in order, or if each group's iterator is
/// dropped without keeping it around, then `GroupBy` uses no
/// dropped without keeping it around, then `ChunkBy` uses no
/// allocations. It needs allocations only if several group iterators
/// are alive at the same time.
///
Expand All @@ -597,7 +600,7 @@ pub trait Itertools: Iterator {
/// let data = vec![1, 3, -2, -2, 1, 0, 1, 2];
/// // groups: |---->|------>|--------->|
///
/// // Note: The `&` is significant here, `GroupBy` is iterable
/// // Note: The `&` is significant here, `ChunkBy` is iterable
/// // only by reference. You can also call `.into_iter()` explicitly.
/// let mut data_grouped = Vec::new();
/// for (key, group) in &data.into_iter().chunk_by(|elt| *elt >= 0) {
Expand All @@ -606,7 +609,7 @@ pub trait Itertools: Iterator {
/// assert_eq!(data_grouped, vec![(true, vec![1, 3]), (false, vec![-2, -2]), (true, vec![1, 0, 1, 2])]);
/// ```
#[cfg(feature = "use_alloc")]
fn chunk_by<K, F>(self, key: F) -> GroupBy<K, Self, F>
fn chunk_by<K, F>(self, key: F) -> ChunkBy<K, Self, F>
where
Self: Sized,
F: FnMut(&Self::Item) -> K,
Expand All @@ -618,7 +621,7 @@ pub trait Itertools: Iterator {
/// See [`.chunk_by()`](Itertools::chunk_by).
#[deprecated(note = "Use .chunk_by() instead", since = "0.13.0")]
#[cfg(feature = "use_alloc")]
fn group_by<K, F>(self, key: F) -> GroupBy<K, Self, F>
fn group_by<K, F>(self, key: F) -> ChunkBy<K, Self, F>
where
Self: Sized,
F: FnMut(&Self::Item) -> K,
Expand All @@ -633,7 +636,7 @@ pub trait Itertools: Iterator {
/// determined by `size`. The last chunk will be shorter if there aren't
/// enough elements.
///
/// `IntoChunks` is based on `GroupBy`: it is iterable (implements
/// `IntoChunks` is based on `ChunkBy`: it is iterable (implements
/// `IntoIterator`, **not** `Iterator`), and it only buffers if several
/// chunk iterators are alive at the same time.
///
Expand Down

0 comments on commit edada59

Please sign in to comment.