Diffstat (limited to 'src/any.rs')
| -rw-r--r-- | src/any.rs | 308 |
1 files changed, 162 insertions, 146 deletions
@@ -6,10 +6,7 @@ pub mod indirect; mod static_wrapper; mod type_name_id; -use crate::{ - higher_ranked_trait, higher_ranked_type, - hkt::{Invariant, Marker}, -}; +use crate::hkt::{Invariant, Marker}; use core::marker::PhantomData; pub use static_wrapper::*; @@ -22,71 +19,81 @@ pub enum StaticType {} pub enum NamedType {} pub enum LifetimeType {} -higher_ranked_trait! { - pub type class TypeName for<'a, 'ctx> { - type Bound = &'a &'ctx (); +#[allow(non_snake_case)] +pub mod TypeName { + pub trait MemberTypeForLt<'a, 'ctx: 'a, B> { + type T: ?Sized + LowerTypeWithBound<'a, 'ctx, &'a &'ctx (), Higher = Self>; + } + + pub trait MemberType: 'static + for<'a, 'ctx> MemberTypeForLt<'a, 'ctx, &'a &'ctx ()> {} - type T: { Send + Sync + 'a } where { - 'ctx: 'a - }; + impl<T: ?Sized> MemberType for T where + T: 'static + for<'a, 'ctx> MemberTypeForLt<'a, 'ctx, &'a &'ctx ()> + { + } - type HigherRanked: { Send + Sync + 'static }; + pub trait LowerTypeWithBound<'a, 'ctx: 'a, B>: 'a + Send + Sync { + type Higher: ?Sized + MemberTypeForLt<'a, 'ctx, &'a &'ctx (), T = Self> + MemberType; } + + pub trait LowerType<'a, 'ctx: 'a>: LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()> {} + + impl<'a, 'ctx: 'a, T: ?Sized> LowerType<'a, 'ctx> for T where + T: LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()> + { + } + + pub type T<'a, 'ctx, __> = <__ as MemberTypeForLt<'a, 'ctx, &'a &'ctx ()>>::T; + pub type HigherRanked<'a, 'ctx, __> = + <__ as LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()>>::Higher; } pub struct RefHrt<T: ?Sized>(Marker<T>); -higher_ranked_type! { - impl TypeName { - impl['a, 'ctx, T] type T['a, 'ctx] for RefHrt<T> = - &'a TypeName::T<'a, 'ctx, T> - where { - T: ?Sized + TypeName::LowerForLt<'a, 'ctx, TypeName::Bound<'a, 'ctx>>, - TypeName::T<'a, 'ctx, T>: 'a - }; - - impl['a, 'ctx, T] type HigherRanked['a, 'ctx] for &'a T = - RefHrt<TypeName::HigherRanked<'a, 'ctx, T>> - where { - T: ?Sized + TypeName::LowerType<'a, 'ctx> - }; - } +impl<'a, 'ctx, T: ?Sized> TypeName::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()> for RefHrt<T> +where + T: TypeName::MemberType, +{ + type T = &'a TypeName::T<'a, 'ctx, T>; +} + +impl<'a, 'ctx, T: ?Sized> TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()> for &'a T +where + T: TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()>, +{ + type Higher = RefHrt<TypeName::HigherRanked<'a, 'ctx, T>>; } pub struct MutHrt<T: ?Sized>(Marker<T>); -higher_ranked_type! { - impl TypeName { - impl['a, 'ctx, T] type T['a, 'ctx] for MutHrt<T> = - &'a mut TypeName::T<'a, 'ctx, T> - where { - T: ?Sized + TypeName::LowerForLt<'a, 'ctx, TypeName::Bound<'a, 'ctx>>, - TypeName::T<'a, 'ctx, T>: 'a - }; - - impl['a, 'ctx, T] type HigherRanked['a, 'ctx] for &'a mut T = - MutHrt<TypeName::HigherRanked<'a, 'ctx, T>> - where { - T: ?Sized + TypeName::RaiseForLt<'a, 'ctx, TypeName::Bound<'a, 'ctx>> - }; - } +impl<'a, 'ctx, T: ?Sized> TypeName::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()> for MutHrt<T> +where + T: TypeName::MemberType, +{ + type T = &'a mut TypeName::T<'a, 'ctx, T>; +} + +impl<'a, 'ctx, T: ?Sized> TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()> for &'a mut T +where + T: TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()>, +{ + type Higher = MutHrt<TypeName::HigherRanked<'a, 'ctx, T>>; } #[cfg(feature = "alloc")] -higher_ranked_type! { - impl TypeName { - impl['a, 'ctx, T] type T['a, 'ctx] for Box<T> = - Box<TypeName::T<'a, 'ctx, T>> - where { - T: ?Sized + TypeName::LowerForLt<'a, 'ctx, TypeName::Bound<'a, 'ctx>>, - }; - - impl['a, 'ctx, T] type HigherRanked['a, 'ctx] for Box<T> = - Box<TypeName::HigherRanked<'a, 'ctx, T>> - where { - T: ?Sized + TypeName::RaiseForLt<'a, 'ctx, TypeName::Bound<'a, 'ctx>> - }; - } +impl<'a, 'ctx, T: ?Sized> TypeName::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()> for Box<T> +where + T: TypeName::MemberType, +{ + type T = Box<TypeName::T<'a, 'ctx, T>>; +} + +#[cfg(feature = "alloc")] +impl<'a, 'ctx, T: ?Sized> TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()> for Box<T> +where + T: TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()>, +{ + type Higher = Box<TypeName::HigherRanked<'a, 'ctx, T>>; } /// Dynamic trait lookup. @@ -95,58 +102,58 @@ higher_ranked_type! { /// given trait object `id`. This is similar to upcasting to the trait given /// by `id` if [`AnyTrait`] had every trait as a super bound. /// -/// ``` -/// use treaty::any::{AnyTrait, any_trait, TypeName}; -/// use treaty::hkt::higher_ranked_type; -/// -/// // Create a test value. -/// let my_num = MyNum(42); -/// -/// // Cast to be a AnyTrait trait object. -/// // Now we don't know the type. -/// let anything: &(dyn AnyTrait<'_> + Send + Sync) = &my_num; -/// -/// // We can still upcast to an impl of ToNum. -/// let to_num_object: &dyn ToNum = anything.upcast::<DynToNum>().unwrap(); -/// -/// assert_eq!(to_num_object.num(), 42); -/// -/// // === Type Setup === -/// -/// // An example trait. -/// trait ToNum { -/// fn num(&self) -> i32; -/// } -/// -/// enum DynToNum {} -/// -/// higher_ranked_type! { -/// impl TypeName { -/// impl['a, 'ctx] type T['a, 'ctx] for DynToNum = -/// dyn ToNum + Send + Sync + 'a; -/// -/// impl['a, 'ctx] type HigherRanked['a, 'ctx] for dyn ToNum + Send + Sync + 'a = -/// DynToNum; -/// } -/// } -/// -/// // An example struct. -/// struct MyNum(i32); -/// -/// // The example struct impls the example trait. -/// impl ToNum for MyNum { -/// fn num(&self) -> i32 { -/// self.0 -/// } -/// } -/// -/// // Allow the example struct's trait impls to be looked up at runtime. -/// // Here the only trait that can be looked up is ToNum as its the only -/// // one in the list. -/// any_trait! { -/// impl['ctx] MyNum = [DynToNum] -/// } -/// ``` +// ``` +// use treaty::any::{AnyTrait, any_trait, TypeName}; +// use treaty::hkt::higher_ranked_type; +// +// // Create a test value. +// let my_num = MyNum(42); +// +// // Cast to be a AnyTrait trait object. +// // Now we don't know the type. +// let anything: &(dyn AnyTrait<'_> + Send + Sync) = &my_num; +// +// // We can still upcast to an impl of ToNum. +// let to_num_object: &dyn ToNum = anything.upcast::<DynToNum>().unwrap(); +// +// assert_eq!(to_num_object.num(), 42); +// +// // === Type Setup === +// +// // An example trait. +// trait ToNum { +// fn num(&self) -> i32; +// } +// +// enum DynToNum {} +// +// higher_ranked_type! { +// impl TypeName { +// impl['a, 'ctx] type T['a, 'ctx] for DynToNum = +// dyn ToNum + Send + Sync + 'a; +// +// impl['a, 'ctx] type HigherRanked['a, 'ctx] for dyn ToNum + Send + Sync + 'a = +// DynToNum; +// } +// } +// +// // An example struct. +// struct MyNum(i32); +// +// // The example struct impls the example trait. +// impl ToNum for MyNum { +// fn num(&self) -> i32 { +// self.0 +// } +// } +// +// // Allow the example struct's trait impls to be looked up at runtime. +// // Here the only trait that can be looked up is ToNum as its the only +// // one in the list. +// any_trait! { +// impl['ctx] MyNum = [DynToNum] +// } +// ``` pub trait AnyTrait<'ctx> { /// Upcast a borrow to the given trait object. /// @@ -389,45 +396,45 @@ impl<'a, 'ctx, I: Indirect<'a>> AnyTraitObject<'a, 'ctx, I> { mod test { use super::*; - #[test] - fn any_trait_macro_implements_the_trait() { - trait Z<'ctx> { - fn get(&self) -> i32; - } - - struct DynZ; - - higher_ranked_type! { - impl TypeName { - impl['a, 'ctx] type T['a, 'ctx] for DynZ = - dyn Z<'ctx> + Send + Sync + 'a; - - impl['a, 'ctx] type HigherRanked['a, 'ctx] for dyn Z<'ctx> + Send + Sync + 'a = - DynZ; - } - } - - struct X<'ctx>(&'ctx i32); - - impl<'ctx> Z<'ctx> for X<'ctx> { - fn get(&self) -> i32 { - *self.0 - } - } - - any_trait! { - impl['ctx] X<'ctx> = [ - DynZ - ] - } - - let z = 42; - let x = X(&z); - let y = (&x as &(dyn AnyTrait<'_> + Send + Sync)) - .upcast::<DynZ>() - .unwrap(); - assert_eq!(y.get(), 42); - } + // #[test] + // fn any_trait_macro_implements_the_trait() { + // trait Z<'ctx> { + // fn get(&self) -> i32; + // } + // + // struct DynZ; + // + // higher_ranked_type! { + // impl TypeName { + // impl['a, 'ctx] type T['a, 'ctx] for DynZ = + // dyn Z<'ctx> + Send + Sync + 'a; + // + // impl['a, 'ctx] type HigherRanked['a, 'ctx] for dyn Z<'ctx> + Send + Sync + 'a = + // DynZ; + // } + // } + // + // struct X<'ctx>(&'ctx i32); + // + // impl<'ctx> Z<'ctx> for X<'ctx> { + // fn get(&self) -> i32 { + // *self.0 + // } + // } + // + // any_trait! { + // impl['ctx] X<'ctx> = [ + // DynZ + // ] + // } + // + // let z = 42; + // let x = X(&z); + // let y = (&x as &(dyn AnyTrait<'_> + Send + Sync)) + // .upcast::<DynZ>() + // .unwrap(); + // assert_eq!(y.get(), 42); + // } // The following proves that the higher ranked types are bijective using the type system. // @@ -462,4 +469,13 @@ mod test { // If A -> B -> C -> B -> A (shown by this assignment), then A and C must be bijective. let _y: &U = x; } + + // fn _is_bijective_raise2<'a, 'ctx: 'a, 'b, 'c: 'b, T>( + // x: &TypeName::T<'b, 'c, TypeName::HigherRanked<'a, 'ctx, T>>, + // ) where + // T: TypeName::LowerType<'a, 'ctx>, + // { + // // If C -> B -> A -> B -> C (shown by this assignment), then C and A must be bijective. + // let _y: &T = x; + // } } |