co3 0.1.0

Build C API from Rust
Documentation
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
#[cfg(feature = "alloc")]
use alloc::{string::String, vec::Vec};
use core::{
    cell::{Cell, UnsafeCell},
    ffi::c_void,
    marker::PhantomData,
    mem::{ManuallyDrop, MaybeUninit},
    num::NonZero,
    ptr::NonNull,
};

use crate::{
    CFnArg, Decode, Encode, ExternC, ReprC,
    borrow::{Borrow, BorrowCast, BorrowCastMut, FromBorrow},
    niche::Niche,
    stored::{DecodeOwned, EmptyStore, EncodeOwned},
    transmute::CheckedTransmute,
};
#[cfg(feature = "alloc")]
use crate::{boxed::CBoxedSlice, stored::Owned};

macro_rules! non_zero_derive {
    ($($primitive:ty),+ $(,)?) => {$(
        unsafe impl Borrow for NonZero<$primitive> {
            type Borrowed<'itm>
                = Self
            where
                Self: 'itm;

            type Owner = ();

            #[inline(always)]
            fn borrow<'itm>(self, (): &mut ()) -> Self::Borrowed<'itm>
            where
                Self: 'itm,
            {
                self
            }
        }
        impl<'itm> FromBorrow<'itm> for NonZero<$primitive> {
            #[inline(always)]
            fn from_borrow(source: Self::Borrowed<'itm>) -> Self {
                source
            }
        }

        impl ExternC for NonZero<$primitive> {
            type CType = $primitive;
        }
        unsafe impl EncodeOwned for NonZero<$primitive> {
            type Store = ();

            #[inline(always)]
            fn soft_encode<'itm>(self, (): &mut ()) -> Self::CType
            where
                Self: 'itm
            {
                self.get()
            }
        }
        unsafe impl<'d> DecodeOwned<'d> for NonZero<$primitive> {
            type Store = ();

            #[inline(always)]
            unsafe fn soft_decode<'itm: 'd>(source: Self::CType, (): &mut ()) -> Option<Self> {
                Self::new(source)
            }
        }

        impl Encode for NonZero<$primitive> {}
        impl Decode<'_> for NonZero<$primitive> {}

        unsafe impl CheckedTransmute for NonZero<$primitive> {
            #[inline(always)]
            unsafe fn is_valid(target: &Self::CType) -> bool {
                *target != 0
            }
        }

        impl Niche for NonZero<$primitive> {
            const NICHE_VALUE: Self::CType = 0;
        }
        )+
    }
}

non_zero_derive! {
    u8, i8, u16, i16, u32, i32, u64, i64, u128, i128,
}

unsafe impl Borrow for c_void {
    type Borrowed<'itm>
        = Self
    where
        Self: 'itm;

    type Owner = ();

    #[inline(always)]
    fn borrow<'itm>(self, (): &mut ()) -> Self::Borrowed<'itm> {
        self
    }
}
impl<'itm> FromBorrow<'itm> for c_void {
    #[inline(always)]
    fn from_borrow(source: Self) -> Self {
        source
    }
}

impl ExternC for c_void {
    type CType = Self;
}
unsafe impl ReprC for c_void {}
unsafe impl BorrowCast for c_void {
    type AsConst = Self;
}
unsafe impl BorrowCastMut for c_void {
    type AsMut = Self;
}

unsafe impl Borrow for () {
    type Borrowed<'itm>
        = Self
    where
        Self: 'itm;

    type Owner = ();

    #[inline(always)]
    fn borrow<'itm>(self, (): &mut ()) -> Self::Borrowed<'itm> {}
}
impl<'itm> FromBorrow<'itm> for () {
    #[inline(always)]
    fn from_borrow(_: ()) -> Self {}
}

impl ExternC for () {
    type CType = Self;
}
unsafe impl EncodeOwned for () {
    type Store = ();

