master
Avril 4 years ago
parent 4f89f698a8
commit c37cb6c3bb
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,6 +1,6 @@
[package] [package]
name = "ad-hoc-iter" name = "ad-hoc-iter"
version = "0.1.1" version = "0.2.0"
description = "Ad-hoc exact size owning iterator macro and other optional utils" description = "Ad-hoc exact size owning iterator macro and other optional utils"
repository = "https://git.flanchan.moe/flanchan/ad-hoc-iter" repository = "https://git.flanchan.moe/flanchan/ad-hoc-iter"
keywords = ["iterator", "macro", "iter"] keywords = ["iterator", "macro", "iter"]
@ -9,9 +9,7 @@ edition = "2018"
license = "MIT" license = "MIT"
[features] [features]
default = ["maybe-many"] # Enable the incomplete `maybe` module
# Enable the `maybe` module
maybe-many = [] maybe-many = []
[dependencies] [dependencies]

@ -7,20 +7,62 @@
/// An iterator that can yield `0,1,2+` items /// An iterator that can yield `0,1,2+` items
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum MaybeMany<T,U> pub enum MaybeMany<T,U>
where U: IntoIterator<Item = T>
{ {
None, None,
One(T), One(T),
Many(U), Many(U),
} }
impl<T,U> MaybeMany<T,U> impl<T> MaybeMany<T, std::iter::Empty<T>>
{ {
/// A single variant
#[inline] pub fn one(value: T) -> Self
{
Self::One(value)
}
}
impl MaybeMany<std::convert::Infallible, std::iter::Empty<std::convert::Infallible>>
{
/// An empty variant that yields no values ever.
#[inline] pub fn none() -> Self
{
Self::None
}
}
impl<T,U> MaybeMany<T,U>
where U: IntoIterator<Item = T>
{
/// Consume into the `Many` variant.
pub fn into_many(self) -> MaybeMany<T, Self>
{
MaybeMany::Many(self)
}
/// Chain another iterator to this one
pub fn chain<I>(self, iter: I) -> MaybeMany<T, std::iter::Chain<<Self as IntoIterator>::IntoIter, I::IntoIter>>
where I: IntoIterator<Item=T>
{
MaybeMany::Many(self.into_iter().chain(iter.into_iter()))
}
/// Into a boxed `MaybeMany` type.
pub fn boxed(self) -> MaybeMany<T, Box<dyn Iterator<Item=T>+'static>>
where U: 'static,
T: 'static
{
MaybeMany::Many(Box::new(self.into_iter()))
}
/// Try to guess the size. /// Try to guess the size.
/// ///
/// * `None` => `Some(0)` /// * `None` => `Some(0)`
/// * `One(_)` => `Some(1)` /// * `One(_)` => `Some(1)`
/// * `Many(_) => `None` /// * `Many(_) => `None`
#[inline] pub const fn size_hint(&self) -> Option<usize> #[inline] pub fn size_hint(&self) -> Option<usize>
{ {
match self { match self {
Self::None => Some(0), Self::None => Some(0),
@ -29,7 +71,7 @@ impl<T,U> MaybeMany<T,U>
} }
} }
/// Is this an empty instance /// Is this an empty instance
#[inline] pub const fn is_none(&self) -> bool #[inline] pub fn is_none(&self) -> bool
{ {
if let Self::None = self { if let Self::None = self {
true true
@ -38,7 +80,7 @@ impl<T,U> MaybeMany<T,U>
} }
} }
/// Is this a single value instance? /// Is this a single value instance?
#[inline] pub const fn is_single(&self) -> bool #[inline] pub fn is_single(&self) -> bool
{ {
if let Self::One(_) = self { if let Self::One(_) = self {
true true
@ -47,7 +89,7 @@ impl<T,U> MaybeMany<T,U>
} }
} }
/// Is this a 2+ value instance? /// Is this a 2+ value instance?
#[inline] pub const fn is_many(&self) -> bool #[inline] pub fn is_many(&self) -> bool
{ {
if let Self::Many(_) = self { if let Self::Many(_) = self {
true true
@ -100,6 +142,7 @@ impl<T,U> MaybeMany<T,U>
/// An iterator for `MaybeMany` instances. /// An iterator for `MaybeMany` instances.
#[non_exhaustive] #[derive(Debug, Clone)] #[non_exhaustive] #[derive(Debug, Clone)]
pub enum MaybeManyIter<T,U> pub enum MaybeManyIter<T,U>
where U: Iterator<Item=T>
{ {
None, None,
One(std::iter::Once<T>), One(std::iter::Once<T>),
@ -145,3 +188,19 @@ impl<T, U: IntoIterator<Item=T>> IntoIterator for MaybeMany<T, U>
} }
} }
} }
#[cfg(test)]
mod tests
{
use super::*;
#[test]
fn into_many()
{
let mut mayb = MaybeMany::one("hello").boxed();
mayb = mayb.chain(vec!["world", "!"]).boxed();
let output: Vec<_> = mayb.into_iter().collect();
assert_eq!(&output[..], &["hello", "world", "!"]);
}
}

Loading…
Cancel
Save