pub trait KaiIterator: IntoIterator + Sized {
// Provided methods
fn chain_if<I, F>(self, condition: bool, f: F) -> ChainIf<Self, I> ⓘ
where I: IntoIterator<Item = Self::Item>,
F: FnOnce() -> I { ... }
fn chain_if_else<J, K, F, G>(
self,
condition: bool,
f: F,
g: G,
) -> ChainIfElse<Self, J, K> ⓘ
where J: IntoIterator<Item = Self::Item>,
K: IntoIterator<Item = Self::Item>,
F: FnOnce() -> J,
G: FnOnce() -> K { ... }
}Expand description
Generates my custom iterator adapters
For convenience, this is implmented for all types that
implement not just Iterator,
but IntoIterator as well.
See methods for usage.
Provided Methods§
Sourcefn chain_if<I, F>(self, condition: bool, f: F) -> ChainIf<Self, I> ⓘ
fn chain_if<I, F>(self, condition: bool, f: F) -> ChainIf<Self, I> ⓘ
Chain this iterator with another if the condition is true
§Example
use kai::*;
let condition = true;
// Turn this
let mut v = vec![1, 2, 3];
if condition {
v.extend(vec![4, 5, 6])
}
// Into this
let w: Vec<_> = vec![1, 2, 3].chain_if(condition, || vec![4, 5, 6]).collect();
assert_eq!(v, w);Sourcefn chain_if_else<J, K, F, G>(
self,
condition: bool,
f: F,
g: G,
) -> ChainIfElse<Self, J, K> ⓘwhere
J: IntoIterator<Item = Self::Item>,
K: IntoIterator<Item = Self::Item>,
F: FnOnce() -> J,
G: FnOnce() -> K,
fn chain_if_else<J, K, F, G>(
self,
condition: bool,
f: F,
g: G,
) -> ChainIfElse<Self, J, K> ⓘwhere
J: IntoIterator<Item = Self::Item>,
K: IntoIterator<Item = Self::Item>,
F: FnOnce() -> J,
G: FnOnce() -> K,
Chain this iterator with one of two options based on the condition
If the condition is true, the iterator generated by f is used.
If the condition is false, the iterator generated by g is used.
§Example
use kai::*;
let v = vec![1, 2, 3];
let len = v.len();
let w: Vec<_> = v.into_iter().chain_if_else(
len == 3,
|| vec![4, 5],
|| Some(6)
).collect();
assert_eq!(
w,
vec![1, 2, 3, 4, 5],
);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.