iter_python/
extension_traits.rs

1pub use ::join_lazy_fmt::Join;
2
3/// Provides a convenience `.vec()` and `.dict()` adapters to collect an `i!(…)`
4/// iterator expression into a [`Vec`] and a
5/// [`HashMap`][`::std::collections::HashMap`] respectively.
6///
7/// # Example
8///
9/// ```rust
10/// use ::iter_python::prelude::*;
11///
12/// assert_eq!(
13///     i!(x for x in 0 .. 5).vec(),
14///     v![x for x in 0 .. 5],
15/// );
16///
17/// assert_eq!(
18///     i!((x, x * x) for x in 1 ..= 2).dict(),
19///     {
20///         let mut map = ::std::collections::HashMap::new();
21///         map.insert(1, 1);
22///         map.insert(2, 4);
23///         map
24///     },
25/// );
26/// ```
27#[cfg(feature = "std")]
28#[cfg_attr(feature = "better-docs",
29    doc(cfg(feature = "std")),
30)]
31pub trait IteratorExt : Sized + Iterator {
32    fn vec<T> (self: Self)
33      -> crate::__::std::vec::Vec<T>
34    where
35        Self : Iterator<Item = T>,
36    {
37        self.collect()
38    }
39
40    fn dict<K, V> (self: Self)
41      -> crate::__::std::collections::HashMap<K, V>
42    where
43        Self : Iterator<Item = (K, V)>,
44        K : ::core::hash::Hash + Eq,
45    {
46        self.collect()
47    }
48}
49
50#[cfg(feature = "std")]
51#[cfg_attr(feature = "better-docs",
52    doc(cfg(feature = "std")),
53)]
54impl<T> IteratorExt for T where Self : Iterator {}