    #[inline(always)]
    fn soft_encode<'itm>(self, (): &'itm mut Self::Store) -> Self::CType
    where
        Self: 'itm,
    {
        self
    }
}
unsafe impl<'d> DecodeOwned<'d> for () {
    type Store = ();

    #[inline(always)]
    unsafe fn soft_decode<'itm: 'd>(source: Self::CType, (): &mut ()) -> Option<Self> {
        Some(source)
    }
}

impl Encode for () {}
impl Decode<'_> for () {}

unsafe impl CheckedTransmute for () {
    #[inline(always)]
    unsafe fn is_valid(_: &Self::CType) -> bool {
        true
    }
}

unsafe impl ReprC for () {}
unsafe impl BorrowCast for () {
    type AsConst = Self;
}
unsafe impl BorrowCastMut for () {
    type AsMut = Self;
}

unsafe impl EmptyStore for () {}

unsafe impl<T: ?Sized> Borrow for PhantomData<T> {
    type Borrowed<'itm>
        = Self
    where
        Self: 'itm;

    type Owner = ();

    #[inline(always)]
    fn borrow<'itm>(self, (): &mut ()) -> Self::Borrowed<'itm>
    where
        Self: 'itm,
    {
        self
    }
}
impl<'itm, T: ?Sized + 'itm> FromBorrow<'itm> for PhantomData<T> {
    #[inline(always)]
    fn from_borrow(source: Self::Borrowed<'itm>) -> Self {
        source
    }
}

impl<T: ?Sized> ExternC for PhantomData<T> {
    type CType = Self;
}
unsafe impl<T: ?Sized> CheckedTransmute for PhantomData<T> {
    #[inline(always)]
    unsafe fn is_valid(_: &Self::CType) -> bool {
        true
    }
}
unsafe impl<T: ?Sized> EncodeOwned for PhantomData<T> {
    type Store = ();

    #[inline(always)]
    fn soft_encode<'itm>(self, (): &'itm mut Self::Store) -> Self::CType
    where
        Self: 'itm,
    {
        self
    }
}
unsafe impl<'d, T: ?Sized> DecodeOwned<'d> for PhantomData<T> {
    type Store = ();

    #[inline(always)]
    unsafe fn soft_decode<'itm: 'd>(source: Self::CType, (): &mut ()) -> Option<Self> {
        Some(source)
    }
}

impl<T: ?Sized> Encode for PhantomData<T> {}
impl<T: ?Sized> Decode<'_> for PhantomData<T> {}

unsafe impl<T: ?Sized> ReprC for PhantomData<T> {}
unsafe impl<T: ?Sized> BorrowCast for PhantomData<T> {
    type AsConst = Self;
}
unsafe impl<T: ?Sized> BorrowCastMut for PhantomData<T> {
    type AsMut = Self;
}

unsafe impl<T: ?Sized> Borrow for NonNull<T> {
    type Borrowed<'itm>
        = Self
    where
        Self: 'itm;

    type Owner = ();

    #[inline(always)]
    fn borrow<'itm>(self, (): &mut ()) -> Self::Borrowed<'itm>
    where
        Self: 'itm,
    {
        self
    }
}
impl<'itm, T: ?Sized + 'itm> FromBorrow<'itm> for NonNull<T> {
    #[inline(always)]
    fn from_borrow(source: Self::Borrowed<'itm>) -> Self {
        source
    }
}

impl<T: ReprC + ?Sized> ExternC for NonNull<T> {
    // TODO: afaik the pointer is not necessarily mutable just non-null
    type CType = <*mut T as ExternC>::CType;
}
unsafe impl<T: ReprC + ?Sized> EncodeOwned for NonNull<T> {
    type Store = <*mut T as EncodeOwned>::Store;

    #[inline(always)]
    fn soft_encode<'itm>(self, store: &'itm mut Self::Store) -> Self::CType
    where
        Self: 'itm,
    {
        self.as_ptr().soft_encode(store)
    }
}
unsafe impl<'d, T: ReprC + ?Sized> DecodeOwned<'d> for NonNull<T> {
    type Store = <*mut T as DecodeOwned<'d>>::Store;

