pub struct Lazy<T>(/* private fields */);Expand description
A lazy evaluated Semigroup with nonempty buffer that is implemented by Vec.
§Properties
crate::Annotate | crate::Monoid | crate::Commutative | |
|---|---|---|---|
| impl | ❌ | ❌ | ✅ |
| where | T: crate::Commutative |
§Examples
§Usage
use semigroup::{op::Coalesce, Lazy, Semigroup};
let a = Coalesce(Some(1));
let b = Coalesce(Some(2));
let c = Coalesce(Some(3));
let lazy = Lazy::from(a).semigroup(b.into()).semigroup(c.into());
assert_eq!(lazy.first(), &Coalesce(Some(1)));
assert_eq!(lazy.last(), &Coalesce(Some(3)));
assert_eq!(lazy.combine(), Coalesce(Some(1)));§Derive
use semigroup::{op::Coalesce, Lazy, Semigroup};
#[derive(Debug, Clone, Copy, PartialEq, Semigroup)]
#[semigroup(annotated, with = "semigroup::op::Coalesce")]
struct ExampleStruct<'a> {
num: Option<u32>,
str: Option<&'a str>,
#[semigroup(with = "semigroup::op::Overwrite")]
boolean: bool,
}
let a = ExampleStruct { num: Some(1), str: None, boolean: true };
let b = ExampleStruct { num: None, str: Some("ten"), boolean: false };
let c = ExampleStruct { num: Some(100), str: None, boolean: false };
let lazy = Lazy::from(a).semigroup(b.into()).semigroup(c.into());
assert_eq!(lazy.clone().map_combine(|x| Coalesce(x.num)), Coalesce(Some(1)));
assert_eq!(lazy.map_combine_rev(|x| Coalesce(x.str)), Coalesce(Some("ten")));Implementations§
Source§impl<T: Semigroup> Lazy<T>
impl<T: Semigroup> Lazy<T>
Sourcepub fn combine_clone(&self) -> Twhere
T: Clone,
pub fn combine_clone(&self) -> Twhere
T: Clone,
Evaluates Lazy buffer like Lazy::combine by cloning each element.
§Examples
use semigroup::{op::Coalesce, Lazy, Semigroup};
let a = Coalesce(Some(1));
let b = Coalesce(Some(2));
let c = Coalesce(Some(3));
let lazy = Lazy::from(a).semigroup(b.into()).semigroup(c.into());
assert_eq!(lazy.combine_clone(), Coalesce(Some(1)));Sourcepub fn combine_rev(self) -> T
pub fn combine_rev(self) -> T
Sourcepub fn combine_rev_clone(&self) -> Twhere
T: Clone,
pub fn combine_rev_clone(&self) -> Twhere
T: Clone,
Evaluates Lazy buffer in reverse order like Lazy::combine_rev by cloning each element.
§Examples
use semigroup::{op::Coalesce, Lazy, Semigroup};
let a = Coalesce(Some(1));
let b = Coalesce(Some(2));
let c = Coalesce(Some(3));
let lazy = Lazy::from(a).semigroup(b.into()).semigroup(c.into());
assert_eq!(lazy.combine_rev_clone(), Coalesce(Some(3)));Source§impl<T: Semigroup + Clone> Lazy<&T>
impl<T: Semigroup + Clone> Lazy<&T>
Sourcepub fn combine_cloned(&self) -> T
pub fn combine_cloned(&self) -> T
TODO
Sourcepub fn combine_rev_cloned(&self) -> T
pub fn combine_rev_cloned(&self) -> T
TODO
Source§impl<T> Lazy<T>
impl<T> Lazy<T>
Sourcepub fn from_iterator<I: IntoIterator<Item = T>>(iter: I) -> Option<Self>
pub fn from_iterator<I: IntoIterator<Item = T>>(iter: I) -> Option<Self>
Create Lazy from iterator. Used by crate::CombineIterator::collect_lazy.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the Lazy buffer contains no elements. It is nonempty, so always returns false.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the Lazy buffer. It is nonempty, so always returns 1 or more.
Sourcepub fn split_first(&self) -> (&T, &[T])
pub fn split_first(&self) -> (&T, &[T])
Returns the reference to the first element and all the rest elements of the Lazy buffer.
Sourcepub fn split_off_first(self) -> (T, Vec<T>)
pub fn split_off_first(self) -> (T, Vec<T>)
Returns the first element and all the rest elements of the Lazy buffer.
Sourcepub fn split_last(&self) -> (&T, &[T])
pub fn split_last(&self) -> (&T, &[T])
Returns the reference to the last element and all the rest elements of the Lazy buffer.
Sourcepub fn split_off_last(self) -> (T, Vec<T>)
pub fn split_off_last(self) -> (T, Vec<T>)
Returns the last element and all the rest elements of the Lazy buffer.
Sourcepub fn get<I: SliceIndex<[T]>>(&self, index: I) -> Option<&I::Output>
pub fn get<I: SliceIndex<[T]>>(&self, index: I) -> Option<&I::Output>
Returns an element or slice like Vec.
Sourcepub fn iter(&self) -> <&[T] as IntoIterator>::IntoIter ⓘ
pub fn iter(&self) -> <&[T] as IntoIterator>::IntoIter ⓘ
Returns an iterator over of the Lazy buffer.
Sourcepub fn map_combine<U: Semigroup, F: FnMut(T) -> U>(self, f: F) -> U
pub fn map_combine<U: Semigroup, F: FnMut(T) -> U>(self, f: F) -> U
Source§impl<T, A: PartialEq> Lazy<Annotated<T, A>>
impl<T, A: PartialEq> Lazy<Annotated<T, A>>
Sourcepub fn find_annotated(&self, annotation: &A) -> Option<&Annotated<T, A>>
pub fn find_annotated(&self, annotation: &A) -> Option<&Annotated<T, A>>
O(n), searches for a value that has the given annotation.
§Examples
use semigroup::{op::Coalesce, Annotate, Lazy, Semigroup};
let a = Coalesce(Some(1)).annotated("edge");
let b = Coalesce(Some(2)).annotated("middle");
let c = Coalesce(Some(3)).annotated("edge");
let lazy = Lazy::from(a).semigroup(b.into()).semigroup(c.into());
assert_eq!(lazy.find_annotated(&"edge"), Some(&a));
assert_eq!(lazy.find_annotated(&"middle"), Some(&b));
assert_eq!(lazy.find_annotated(&"where"), None);
assert_eq!(lazy.combine(), Coalesce(Some(1)).annotated("edge"));Sourcepub fn position_annotated(&self, annotation: &A) -> Option<usize>
pub fn position_annotated(&self, annotation: &A) -> Option<usize>
O(n), searches for a value’s index that has the given annotation.
§Examples
use semigroup::{op::Coalesce, Annotate, Lazy, Semigroup};
let a = Coalesce(Some(1)).annotated("edge");
let b = Coalesce(Some(2)).annotated("middle");
let c = Coalesce(Some(3)).annotated("edge");
let lazy = Lazy::from(a).semigroup(b.into()).semigroup(c.into());
assert_eq!(lazy.position_annotated(&"edge"), Some(0));
assert_eq!(lazy.position_annotated(&"middle"), Some(1));
assert_eq!(lazy.position_annotated(&"where"), None);
assert_eq!(lazy.combine(), Coalesce(Some(1)).annotated("edge"));Sourcepub fn rfind_annotated(&self, annotation: &A) -> Option<&Annotated<T, A>>
pub fn rfind_annotated(&self, annotation: &A) -> Option<&Annotated<T, A>>
O(n), searches for a value that has the given annotation from the end.
§Examples
use semigroup::{op::Coalesce, Annotate, Lazy, Semigroup};
let a = Coalesce(Some(1)).annotated("edge");
let b = Coalesce(Some(2)).annotated("middle");
let c = Coalesce(Some(3)).annotated("edge");
let lazy = Lazy::from(a).semigroup(b.into()).semigroup(c.into());
assert_eq!(lazy.rfind_annotated(&"edge"), Some(&c));
assert_eq!(lazy.rfind_annotated(&"middle"), Some(&b));
assert_eq!(lazy.rfind_annotated(&"where"), None);
assert_eq!(lazy.combine(), Coalesce(Some(1)).annotated("edge"));Sourcepub fn rposition_annotated(&self, annotation: &A) -> Option<usize>
pub fn rposition_annotated(&self, annotation: &A) -> Option<usize>
O(n), searches for a value’s index that has the given annotation from the end.
§Examples
use semigroup::{op::Coalesce, Annotate, Lazy, Semigroup};
let a = Coalesce(Some(1)).annotated("edge");
let b = Coalesce(Some(2)).annotated("middle");
let c = Coalesce(Some(3)).annotated("edge");
let lazy = Lazy::from(a).semigroup(b.into()).semigroup(c.into());
assert_eq!(lazy.rposition_annotated(&"edge"), Some(2));
assert_eq!(lazy.rposition_annotated(&"middle"), Some(1));
assert_eq!(lazy.rposition_annotated(&"where"), None);
assert_eq!(lazy.combine(), Coalesce(Some(1)).annotated("edge"));Sourcepub fn find_annotated_all<'a>(
&'a self,
annotation: &'a A,
) -> impl 'a + Iterator<Item = &'a Annotated<T, A>>
pub fn find_annotated_all<'a>( &'a self, annotation: &'a A, ) -> impl 'a + Iterator<Item = &'a Annotated<T, A>>
O(n), searches for all values that have the given annotation.
§Examples
use semigroup::{op::Coalesce, Annotate, Lazy, Semigroup};
let a = Coalesce(Some(1)).annotated("edge");
let b = Coalesce(Some(2)).annotated("middle");
let c = Coalesce(Some(3)).annotated("edge");
let lazy = Lazy::from(a).semigroup(b.into()).semigroup(c.into());
assert_eq!(lazy.find_annotated_all(&"edge").collect::<Vec<_>>(), vec![&a, &c]);
assert_eq!(lazy.find_annotated_all(&"middle").collect::<Vec<_>>(), vec![&b]);
assert_eq!(lazy.find_annotated_all(&"where").collect::<Vec<_>>(), vec![&a; 0]);
assert_eq!(lazy.combine(), Coalesce(Some(1)).annotated("edge"));Sourcepub fn position_annotated_all<'a>(
&'a self,
annotation: &'a A,
) -> impl 'a + Iterator<Item = usize>
pub fn position_annotated_all<'a>( &'a self, annotation: &'a A, ) -> impl 'a + Iterator<Item = usize>
O(n), searches for all values’ indices that have the given annotation.
§Examples
use semigroup::{op::Coalesce, Annotate, Lazy, Semigroup};
let a = Coalesce(Some(1)).annotated("edge");
let b = Coalesce(Some(2)).annotated("middle");
let c = Coalesce(Some(3)).annotated("edge");
let lazy = Lazy::from(a).semigroup(b.into()).semigroup(c.into());
assert_eq!(lazy.position_annotated_all(&"edge").collect::<Vec<_>>(), vec![0, 2]);
assert_eq!(lazy.position_annotated_all(&"middle").collect::<Vec<_>>(), vec![1]);
assert_eq!(lazy.position_annotated_all(&"where").collect::<Vec<_>>(), vec![0; 0]);
assert_eq!(lazy.combine(), Coalesce(Some(1)).annotated("edge"));Trait Implementations§
Source§impl<'de, T: Deserialize<'de>> Deserialize<'de> for Lazy<T>
Available on crate feature serde only.
impl<'de, T: Deserialize<'de>> Deserialize<'de> for Lazy<T>
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<T> Extend<T> for Lazy<T>
impl<T> Extend<T> for Lazy<T>
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<T> IntoIterator for Lazy<T>
impl<T> IntoIterator for Lazy<T>
Source§impl<T: Ord> Ord for Lazy<T>
impl<T: Ord> Ord for Lazy<T>
Source§impl<T: PartialOrd> PartialOrd for Lazy<T>
impl<T: PartialOrd> PartialOrd for Lazy<T>
Source§impl<T: Clone + Semigroup + Serialize> Serialize for Lazy<T>
Available on crate feature serde only.
impl<T: Clone + Semigroup + Serialize> Serialize for Lazy<T>
serde only.impl<T> Commutative for Lazy<T>where
T: Commutative,
impl<T: Eq> Eq for Lazy<T>
impl<T> StructuralPartialEq for Lazy<T>
Auto Trait Implementations§
impl<T> Freeze for Lazy<T>
impl<T> RefUnwindSafe for Lazy<T>where
T: RefUnwindSafe,
impl<T> Send for Lazy<T>where
T: Send,
impl<T> Sync for Lazy<T>where
T: Sync,
impl<T> Unpin for Lazy<T>where
T: Unpin,
impl<T> UnwindSafe for Lazy<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> AsyncCommutative for Twhere
T: Commutative,
impl<T> AsyncCommutative for Twhere
T: Commutative,
Source§fn fold_stream(
init: Self,
stream: impl Stream<Item = Self>,
) -> impl Future<Output = Self>
fn fold_stream( init: Self, stream: impl Stream<Item = Self>, ) -> impl Future<Output = Self>
async only.CombineStream::fold_semigroup.Source§fn reduce_stream(
stream: impl Stream<Item = Self> + Unpin,
) -> impl Future<Output = Option<Self>>
fn reduce_stream( stream: impl Stream<Item = Self> + Unpin, ) -> impl Future<Output = Option<Self>>
async only.CombineStream::reduce_semigroup.Source§fn combine_stream(
stream: impl Stream<Item = Self>,
) -> impl Future<Output = Self>where
Self: Monoid,
fn combine_stream(
stream: impl Stream<Item = Self>,
) -> impl Future<Output = Self>where
Self: Monoid,
async and monoid only.CombineStream::combine_monoid.Source§impl<T> AsyncSemigroup for Twhere
T: Semigroup,
impl<T> AsyncSemigroup for Twhere
T: Semigroup,
Source§async fn async_op_assign(base: &mut T, other: T)
async fn async_op_assign(base: &mut T, other: T)
async only.Source§fn async_op(base: Self, other: Self) -> impl Future<Output = Self>
fn async_op(base: Self, other: Self) -> impl Future<Output = Self>
async only.Source§fn async_semigroup_assign(&mut self, other: Self) -> impl Future<Output = ()>
fn async_semigroup_assign(&mut self, other: Self) -> impl Future<Output = ()>
async only.Source§fn async_semigroup(self, other: Self) -> impl Future<Output = Self>
fn async_semigroup(self, other: Self) -> impl Future<Output = Self>
async only.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> TryAsyncCommutative for Twhere
T: Commutative,
impl<T> TryAsyncCommutative for Twhere
T: Commutative,
Source§fn try_fold_stream<S: TryStream<Ok = Self>>(
init: Self,
stream: S,
) -> impl Future<Output = Result<Self, S::Error>>
fn try_fold_stream<S: TryStream<Ok = Self>>( init: Self, stream: S, ) -> impl Future<Output = Result<Self, S::Error>>
async only.TryCombineStream::try_fold_semigroup.Source§fn try_reduce_stream<S: TryStream<Item = Result<Self, E>, Ok = Self, Error = E> + Unpin, E>(
stream: S,
) -> impl Future<Output = Option<Result<Self, E>>>
fn try_reduce_stream<S: TryStream<Item = Result<Self, E>, Ok = Self, Error = E> + Unpin, E>( stream: S, ) -> impl Future<Output = Option<Result<Self, E>>>
async only.Source§fn try_combine_stream<S: TryStream<Ok = Self>>(
stream: S,
) -> impl Future<Output = Result<Self, S::Error>>where
Self: Monoid,
fn try_combine_stream<S: TryStream<Ok = Self>>(
stream: S,
) -> impl Future<Output = Result<Self, S::Error>>where
Self: Monoid,
async and monoid only.TryCombineStream::try_combine_monoid.