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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
/*!
Where the StableAbi trait is declared,as well as related types/traits.
*/

use core_extensions::type_level_bool::{Boolean, False, True};
use std::{
    cell::{Cell,UnsafeCell},
    cmp::{Eq,PartialEq},
    fmt,
    marker::{PhantomData,PhantomPinned},
    mem::ManuallyDrop,
    num::{NonZeroU8,NonZeroU16,NonZeroU32,NonZeroU64,NonZeroUsize,Wrapping},
    pin::Pin,
    ptr::NonNull,
    sync::atomic::{AtomicBool, AtomicIsize, AtomicPtr, AtomicUsize},
};

use crate::{
    abi_stability::get_static_equivalent::GetStaticEquivalent_,
    std_types::{RNone, RSome, utypeid::UTypeId},
    sabi_types::ReturnValueEquality,
    reflection::ModReflMode,
    type_layout::{
        LifetimeIndex, TLData, TLField, TypeLayout, TypeLayoutParams,
        ItemInfo,ReprAttr,TLPrimitive,TLEnum,TLDiscriminants,IsExhaustive,
    },
};


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

/**
Represents a type whose layout is stable.

This trait can indirectly be derived using `#[derive(StableAbi)]`
(There is a blanket impl of StableAbi for `SharedStableAbi<Kind=ValueKind>`,
    which `#[derive(StableAbi)]` implements.
).

There is a blanket impl of this trait for all `SharedStableAbi<Kind=ValueKind>` types.
*/
pub unsafe trait StableAbi:SharedStableAbi<Kind=ValueKind> {
    /// The layout of the type provided by implementors.
    const LAYOUT: &'static TypeLayout;

    /// The layout of the type,derived from Self::LAYOUT and associated types.
    const ABI_INFO: &'static AbiInfoWrapper;
}


/**
Represents a type whose layout is stable.

This trait can be derived using ``.

# Safety

The layout of types implementing this trait can only change by 
adding fields at the end,if it stores a `TLData::PrefixType` in `TypeLayout.data`,

# Caveats

This trait cannot be directly implemented for functions that take lifetime parameters,
because of that,`#[derive(StableAbi)]` detects the presence of `extern fn` types 
in type definitions.

*/
pub unsafe trait SharedStableAbi:GetStaticEquivalent_ {
    /**
Whether this type has a single invalid bit-pattern.

Possible values:True/False

Some standard library types have a single value that is invalid for them eg:0,null.
these types are the only ones which can be stored in a `Option<_>` that implements AbiStable.

An alternative for types where `IsNonZeroType=False`,you can use `abi_stable::ROption`.

Non-exhaustive list of std types that are NonZero:

- &T (any T).

- &mut T (any T).

- extern fn().

- std::ptr::NonNull

- std::num::NonZero* 

    */
    type IsNonZeroType: Boolean;

    /**
The kind of abi stability of this type,there are 2:

- ValueKind:The layout of this type does not change in minor versions.

- PrefixKind:
    A struct which can add fields in minor versions,
    only usable behind a shared reference,
    used to implement extensible vtables and modules.

    */
    type Kind:TypeKindTrait;

    /// The layout of the type provided by implementors.
    const S_LAYOUT: &'static TypeLayout;

    /// The layout of the type,derived from Self::LAYOUT and associated types.
    const S_ABI_INFO: &'static AbiInfoWrapper = {
        let info = AbiInfo {
            kind:<Self::Kind as TypeKindTrait>::VALUE,
            prefix_kind: <Self::Kind as TypeKindTrait>::IS_PREFIX,
            type_id:make_rve_utypeid!(Self::StaticEquivalent),
            is_nonzero: <Self::IsNonZeroType as Boolean>::VALUE,
            layout: Self::S_LAYOUT,
        };

        &AbiInfoWrapper::new(info)
    };
}


unsafe impl<This> StableAbi for This
where 
    This:SharedStableAbi<Kind=ValueKind>,
{
    const LAYOUT: &'static TypeLayout=<This as SharedStableAbi>::S_LAYOUT;
    const ABI_INFO: &'static AbiInfoWrapper=<This as SharedStableAbi>::S_ABI_INFO;
}


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

/// Wraps a correctly constructed AbiInfo.
#[derive(Debug, Copy, Clone, PartialEq)]
#[repr(C)]
#[derive(StableAbi)]
pub struct AbiInfoWrapper {
    inner: AbiInfo,
    _priv: (),
}

