Diffstat (limited to 'src/any/static_wrapper.rs')
| -rw-r--r-- | src/any/static_wrapper.rs | 189 |
1 files changed, 132 insertions, 57 deletions
diff --git a/src/any/static_wrapper.rs b/src/any/static_wrapper.rs index 4acb200..4b238b9 100644 --- a/src/any/static_wrapper.rs +++ b/src/any/static_wrapper.rs @@ -1,162 +1,237 @@ -//! Wrapper types that impl [`TypeNameable`] when their generic type is `'static`. +//! Wrapper types that impl [`TypeName`] when their generic type `T` is `'static`. use super::*; -/// Impl of [`TypeNameable`] for `'static` types that are owned (`T`). +/// Owned static `T`. #[derive(PartialEq, Clone, Copy, Debug)] #[repr(transparent)] pub struct OwnedStatic<T: ?Sized>(pub T); -// bijective_higher_ranked_type! { -// pub type DynOwnedStatic['lt][T][]: MaybeSized['lt][] -// for<'a> -// (OwnedStatic<T>) -// where { -// T: ?Sized + 'lt -// } -// } - +/// Higher ranked type for [`OwnedStatic`]. pub struct DynOwnedStatic<T: ?Sized>(PhantomData<fn() -> *const T>); -impl<'a, 'lt, T: ?Sized + 'lt> MaybeSized::LowerForLt<'a, 'lt, &'a (&'lt (),)> +impl<'a, 'ctx, T: ?Sized + 'ctx> WithContextLt::LowerForLt<'a, 'ctx, &'a (&'ctx (),)> for DynOwnedStatic<T> { type T = OwnedStatic<T>; } -impl<'a, 'lt, T: ?Sized + 'lt> MaybeSized::RaiseForLt<'a, 'lt, &'a (&'lt (),)> for OwnedStatic<T> { +impl<'a, 'ctx, T: ?Sized + 'ctx> WithContextLt::RaiseForLt<'a, 'ctx, &'a (&'ctx (),)> + for OwnedStatic<T> +{ type HigherRanked = DynOwnedStatic<T>; } bijective_higher_ranked_type! { pub type [][T][]: TypeName[][] - for<'lt> + for<'ctx> (DynOwnedStatic<T>) where { T: ?Sized + 'static } } -/// Impl of [`TypeNameable`] for `'static` types that are borrowed (`&'lt T`). +/// Borrowed static `T` for `'ctx`. #[repr(transparent)] -pub struct BorrowedStatic<'lt, T: ?Sized>(pub &'lt T); +pub struct BorrowedStatic<'ctx, T: ?Sized>(pub &'ctx T); bijective_higher_ranked_type! { - pub type DynBorrowedStatic['lt][T]: MaybeSized['lt][] + /// Higher ranked type for [`BorrowedStatic`]. + pub type DynBorrowedStatic['ctx][T]: WithContextLt['ctx][] for<'a> - (BorrowedStatic<'lt, T>) + (BorrowedStatic<'ctx, T>) where { - T: ?Sized + 'lt, + T: ?Sized + 'ctx, } } bijective_higher_ranked_type! { pub type [][T][]: TypeName[][] - for<'lt> - (DynBorrowedStatic<'lt, T>) + for<'ctx> + (DynBorrowedStatic<'ctx, T>) where { T: ?Sized + 'static } } -/// Impl of [`TypeNameable`] for `'static` types that are temporarily borrowed (`&'a T`). +/// Borrowed static `T` for `'a`. #[repr(transparent)] pub struct TempBorrowedStatic<'a, T: ?Sized>(pub &'a T); bijective_higher_ranked_type! { - pub type DynTempBorrowedStatic['lt][T]: MaybeSized['lt][] + /// Higher ranked type for [`TempBorrowedStatic`]. + pub type DynTempBorrowedStatic['ctx][T]: WithContextLt['ctx][] for<'a> (TempBorrowedStatic<'a, T>) where { - T: ?Sized + 'lt, + T: ?Sized + 'ctx, } } bijective_higher_ranked_type! { pub type [][T][]: TypeName[][] - for<'lt> - (DynTempBorrowedStatic<'lt, T>) + for<'ctx> + (DynTempBorrowedStatic<'ctx, T>) where { T: ?Sized + 'static } } -/// Impl of [`TypeNameable`] for `'static` types that are borrowed mutably (`&'lt mut T`). +/// Mutably borrowed static `T` for `'ctx`. #[repr(transparent)] -pub struct BorrowedMutStatic<'lt, T: ?Sized>(pub &'lt mut T); +pub struct BorrowedMutStatic<'ctx, T: ?Sized>(pub &'ctx mut T); bijective_higher_ranked_type! { - pub type DynBorrowedMutStatic['lt][T]: MaybeSized['lt][] + /// Higher ranked type for [`BorrowedMutStatic`]. + pub type DynBorrowedMutStatic['ctx][T]: WithContextLt['ctx][] for<'a> - (BorrowedMutStatic<'lt, T>) + (BorrowedMutStatic<'ctx, T>) where { - T: ?Sized + 'lt, + T: ?Sized + 'ctx, } } bijective_higher_ranked_type! { pub type [][T][]: TypeName[][] - for<'lt> - (DynBorrowedMutStatic<'lt, T>) + for<'ctx> + (DynBorrowedMutStatic<'ctx, T>) where { T: ?Sized + 'static } } -/// Impl of [`TypeNameable`] for `'static` types that are temporarily borrowed mutably (`&'a mut T`). +/// Mutably borrowed static `T` for `'a`. #[repr(transparent)] pub struct TempBorrowedMutStatic<'a, T: ?Sized>(pub &'a mut T); bijective_higher_ranked_type! { - pub type DynTempBorrowedMutStatic['lt][T]: MaybeSized['lt][] + /// Higher ranked type for [`TempBorrowedMutStatic`]. + pub type DynTempBorrowedMutStatic['ctx][T]: WithContextLt['ctx][] for<'a> (TempBorrowedMutStatic<'a, T>) where { - T: ?Sized + 'lt, + T: ?Sized + 'ctx, } } bijective_higher_ranked_type! { pub type [][T][]: TypeName[][] - for<'lt> - (DynTempBorrowedMutStatic<'lt, T>) + for<'ctx> + (DynTempBorrowedMutStatic<'ctx, T>) where { T: ?Sized + 'static } } -/// Impl of [`TypeNameable`] for `'static` types that are in a [`Box`] (`Box<T>`). +/// Boxed static `T`. #[cfg(feature = "alloc")] +#[repr(transparent)] pub struct BoxedStatic<T: ?Sized>(pub Box<T>); +/// Higher ranked type for [`BoxedStatic`]. #[cfg(feature = "alloc")] -bijective_higher_ranked_type! { - pub type DynBoxedStatic['lt][T]: MaybeSized['lt][] - for<'a> - (BoxedStatic<T>) - where { - T: ?Sized + 'lt, - } +pub struct DynBoxedStatic<T: ?Sized>(PhantomData<fn() -> *const T>); + +#[cfg(feature = "alloc")] +impl<'a, 'ctx, T: ?Sized + 'ctx> WithContextLt::LowerForLt<'a, 'ctx, &'a (&'ctx (),)> + for DynBoxedStatic<T> +{ + type T = BoxedStatic<T>; +} + +#[cfg(feature = "alloc")] +impl<'a, 'ctx, T: ?Sized + 'ctx> WithContextLt::RaiseForLt<'a, 'ctx, &'a (&'ctx (),)> + for BoxedStatic<T> +{ + type HigherRanked = DynBoxedStatic<T>; } #[cfg(feature = "alloc")] bijective_higher_ranked_type! { pub type [][T][]: TypeName[][] - for<'lt> - (DynBoxedStatic<'lt, T>) + for<'ctx> + (DynBoxedStatic<T>) where { T: ?Sized + 'static } } -#[derive(Debug)] -pub enum BorrowedStaticValue<'ctx, T: ?Sized + 'static> { - Ctx(&'ctx T), - Static(&'static T), -} +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn owned_static_has_type_name() { + let a = TypeNameId::of_value(&OwnedStatic(42_i32)); + let b = TypeNameId::of_value(&OwnedStatic(123_i32)); + let c = TypeNameId::of_value(&OwnedStatic(true)); + + assert_eq!(a, b); + assert_ne!(a, c); + assert_ne!(b, c); + } + + #[test] + fn borrowed_static_has_type_name() { + let a = TypeNameId::of_value(&BorrowedStatic(&42_i32)); + let b = TypeNameId::of_value(&BorrowedStatic(&123_i32)); + let c = TypeNameId::of_value(&BorrowedStatic(&true)); + + assert_eq!(a, b); + assert_ne!(a, c); + assert_ne!(b, c); + } + + #[test] + fn temp_borrowed_static_has_type_name() { + let a = TypeNameId::of_value(&TempBorrowedStatic(&42_i32)); + let b = TypeNameId::of_value(&TempBorrowedStatic(&123_i32)); + let c = TypeNameId::of_value(&TempBorrowedStatic(&true)); + + assert_eq!(a, b); + assert_ne!(a, c); + assert_ne!(b, c); + } + + #[test] + fn borrowed_mut_static_has_type_name() { + let mut a = 42_i32; + let mut b = 123_i32; + let mut c = true; + + let a = TypeNameId::of_value(&BorrowedMutStatic(&mut a)); + let b = TypeNameId::of_value(&BorrowedMutStatic(&mut b)); + let c = TypeNameId::of_value(&BorrowedMutStatic(&mut c)); -#[derive(Debug)] -pub enum BorrowedMutStaticValue<'ctx, T: ?Sized + 'static> { - Ctx(&'ctx mut T), - Static(&'static mut T), + assert_eq!(a, b); + assert_ne!(a, c); + assert_ne!(b, c); + } + + #[test] + fn temp_borrowed_mut_static_has_type_name() { + let mut a = 42_i32; + let mut b = 123_i32; + let mut c = true; + + let a = TypeNameId::of_value(&TempBorrowedMutStatic(&mut a)); + let b = TypeNameId::of_value(&TempBorrowedMutStatic(&mut b)); + let c = TypeNameId::of_value(&TempBorrowedMutStatic(&mut c)); + + assert_eq!(a, b); + assert_ne!(a, c); + assert_ne!(b, c); + } + + #[test] + #[cfg(feature = "alloc")] + fn boxed_static_has_type_name() { + let a = TypeNameId::of_value(&BoxedStatic(Box::new(42_i32))); + let b = TypeNameId::of_value(&BoxedStatic(Box::new(123_i32))); + let c = TypeNameId::of_value(&BoxedStatic(Box::new(true))); + + assert_eq!(a, b); + assert_ne!(a, c); + assert_ne!(b, c); + } } |