//! Wrapper types that impl [`TypeName`] when their generic type `T` is `'static`.
use crate::hkt::Marker;
use super::*;
/// Owned static `T`.
#[derive(PartialEq, Clone, Copy, Debug)]
#[repr(transparent)]
pub struct OwnedStatic<T: ?Sized>(pub T);
impl<'a, 'ctx, T> TypeName::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()> for OwnedStatic<T>
where
T: 'static + Send + Sync,
{
type T = OwnedStatic<T>;
}
impl<'a, 'ctx, T> TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()> for OwnedStatic<T>
where
T: 'static + Send + Sync,
{
type Higher = OwnedStatic<T>;
}
/// Borrowed static `T` for `'ctx`.
#[derive(PartialEq, Clone, Copy, Debug)]
#[repr(transparent)]
pub struct BorrowedStatic<'ctx, T: ?Sized>(pub &'ctx T);
pub struct BorrowedStaticHrt<T: ?Sized>(Marker<T>);
impl<'a, 'ctx, T: ?Sized> TypeName::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()> for BorrowedStaticHrt<T>
where
T: 'static + Send + Sync,
{
type T = BorrowedStatic<'ctx, T>;
}
impl<'a, 'ctx, T: ?Sized> TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()>
for BorrowedStatic<'ctx, T>
where
T: 'static + Send + Sync,
{
type Higher = BorrowedStaticHrt<T>;
}
/// Borrowed static `T` for `'a`.
#[derive(PartialEq, Clone, Copy, Debug)]
#[repr(transparent)]
pub struct TempBorrowedStatic<'a, T: ?Sized>(pub &'a T);
pub struct TempBorrowedStaticHrt<T: ?Sized>(Marker<T>);
impl<'a, 'ctx, T: ?Sized> TypeName::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()>
for TempBorrowedStaticHrt<T>
where
T: 'static + Send + Sync,
{
type T = TempBorrowedStatic<'a, T>;
}
impl<'a, 'ctx, T: ?Sized> TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()>
for TempBorrowedStatic<'a, T>
where
T: 'static + Send + Sync,
{
type Higher = TempBorrowedStaticHrt<T>;
}
/// Mutably borrowed static `T` for `'ctx`.
#[derive(PartialEq, Debug)]
#[repr(transparent)]
pub struct BorrowedMutStatic<'ctx, T: ?Sized>(pub &'ctx mut T);
pub struct BorrowedMutStaticHrt<T: ?Sized>(Marker<T>);
impl<'a, 'ctx, T: ?Sized> TypeName::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()>
for BorrowedMutStaticHrt<T>
where
T: 'static + Send + Sync,
{
type T = BorrowedMutStatic<'ctx, T>;
}
impl<'a, 'ctx, T: ?Sized> TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()>
for BorrowedMutStatic<'ctx, T>
where
T: 'static + Send + Sync,
{
type Higher = BorrowedMutStaticHrt<T>;
}
/// Mutably borrowed static `T` for `'a`.
#[derive(PartialEq, Debug)]
#[repr(transparent)]
pub struct TempBorrowedMutStatic<'a, T: ?Sized>(pub &'a mut T);
pub struct TempBorrowedMutStaticHrt<T: ?Sized>(Marker<T>);
impl<'a, 'ctx, T: ?Sized> TypeName::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()>
for TempBorrowedMutStaticHrt<T>
where
T: 'static + Send + Sync,
{
type T = TempBorrowedMutStatic<'a, T>;
}
impl<'a, 'ctx, T: ?Sized> TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()>
for TempBorrowedMutStatic<'a, T>
where
T: 'static + Send + Sync,
{
type Higher = TempBorrowedMutStaticHrt<T>;
}
/// Boxed static `T`.
#[cfg(feature = "alloc")]
#[repr(transparent)]
pub struct BoxedStatic<T: ?Sized>(pub Box<T>);
#[cfg(feature = "alloc")]
impl<'a, 'ctx, T: ?Sized> TypeName::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()> for BoxedStatic<T>
where
T: 'static + Send + Sync,
{
type T = BoxedStatic<T>;
}
#[cfg(feature = "alloc")]
impl<'a, 'ctx, T: ?Sized> TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()> for BoxedStatic<T>
where
T: 'static + Send + Sync,
{
type Higher = BoxedStatic<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));
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);
}
}