impl AbiInfoWrapper {
    const fn new(inner: AbiInfo) -> Self {
        Self { inner, _priv: () }
    }
    /// Unsafely constructs AbiInfoWrapper from any AbiInfo.
    ///
    /// # Safety
    ///
    /// Callers must ensure that the layout is that of the datatype this AbiInfo represents,
    /// and that it stays consistent with it across time.
    pub const unsafe fn new_unchecked(inner: AbiInfo) -> Self {
        Self::new(inner)
    }
    /// Gets the wrapped AbiInfo.
    pub const fn get(&self) -> &AbiInfo {
        &self.inner
    }
}

/// Describes the abi of some type.
///
/// # Safety
///
/// You must ensure that it describes the actual abi of the type.
#[derive(Debug, Copy, Clone, PartialEq)]
#[repr(C)]
#[derive(StableAbi)]
pub struct AbiInfo {
    pub kind:TypeKind,
    /// Whether this is a prefix,
    pub prefix_kind: bool,
    /// Equivalent to the UTypeId returned by the function in ReturnValueEquality.
    pub type_id:ReturnValueEquality<UTypeId>,
    /// Whether the type uses non-zero value optimization,
    /// if true then an Option<Self> implements StableAbi.
    pub is_nonzero: bool,
    /// The layout of the type.
    pub layout: &'static TypeLayout,
}


impl AbiInfo{
    /// Gets the UTypeId for the `'static` equivalent of the type that created this AbiInfo.
    pub fn get_utypeid(&self)->UTypeId{
        (self.type_id.function)()
    }
}

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

/// The abi_stable kind of a type.
#[repr(u8)]
#[derive(Debug, Copy, Clone, PartialEq,Eq,Ord,PartialOrd,Hash,StableAbi)]
pub enum TypeKind{
    /// A value whose layout must not change in minor versions
    Value,
    /// A struct whose fields can be extended in minor versions,
    /// but only behind a shared reference,
    /// used to implement vtables and modules.
    Prefix,
}


/// For marker types that represent variants of TypeKind.
pub trait TypeKindTrait:sealed::Sealed{
    /// The equivalent TypeKind of this type.
    const VALUE:TypeKind;
    /// Whether this is a prefix-kind
    const IS_PREFIX:bool;
}

/// The kind of a regular value,the default kind.
pub struct ValueKind;

/// The kind of a prefix-type,vtables and modules.
pub struct PrefixKind;


mod sealed{
    pub trait Sealed{}
}

impl sealed::Sealed for ValueKind{}
impl sealed::Sealed for PrefixKind{}

impl TypeKindTrait for ValueKind {
    const VALUE:TypeKind=TypeKind::Value;
    const IS_PREFIX:bool=false;
}

impl TypeKindTrait for PrefixKind {
    const VALUE:TypeKind=TypeKind::Prefix;
    const IS_PREFIX:bool=true;
}

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

/// Gets for the AbiInfo of some type,wraps an `extern "C" fn() -> &'static AbiInfo`.
#[derive(Copy, Clone)]
#[repr(transparent)]
#[derive(StableAbi)]
// #[sabi(debug_print)]
pub struct GetAbiInfo {
    abi_info: extern "C" fn() -> &'static AbiInfo,
}

impl fmt::Debug for GetAbiInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self.get(), f)
    }
}

impl GetAbiInfo {
    /// Gets the `&'static AbiInfo` of some type.
    pub fn get(self) -> &'static AbiInfo {
        (self.abi_info)()
    }
}

impl Eq for GetAbiInfo{}

impl PartialEq for GetAbiInfo{
    fn eq(&self,other:&Self)->bool{
        self.get()==other.get()
    }
}


/// Constructs the GetAbiInfo for Self.
///
/// # Safety
///
/// Implementors must make sure that the AbiInfo actually describes the layout of the type.
pub unsafe trait MakeGetAbiInfo<B> {
    const CONST: GetAbiInfo;
}

unsafe impl<T> MakeGetAbiInfo<StableAbi_Bound> for T
where
    T: StableAbi,
{
    const CONST: GetAbiInfo = GetAbiInfo {
        abi_info: get_abi_info::<T>,
    };
}

unsafe impl<T> MakeGetAbiInfo<SharedStableAbi_Bound> for T
where
    T: SharedStableAbi,
{
    const CONST: GetAbiInfo = GetAbiInfo {
        abi_info: get_ssa_abi_info::<T>,
    };
}

