1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//! Zero-sized types .

use std::{cell::Cell, marker::PhantomData, rc::Rc};

use crate::{
    abi_stability::PrefixStableAbi,
    derive_macro_reexports::*,
    type_layout::{GenericTLData, MonoTLData, MonoTypeLayout, ReprAttr, TypeLayout},
};

#[macro_use]
mod stable_abi_impls;

/////////////////

/// Marker type used to mark a type as being `Send + Sync`.
#[repr(C)]
#[derive(StableAbi)]
pub struct SyncSend;

const _: () = zst_assert! {SyncSend};

/////////////////

/// Marker type used to mark a type as being `!Send + !Sync`.
pub struct UnsyncUnsend {
    _marker: UnsafeIgnoredType<Rc<()>>,
}

monomorphic_marker_type! {UnsyncUnsend, UnsafeIgnoredType<Rc<()>>}

impl UnsyncUnsend {
    /// Constructs a `UnsyncUnsend`
    pub const NEW: Self = Self {
        _marker: UnsafeIgnoredType::NEW,
    };
}

/////////////////

/// Marker type used to mark a type as being `Send + !Sync`.
pub struct UnsyncSend {
    _marker: UnsafeIgnoredType<Cell<()>>,
}

monomorphic_marker_type! {UnsyncSend, UnsafeIgnoredType<Cell<()>>}

impl UnsyncSend {
    /// Constructs a `UnsyncSend`
    pub const NEW: Self = Self {
        _marker: UnsafeIgnoredType::NEW,
    };
}

/////////////////

/// Marker type used to mark a type as being `!Send + Sync`.
// #[sabi(debug_print)]
pub struct SyncUnsend {
    _marker: UnsyncUnsend,
}

monomorphic_marker_type! {SyncUnsend, UnsyncUnsend}

impl SyncUnsend {
    /// Constructs a `SyncUnsend`
    pub const NEW: Self = Self {
        _marker: UnsyncUnsend::NEW,
    };
}

unsafe impl Sync for SyncUnsend {}

/////////////////

/// Zero-sized marker type used to signal that even though a type
/// could implement `Copy` and `Clone`,
/// it is semantically an error to do so.
#[repr(C)]
#[derive(StableAbi)]
// #[sabi(debug_print)]
pub struct NotCopyNotClone;

const _: () = zst_assert! {NotCopyNotClone};

//////////////////////////////////////////////////////////////

/// Used by vtables/pointers to signal that the type has been erased.
///
#[repr(C)]
#[derive(StableAbi)]
pub struct ErasedObject<T = ()> {
    _marker: NonOwningPhantom<T>,
}

const _: () = zst_assert! {ErasedObject};

//////////////////////////////////////////////////////////////

/// Used by pointers to vtables/modules to signal that the type has been erased.
///
pub struct ErasedPrefix {
    _priv: PhantomData<u8>,
}

const _: () = zst_assert!(ErasedPrefix);

unsafe impl GetStaticEquivalent_ for ErasedPrefix {
    type StaticEquivalent = ErasedPrefix;
}

unsafe impl PrefixStableAbi for ErasedPrefix {
    type IsNonZeroType = False;
    const LAYOUT: &'static TypeLayout = <ErasedObject as StableAbi>::LAYOUT;
}

//////////////////////////////////////////////////////////////

/// MarkerType which ignores its type parameter in its [`StableAbi`] implementation.
///
/// # Safety
///
/// `Unsafe` is part of its name,
/// because users can violate memory safety
/// if they depend on the value of the type parameter passed to `UnsafeIgnoredType` for safety,
/// since the type parameter is ignored when type checking dynamic libraries.
///
///
/// [`StableAbi`]: ../trait.StableAbi.html
///
pub struct UnsafeIgnoredType<T: ?Sized> {
    /// This field must be public to promise (for semver) that a repr change would be a breaking
    /// change (see <https://github.com/rust-lang/rust/issues/78586>), which is important as this is
    /// used as a zero-sized type in `repr(transparent)` structs.
    pub _inner: PhantomData<T>,
}

impl<T: ?Sized> UnsafeIgnoredType<T> {
    /// Constructs an `UnsafeIgnoredType`.
    pub const DEFAULT: Self = Self {
        _inner: PhantomData,
    };

    /// Constructs an `UnsafeIgnoredType`.
    pub const NEW: Self = Self {
        _inner: PhantomData,
    };
}

impl<T: ?Sized> Copy for UnsafeIgnoredType<T> {}

impl<T: ?Sized> Default for UnsafeIgnoredType<T> {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl<T: ?Sized> Clone for UnsafeIgnoredType<T> {
    fn clone(&self) -> Self {
        *self
    }
}

unsafe impl<T> GetStaticEquivalent_ for UnsafeIgnoredType<T> {
    type StaticEquivalent = ();
}
unsafe impl<T> StableAbi for UnsafeIgnoredType<T> {
    type IsNonZeroType = False;

    const LAYOUT: &'static TypeLayout = {
        const MONO_TYPE_LAYOUT: &MonoTypeLayout = &MonoTypeLayout::new(
            *mono_shared_vars,
            rstr!("UnsafeIgnoredType"),
            make_item_info!(),
            MonoTLData::struct_(rslice![]),
            tl_genparams!(;;),
            ReprAttr::C,
            ModReflMode::Module,
            rslice![],
        );

        make_shared_vars! {
            impl[T] UnsafeIgnoredType<T>;

            let (mono_shared_vars,shared_vars)={};
        }

        zst_assert!(Self);

        &TypeLayout::from_std::<Self>(
            shared_vars,
            MONO_TYPE_LAYOUT,
            Self::ABI_CONSTS,
            GenericTLData::Struct,
        )
    };
}

//////////////////////////////////////////////////////////////

/// An ffi-safe equivalent of a `PhantomData<fn()->T>`
pub struct NonOwningPhantom<T: ?Sized> {
    // The StableAbi layout for a `NonOwningPhantom<T>` is the same as `PhantomData<T>`,
    // the type of this field is purely for variance.
    _marker: PhantomData<extern "C" fn() -> T>,
}

impl<T: ?Sized> NonOwningPhantom<T> {
    /// Constructs a `NonOwningPhantom`
    pub const DEFAULT: Self = Self {
        _marker: PhantomData,
    };

    /// Constructs a `NonOwningPhantom`
    pub const NEW: Self = Self {
        _marker: PhantomData,
    };
}

impl<T: ?Sized> Copy for NonOwningPhantom<T> {}

impl<T: ?Sized> Default for NonOwningPhantom<T> {
    #[inline(always)]
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl<T: ?Sized> Clone for NonOwningPhantom<T> {
    #[inline(always)]
    fn clone(&self) -> Self {
        *self
    }
}

unsafe impl<T: ?Sized> GetStaticEquivalent_ for NonOwningPhantom<T>
where
    PhantomData<T>: GetStaticEquivalent_,
{
    type StaticEquivalent = GetStaticEquivalent<PhantomData<T>>;
}

unsafe impl<T: ?Sized> StableAbi for NonOwningPhantom<T>
where
    PhantomData<T>: StableAbi,
{
    type IsNonZeroType = False;

    const LAYOUT: &'static TypeLayout = {
        zst_assert!(Self);
        <PhantomData<T> as StableAbi>::LAYOUT
    };
}