//! Wrapper types that impl [`TypeName`] when their generic type `T` is `'static`.
use super::*;
/// Owned static `T`.
#[derive(PartialEq, Clone, Copy, Debug)]
#[repr(transparent)]
pub struct OwnedStatic<T: ?Sized>(pub T);
bijective_higher_ranked_type! {
/// Higher ranked type for [`OwnedStatic`].
pub for['ctx] type DynOwnedStatic[][T]: WithContextLt['ctx][]
for<'a>
(OwnedStatic<T>)
where {
T: ?Sized + 'ctx,
}
}
bijective_higher_ranked_type! {
pub type [][T][]: TypeName[][]
for<'ctx>
(DynOwnedStatic<T>)
where {
T: ?Sized + 'static
}
}
/// Borrowed static `T` for `'ctx`.
#[repr(transparent)]
pub struct BorrowedStatic<'ctx, T: ?Sized>(pub &'ctx T);
bijective_higher_ranked_type! {
/// Higher ranked type for [`BorrowedStatic`].
pub type DynBorrowedStatic['ctx][T]: WithContextLt['ctx][]
for<'a>
(BorrowedStatic<'ctx, T>)
where {
T: ?Sized + 'ctx,
}
}
bijective_higher_ranked_type! {
pub type [][T][]: TypeName[][]
for<'ctx>
(DynBorrowedStatic<'ctx, T>)
where {
T: ?Sized + 'static
}
}
/// Borrowed static `T` for `'a`.
#[repr(transparent)]
pub struct TempBorrowedStatic<'a, T: ?Sized>(pub &'a T);
bijective_higher_ranked_type! {
/// Higher ranked type for [`TempBorrowedStatic`].
pub type DynTempBorrowedStatic['ctx][T]: WithContextLt['ctx][]
for<'a>
(TempBorrowedStatic<'a, T>)
where {
T: ?Sized + 'ctx,
}
}
bijective_higher_ranked_type! {
pub type [][T][]: TypeName[][]
for<'ctx>
(DynTempBorrowedStatic<'ctx, T>)
where {
T: ?Sized + 'static
}
}
/// Mutably borrowed static `T` for `'ctx`.
#[repr(transparent)]
pub struct BorrowedMutStatic<'ctx, T: ?Sized>(pub &'ctx mut T);
bijective_higher_ranked_type! {
/// Higher ranked type for [`BorrowedMutStatic`].
pub type DynBorrowedMutStatic['ctx][T]: WithContextLt['ctx][]
for<'a>
(BorrowedMutStatic<'ctx, T>)
where {
T: ?Sized + 'ctx,
}
}
bijective_higher_ranked_type! {
pub type [][T][]: TypeName[][]
for<'ctx>
(DynBorrowedMutStatic<'ctx, T>)
where {
T: ?Sized + 'static
}
}
/// Mutably borrowed static `T` for `'a`.
#[repr(transparent)]
pub struct TempBorrowedMutStatic<'a, T: ?Sized>(pub &'a mut T);
bijective_higher_ranked_type! {
/// Higher ranked type for [`TempBorrowedMutStatic`].
pub type DynTempBorrowedMutStatic['ctx][T]: WithContextLt['ctx][]
for<'a>
(TempBorrowedMutStatic<'a, T>)
where {
T: ?Sized + 'ctx,
}
}
bijective_higher_ranked_type! {
pub type [][T][]: TypeName[][]
for<'ctx>
(DynTempBorrowedMutStatic<'ctx, T>)
where {
T: ?Sized + 'static
}
}
/// Boxed static `T`.
#[cfg(feature = "alloc")]
#[repr(transparent)]
pub struct BoxedStatic<T: ?Sized>(pub Box<T>);
#[cfg(feature = "alloc")]
bijective_higher_ranked_type! {
/// Higher ranked type for [`BoxedStatic`].
pub for['ctx] type DynBoxedStatic[][T]: WithContextLt['ctx][]
for<'a>
(BoxedStatic<T>)
where {
T: ?Sized + 'ctx,
}
}
#[cfg(feature = "alloc")]
bijective_higher_ranked_type! {
pub type [][T][]: TypeName[][]
for<'ctx>
(DynBoxedStatic<T>)
where {
T: ?Sized + 'static
}
}
#[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);
}
}