unsafe impl<T> MakeGetAbiInfo<UnsafeOpaqueField_Bound> for T {
    const CONST: GetAbiInfo = GetAbiInfo {
        abi_info: get_abi_info::<UnsafeOpaqueField<T>>,
    };
}


#[doc(hidden)]
pub struct MakeGetAbiInfoSA<T>(T);

impl<T> MakeGetAbiInfoSA<T>
where T: StableAbi,
{
    pub const STABLE_ABI:GetAbiInfo=GetAbiInfo {
        abi_info: get_abi_info::<T>,
    };
}


#[doc(hidden)]
pub struct MakeGetAbiInfoUF<T>(T);

impl<T> MakeGetAbiInfoUF<T>{
    pub const OPAQUE_FIELD:GetAbiInfo=GetAbiInfo {
        abi_info: get_abi_info::<UnsafeOpaqueField<T>>,
    };
}


/// Determines that MakeGetAbiInfo constructs the AbiInfo for a 
/// type that implements StableAbi.
#[allow(non_camel_case_types)]
pub struct StableAbi_Bound;

/// Determines that MakeGetAbiInfo constructs the AbiInfo for a 
/// type that implements SharedStableAbi.
#[allow(non_camel_case_types)]
pub struct SharedStableAbi_Bound;

/// Determines that MakeGetAbiInfo constructs the AbiInfo for any type (this is unsafe).
#[allow(non_camel_case_types)]
pub struct UnsafeOpaqueField_Bound;

/// Retrieves the AbiInfo of `T:StableAbi`,
pub extern "C" fn get_abi_info<T>() -> &'static AbiInfo
where
    T: StableAbi,
{
    T::ABI_INFO.get()
}

/// Retrieves the AbiInfo of `T:SharedStableAbi`,
pub extern "C" fn get_ssa_abi_info<T>() -> &'static AbiInfo
where
    T: SharedStableAbi,
{
    T::S_ABI_INFO.get()
}


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

/////////////////////////////////////////////////////////////////////////////
////                Implementations
/////////////////////////////////////////////////////////////////////////////

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

unsafe impl<T> GetStaticEquivalent_ for PhantomData<T> 
where T:GetStaticEquivalent_
{
    type StaticEquivalent=PhantomData<T::StaticEquivalent>;
}

unsafe impl<T> SharedStableAbi for PhantomData<T> 
where T:StableAbi
{
    type Kind=ValueKind;
    type IsNonZeroType = False;

    const S_LAYOUT: &'static TypeLayout = &TypeLayout::from_std_full::<Self>(
        "PhantomData",
        RNone,
        ItemInfo::std_type_in("std::marker"),
        TLData::EMPTY,
        ReprAttr::c(),
        tl_genparams!(;;),
        &[TLField::new("0",&[],<T as MakeGetAbiInfo<StableAbi_Bound>>::CONST,)],
    );
}


unsafe impl GetStaticEquivalent_ for () {
    type StaticEquivalent=();
}
unsafe impl SharedStableAbi for () {
    type Kind=ValueKind;
    type IsNonZeroType = False;

    const S_LAYOUT: &'static TypeLayout =
        &TypeLayout::from_std::<Self>(
            "()", 
            TLData::EMPTY,
            ReprAttr::c(),
            ItemInfo::primitive(), 
            tl_genparams!(;;)
        );
}


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


unsafe impl<'a, T> GetStaticEquivalent_ for &'a T
where
    T: 'a + GetStaticEquivalent_,
{
    type StaticEquivalent=&'static T::StaticEquivalent;
}

// Does not allow ?Sized types because the DST fat pointer does not have a stable layout.
unsafe impl<'a, T> SharedStableAbi for &'a T
where
    T: 'a + SharedStableAbi,
{
    type Kind=ValueKind;
    type IsNonZeroType = True;

    const S_LAYOUT: &'static TypeLayout = &TypeLayout::from_std_full::<Self>(
        "&",
        RSome(TLPrimitive::SharedRef),
        ItemInfo::primitive(),
        TLData::Primitive(TLPrimitive::SharedRef),
        ReprAttr::Primitive,
        tl_genparams!('a;T;),
        &[TLField::new(
            "0",
            &[LifetimeIndex::Param(0)],
            <T as MakeGetAbiInfo<SharedStableAbi_Bound>>::CONST,
        )],
    ).set_mod_refl_mode(ModReflMode::DelegateDeref{phantom_field_index:0});
}


