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