Diffstat (limited to 'src/any.rs')
| -rw-r--r-- | src/any.rs | 25 |
1 files changed, 14 insertions, 11 deletions
@@ -3,12 +3,13 @@ //! The `AnyTrait` trait provides dynamic upcasting to trait objects. pub mod indirect; -pub mod static_wrapper; +mod static_wrapper; mod type_name_id; use crate::{bijective_higher_ranked_trait, bijective_higher_ranked_type, hkt::Invariant}; use core::marker::PhantomData; +pub use static_wrapper::*; pub use type_name_id::*; #[cfg(all(feature = "alloc", not(feature = "std")))] @@ -31,13 +32,13 @@ bijective_higher_ranked_trait! { /// /// Types of this class can usually be sealed as they don't need to be named directly. /// - /// Higher ranked types of this form have a [`TypeId`] associated with them. + /// Higher ranked types of this form have a [`TypeId`][core::any::TypeId] associated with them. /// This allows them to be used as a name for lifetime containing types. /// /// This type class has members in the [`WithContextLt`] higher ranked type class. /// To get a concrete type two lowerings need to be applied to inject two lifetimes. /// One for the context lifetime, and one for the lifetime of the concrete type. - pub type class TypeName[][]: {'static} [for<'ctx> WithContextLt::MemberType<'ctx> + Send] + pub type class TypeName[][]: {'static} [for<'ctx> WithContextLt::MemberType<'ctx>] } bijective_higher_ranked_type! { @@ -208,8 +209,8 @@ impl<'b, 'ctx: 'b> dyn AnyTrait<'ctx> + Send + 'b { pub fn upcast<'a, Trait: ?Sized + TypeName::LowerType<'ctx>>( &'a self, ) -> Option<&'a WithContextLt::T<'a, 'ctx, Trait>> { - self.upcast_to_id(TypeNameId::of::<Trait>()).map(|object| { - match object.downcast() { + self.upcast_to_id(TypeNameId::of::<Trait>()) + .map(|object| match object.downcast() { Ok(object) => object, Err(object) => panic!( "Unexpected trait object. This means a bad impl of \ @@ -217,8 +218,7 @@ impl<'b, 'ctx: 'b> dyn AnyTrait<'ctx> + Send + 'b { TypeNameId::of::<Trait>(), object.id() ), - } - }) + }) } /// Upcast a mutable borrow to the given trait object type. @@ -333,7 +333,7 @@ use self::indirect::{sealed::RawIndirect, Indirect}; /// /// `&'a dyn MyTrait<ctx>` becomes `AnyTraitObject<'a, 'ctx, Ref>`. /// -/// The `I` generic is the flavor if pointer being used. It can be [`Ref`] or [`Mut`]. +/// The `I` generic is the flavor if pointer being used. It can be [`Ref`][indirect::Ref] or [`Mut`][indirect::Mut]. #[must_use] pub struct AnyTraitObject<'a, 'ctx: 'a, I: Indirect<'a>> { /// The extra vtable pointer. @@ -378,7 +378,7 @@ impl<'a, 'ctx, I: Indirect<'a>> AnyTraitObject<'a, 'ctx, I> { /// returned as is. pub fn downcast<T: ?Sized + WithContextLt::LowerType<'a, 'ctx>>( self, - ) -> Result<I::ForT<T>, Self> + ) -> Result<I::ForT<T>, Self> where WithContextLt::HigherRanked<'a, 'ctx, T>: TypeName::LowerType<'ctx>, { @@ -450,7 +450,7 @@ mod test { // We have the type tower: T<'a, 'ctx> <-> DynT<'ctx> <-> NameT // We want every T, DynT, NameT set to be unique. // - // Assume there was a U that tried to use NameT in it's type tower: + // Assume there was a U that tried to use NameT in it's type tower: // U<'a, 'ctx> <-> DynU<'ctx> <-> NameT // // If we traverse the type tower in this order: T -r-> A -r-> B -l-> C -l-> D @@ -464,7 +464,10 @@ mod test { x: &WithContextLt::T< 'a, 'ctx, - TypeName::T<'ctx, TypeName::HigherRanked<'ctx, WithContextLt::HigherRanked<'a, 'ctx, T>>>, + TypeName::T< + 'ctx, + TypeName::HigherRanked<'ctx, WithContextLt::HigherRanked<'a, 'ctx, T>>, + >, >, ) where T: WithContextLt::LowerType<'a, 'ctx>, |