unsafe impl<'a, T> GetStaticEquivalent_ for &'a mut T
where
    T: 'a + GetStaticEquivalent_,
{
    type StaticEquivalent=&'static mut T::StaticEquivalent;
}

// Does not allow ?Sized types because the DST fat pointer does not have a stable layout.
unsafe impl<'a, T> SharedStableAbi for &'a mut T
where
    T: 'a + StableAbi,
{
    type Kind=ValueKind;
    type IsNonZeroType = True;

    const S_LAYOUT: &'static TypeLayout = &TypeLayout::from_std_full::<Self>(
        "&mut",
        RSome(TLPrimitive::MutRef),
        ItemInfo::primitive(),
        TLData::Primitive(TLPrimitive::MutRef),
        ReprAttr::Primitive,
        tl_genparams!('a;T;),
        &[TLField::new(
            "0",
            &[LifetimeIndex::Param(0)],
            <T as MakeGetAbiInfo<StableAbi_Bound>>::CONST,
        )],
    );
}


unsafe impl<T> GetStaticEquivalent_ for NonNull<T>
where
    T: GetStaticEquivalent_,
{
    type StaticEquivalent=NonNull<T::StaticEquivalent>;
}

// Does not allow ?Sized types because the DST fat pointer does not have a stable layout.
unsafe impl<T> SharedStableAbi for NonNull<T>
where
    T: StableAbi,
{
    type Kind=ValueKind;
    type IsNonZeroType = True;

    const S_LAYOUT: &'static TypeLayout = &TypeLayout::from_std_full::<Self>(
        "NonNull",
        RNone,
        ItemInfo::std_type_in("std::ptr"),
        TLData::struct_(&[
            TLField::new(
                "0",
                &[],
                <*mut T as MakeGetAbiInfo<StableAbi_Bound>>::CONST,
            )
        ]),
        ReprAttr::Transparent,
        tl_genparams!(;T;),
        &[],
    );
}


unsafe impl<T> GetStaticEquivalent_ for AtomicPtr<T>
where
    T: GetStaticEquivalent_,
{
    type StaticEquivalent=AtomicPtr<T::StaticEquivalent>;
}

unsafe impl<T> SharedStableAbi for AtomicPtr<T>
where
    T: StableAbi,
{
    type Kind=ValueKind;
    type IsNonZeroType = False;

    const S_LAYOUT: &'static TypeLayout = &TypeLayout::from_std_full::<Self>(
        "AtomicPtr",
        RNone,
        ItemInfo::std_type_in("std::sync::atomic"),
        TLData::struct_(&[
            TLField::new(
                "0",
                &[],
                <*mut T as MakeGetAbiInfo<StableAbi_Bound>>::CONST,
            )
        ]),
        ReprAttr::Transparent,
        tl_genparams!(;T;),
        &[],
    );
}

unsafe impl<T> GetStaticEquivalent_ for *const T
where
    T: GetStaticEquivalent_,
{
    type StaticEquivalent=*const T::StaticEquivalent;
}
// Does not allow ?Sized types because the DST fat pointer does not have a stable layout.
unsafe impl<T> SharedStableAbi for *const T
where
    T: SharedStableAbi,
{
    type Kind=ValueKind;
    type IsNonZeroType = False;

    const S_LAYOUT: &'static TypeLayout = &TypeLayout::from_std_full::<Self>(
        "*const",
        RSome(TLPrimitive::ConstPtr),
        ItemInfo::primitive(),
        TLData::Primitive(TLPrimitive::ConstPtr),
        ReprAttr::Primitive,
        tl_genparams!(;T;),
        &[TLField::new(
            "0",
            &[],
            <T as MakeGetAbiInfo<SharedStableAbi_Bound>>::CONST,
        )],
    );
}