    #[inline(always)]
    unsafe fn soft_decode<'itm: 'd>(
        source: Self::CType,
        store: &'itm mut Self::Store,
    ) -> Option<Self> {
        let ptr = unsafe { DecodeOwned::soft_decode(source, store)? };
        NonNull::new(ptr)
    }
}

impl<T: ReprC + ?Sized> Encode for NonNull<T> {}
impl<T: ReprC + ?Sized> Decode<'_> for NonNull<T> {}

unsafe impl<T: ReprC + ?Sized> CheckedTransmute for NonNull<T> {
    #[inline(always)]
    unsafe fn is_valid(target: &Self::CType) -> bool {
        !target.is_null()
    }
}

#[cfg(feature = "alloc")]
impl Owned for str {
    type Owned = String;
}

impl ExternC for str {
    type CType = [u8];
}

#[cfg(feature = "alloc")]
unsafe impl Borrow for String {
    type Borrowed<'itm>
        = &'itm str
    where
        Self: 'itm;

    type Owner = Self;

    #[inline(always)]
    fn borrow<'itm>(self, owner: &'itm mut Self::Owner) -> Self::Borrowed<'itm>
    where
        Self: 'itm,
    {
        *owner = self;
        owner
    }
}
#[cfg(feature = "alloc")]
impl<'itm> FromBorrow<'itm> for String {
    #[inline(always)]
    fn from_borrow(source: Self::Borrowed<'itm>) -> Self {
        source.into()
    }
}

#[cfg(feature = "alloc")]
impl ExternC for String {
    type CType = <Vec<u8> as ExternC>::CType;
}
#[cfg(feature = "alloc")]
unsafe impl EncodeOwned for String {
    type Store = ();

    #[inline(always)]
    fn soft_encode<'itm>(self, (): &mut ()) -> Self::CType
    where
        Self: 'itm,
    {
        crate::stored::encode_owned(self.into_bytes())
    }
}
#[cfg(feature = "alloc")]
unsafe impl<'d> DecodeOwned<'d> for String {
    type Store = ();

    #[inline(always)]
    unsafe fn soft_decode<'itm: 'd>(source: Self::CType, (): &mut ()) -> Option<Self> {
        let bytes = unsafe { crate::stored::decode_owned(source)? };
        String::from_utf8(bytes).ok()
    }
}

#[cfg(feature = "alloc")]
impl Encode for String {}
#[cfg(feature = "alloc")]
impl Decode<'_> for String {}

#[cfg(feature = "alloc")]
impl Niche for String {
    const NICHE_VALUE: Self::CType = CBoxedSlice::NICHE_VALUE;
}

unsafe impl<T: Borrow> Borrow for UnsafeCell<T> {
    type Borrowed<'itm>
        = T::Borrowed<'itm>
    where
        Self: 'itm;

    type Owner = T::Owner;

    #[inline(always)]
    fn borrow<'itm>(self, owner: &'itm mut Self::Owner) -> Self::Borrowed<'itm>
    where
        Self: 'itm,
    {
        self.into_inner().borrow(owner)
    }
}
impl<'itm, T: FromBorrow<'itm>> FromBorrow<'itm> for UnsafeCell<T> {
    #[inline(always)]
    fn from_borrow(source: Self::Borrowed<'itm>) -> Self {
        UnsafeCell::new(T::from_borrow(source))
    }
}

impl<R: ExternC + ?Sized> ExternC for UnsafeCell<R> {
    type CType = R::CType;
}
unsafe impl<R: EncodeOwned<CType: Copy>> EncodeOwned for UnsafeCell<R> {
    type Store = R::Store;

