Diffstat (limited to 'src/any.rs')
| -rw-r--r-- | src/any.rs | 239 |
1 files changed, 74 insertions, 165 deletions
@@ -19,9 +19,9 @@ //! without unsafe code. However, its recommended to use the provided [`nameable`] //! macro when possible. -// pub mod static_wrapper; +pub mod static_wrapper; -use crate::bijective_higher_ranked_trait; +use crate::{bijective_higher_ranked_trait, bijective_higher_ranked_type}; use core::{ marker::{PhantomData, PhantomPinned}, mem::{ManuallyDrop, MaybeUninit}, @@ -38,165 +38,67 @@ bijective_higher_ranked_trait! { pub type class TypeName[][]: {'static} [for<'lt> MaybeSized::Trait<'lt>] } -// trait Other<'lt> {} -// -// trait Demo<'lt> { -// fn demo(&self) -> &'lt i32; -// } -// -// bijective_higher_ranked_type! { -// type DynDemo['lt][]: (MaybeSized)['lt][] = for<'a> dyn Demo<'lt> + 'a -// } -// -// bijective_higher_ranked_type! { -// type DemoName[][]: (TypeName)[][] = for<'lt> DynDemo<'lt> -// } - -// bijective_higher_ranked_type! { -// pub type MaybeSizedRef['lt][T]: (MaybeSized)['lt][] = for<'a> &'lt T where { T: 'lt } -// } - -const _: () = { - pub struct Name<T: ?Sized>(PhantomData<fn() -> *const T>); - pub struct LowerName<'lt, T: ?Sized>(PhantomData<fn() -> (&'lt (), *const T)>); - - impl<'a, 'lt, T: ?Sized + MaybeSized::LowerForLt<'a, 'lt, &'a (&'lt (),)>> - MaybeSized::LowerForLt<'a, 'lt, &'a (&'lt (),)> for LowerName<'lt, T> - { - type T = &'a <T as MaybeSized::LowerForLt<'a, 'lt, &'a (&'lt (),)>>::T; - } - - impl<'a, 'lt, T: ?Sized + MaybeSized::RaiseForLt<'a, 'lt, &'a (&'lt (),)>> - MaybeSized::RaiseForLt<'a, 'lt, &'a (&'lt (),)> for &'a T - { - type HigherRanked = - LowerName<'lt, <T as MaybeSized::RaiseForLt<'a, 'lt, &'a (&'lt (),)>>::HigherRanked>; - } - - impl<'lt, T: ?Sized + TypeName::LowerForLt<'lt, &'lt ()>> TypeName::LowerForLt<'lt, &'lt ()> - for Name<T> - { - type T = LowerName<'lt, <T as TypeName::LowerForLt<'lt, &'lt ()>>::T>; - } - - impl<'lt, T: ?Sized + 'lt + TypeName::RaiseForLt<'lt, &'lt ()>> - TypeName::RaiseForLt<'lt, &'lt ()> for LowerName<'lt, T> - { - type HigherRanked = Name<TypeName::HigherRanked<'lt, T>>; - } -}; - -const _: () = { - pub struct Name<T: ?Sized>(PhantomData<fn() -> *const T>); - pub struct LowerName<'lt, T: ?Sized>(PhantomData<fn() -> (&'lt (), *const T)>); - - impl<'a, 'lt, T: ?Sized + MaybeSized::LowerForLt<'a, 'lt, &'a (&'lt (),)>> - MaybeSized::LowerForLt<'a, 'lt, &'a (&'lt (),)> for LowerName<'lt, T> - { - type T = &'a mut <T as MaybeSized::LowerForLt<'a, 'lt, &'a (&'lt (),)>>::T; +bijective_higher_ranked_type! { + pub type DynRef['lt][][T['lt][]]: MaybeSized['lt][] + for<'a> + (&'a MaybeSized::T<'a, 'lt, T>) + (&'a T) + where { + T: ?Sized } +} - impl<'a, 'lt, T: ?Sized + MaybeSized::RaiseForLt<'a, 'lt, &'a (&'lt (),)>> - MaybeSized::RaiseForLt<'a, 'lt, &'a (&'lt (),)> for &'a mut T - { - type HigherRanked = - LowerName<'lt, <T as MaybeSized::RaiseForLt<'a, 'lt, &'a (&'lt (),)>>::HigherRanked>; +bijective_higher_ranked_type! { + pub type [][][T[][]]: TypeName[][] + for<'lt> + (DynRef<'lt, TypeName::T<'lt, T>>) + (DynRef<'lt, T>) + where { + T: ?Sized } +} - impl<'lt, T: ?Sized + TypeName::LowerForLt<'lt, &'lt ()>> TypeName::LowerForLt<'lt, &'lt ()> - for Name<T> - { - type T = LowerName<'lt, <T as TypeName::LowerForLt<'lt, &'lt ()>>::T>; +bijective_higher_ranked_type! { + pub type DynMut['lt][][T['lt][]]: MaybeSized['lt][] + for<'a> + (&'a mut MaybeSized::T<'a, 'lt, T>) + (&'a mut T) + where { + T: ?Sized } +} - impl<'lt, T: ?Sized + 'lt + TypeName::RaiseForLt<'lt, &'lt ()>> - TypeName::RaiseForLt<'lt, &'lt ()> for LowerName<'lt, T> - { - type HigherRanked = Name<TypeName::HigherRanked<'lt, T>>; +bijective_higher_ranked_type! { + pub type [][][T[][]]: TypeName[][] + for<'lt> + (DynMut<'lt, TypeName::T<'lt, T>>) + (DynMut<'lt, T>) + where { + T: ?Sized } -}; +} #[cfg(feature = "alloc")] -const _: () = { - pub struct Name<T: ?Sized>(PhantomData<fn() -> *const T>); - pub struct LowerName<'lt, T: ?Sized>(PhantomData<fn() -> (&'lt (), *const T)>); - - impl<'a, 'lt, T: ?Sized + MaybeSized::LowerForLt<'a, 'lt, &'a (&'lt (),)>> - MaybeSized::LowerForLt<'a, 'lt, &'a (&'lt (),)> for LowerName<'lt, T> - { - type T = Box<<T as MaybeSized::LowerForLt<'a, 'lt, &'a (&'lt (),)>>::T>; - } - - impl<'a, 'lt, T: ?Sized + MaybeSized::RaiseForLt<'a, 'lt, &'a (&'lt (),)>> - MaybeSized::RaiseForLt<'a, 'lt, &'a (&'lt (),)> for Box<T> - { - type HigherRanked = - LowerName<'lt, <T as MaybeSized::RaiseForLt<'a, 'lt, &'a (&'lt (),)>>::HigherRanked>; - } - - impl<'lt, T: ?Sized + TypeName::LowerForLt<'lt, &'lt ()>> TypeName::LowerForLt<'lt, &'lt ()> - for Name<T> - { - type T = LowerName<'lt, <T as TypeName::LowerForLt<'lt, &'lt ()>>::T>; +bijective_higher_ranked_type! { + pub type DynBox['lt][][T['lt][]]: MaybeSized['lt][] + for<'a> + (Box<MaybeSized::T<'a, 'lt, T>>) + (Box<T>) + where { + T: ?Sized } +} - impl<'lt, T: ?Sized + 'lt + TypeName::RaiseForLt<'lt, &'lt ()>> - TypeName::RaiseForLt<'lt, &'lt ()> for LowerName<'lt, T> - { - type HigherRanked = Name<TypeName::HigherRanked<'lt, T>>; +#[cfg(feature = "alloc")] +bijective_higher_ranked_type! { + pub type [][][T[][]]: TypeName[][] + for<'lt> + (DynBox<'lt, TypeName::T<'lt, T>>) + (DynBox<'lt, T>) + where { + T: ?Sized } -}; - -// nameable! { -// pub struct Name['a, 'lt, T]; -// impl [T::ForLtName] for &'lt T where { -// T: TypeNameableForLt<'a, 'lt, &'a &'lt ()> + ?Sized -// } -// impl [T] where &'lt T::ForLtNameable { -// T: TypeNameForLt<'a, 'lt, &'a &'lt ()> + ?Sized, T::ForLtNameable: 'lt -// } -// } - -// nameable! { -// pub struct Name['a, 'lt, T]; -// impl [<T as TypeNameableHelper<'lt>>::Name] for &'lt mut T where { -// T: TypeNameable<'lt> + ?Sized -// } -// impl [T] where &'lt mut T::Nameable { -// T: TypeNameForLt<'a, 'lt, &'a &'lt ()> + ?Sized, T::Nameable: 'lt -// } -// } -// -// nameable! { -// pub struct Name['a, 'lt, T]; -// impl [<T as TypeNameableHelper<'lt>>::Name] for *const T where { -// T: TypeNameable<'lt> + ?Sized -// } -// impl [T] where *const T::Nameable { -// T: TypeNameForLt<'a, 'lt, &'a &'lt ()> + ?Sized -// } -// } -// -// nameable! { -// pub struct Name['a, 'lt, T]; -// impl [<T as TypeNameableHelper<'lt>>::Name] for *mut T where { -// T: TypeNameable<'lt> + ?Sized -// } -// impl [T] where *mut T::Nameable { -// T: TypeNameForLt<'a, 'lt, &'a &'lt ()> + ?Sized -// } -// } -// -// #[cfg(feature = "alloc")] -// nameable! { -// pub struct Name['a, 'lt, T]; -// impl [<T as TypeNameableHelper<'lt>>::Name] for Box<T> where { -// T: TypeNameable<'lt> + ?Sized -// } -// impl [T] where Box<T::Nameable> { -// T: TypeNameForLt<'a, 'lt, &'a &'lt ()> + ?Sized -// } -// } +} /// [`TypeId`][core::any::TypeId] with a lifetime generic `'lt`. /// @@ -370,7 +272,10 @@ pub trait AnyTrait<'lt> { /// If `self` doesn't support upcasting to the requested type /// then `None` is returned. The returned trait object is type erased so this trait /// is object safe. - fn upcast_to_id_mut<'a>(&'a mut self, id: LtTypeId<'lt>) -> Option<IndirectLtAny<'a, 'lt, Mut>> + fn upcast_to_id_mut<'a: 'b, 'b>( + &'a mut self, + id: LtTypeId<'lt>, + ) -> Option<IndirectLtAny<'b, 'lt, Mut>> where 'lt: 'a; } @@ -427,22 +332,22 @@ impl<'lt> dyn AnyTrait<'lt> + Send + '_ { #[macro_export] macro_rules! any_trait { { - impl[$a:lifetime, $lt:lifetime $($generic:tt)*] $name:ty = [$($protocol:ty),* $(,)?] $(else $fallback:path)? $(where $($bound:tt)*)? + impl[$lt:lifetime $($generic:tt)*] $name:ty = [$($protocol:ty),* $(,)?] $(else $fallback:path)? $(where $($bound:tt)*)? } => { impl<$lt $($generic)*> $crate::any::AnyTrait<$lt> for $name $(where $($bound)*)? { #[inline] - fn upcast_to_id<$a>( - &$a self, + fn upcast_to_id<'__>( + &'__ self, id: $crate::any::LtTypeId<$lt> - ) -> ::core::option::Option<$crate::any::IndirectLtAny<$a, $lt, $crate::any::Ref>> + ) -> ::core::option::Option<$crate::any::IndirectLtAny<'__, $lt, $crate::any::Ref>> where - $lt: $a + $lt: '__ { match id { $(id if id == $crate::any::LtTypeId::of::<$protocol>() - => ::core::option::Option::Some($crate::any::IndirectLtAny::<$a, $lt, _>::new::<$protocol>(self as _)),)* + => ::core::option::Option::Some($crate::any::IndirectLtAny::<'__, $lt, _>::new::<$protocol>(self as _)),)* _ => { $($fallback(id);)? ::core::option::Option::None @@ -451,16 +356,16 @@ macro_rules! any_trait { } #[inline] - fn upcast_to_id_mut<$a>( - &$a mut self, + fn upcast_to_id_mut<'__: '___, '___>( + &'__ mut self, id: $crate::any::LtTypeId<$lt> - ) -> ::core::option::Option<$crate::any::IndirectLtAny<$a, $lt, $crate::any::Mut>> + ) -> ::core::option::Option<$crate::any::IndirectLtAny<'___, $lt, $crate::any::Mut>> where - $lt: $a + $lt: '__ { match id { $(id if id == $crate::any::LtTypeId::of::<$protocol>() - => ::core::option::Option::Some($crate::any::IndirectLtAny::<$a, $lt, _>::new::<$protocol>(self as _)),)* + => ::core::option::Option::Some($crate::any::IndirectLtAny::<'___, $lt, _>::new::<$protocol>(self as _)),)* _ => { $($fallback(id);)? ::core::option::Option::None @@ -485,7 +390,11 @@ pub use any_trait; pub struct IndirectLtAny<'a, 'lt: 'a, I: Indirect<'a>> { info: fn() -> (LtTypeId<'lt>, unsafe fn(RawIndirect)), indirect: RawIndirect, - _marker: PhantomData<(I::ForT<fn(&'lt ()) -> &'lt ()>, PhantomPinned, *const ())>, + _marker: PhantomData<( + I::ForT<fn(&'lt (), &'a ()) -> (&'lt (), &'a ())>, + PhantomPinned, + *const (), + )>, } impl<'a, 'lt, I: Indirect<'a>> Drop for IndirectLtAny<'a, 'lt, I> { @@ -671,11 +580,11 @@ mod test { } bijective_higher_ranked_type! { - type DynZ['ctx][]: (MaybeSized)['ctx][] = for<'a> dyn Z<'ctx> + 'a + type DynZ['ctx][]: MaybeSized['ctx][] for<'a> (dyn Z<'ctx> + 'a) } bijective_higher_ranked_type! { - type [][]: (TypeName)[][] = for<'lt> DynZ<'lt> + type [][]: TypeName[][] for<'lt> (DynZ<'lt>) } struct X<'ctx>(&'ctx i32); @@ -687,7 +596,7 @@ mod test { } any_trait! { - impl['a, 'ctx] X<'ctx> = [ + impl['ctx] X<'ctx> = [ DynZ<'ctx> ] } |