unsafe impl<T> GetStaticEquivalent_ for *mut T
where
    T: GetStaticEquivalent_,
{
    type StaticEquivalent=*mut T::StaticEquivalent;
}
// Does not allow ?Sized types because the DST fat pointer does not have a stable layout.
unsafe impl<T> SharedStableAbi for *mut T
where
    T: StableAbi,
{
    type Kind=ValueKind;
    type IsNonZeroType = False;

    const S_LAYOUT: &'static TypeLayout = &TypeLayout::from_std_full::<Self>(
        "*mut",
        RSome(TLPrimitive::MutPtr),
        ItemInfo::primitive(),
        TLData::Primitive(TLPrimitive::MutPtr),
        ReprAttr::Primitive,
        tl_genparams!(;T;),
        &[TLField::new(
            "0",
            &[],
            <T as MakeGetAbiInfo<StableAbi_Bound>>::CONST,
        )],
    );
}

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

macro_rules! impl_stable_abi_array {
    ($($size:expr),*)=>{
        $(
            unsafe impl<T> GetStaticEquivalent_ for [T;$size]
            where T:GetStaticEquivalent_
            {
                type StaticEquivalent=[T::StaticEquivalent;$size];
            }

            unsafe impl<T> SharedStableAbi for [T;$size]
            where T:StableAbi
            {
                type Kind=ValueKind;
                type IsNonZeroType=False;

                const S_LAYOUT:&'static TypeLayout=&TypeLayout::from_std_full::<Self>(
                    "array",
                    RSome(TLPrimitive::Array{len:$size}),
                    ItemInfo::primitive(),
                    TLData::Primitive(TLPrimitive::Array{len:$size}),
                    ReprAttr::Primitive,
                    tl_genparams!(;T;$size),
                    &[
                        TLField::new(
                            "element", 
                            &[],
                            <T as MakeGetAbiInfo<SharedStableAbi_Bound>>::CONST
                        )
                    ],
                );
            }
        )*
    }
}

impl_stable_abi_array! {
    00,01,02,03,04,05,06,07,08,09,
    10,11,12,13,14,15,16,17,18,19,
    20,21,22,23,24,25,26,27,28,29,
    30,31,32
}

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

unsafe impl<T> GetStaticEquivalent_ for Option<T>
where
    T: GetStaticEquivalent_,
{
    type StaticEquivalent=Option<T::StaticEquivalent>;
}
/// Implementing abi stability for Option<T> is fine if
/// T is a NonZero primitive type.
unsafe impl<T> SharedStableAbi for Option<T>
where
    T: StableAbi<IsNonZeroType = True>,
{
    type Kind=ValueKind;
    type IsNonZeroType = False;

    const S_LAYOUT: &'static TypeLayout = &TypeLayout::from_std_full::<Self>(
        "Option",
        RNone,
        ItemInfo::primitive(),
        TLData::Enum(&TLEnum::new(
            "Some;None;",
            IsExhaustive::exhaustive(),
            &[
                TLField::new(
                    "0",
                    &[],
                    <T as MakeGetAbiInfo<StableAbi_Bound>>::CONST,
                )
            ],
            TLDiscriminants::from_u8_slice(&[0,1]),
            &[1,0]
        )),
        ReprAttr::OptionNonZero,
        tl_genparams!(;T;),
        &[],
    );
}

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


macro_rules! impl_for_primitive_ints {
    (
        $( ($zeroable:ty,$tl_primitive:expr) ,)*
    ) => (
        $(
            unsafe impl GetStaticEquivalent_ for $zeroable {
                type StaticEquivalent=Self;
            }
            unsafe impl SharedStableAbi for $zeroable {
                type Kind=ValueKind;
                type IsNonZeroType=False;

                const S_LAYOUT:&'static TypeLayout=&TypeLayout::from_std_full::<Self>(
                    stringify!($zeroable),
                    RSome($tl_primitive),
                    ItemInfo::primitive(),
                    TLData::Primitive($tl_primitive),
                    ReprAttr::Primitive,
                    tl_genparams!(;;),
                    &[],
                );
            }
        )*
    )
}

impl_for_primitive_ints!{
    (u8   ,TLPrimitive::U8),
    (i8   ,TLPrimitive::I8),
    (u16  ,TLPrimitive::U16),
    (i16  ,TLPrimitive::I16),
    (u32  ,TLPrimitive::U32),
    (i32  ,TLPrimitive::I32),
    (u64  ,TLPrimitive::U64),
    (i64  ,TLPrimitive::I64),
    (usize,TLPrimitive::Usize),
    (isize,TLPrimitive::Isize),
    (bool ,TLPrimitive::Bool),
}