    #[inline(always)]
    fn soft_encode<'itm>(self, store: &'itm mut Self::Store) -> Self::CType
    where
        Self: 'itm,
    {
        self.into_inner().soft_encode(store)
    }
}
unsafe impl<'d, R: DecodeOwned<'d, CType: Copy>> DecodeOwned<'d> for UnsafeCell<R> {
    type Store = R::Store;

    #[inline(always)]
    unsafe fn soft_decode<'itm: 'd>(
        source: Self::CType,
        store: &'itm mut Self::Store,
    ) -> Option<Self> {
        unsafe { R::soft_decode(source, store) }.map(Self::new)
    }
}

impl<R: Encode<CType: Copy>> Encode for UnsafeCell<R> {}
impl<'d, R: Decode<'d, CType: Copy>> Decode<'d> for UnsafeCell<R> {}

unsafe impl<T: CheckedTransmute + ?Sized> CheckedTransmute for UnsafeCell<T> {
    #[inline(always)]
    unsafe fn is_valid(target: &Self::CType) -> bool {
        unsafe { T::is_valid(target) }
    }
}

unsafe impl<T: EmptyStore> EmptyStore for UnsafeCell<T> {}

unsafe impl<T: Borrow> Borrow for Cell<T> {
    type Borrowed<'itm>
        = T::Borrowed<'itm>
    where
        Self: 'itm;

    type Owner = T::Owner;

    #[inline(always)]
    fn borrow<'itm>(self, owner: &'itm mut Self::Owner) -> Self::Borrowed<'itm>
    where
        Self: 'itm,
    {
        T::borrow(Cell::into_inner(self), owner)
    }
}
impl<'itm, T: FromBorrow<'itm>> FromBorrow<'itm> for Cell<T> {
    #[inline(always)]
    fn from_borrow(source: Self::Borrowed<'itm>) -> Self {
        Cell::new(T::from_borrow(source))
    }
}

impl<R: ExternC + ?Sized> ExternC for Cell<R> {
    type CType = R::CType;
}
unsafe impl<R: EncodeOwned<CType: Copy>> EncodeOwned for Cell<R> {
    type Store = R::Store;

    #[inline(always)]
    fn soft_encode<'itm>(self, store: &'itm mut Self::Store) -> Self::CType
    where
        Self: 'itm,
    {
        self.into_inner().soft_encode(store)
    }
}
unsafe impl<'d, R: DecodeOwned<'d, CType: Copy>> DecodeOwned<'d> for Cell<R> {
    type Store = R::Store;

    #[inline(always)]
    unsafe fn soft_decode<'itm: 'd>(
        source: Self::CType,
        store: &'itm mut Self::Store,
    ) -> Option<Self> {
        unsafe { R::soft_decode(source, store) }.map(Self::new)
    }
}

impl<R: Encode<CType: Copy>> Encode for Cell<R> {}
impl<'d, R: Decode<'d, CType: Copy>> Decode<'d> for Cell<R> {}

unsafe impl<T: CheckedTransmute + ?Sized> CheckedTransmute for Cell<T> {
    #[inline(always)]
    unsafe fn is_valid(target: &Self::CType) -> bool {
        unsafe { T::is_valid(target) }
    }
}

unsafe impl<T: EmptyStore> EmptyStore for Cell<T> {}

unsafe impl<T> Borrow for MaybeUninit<T> {
    type Borrowed<'itm>
        = Self
    where
        Self: 'itm;

    type Owner = ();

    #[inline(always)]
    fn borrow<'itm>(self, (): &mut ()) -> Self::Borrowed<'itm>
    where
        Self: 'itm,
    {
        self
    }
}
impl<'itm, T: 'itm> FromBorrow<'itm> for MaybeUninit<T> {
    #[inline(always)]
    fn from_borrow(source: Self::Borrowed<'itm>) -> Self {
        source
    }
}

impl<T: CheckedTransmute<CType: Sized>> ExternC for MaybeUninit<T> {
    type CType = MaybeUninit<T::CType>;
}
unsafe impl<T: CheckedTransmute<CType: Sized>> EncodeOwned for MaybeUninit<T> {
    type Store = ();

