SharedFunctorAny
This commit is contained in:
parent
bff238ff00
commit
1012f9be5e
@ -1,26 +1,26 @@
|
|||||||
use quote::quote;
|
use quote::quote;
|
||||||
use syn::{parse_macro_input, parse_quote, DeriveInput, GenericParam, Generics};
|
use syn::{parse_macro_input, parse_quote, DeriveInput, GenericParam, Generics};
|
||||||
|
|
||||||
#[proc_macro_derive(SharedFunctor)]
|
#[proc_macro_derive(SharedFunctorAny)]
|
||||||
pub fn derive_shared_functor(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
pub fn derive_shared_functor(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||||
let input = parse_macro_input!(input as DeriveInput);
|
let input = parse_macro_input!(input as DeriveInput);
|
||||||
let name = input.ident;
|
let name = input.ident;
|
||||||
let generics = add_clone_bounds(input.generics);
|
let generics = add_clone_bounds(input.generics);
|
||||||
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
|
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
|
||||||
let expanded = quote! {
|
let expanded = quote! {
|
||||||
impl #impl_generics SharedFunctor for #name #ty_generics #where_clause {
|
impl #impl_generics SharedFunctorAny for #name #ty_generics #where_clause {
|
||||||
type Shared<'a, A: 'a + Clone> = Self::F<'a, A>
|
type SharedAny<'a, A: 'a + Clone> = Self::F<'a, A>
|
||||||
where
|
where
|
||||||
Self: 'a;
|
Self: 'a;
|
||||||
|
|
||||||
fn share<'a, A: 'a + Clone>(fa: Self::F<'a, A>) -> Self::Shared<'a, A>
|
fn share<'a, A: 'a + Clone>(fa: Self::F<'a, A>) -> Self::SharedAny<'a, A>
|
||||||
where
|
where
|
||||||
Self: 'a,
|
Self: 'a,
|
||||||
{
|
{
|
||||||
fa
|
fa
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unshare<'a, A: 'a + Clone>(sa: Self::Shared<'a, A>) -> Self::F<'a, A>
|
fn unshare<'a, A: 'a + Clone>(sa: Self::SharedAny<'a, A>) -> Self::F<'a, A>
|
||||||
where
|
where
|
||||||
Self: 'a,
|
Self: 'a,
|
||||||
{
|
{
|
||||||
|
30
src/func.rs
30
src/func.rs
@ -21,7 +21,7 @@ pub mod tests;
|
|||||||
|
|
||||||
pub use std::ops::ControlFlow;
|
pub use std::ops::ControlFlow;
|
||||||
|
|
||||||
pub use radn_derive::SharedFunctor;
|
pub use radn_derive::SharedFunctorAny;
|
||||||
|
|
||||||
pub use self::applicative_select::{
|
pub use self::applicative_select::{
|
||||||
ApplicativeSelect, ApplicativeSelectExt, Selected, SelectedWrapped,
|
ApplicativeSelect, ApplicativeSelectExt, Selected, SelectedWrapped,
|
||||||
@ -268,24 +268,36 @@ pub trait MonadFailAnyExt<'a>: MonadFailAny<'a> {
|
|||||||
|
|
||||||
impl<'a, Fallible: ?Sized + MonadFailAny<'a>> MonadFailAnyExt<'a> for Fallible {}
|
impl<'a, Fallible: ?Sized + MonadFailAny<'a>> MonadFailAnyExt<'a> for Fallible {}
|
||||||
|
|
||||||
pub trait SharedFunctor: WeakFunctor {
|
pub trait SharedFunctorAny: WeakFunctor {
|
||||||
type Shared<'a, A: 'a + Clone>: 'a + Clone
|
type SharedAny<'a, A: 'a + Clone>: 'a + Clone
|
||||||
where
|
where
|
||||||
Self: 'a;
|
Self: 'a;
|
||||||
|
|
||||||
fn share<'a, A: 'a + Clone>(fa: Self::F<'a, A>) -> Self::Shared<'a, A>
|
fn share<'a, A: 'a + Clone>(fa: Self::F<'a, A>) -> Self::SharedAny<'a, A>
|
||||||
where
|
where
|
||||||
Self: 'a;
|
Self: 'a;
|
||||||
|
|
||||||
fn unshare<'a, A: 'a + Clone>(sa: Self::Shared<'a, A>) -> Self::F<'a, A>
|
fn unshare<'a, A: 'a + Clone>(sa: Self::SharedAny<'a, A>) -> Self::F<'a, A>
|
||||||
where
|
where
|
||||||
Self: 'a;
|
Self: 'a;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait SharedFunctorA<'a>: WeakFunctorA<'a> {
|
pub trait SharedFunctor<'a>: WeakFunctorA<'a> {
|
||||||
type SharedA<A: 'a + Clone>: 'a + Clone;
|
type Shared<A: 'a + Clone>: 'a + Clone;
|
||||||
|
|
||||||
fn share<A: 'a + Clone>(fa: Self::Fa<A>) -> Self::SharedA<A>;
|
fn share<A: 'a + Clone>(fa: Self::Fa<A>) -> Self::Shared<A>;
|
||||||
|
|
||||||
fn unshare<A: 'a + Clone>(sa: Self::SharedA<A>) -> Self::Fa<A>;
|
fn unshare<A: 'a + Clone>(sa: Self::Shared<A>) -> Self::Fa<A>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: 'a + SharedFunctorAny> SharedFunctor<'a> for T {
|
||||||
|
type Shared<A: 'a + Clone> = T::SharedAny<'a, A>;
|
||||||
|
|
||||||
|
fn share<A: 'a + Clone>(fa: Self::Fa<A>) -> Self::Shared<A> {
|
||||||
|
T::share(fa)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unshare<A: 'a + Clone>(sa: Self::Shared<A>) -> Self::Fa<A> {
|
||||||
|
T::unshare(sa)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -136,16 +136,16 @@ impl<'a, U: LocalFunctor<'a> + Functor<'a>, V: LocalFunctor<'a>> LocalFunctor<'a
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, U: SharedFunctorA<'a> + Functor<'a>, V: SharedFunctorA<'a>> SharedFunctorA<'a>
|
impl<'a, U: SharedFunctor<'a> + Functor<'a>, V: SharedFunctor<'a>> SharedFunctor<'a>
|
||||||
for CompositionInstance<U, V>
|
for CompositionInstance<U, V>
|
||||||
{
|
{
|
||||||
type SharedA<A: 'a + Clone> = U::SharedA<V::SharedA<A>>;
|
type Shared<A: 'a + Clone> = U::Shared<V::Shared<A>>;
|
||||||
|
|
||||||
fn share<A: 'a + Clone>(fa: Self::Fa<A>) -> Self::SharedA<A> {
|
fn share<A: 'a + Clone>(fa: Self::Fa<A>) -> Self::Shared<A> {
|
||||||
U::share(U::fmap(V::share, fa))
|
U::share(U::fmap(V::share, fa))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unshare<A: 'a + Clone>(sa: Self::SharedA<A>) -> Self::Fa<A> {
|
fn unshare<A: 'a + Clone>(sa: Self::Shared<A>) -> Self::Fa<A> {
|
||||||
U::fmap(V::unshare, U::unshare(sa))
|
U::fmap(V::unshare, U::unshare(sa))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ impl<A, E: Effect> WithEffect<A, E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(SharedFunctor)]
|
#[derive(SharedFunctorAny)]
|
||||||
pub struct EffectInstance<E>(E);
|
pub struct EffectInstance<E>(E);
|
||||||
|
|
||||||
impl<E> WeakFunctor for EffectInstance<E> {
|
impl<E> WeakFunctor for EffectInstance<E> {
|
||||||
|
@ -107,14 +107,14 @@ impl<'a> Monad<'a> for FutureInstance {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> SharedFunctorA<'a> for FutureInstance {
|
impl<'a> SharedFunctor<'a> for FutureInstance {
|
||||||
type SharedA<A: 'a + Clone> = Shared<Pin<Box<dyn 'a + Future<Output = A>>>>;
|
type Shared<A: 'a + Clone> = Shared<Pin<Box<dyn 'a + Future<Output = A>>>>;
|
||||||
|
|
||||||
fn share<A: 'a + Clone>(fa: Self::Fa<A>) -> Self::SharedA<A> {
|
fn share<A: 'a + Clone>(fa: Self::Fa<A>) -> Self::Shared<A> {
|
||||||
fa.shared()
|
fa.shared()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unshare<A: 'a + Clone>(sa: Self::SharedA<A>) -> Self::Fa<A> {
|
fn unshare<A: 'a + Clone>(sa: Self::Shared<A>) -> Self::Fa<A> {
|
||||||
Box::pin(sa)
|
Box::pin(sa)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -103,14 +103,14 @@ fn unshare<'a, A: 'a + Clone>(shared: &mut Option<Box<dyn 'a + FnOnce() -> A>>)
|
|||||||
a
|
a
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> SharedFunctorA<'a> for LazyInstance {
|
impl<'a> SharedFunctor<'a> for LazyInstance {
|
||||||
type SharedA<A: 'a + Clone> = Rc<RefCell<Option<Box<dyn 'a + FnOnce() -> A>>>>;
|
type Shared<A: 'a + Clone> = Rc<RefCell<Option<Box<dyn 'a + FnOnce() -> A>>>>;
|
||||||
|
|
||||||
fn share<A: 'a + Clone>(fa: Self::Fa<A>) -> Self::SharedA<A> {
|
fn share<A: 'a + Clone>(fa: Self::Fa<A>) -> Self::Shared<A> {
|
||||||
Rc::new(RefCell::new(Some(fa)))
|
Rc::new(RefCell::new(Some(fa)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unshare<A: 'a + Clone>(sa: Self::SharedA<A>) -> Self::Fa<A> {
|
fn unshare<A: 'a + Clone>(sa: Self::Shared<A>) -> Self::Fa<A> {
|
||||||
Box::new(move || unshare(&mut *sa.borrow_mut()))
|
Box::new(move || unshare(&mut *sa.borrow_mut()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
use crate::func::*;
|
use crate::func::*;
|
||||||
|
|
||||||
#[derive(SharedFunctor)]
|
#[derive(SharedFunctorAny)]
|
||||||
pub struct OptionInstance;
|
pub struct OptionInstance;
|
||||||
|
|
||||||
impl WeakFunctor for OptionInstance {
|
impl WeakFunctor for OptionInstance {
|
||||||
|
@ -194,16 +194,16 @@ impl<'a, Ex: 'a, Fallible: MonadFailAny<'a>> MonadFailAny<'a> for DeriveFailAny<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: SharedFunctorA<'a>, O: 'a + DeriveWeakFunctor> SharedFunctorA<'a>
|
impl<'a, T: SharedFunctor<'a>, O: 'a + DeriveWeakFunctor> SharedFunctor<'a>
|
||||||
for OverloadInstance<T, O>
|
for OverloadInstance<T, O>
|
||||||
{
|
{
|
||||||
type SharedA<A: 'a + Clone> = T::SharedA<A>;
|
type Shared<A: 'a + Clone> = T::Shared<A>;
|
||||||
|
|
||||||
fn share<A: 'a + Clone>(fa: Self::Fa<A>) -> Self::SharedA<A> {
|
fn share<A: 'a + Clone>(fa: Self::Fa<A>) -> Self::Shared<A> {
|
||||||
T::share(fa)
|
T::share(fa)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unshare<A: 'a + Clone>(sa: Self::SharedA<A>) -> Self::Fa<A> {
|
fn unshare<A: 'a + Clone>(sa: Self::Shared<A>) -> Self::Fa<A> {
|
||||||
T::unshare(sa)
|
T::unshare(sa)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
use crate::func::*;
|
use crate::func::*;
|
||||||
|
|
||||||
#[derive(SharedFunctor)]
|
#[derive(SharedFunctorAny)]
|
||||||
pub struct ResultInstance<E>(E);
|
pub struct ResultInstance<E>(E);
|
||||||
|
|
||||||
impl<E> WeakFunctor for ResultInstance<E> {
|
impl<E> WeakFunctor for ResultInstance<E> {
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
use crate::func::*;
|
use crate::func::*;
|
||||||
|
|
||||||
#[derive(SharedFunctor)]
|
#[derive(SharedFunctorAny)]
|
||||||
pub struct SoloInstance;
|
pub struct SoloInstance;
|
||||||
|
|
||||||
impl WeakFunctor for SoloInstance {
|
impl WeakFunctor for SoloInstance {
|
||||||
|
@ -104,14 +104,14 @@ impl<'a, E: 'a> Monad<'a> for TryFutureInstance<E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, E: 'a + Clone> SharedFunctorA<'a> for TryFutureInstance<E> {
|
impl<'a, E: 'a + Clone> SharedFunctor<'a> for TryFutureInstance<E> {
|
||||||
type SharedA<A: 'a + Clone> = Shared<Pin<Box<dyn 'a + Future<Output = Result<A, E>>>>>;
|
type Shared<A: 'a + Clone> = Shared<Pin<Box<dyn 'a + Future<Output = Result<A, E>>>>>;
|
||||||
|
|
||||||
fn share<A: 'a + Clone>(fa: Self::Fa<A>) -> Self::SharedA<A> {
|
fn share<A: 'a + Clone>(fa: Self::Fa<A>) -> Self::Shared<A> {
|
||||||
fa.shared()
|
fa.shared()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unshare<A: 'a + Clone>(sa: Self::SharedA<A>) -> Self::Fa<A> {
|
fn unshare<A: 'a + Clone>(sa: Self::Shared<A>) -> Self::Fa<A> {
|
||||||
Box::pin(sa)
|
Box::pin(sa)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user