macro_rules! impl_for_concrete {
    (
        type IsNonZeroType=$zeroness:ty;
        [
            $( ($this:ty,$prim_repr:ty,$in_mod:expr) ,)*
        ]
    ) => (
        $(
            unsafe impl GetStaticEquivalent_ for $this {
                type StaticEquivalent=Self;
            }
            unsafe impl SharedStableAbi for $this {
                type Kind=ValueKind;
                type IsNonZeroType=$zeroness;

                const S_LAYOUT:&'static TypeLayout=&TypeLayout::from_std::<Self>(
                    stringify!($this),
                    TLData::struct_(&[
                        TLField::new(
                            "0",
                            &[],
                            <$prim_repr as MakeGetAbiInfo<StableAbi_Bound>>::CONST,
                        )
                    ]),
                    ReprAttr::Transparent,
                    ItemInfo::std_type_in($in_mod),
                    tl_genparams!(;;),
                );
            }
        )*
    )
}



impl_for_concrete! {
    type IsNonZeroType=False;
    [
        (AtomicBool ,bool,"std::sync::atomic"),
        (AtomicIsize,isize,"std::sync::atomic"),
        (AtomicUsize,usize,"std::sync::atomic"),
    ]
}

impl_for_concrete! {
    type IsNonZeroType=True;
    [
        (NonZeroU8   ,u8,"std::num"),
        (NonZeroU16  ,u16,"std::num"),
        (NonZeroU32  ,u32,"std::num"),
        (NonZeroU64  ,u64,"std::num"),
        (NonZeroUsize,usize,"std::num"),
    ]
}
/////////////


#[cfg(any(rust_1_34,feature="rust_1_34"))]
mod rust_1_34_impls{
    use super::*;
    use std::sync::atomic::*;
    use core::num::*;

    impl_for_concrete! {
        type IsNonZeroType=False;
        [
            (AtomicI8 ,i8,"std::sync::atomic"),
            (AtomicI16,i16,"std::sync::atomic"),
            (AtomicI32,i32,"std::sync::atomic"),
            (AtomicI64,i64,"std::sync::atomic"),
            (AtomicU8 ,u8,"std::sync::atomic"),
            (AtomicU16,u16,"std::sync::atomic"),
            (AtomicU32,u32,"std::sync::atomic"),
            (AtomicU64,u64,"std::sync::atomic"),
        ]
    }

    impl_for_concrete! {
        type IsNonZeroType=True;
        [
            (NonZeroI8   ,i8,"core::num"),
            (NonZeroI16  ,i16,"core::num"),
            (NonZeroI32  ,i32,"core::num"),
            (NonZeroI64  ,i64,"core::num"),
            (NonZeroIsize,isize,"core::num"),
        ]
    }
}

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

macro_rules! impl_stableabi_for_repr_transparent {
    (
        $type_constr:ident
        $(where[ $($where_clause:tt)* ])* ,
        $item_info:expr
    ) => (
        unsafe impl<P> GetStaticEquivalent_ for $type_constr<P>
        where
            P: GetStaticEquivalent_,
            $($($where_clause)*)*
        {
            type StaticEquivalent=$type_constr<P::StaticEquivalent>;
        }
        unsafe impl<P> SharedStableAbi for $type_constr<P>
        where
            P: StableAbi,
            $($($where_clause)*)*
        {
            type Kind=ValueKind;
            type IsNonZeroType = P::IsNonZeroType;

            const S_LAYOUT: &'static TypeLayout = &TypeLayout::from_std::<Self>(
                stringify!($type_constr),
                TLData::struct_(&[
                    TLField::new("0",&[],<P as MakeGetAbiInfo<StableAbi_Bound>>::CONST,)
                ]),
                ReprAttr::Transparent,
                $item_info,
                tl_genparams!(;P;),
            );
        }
    )
}


impl_stableabi_for_repr_transparent!{ Wrapping ,ItemInfo::std_type_in("std::num") }
impl_stableabi_for_repr_transparent!{ Pin         ,ItemInfo::std_type_in("std::pin") }
impl_stableabi_for_repr_transparent!{ ManuallyDrop,ItemInfo::std_type_in("std::mem") }
impl_stableabi_for_repr_transparent!{ Cell        ,ItemInfo::std_type_in("std::cell") }
impl_stableabi_for_repr_transparent!{ UnsafeCell  ,ItemInfo::std_type_in("std::cell") }

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