    #[inline(always)]
    fn soft_encode<'itm>(self, (): &mut ()) -> Self::CType
    where
        Self: 'itm,
    {
        unsafe { core::mem::transmute_copy(&self) }
    }
}
unsafe impl<'d, T: CheckedTransmute<CType: Sized>> DecodeOwned<'d> for MaybeUninit<T> {
    type Store = ();

    #[inline(always)]
    unsafe fn soft_decode<'itm: 'd>(source: Self::CType, (): &mut ()) -> Option<Self> {
        Some(unsafe { core::mem::transmute_copy(&source) })
    }
}

impl<T: CheckedTransmute<CType: Sized>> Encode for MaybeUninit<T> {}
impl<'d, T: CheckedTransmute<CType: Sized>> Decode<'d> for MaybeUninit<T> {}

unsafe impl<T: CheckedTransmute<CType: Sized>> CheckedTransmute for MaybeUninit<T> {
    #[inline(always)]
    unsafe fn is_valid(_: &Self::CType) -> bool {
        true
    }
}

unsafe impl<T: ReprC> ReprC for MaybeUninit<T> {}
unsafe impl<T: CFnArg> CFnArg for MaybeUninit<T> {}

unsafe impl<T: ReprC> BorrowCast for MaybeUninit<T> {
    type AsConst = Self;
}
unsafe impl<T: ReprC> BorrowCastMut for MaybeUninit<T> {
    type AsMut = Self;
}

unsafe impl<T: EmptyStore> EmptyStore for MaybeUninit<T> {}

unsafe impl<T: Borrow> Borrow for ManuallyDrop<T> {
    type Borrowed<'itm>
        = T::Borrowed<'itm>
    where
        Self: 'itm;

    type Owner = T::Owner;

    #[inline(always)]
    fn borrow<'itm>(self, owner: &'itm mut Self::Owner) -> Self::Borrowed<'itm>
    where
        Self: 'itm,
    {
        ManuallyDrop::into_inner(self).borrow(owner)
    }
}
impl<'itm, T: FromBorrow<'itm>> FromBorrow<'itm> for ManuallyDrop<T> {
    #[inline(always)]
    fn from_borrow(source: Self::Borrowed<'itm>) -> Self {
        ManuallyDrop::new(T::from_borrow(source))
    }
}

impl<T: ExternC + ?Sized> ExternC for ManuallyDrop<T> {
    type CType = T::CType;
}
// FIXME: I think t's not ok to get owned type and encode it
// ManuallyDrop::into_inner(self).soft_encode(store)
//unsafe impl<R: EncodeOwned<CType: Copy>> EncodeOwned for ManuallyDrop<R> {
//    type Store = R::Store;
//
//    #[inline(always)]
//    fn soft_encode<'itm>(self, store: &'itm mut Self::Store) -> Self::CType
//    where
//        Self: 'itm,
//    {
//        unimplemented!()
//    }
//}
//unsafe impl<'d, R: DecodeOwned<'d, CType: Copy>> DecodeOwned<'d> for ManuallyDrop<R> {
//    type Store = R::Store;
//
//    #[inline(always)]
//    unsafe fn soft_decode<'itm: 'd>(
//        source: Self::CType,
//        store: &'itm mut Self::Store,
//    ) -> Option<Self> {
//        unimplemented!()
//    }
//}

//impl<R: Encode<CType: Copy>> Encode for ManuallyDrop<R> {}
//impl<'d, R: Decode<'d, CType: Copy>> Decode<'d> for ManuallyDrop<R> {}

unsafe impl<T: CheckedTransmute + ?Sized> CheckedTransmute for ManuallyDrop<T> {
    #[inline(always)]
    unsafe fn is_valid(target: &Self::CType) -> bool {
        unsafe { T::is_valid(target) }
    }
}

impl<T: Niche> Niche for ManuallyDrop<T> {
    const NICHE_VALUE: Self::CType = T::NICHE_VALUE;
}

unsafe impl<T: EmptyStore> EmptyStore for ManuallyDrop<T> {}

