Skip to content

Introduce Itertools::owned() / Owned. #1015

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions benches/specializations.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![allow(unstable_name_collisions)]

use std::ffi::CString;

use criterion::black_box;
use criterion::BenchmarkId;
use itertools::Itertools;
Expand Down Expand Up @@ -666,4 +668,10 @@ bench_specializations! {
}
v.iter().copied().flatten_ok()
}
owned {
{
let v = black_box((0..1024).map(|_| CString::new("foo bar zoo").unwrap()).collect_vec());
}
v.iter().map(CString::as_c_str).owned()
}
}
50 changes: 50 additions & 0 deletions src/adaptors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1263,3 +1263,53 @@ where
F: FnMut(&mut I::Item),
{
}

/// An iterator adapter to transform borrowed items ([`ToOwned`]) into their owned counterparts.
///
/// See [`.owned()`](crate::Itertools::owned) for more information.
#[derive(Clone, Debug)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Owned<I> {
iter: I,
}

impl<'a, I, T> Iterator for Owned<I>
where
I: Iterator<Item = &'a T>,
T: 'a + ?Sized + ToOwned,
{
type Item = T::Owned;

fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(ToOwned::to_owned)
}
}

impl<'a, I, T> ExactSizeIterator for Owned<I>
where
I: ExactSizeIterator<Item = &'a T>,
T: 'a + ?Sized + ToOwned,
{
}

impl<'a, I, T> DoubleEndedIterator for Owned<I>
where
I: DoubleEndedIterator<Item = &'a T>,
T: 'a + ?Sized + ToOwned,
{
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(ToOwned::to_owned)
}
}

impl<'a, I, T> FusedIterator for Owned<I>
where
I: FusedIterator<Item = &'a T>,
T: 'a + ?Sized + ToOwned,
{
}

/// Create a new `Owned` iterator.
pub fn owned<I>(iter: I) -> Owned<I> {
Owned { iter }
}
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ pub use std::iter as __std_iter;
pub mod structs {
#[cfg(feature = "use_alloc")]
pub use crate::adaptors::MultiProduct;
#[cfg(feature = "use_alloc")]
pub use crate::adaptors::Owned;
pub use crate::adaptors::{
Batching, Coalesce, Dedup, DedupBy, DedupByWithCount, DedupWithCount, FilterMapOk,
FilterOk, Interleave, InterleaveShortest, MapInto, MapOk, Positions, Product, PutBack,
Expand Down Expand Up @@ -4581,6 +4583,21 @@ pub trait Itertools: Iterator {
_ => Err(sh),
}
}

/// Create a new iterator that transforms borrowed items of an underlying iterator into their owned counterparts.
///
/// ```
/// use itertools::Itertools;
///
/// assert_eq!(["Hello", " ", "world!"].iter().owned().collect_vec(), vec!["Hello".to_owned(), " ".to_owned(), "world!".to_owned()]);
/// ```
#[cfg(feature = "use_alloc")]
fn owned(self) -> Owned<Self>
where
Self: Sized,
{
adaptors::owned(self)
}
}

impl<T> Itertools for T where T: Iterator + ?Sized {}
Expand Down
14 changes: 14 additions & 0 deletions tests/test_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use rand::{
Rng, SeedableRng,
};
use rand::{seq::SliceRandom, thread_rng};
use std::ffi::CString;
use std::{cmp::min, fmt::Debug, marker::PhantomData};

#[test]
Expand Down Expand Up @@ -1567,3 +1568,16 @@ fn multiunzip() {
)
);
}

#[test]
fn owned() {
let owned = [
CString::new("foo").unwrap(),
CString::new("bar").unwrap(),
CString::new("").unwrap(),
CString::new("zoo").unwrap(),
];
let borrowed = owned.iter().map(CString::as_c_str);
let iter = borrowed.owned();
assert_eq!(iter.collect_array(), Some(owned));
}
Loading