macro_rules! impl_stableabi_for_unit_struct {
    (
        $type_constr:ident,
        $item_info:expr
    ) => (
        unsafe impl GetStaticEquivalent_ for $type_constr{
            type StaticEquivalent=$type_constr;
        }
        unsafe impl SharedStableAbi for $type_constr{
            type Kind=ValueKind;
            type IsNonZeroType = False;

            const S_LAYOUT: &'static TypeLayout = &TypeLayout::from_std::<Self>(
                stringify!($type_constr),
                TLData::EMPTY,
                ReprAttr::c(),
                $item_info,
                tl_genparams!(;;),
            );
        }
    )
}


impl_stableabi_for_unit_struct!{ PhantomPinned,ItemInfo::std_type_in("std::marker") }

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


unsafe impl GetStaticEquivalent_ for core_extensions::Void {
    type StaticEquivalent=Self;
}
unsafe impl SharedStableAbi for core_extensions::Void {
    type Kind=ValueKind;
    type IsNonZeroType = False;


    const S_LAYOUT: &'static TypeLayout =
        &TypeLayout::from_params::<Self>(TypeLayoutParams {
            name: "Void",
            item_info:ItemInfo::package_and_mod("core_extensions;0.0.0","core_extensions"),
            data: TLData::Enum(&TLEnum::new(
                "",
                IsExhaustive::exhaustive(),
                &[],
                TLDiscriminants::from_u8_slice(&[]),
                &[]
            )),
            generics: tl_genparams!(;;),
        });
}



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

/// The layout of `extern fn()` and `unsafe extern fn()`
macro_rules! empty_extern_fn_layout{
    ($this:ty) => (
        &TypeLayout::from_params::<extern "C" fn()>(TypeLayoutParams {
            name: "AFunctionPointer",
            item_info:make_item_info!(),
            data: TLData::struct_(&[]),
            generics: tl_genparams!(;;),
        })
    )
}


/// This is the only function type that implements StableAbi
/// so as to make it more obvious that functions involving lifetimes
/// cannot implement this trait directly (because of higher ranked trait bounds).
unsafe impl GetStaticEquivalent_ for extern "C" fn() {
    type StaticEquivalent=Self;
}
unsafe impl SharedStableAbi for extern "C" fn() {
    type Kind=ValueKind;
    type IsNonZeroType = True;

    const S_LAYOUT: &'static TypeLayout = empty_extern_fn_layout!(Self);
}

/// This is the only function type that implements StableAbi
/// so as to make it more obvious that functions involving lifetimes
/// cannot implement this trait directly (because of higher ranked trait bounds).
unsafe impl GetStaticEquivalent_ for unsafe extern "C" fn() {
    type StaticEquivalent=Self;
}
unsafe impl SharedStableAbi for unsafe extern "C" fn() {
    type Kind=ValueKind;
    type IsNonZeroType = True;

    const S_LAYOUT: &'static TypeLayout = empty_extern_fn_layout!(Self);
}


/// The GetAbiInfo of an `unsafe extern fn()`
pub const UNSAFE_EXTERN_FN_ABI_INFO:GetAbiInfo=MakeGetAbiInfoSA::<unsafe extern fn()>::STABLE_ABI;

/// The GetAbiInfo of an `extern fn()`
pub const EXTERN_FN_ABI_INFO:GetAbiInfo=MakeGetAbiInfoSA::<extern fn()>::STABLE_ABI;


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

/// Allows one to create the TypeLayout/AbiInfoWrapper for any type `T`,
/// by pretending that it is a primitive type.
/// 
/// Used by the StableAbi derive macro by fields marker as `#[sabi(unsafe_opaque_field)]`.
/// 
/// # Safety
/// 
/// You must ensure that the layout of `T` is compatible through other means.
#[repr(transparent)]
pub struct UnsafeOpaqueField<T>(T);

unsafe impl<T> GetStaticEquivalent_ for UnsafeOpaqueField<T> {
    /// it is fine to use `()` because this type is treated as opaque anyway.
    type StaticEquivalent=();
}
unsafe impl<T> SharedStableAbi for UnsafeOpaqueField<T> {
    type Kind=ValueKind;
    type IsNonZeroType = False;

    const S_LAYOUT: &'static TypeLayout = &TypeLayout::from_params::<Self>(TypeLayoutParams {
        name: "OpaqueField",
        item_info:make_item_info!(),
        data: TLData::Opaque,
        generics: tl_genparams!(;;),
    });
}

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