#[cfg(test)]
mod tests {
    #[cfg(feature = "alloc")]
    use alloc::{boxed::Box, vec};

    use static_assertions::{assert_impl_all, assert_not_impl_any};

    use super::*;
    #[cfg(feature = "alloc")]
    use crate::boxed::{CBox, CBoxedSlice};
    use crate::{
        CFnArg, Decode, Encode,
        option::ReprCOption,
        slice::{CSlice, CSliceMut},
    };

    #[test]
    fn maybe_uninit_lowers_without_validating_or_encoding_the_inner_value() {
        assert_impl_all!(MaybeUninit<bool>:
            ExternC<CType = MaybeUninit<u8>>,
            CheckedTransmute,
            Decode<'static>,
            Encode,
        );
        assert_impl_all!(MaybeUninit<u8>: ReprC, CFnArg);

        let source = MaybeUninit::new(2_u8);
        assert!(unsafe { <MaybeUninit<bool> as CheckedTransmute>::is_valid(&source) });

        let decoded = unsafe { crate::decode::<MaybeUninit<bool>>(source) }.unwrap();
        let encoded = crate::encode(decoded);

        assert_eq!(unsafe { encoded.assume_init() }, 2);
    }

    // TODO: Enable
    //#[test]
    //fn manually_drop_inner_without_drop() {
    //    assert_impl_all!(ManuallyDrop<u8>:
    //        ExternC<CType = u8>,
    //        Decode<'static>,
    //        Encode,
    //    );
    //    assert_impl_all!(&ManuallyDrop<u8>:
    //        Niche<CType = *const u8>,
    //        RustSpec<Niche = rust_spec::niche::WithNiche<rust_spec::Stable>>,
    //        Decode<'static>,
    //        Encode,
    //    );
    //    assert_impl_all!(&mut ManuallyDrop<u8>:
    //        Niche<CType = *mut u8>,
    //        RustSpec<Niche = rust_spec::niche::WithNiche<rust_spec::Stable>>,
    //        Decode<'static>,
    //        Encode,
    //    );
    //    #[cfg(feature = "alloc")]
    //    assert_impl_all!(Box<ManuallyDrop<u8>>:
    //        Niche<CType = CBox<u8>>,
    //        Decode<'static>,
    //        Encode,
    //    );
    //    assert_impl_all!(&[ManuallyDrop<u8>]:
    //        Niche<CType = CSlice<u8>>,
    //        Decode<'static>,
    //        Encode,
    //    );
    //    assert_impl_all!(&mut [ManuallyDrop<u8>]:
    //        Niche<CType = CSliceMut<u8>>,
    //        Decode<'static>,
    //        Encode,
    //    );
    //    #[cfg(feature = "alloc")]
    //    assert_impl_all!(Box<[ManuallyDrop<u8>]>:
    //        Niche<CType = CBoxedSlice<u8>>,
    //        Decode<'static>,
    //        Encode,
    //    );
    //    #[cfg(feature = "alloc")]
    //    assert_impl_all!(Vec<ManuallyDrop<u8>>:
    //        Niche<CType = CBoxedSlice<u8>>,
    //        Decode<'static>,
    //        Encode,
    //    );
    //    assert_impl_all!([ManuallyDrop<u8>; 2]:
    //        ExternC<CType = [u8; 2]>,
    //        Decode<'static>,
    //        Encode,
    //    );
    //    assert_impl_all!(Option<ManuallyDrop<u8>>:
    //        Niche<CType = ReprCOption<u8>>,
    //        Decode<'static>,
    //        Encode,
    //    );

    //    assert_not_impl_any!(ManuallyDrop<u8>: ReprC);
    //}

