abi_stable/proc_macro_reexports/
get_static_equivalent.rs

1/**
2
3The `GetStaticEquivalent` macro derives the [`GetStaticEquivalent_`] trait.
4
5Implementing [`GetStaticEquivalent_`] allows the type to be passed as a 
6type argument of a type deriving `StableAbi`,
7that used the 
8[`#[sabi(not_stableabi(TypeParameter))]`](derive@crate::StableAbi#sabinot_stableabitypeparameter)
9helper attribute.
10
11# Container Attributes
12
13These helper attributes are applied on the type declaration.
14
15<span id = "impl_InterfaceType"></span>
16### `#[sabi(impl_InterfaceType(...))]`
17
18Implements the `InterfaceType` trait for a type,
19defining the usable/required traits when creating a 
20`DynTrait<_, ThisType>`/`NonExhaustive<_, _, ThisType>`.
21
22Syntax: `#[sabi(impl_InterfaceType(Trait0, Trait1, ..., TraitN))]`
23
24If a trait is not specified,
25it will not be required when constructing DynTrait/NonExhaustive,
26and won't be usable afterwards.
27
28<a href = "./derive.StableAbi.html#InterfaceType_traits">
29    The list of valid traits is here 
30</a>
31
32# Examples
33
34###  Using an associated constant 
35
36This example demonstrates how one can have a type parameter,
37and use the value of an associated constant as the identity of the type.
38
39*/
40#[cfg_attr(not(feature = "no_fn_promotion"), doc = "```rust")]
41#[cfg_attr(feature = "no_fn_promotion", doc = "```ignore")]
42/**
43use std::marker::PhantomData;
44
45use abi_stable::{
46    abi_stability::check_layout_compatibility, marker_type::UnsafeIgnoredType, tag,
47    GetStaticEquivalent, StableAbi,
48};
49
50#[repr(C)]
51#[derive(StableAbi)]
52#[sabi(
53    not_stableabi(T),
54    bound(T: WithName),
55    tag = tag!( <T as WithName>::NAME )
56)]
57struct WithMarker<T>(UnsafeIgnoredType<T>);
58
59impl<T> WithMarker<T> {
60    const NEW: Self = WithMarker(UnsafeIgnoredType::NEW);
61}
62
63trait WithName {
64    const NAME: &'static str;
65}
66
67#[derive(GetStaticEquivalent)]
68struct Mark;
69impl WithName for Mark {
70    const NAME: &'static str = "Mark";
71}
72
73#[derive(GetStaticEquivalent)]
74struct John;
75impl WithName for John {
76    const NAME: &'static str = "John";
77}
78
79#[derive(GetStaticEquivalent)]
80struct JessiJames;
81impl WithName for JessiJames {
82    const NAME: &'static str = "JessiJames";
83}
84
85# fn main(){
86
87// This checks that the two types aren't considered compatible.
88assert!(check_layout_compatibility(
89    <WithMarker<Mark> as StableAbi>::LAYOUT,
90    <WithMarker<John> as StableAbi>::LAYOUT,
91)
92.is_err());
93
94// This checks that the two types aren't considered compatible.
95assert!(check_layout_compatibility(
96    <WithMarker<John> as StableAbi>::LAYOUT,
97    <WithMarker<JessiJames> as StableAbi>::LAYOUT,
98)
99.is_err());
100
101// This checks that the two types aren't considered compatible.
102assert!(check_layout_compatibility(
103    <WithMarker<JessiJames> as StableAbi>::LAYOUT,
104    <WithMarker<Mark> as StableAbi>::LAYOUT,
105)
106.is_err());
107
108# }
109
110
111```
112
113###  Using an associated type 
114
115This example demonstrates how one can have a type parameter,
116and use its associated type as a field.
117
118```rust
119use abi_stable::{std_types::RVec, StableAbi};
120
121#[repr(C)]
122#[derive(StableAbi)]
123#[sabi(not_stableabi(I), bound(<I as IntoIterator>::Item : StableAbi))]
124pub struct CollectedIterator<I>
125where
126    I: IntoIterator,
127{
128    vec: RVec<I::Item>,
129}
130
131
132```
133
134
135[`GetStaticEquivalent_`]: abi_stable::abi_stability::get_static_equivalent::GetStaticEquivalent_
136
137*/
138
139#[doc(inline)]
140pub use abi_stable_derive::GetStaticEquivalent;