Skip to content

Commit f34d53e

Browse files
committed
feat(iter): add shortcut functions
1 parent ed2c440 commit f34d53e

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ unstable = []
2525

2626
[dependencies]
2727
simdutf8 = { version = "0.1.5", default-features = false }
28-
thiserror = "2.0.11"
28+
thiserror = "2.0.12"

src/iter.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ use core::fmt;
44
#[cfg(feature = "alloc")]
55
use alloc::string::String;
66

7+
#[cfg(feature = "alloc")]
8+
use alloc::vec::Vec;
9+
710
pub trait IteratorExt: Iterator {
811
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
912
#[cfg(feature = "alloc")]
@@ -30,3 +33,41 @@ pub trait IteratorExt: Iterator {
3033
}
3134

3235
impl<I: Iterator> IteratorExt for I {}
36+
37+
pub fn map_collect<C, T, I, F>(iterable: I, f: F) -> C
38+
where
39+
I: IntoIterator,
40+
F: FnMut(I::Item) -> T,
41+
C: FromIterator<T>,
42+
{
43+
iterable.into_iter().map(f).collect()
44+
}
45+
46+
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
47+
#[cfg(feature = "alloc")]
48+
pub fn map_collect_vec<T, I, F>(iterable: I, f: F) -> Vec<T>
49+
where
50+
I: IntoIterator,
51+
F: FnMut(I::Item) -> T,
52+
{
53+
map_collect(iterable, f)
54+
}
55+
56+
pub fn filter_map_collect<C, T, I, F>(iterable: I, f: F) -> C
57+
where
58+
I: IntoIterator,
59+
F: FnMut(I::Item) -> Option<T>,
60+
C: FromIterator<T>,
61+
{
62+
iterable.into_iter().filter_map(f).collect()
63+
}
64+
65+
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
66+
#[cfg(feature = "alloc")]
67+
pub fn filter_map_collect_vec<T, I, F>(iterable: I, f: F) -> Vec<T>
68+
where
69+
I: IntoIterator,
70+
F: FnMut(I::Item) -> Option<T>,
71+
{
72+
filter_map_collect(iterable, f)
73+
}

0 commit comments

Comments
 (0)