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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//! 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.
///
pub struct ErasedObject<T = ()> {
    _marker: NonOwningPhantom<T>,
}

// Delete this once the abi_stable version number isn't 0.9
//
const _ITEM_INFO_CONST_ERASEDOBJECT: abi_stable::type_layout::ItemInfo =
    abi_stable::make_item_info!();
const _SHARED_VARS_STRINGS_ERASEDOBJECT: ::abi_stable::std_types::RStr<'static> =
    abi_stable::std_types::RStr::from_str("_priv;_marker;");
#[allow(non_upper_case_globals)]
mod _sabi_erasedobject {
    use super::*;
    pub(super) use :: abi_stable;
    #[allow(unused_imports)]
    pub(super) use ::abi_stable::derive_macro_reexports::{self as __sabi_re, renamed::*};
    pub struct _static_ErasedObject<T: ?Sized>(extern "C" fn(&T));
    unsafe impl<T> __GetStaticEquivalent_ for ErasedObject<T>
    where
        T: __StableAbi,
    {
        type StaticEquivalent = _static_ErasedObject<__GetStaticEquivalent<T>>;
    }
    #[doc(hidden)]
    pub(super) const _MONO_LAYOUT_ErasedObject: &__sabi_re::MonoTypeLayout =
        &__sabi_re::MonoTypeLayout::from_derive(__sabi_re::_private_MonoTypeLayoutDerive {
            name: abi_stable::std_types::RStr::from_str("ErasedObject"),
            item_info: _ITEM_INFO_CONST_ERASEDOBJECT,
            data: __sabi_re::MonoTLData::derive_struct(__CompTLFields::new(
                abi_stable::std_types::RSlice::from_slice(&[
                    562949953748992u64,
                    1688849860722694u64,
                ]),
                None,
            )),
            generics: abi_stable ::tl_genparams !(
                ;
                __StartLen :: new(0u16, 0u16) ;
                __StartLen :: new(0u16, 0u16)
            ),
            mod_refl_mode: __ModReflMode::Opaque,
            repr_attr: __ReprAttr::C,
            phantom_fields: abi_stable::std_types::RSlice::from_slice(&[]),
            shared_vars: abi_stable::type_layout::MonoSharedVars::new(
                _SHARED_VARS_STRINGS_ERASEDOBJECT,
                abi_stable::std_types::RSlice::from_slice(&[]),
            ),
        });
    impl<T> ErasedObject<T>
    where
        T: __StableAbi,
    {
        const __SABI_CONST_PARAMS_A: &'static [&'static __sabi_re::ConstGenericErasureHack<
            dyn ::std::marker::Send,
        >] = &[];
        const __SABI_CONST_PARAMS_B: &'static [__ConstGeneric] = &[];
        const __SABI_SHARED_VARS: &'static __sabi_re::SharedVars =
            &abi_stable::type_layout::SharedVars::new(
                _MONO_LAYOUT_ErasedObject.shared_vars_static(),
                abi_stable::_sabi_type_layouts!([u8; 0], NonOwningPhantom<T>,),
                __sabi_re::RSlice::from_slice(Self::__SABI_CONST_PARAMS_B),
            );
    }
    unsafe impl<T> __sabi_re::StableAbi for ErasedObject<T>
    where
        T: __StableAbi,
    {
        type IsNonZeroType = __sabi_re::False;
        const LAYOUT: &'static __sabi_re::TypeLayout = {
            zst_assert! {Self}

            &__sabi_re::TypeLayout::from_derive::<Self>(__sabi_re::_private_TypeLayoutDerive {
                shared_vars: Self::__SABI_SHARED_VARS,
                mono: _MONO_LAYOUT_ErasedObject,
                abi_consts: Self::ABI_CONSTS,
                data: __sabi_re::GenericTLData::Struct,
                tag: None,
                extra_checks: None,
            })
        };
    }
}

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

/// 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> {
    _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
    };
}