    //#[test]
    //#[cfg(feature = "alloc")]
    //fn manually_drop_inner_with_drop() {
    //    assert_impl_all!(ManuallyDrop<String>:
    //        Niche<CType = CBoxedSlice<u8>>,
    //        Decode<'static>,
    //        Encode,
    //    );
    //    assert_impl_all!(&ManuallyDrop<String>:
    //        Niche<CType = *const CBoxedSlice<u8>,
    //        Decode<'static>,
    //        Encode,
    //    );
    //    assert_impl_all!(&mut ManuallyDrop<String>:
    //        Niche<CType = *mut CBoxedSlice<u8>,
    //        Decode<'static>,
    //        Encode,
    //    );
    //    assert_impl_all!(Box<ManuallyDrop<String>>:
    //        Niche<CType = CBox<CBoxedSlice<u8>>,
    //        RustSpec<Niche = rust_spec::niche::WithNiche<rust_spec::Stable>>>>,
    //        DecodeOwned<'static>,
    //        EncodeOwned,
    //    );
    //    assert_impl_all!(&[ManuallyDrop<String>]:
    //        Niche<CType = CSlice<CBoxedSlice<u8>>>,
    //        Decode<'static>,
    //        Encode,
    //    );
    //    assert_impl_all!(&mut [ManuallyDrop<String>]:
    //        Niche<CType = CSliceMut<CBoxedSlice<u8>>>,
    //        Decode<'static>,
    //        Encode,
    //    );
    //    assert_impl_all!(Box<[ManuallyDrop<String>]>:
    //        Niche<CType = CBoxedSlice<CBoxedSlice<u8>>>,
    //        DecodeOwned<'static>,
    //        EncodeOwned,
    //    );
    //    assert_impl_all!(Vec<ManuallyDrop<String>>:
    //        Niche<CType = CBoxedSlice<CBoxedSlice<u8>>>,
    //        DecodeOwned<'static>,
    //        EncodeOwned,
    //    );
    //    assert_impl_all!([ManuallyDrop<String>; 2]:
    //        Niche<CType = [CBoxedSlice<u8>; 2]>,
    //        Decode<'static>,
    //        Encode,
    //    );
    //    assert_impl_all!(Option<ManuallyDrop<String>>:
    //        //Niche<CType = CBoxedSlice<u8>>,
    //        ExternC<CType = CBoxedSlice<u8>>,
    //        Decode<'static>,
    //        Encode,
    //    );
    //    assert_not_impl_any!(ManuallyDrop<String>: ReprC);

    //    #[cfg(feature = "alloc")]
    //    assert_not_impl_any!(Box<[ManuallyDrop<String>]>: Encode, Decode<'static>);
    //    #[cfg(feature = "alloc")]
    //    assert_not_impl_any!(Vec<ManuallyDrop<String>>: Encode, Decode<'static>);
    //}

    #[test]
    fn str_is_supported() {
        assert_impl_all!(&str:
            Niche<CType = CSlice<u8>>,
            Decode<'static>,
            Encode,
        );

        #[cfg(feature = "alloc")]
        assert_impl_all!(&mut str:
            Niche<CType = CSliceMut<u8>>,
            Decode<'static>,
            Encode,
        );

        #[cfg(feature = "alloc")]
        assert_impl_all!(Box<str>:
            Niche<CType = CBoxedSlice<u8>>,
            Decode<'static>,
            Encode,
        );
    }

    #[test]
    fn str_decode_rejects_invalid_utf8() {
        let invalid = [0xff];

        let source = CSlice::from_raw_parts(invalid.as_ptr(), invalid.len());
        let decoded = unsafe { crate::decode::<&str>(source) };
        assert!(decoded.is_none());

        let mut invalid = [0xff];
        let source = CSliceMut::from_raw_parts_mut(invalid.as_mut_ptr(), invalid.len());
        let decoded = unsafe { crate::decode::<&mut str>(source) };
        assert!(decoded.is_none());

        #[cfg(feature = "alloc")]
        {
            let source = CBoxedSlice::from_boxed_slice(vec![0xff].into_boxed_slice());
            let decoded = unsafe { crate::decode::<Box<str>>(source) };
            assert!(decoded.is_none());
        }
    }

