[no description]
take a step backwards
| -rw-r--r-- | src/lib.rs | 13 | ||||
| -rw-r--r-- | src/option.rs | 76 | ||||
| -rw-r--r-- | src/try.rs | 71 |
3 files changed, 78 insertions, 82 deletions
@@ -1,4 +1,7 @@ #![feature( + lazy_type_alias, + try_trait_v2_residual, + try_trait_v2, type_alias_impl_trait, tuple_trait, import_trait_associated_functions, @@ -20,10 +23,8 @@ pub use iterator::{ IteratorOfTuplesWithF, }; pub mod fns; -mod option; -pub use option::{ - OptionOfMutRefToTuple, OptionOfRefToTuple, OptionOfTuple, -}; +mod r#try; +pub use r#try::{TryRefMutTuple, TryRefTuple, TryTuple}; /// Reverse a tuple. pub trait Reverse: Tupl { type Reversed: Tupl; @@ -201,11 +202,11 @@ pub trait Tupl: core::marker::Tuple { last: Self::Last, ) -> Self; - type AsRef<'a> + type AsRef<'a>: Tupl where Self: 'a; fn as_ref(&self) -> Self::AsRef<'_>; - type AsMut<'a> + type AsMut<'a>: Tupl where Self: 'a; fn as_mut(&mut self) -> Self::AsMut<'_>; diff --git a/src/option.rs b/src/option.rs deleted file mode 100644 index 0ab0cd8..0000000 --- a/src/option.rs +++ /dev/null @@ -1,76 +0,0 @@ -use crate::{Pick, Tupl}; - -pub trait OptionOfTuple<T: Tupl> { - fn on<const N: usize>(self) -> Option<T::At> - where - T: Pick<N>; -} - -impl<T: Tupl> OptionOfTuple<T> for Option<T> { - fn on<const N: usize>(self) -> Option<<T>::At> - where - T: Pick<N>, - { - self.map(T::pick) - } -} -macro_rules! option_of_ref_to_tuple { - ($name1:ident, $name2:ident, $as_ref:ident, $AsRef:ident $(, $mut:tt)?) => { - pub trait $name1<'a, T: Tupl> { - fn on<const N: usize>( - self, - ) -> Option<<T::$AsRef<'a> as Pick<N>>::At> - where - T::$AsRef<'a>: Pick<N>; - } - impl<'a, T: Tupl> $name1<'a, T> for Option<&'a $($mut)? T> { - fn on<const N: usize>( - self, - ) -> Option<<T::$AsRef<'a> as Pick<N>>::At> - where - T::$AsRef<'a>: Pick<N>, - { - match self { - Some(x) => Some(x.$as_ref().pick()), - None => None, - } - } - } - impl<'a, T: Tupl> $name1<'a, T> for &'a $($mut)? Option< T> { - fn on<const N: usize>( - self, - ) -> Option<<T::$AsRef<'a> as Pick<N>>::At> - where - T::$AsRef<'a>: Pick<N>, - { - match self { - Some(x) => Some(x.$as_ref().pick()), - None => None, - } - } - } - }; -} - -option_of_ref_to_tuple!( - OptionOfRefToTuple, - OptionOfRefToTupleWithF, - as_ref, - AsRef -); - -option_of_ref_to_tuple!( - OptionOfMutRefToTuple, - OptionOfMutRefToTupleWithF, - as_mut, - AsMut, - mut -); -#[test] -fn x() { - struct Y; - let x = Some(&mut (Y, 2)); - let x = x.on::<1>(); - let x = &mut Some((Y, 2)); - let x = x.on::<1>(); -} diff --git a/src/try.rs b/src/try.rs new file mode 100644 index 0000000..6fcd740 --- /dev/null +++ b/src/try.rs @@ -0,0 +1,71 @@ +use core::ops::{FromResidual, Residual, Try}; + +use crate::{Pick, Tupl}; + +macro_rules! option_of_ref_to_tuple { + ($name1:ident, $name2:ident, $as_ref:ident, $AsRef:ident $(, $mut:tt)?) => { + +pub trait $name1<'a, T: Tupl + 'a>: Try<Output = &'a $($mut)? T> + Sized { + fn on<const N: usize>( + self, + ) -> ChangeOutputType<Self, <T::$AsRef<'a> as Pick<N>>::At> + where + <T as Tupl>::$AsRef<'a>: Pick<N>, + Self: + Try<Residual: Residual<<T::$AsRef<'a> as Pick<N>>::At> + Sized>, + { + match self.branch() { + core::ops::ControlFlow::Continue(x) => + <_>::from_output(x.$as_ref().pick()), + core::ops::ControlFlow::Break(x) => <_>::from_residual(x), + } + } +} +impl<'a, T: Tupl + 'a, R: Try<Output = &'a $($mut)? T>> $name1<'a, T> for R {} + }; +} +pub trait TryTuple<T: Tupl>: Try<Output = T> { + fn on<const N: usize>(self) -> ChangeOutputType<Self, T::At> + where + T: Pick<N>, + Self: Try<Residual: Residual<T::At>> + Sized, + { + match self.branch() { + core::ops::ControlFlow::Continue(x) => + <_>::from_output(x.pick()), + core::ops::ControlFlow::Break(x) => <_>::from_residual(x), + } + } +} +pub type ChangeOutputType<T: Try<Residual: Residual<V>>, V> = + <T::Residual as Residual<V>>::TryType; + +impl<T: Tupl, Tr: Try<Output = T>> TryTuple<T> for Tr {} + +// impl<T: Tupl, Tr: Try<Output = T>> TryTuple<T> for Tr {} +// impl<T: Tupl, Tr: Try<Output = T>> TryTuple<T> for Tr {} + +option_of_ref_to_tuple!( + TryRefTuple, + OptionOfRefToTupleWithF, + as_ref, + AsRef +); + +option_of_ref_to_tuple!( + TryRefMutTuple, + OptionOfMutRefToTupleWithF, + as_mut, + AsMut, + mut +); + +#[test] +fn x() { + struct Y; + let x = Some(&mut (Y, 2)); + let x = x.on::<1>(); + let x = &mut Some((Y, 2)); + let x = x.as_mut().on::<1>(); + let x = (&Ok::<_, ()>((Y, Y))).as_ref().on::<1>(); +} |