abi_stable/abi_stability/
get_static_equivalent.rs

1//! Contains the `GetStaticEquivalent_` trait and related items.
2
3/// A type that stands in for `Self`,used to create a `UTypeId` for doing layout checking.
4///
5/// This may or may not have the same TypeId as Self.
6///
7/// # Safety
8///
9/// The `StaticEquivalent` associated type must be either of:
10/// - the same type as `Self`, ignoring lifetime arguments.
11/// - a type declared specifically to be the `StaticEquivalent`
12/// associated type of `Self`(and no other type),
13/// with the same type and const arguments as `Self`.
14///
15/// In either case, non-`'static` type parameters can be replaced with their
16/// `GetStaticEquivalent_::StaticEquivalent` associated type.
17///
18pub unsafe trait GetStaticEquivalent_ {
19    /// The `'static` equivalent of `Self`
20    type StaticEquivalent: 'static;
21}
22
23/// Gets the `'static` equivalent of a type,only for use in creating a `UTypeId`.
24pub type GetStaticEquivalent<T> = <T as GetStaticEquivalent_>::StaticEquivalent;
25
26/// Used to avoid a `?Sized` bound on `GetStaticEquivalent_::StaticEquivalent`.
27///
28/// It's fine to use this instead of `str` and `[T]` since the type is
29/// only required to be unique.
30pub struct Unsized<T: ?Sized>(fn(&T));
31
32////////////////////////////////////////////////////////////////////////////////
33//                      Impls for non-StableAbi types
34////////////////////////////////////////////////////////////////////////////////
35
36unsafe impl GetStaticEquivalent_ for str {
37    type StaticEquivalent = Unsized<str>;
38}
39
40unsafe impl<T> GetStaticEquivalent_ for [T]
41where
42    T: GetStaticEquivalent_,
43{
44    type StaticEquivalent = Unsized<[T::StaticEquivalent]>;
45}
46
47unsafe impl<T: ?Sized> GetStaticEquivalent_ for Unsized<T>
48where
49    T: GetStaticEquivalent_,
50{
51    type StaticEquivalent = Unsized<T::StaticEquivalent>;
52}