//! MutPredicates are `RefProjection`s towards [`bool`]. //! `MutPredicate`s are `MutProjection`s towards [`bool`]. //! //! Prefer using `Into` `Predicate` types over `Projection` types in your function signatures to make them more intuitively readable. //! //! # Example //! //! `Predicate`s are useful for all sorts of async combinators, like filtering a Future: //! //! ``` //! use core::{future::Future, pin::Pin}; //! use ergo_pin::ergo_pin; //! use percolate::predicate::{IntoPredicateMut, PredicateMut}; //! //! #[ergo_pin] //! async fn filter_future( //! source: impl Future, //! predicate: impl IntoPredicateMut, //! ) -> Option { //! let item = source.await; //! pin!(predicate.into_predicate_mut()) //! .test(&item) //! .await //! .then(move || item) //! } //! ``` use crate::{ handles::PinHandleMut, projection::{ self, FusedMutProjectionMut, FusedRefProjectionMut, IntoFusedMutProjectionMut, IntoFusedRefProjectionMut, IntoMutProjectionMut, IntoRefProjectionMut, MutProjectionMut, RefProjectionMut, }, }; use core::{future::Future, pin::Pin}; use futures_core::FusedFuture; /// alias: [`RefProjectionMut`] pub trait PredicateMut: RefProjectionMut + MutPredicateMut { fn test<'a>( self: Pin<&'a mut Self>, value: &'a T, ) -> PinHandleMut<'a, dyn 'a + Future> { self.project_ref(value) } } impl PredicateMut for P where P: RefProjectionMut {} /// alias: [`MutProjectionMut`] pub trait MutPredicateMut: MutProjectionMut { fn test_mut<'a>( self: Pin<&'a mut Self>, value: &'a mut T, ) -> PinHandleMut<'a, dyn 'a + Future> { self.project_mut(value) } } impl MutPredicateMut for P where P: MutProjectionMut {} /// alias: [`FusedRefProjectionMut`] pub trait FusedPredicateMut: FusedRefProjectionMut + PredicateMut + FusedMutPredicateMut { fn test<'a>( self: Pin<&'a mut Self>, value: &'a T, ) -> PinHandleMut<'a, dyn 'a + FusedFuture> { self.project_ref_fused(value) } } impl FusedPredicateMut for P where P: FusedRefProjectionMut {} /// alias: [`FusedMutProjectionMut`] pub trait FusedMutPredicateMut: FusedMutProjectionMut + MutPredicateMut { fn test<'a>( self: Pin<&'a mut Self>, value: &'a mut T, ) -> PinHandleMut<'a, dyn 'a + FusedFuture> { self.project_mut_fused(value) } } impl FusedMutPredicateMut for P where P: FusedMutProjectionMut {} /// alias: [`IntoRefProjectionMut`] pub trait IntoPredicateMut: IntoRefProjectionMut + IntoMutPredicateMut { type IntoPredMut: PredicateMut; #[must_use] fn into_predicate_mut(self) -> Self::IntoPredMut; } impl IntoPredicateMut for P where P: IntoRefProjectionMut, { type IntoPredMut = Self::IntoRefProjMut; fn into_predicate_mut(self) -> Self::IntoPredMut { self.into_ref_projection_mut() } } /// alias: [`IntoMutProjectionMut`] pub trait IntoMutPredicateMut: IntoMutProjectionMut { type IntoMutPredMut: MutPredicateMut; #[must_use] fn into_mut_predicate_mut(self) -> Self::IntoMutPredMut; } impl IntoMutPredicateMut for P where P: IntoMutProjectionMut, { type IntoMutPredMut = Self::IntoMutProjMut; fn into_mut_predicate_mut(self) -> Self::IntoMutPredMut { self.into_mut_projection_mut() } } /// alias: [`IntoFusedRefProjectionMut`] pub trait IntoFusedPredicateMut: IntoFusedRefProjectionMut + IntoPredicateMut + IntoFusedMutPredicateMut { type IntoFusedPredMut: FusedPredicateMut; #[must_use] fn into_fused_predicate_mut(self) -> Self::IntoFusedPredMut; } impl IntoFusedPredicateMut for P where P: IntoFusedRefProjectionMut, { type IntoFusedPredMut = Self::IntoFusedRefProjMut; fn into_fused_predicate_mut(self) -> Self::IntoFusedPredMut { self.into_fused_ref_projection_mut() } } /// alias: [`IntoFusedMutProjectionMut`] pub trait IntoFusedMutPredicateMut: IntoFusedMutProjectionMut + IntoMutPredicateMut { type IntoFusedMutPredMut: FusedMutPredicateMut; #[must_use] fn into_fused_mut_predicate_mut(self) -> Self::IntoFusedMutPredMut; } impl IntoFusedMutPredicateMut for P where P: IntoFusedMutProjectionMut, { type IntoFusedMutPredMut = Self::IntoFusedMutProjMut; fn into_fused_mut_predicate_mut(self) -> Self::IntoFusedMutPredMut { self.into_fused_mut_projection_mut() } } /// alias: [`projection::from_ref_blocking_mut(…)`](`projection::from_ref_blocking_mut`) #[must_use] pub fn from_blocking_mut(predicate_mut: P) -> FusedBlockingMut where P: FnMut(&T) -> bool, { projection::from_ref_blocking_mut(predicate_mut) } /// alias: [`projection::FusedRefBlockingMut`] pub type FusedBlockingMut = projection::FusedRefBlockingMut;