    #[test]
    fn robust_unsafe_cell() {
        assert_impl_all!(UnsafeCell<u8>:
            ExternC<CType = u8>,
            Decode<'static>,
            Encode,
        );
        assert_impl_all!(&UnsafeCell<u8>:
            Niche<CType = *mut u8>,
            Decode<'static>,
            Encode,
        );
        assert_impl_all!(&mut UnsafeCell<u8>:
            Niche<CType = *mut u8>,
            Decode<'static>,
            Encode,
        );
        #[cfg(feature = "alloc")]
        assert_impl_all!(Box<UnsafeCell<u8>>:
            Niche<CType = CBox<u8>>,
            Decode<'static>,
            Encode,
        );
        assert_impl_all!(&[UnsafeCell<u8>]:
            Niche<CType = CSlice<u8>>,
            Decode<'static>,
            Encode,
        );
        assert_impl_all!(&mut [UnsafeCell<u8>]:
            Niche<CType = CSliceMut<u8>>,
            Decode<'static>,
            Encode,
        );
        #[cfg(feature = "alloc")]
        assert_impl_all!(Box<[UnsafeCell<u8>]>:
            Niche<CType = CBoxedSlice<u8>>,
            Decode<'static>,
            Encode,
        );
        #[cfg(feature = "alloc")]
        assert_impl_all!(Vec<UnsafeCell<u8>>:
            Niche<CType = CBoxedSlice<u8>>,
            Decode<'static>,
            Encode,
        );
        assert_impl_all!([UnsafeCell<u8>; 2]:
            ExternC<CType = [u8; 2]>,
            Decode<'static>,
            Encode,
        );
        assert_impl_all!(Option<UnsafeCell<u8>>:
            Niche<CType = ReprCOption<u8>>,
            Decode<'static>,
            Encode,
        );

        assert_not_impl_any!(UnsafeCell<u8>: ReprC);
    }

    #[test]
    fn non_robust_unsafe_cell() {
        assert_impl_all!(UnsafeCell<NonZero<u8>>:
            ExternC<CType = u8>,
            Decode<'static>,
            Encode,
        );
        assert_impl_all!(&UnsafeCell<NonZero<u8>>:
            Niche<CType = *mut u8>,
            Decode<'static>,
            //Encode,
        );
        assert_impl_all!(&mut UnsafeCell<NonZero<u8>>:
            Niche<CType = *mut u8>,
            Decode<'static>,
        );
        #[cfg(feature = "alloc")]
        assert_impl_all!(Box<UnsafeCell<NonZero<u8>>>:
            Niche<CType = CBox<u8>>,
            Decode<'static>,
            Encode,
        );
        assert_impl_all!(&[UnsafeCell<NonZero<u8>>]:
            Niche<CType = CSlice<u8>>,
            Decode<'static>,
            Encode,
        );
        assert_impl_all!(&mut [UnsafeCell<NonZero<u8>>]:
            Niche<CType = CSliceMut<u8>>,
            Decode<'static>,
        );
        #[cfg(feature = "alloc")]
        assert_impl_all!(Box<[UnsafeCell<NonZero<u8>>]>:
            Niche<CType = CBoxedSlice<u8>>,
            Decode<'static>,
            Encode,
        );
        #[cfg(feature = "alloc")]
        assert_impl_all!(Vec<UnsafeCell<NonZero<u8>>>:
            Niche<CType = CBoxedSlice<u8>>,
            Decode<'static>,
            Encode,
        );
        assert_impl_all!([UnsafeCell<NonZero<u8>>; 2]:
            ExternC<CType = [u8; 2]>,
            Decode<'static>,
            Encode,
        );
        assert_impl_all!(Option<UnsafeCell<NonZero<u8>>>:
            Niche<CType = ReprCOption<u8>>,
            Decode<'static>,
            Encode,
        );

        assert_not_impl_any!(UnsafeCell<NonZero<u8>>: ReprC);
    }
}