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
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
#![no_std]

#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

#[cfg(feature = "rc")]
use alloc::rc::Rc;
#[cfg(feature = "alloc")]
use alloc::{boxed::Box, vec::Vec};
#[cfg(feature = "assume_cell_layout")]
use core::cell::Cell;
use core::cell::UnsafeCell;
use core::ops::Deref;
use core::ops::DerefMut;
use core::ops::Index;
use core::ops::IndexMut;
use index::SliceCellIndex;

mod index;
#[cfg(feature = "std")]
pub mod io;

/// A [`Cell<[T; N]>`](core::cell::Cell)-like type that has some additional slice-like API.
///
/// This type dereferences to [`SliceCell<T>`](SliceCell).
///
/// Under the `assume_cell_layout` cargo feature, this type can be converted to and from `Cell<[T; N]>` and `[Cell<T>; N]` in several ways.
#[repr(transparent)]
pub struct ArrayCell<T, const N: usize> {
    inner: UnsafeCell<[T; N]>,
}

/// A [`Cell<[T]>`](core::cell::Cell)-like type that has some additional slice-like API.
///
/// References to this type can be gotten from dereferencing an [`ArrayCell<T, N>`](ArrayCell), or using [`from_mut`](SliceCell::from_mut).
///
/// Under the `assume_cell_layout` cargo feature, this type can be converted to and from `Cell<[T]>` and `[Cell<T>]` in several ways.
#[repr(transparent)]
pub struct SliceCell<T> {
    inner: UnsafeCell<[T]>,
}

#[cfg(feature = "assume_cell_layout")]
impl<T, const N: usize> ArrayCell<T, N> {
    /// View this [`ArrayCell`] as a [`Cell`] of an [array] of `N` elements.
    pub fn as_std_ref(&self) -> &Cell<[T; N]> {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do:
        // 1a. `Cell<T>` has the same layout as `T`.
        // 1b. `ArrayCell<T, N>` has the same layout as `[T; N]`.
        // 1c. `SliceCell<T>` has the same layout as `[T]`.
        // 2. `&Cell<T>` does not allow arbitrary user code to access `T` by reference directly.
        // Additional assumptions:
        // 3. `Cell<[T; N]>` has the same layout as `[Cell<T>; N]` (implied by 1).
        unsafe { &*(self as *const Self).cast() }
    }
    /// View this [`ArrayCell`] as an [array] of `N` [`Cell`]s.
    pub fn as_std_transposed_ref(&self) -> &[Cell<T>; N] {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { &*(self as *const Self).cast() }
    }
    /// View a [`Cell`] of an [array] of `N` elements as an [`ArrayCell`].
    pub fn from_std_ref(std: &Cell<[T; N]>) -> &Self {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { &*(std as *const Cell<[T; N]>).cast() }
    }
    /// View an [array] of `N` [`Cell`]s as an [`ArrayCell`].
    pub fn from_std_transposed_ref(std: &[Cell<T>; N]) -> &Self {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { &*(std as *const [Cell<T>; N]).cast() }
    }
    /// View this [`ArrayCell`] as a [`Cell`] of an [array] of `N` elements.
    pub fn as_std_mut(&mut self) -> &mut Cell<[T; N]> {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { &mut *(self as *mut Self).cast() }
    }
    /// View this [`ArrayCell`] as an [array] of `N` [`Cell`]s.
    pub fn as_std_transposed_mut(&mut self) -> &mut [Cell<T>; N] {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { &mut *(self as *mut Self).cast() }
    }
    /// View a [`Cell`] of an [array] of `N` elements as an [`ArrayCell`].
    pub fn from_std_mut(std: &mut Cell<[T; N]>) -> &mut Self {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { &mut *(std as *mut Cell<[T; N]>).cast() }
    }
    /// View an [array] of `N` [`Cell`]s as an [`ArrayCell`].
    pub fn from_std_transposed_mut(std: &mut [Cell<T>; N]) -> &mut Self {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { &mut *(std as *mut [Cell<T>; N]).cast() }
    }
}

#[cfg(all(feature = "assume_cell_layout", feature = "alloc"))]
impl<T, const N: usize> ArrayCell<T, N> {
    /// View this [`ArrayCell`] as a [`Cell`] of an [array] of `N` elements.
    pub fn into_std_boxed(self: Box<Self>) -> Box<Cell<[T; N]>> {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { Box::from_raw(Box::into_raw(self).cast()) }
    }
    /// View this [`ArrayCell`] as an [array] of `N` [`Cell`]s.
    pub fn into_std_transposed_boxed(self: Box<Self>) -> Box<[Cell<T>; N]> {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { Box::from_raw(Box::into_raw(self).cast()) }
    }
    /// View a [`Cell`] of an [array] of `N` elements as an [`ArrayCell`].
    pub fn from_std_boxed(std: Box<Cell<[T; N]>>) -> Box<Self> {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { Box::from_raw(Box::into_raw(std).cast()) }
    }
    /// View an [array] of `N` [`Cell`]s as an [`ArrayCell`].
    pub fn from_std_transposed_boxed(std: Box<[Cell<T>; N]>) -> Box<Self> {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { Box::from_raw(Box::into_raw(std).cast()) }
    }
}

#[cfg(all(feature = "assume_cell_layout", feature = "rc"))]
impl<T, const N: usize> ArrayCell<T, N> {
    /// View this [`ArrayCell`] as a [`Cell`] of an [array] of `N` elements.
    pub fn into_std_rc(self: Rc<Self>) -> Rc<Cell<[T; N]>> {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { Rc::from_raw(Rc::into_raw(self).cast()) }
    }
    /// View this [`ArrayCell`] as an [array] of `N` [`Cell`]s.
    pub fn into_std_transposed_rc(self: Rc<Self>) -> Rc<[Cell<T>; N]> {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { Rc::from_raw(Rc::into_raw(self).cast()) }
    }
    /// View a [`Cell`] of an [array] of `N` elements as an [`ArrayCell`].
    pub fn from_std_rc(std: Rc<Cell<[T; N]>>) -> Rc<Self> {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { Rc::from_raw(Rc::into_raw(std).cast()) }
    }
    /// View an [array] of `N` [`Cell`]s as an [`ArrayCell`].
    pub fn from_std_transposed_rc(std: Rc<[Cell<T>; N]>) -> Rc<Self> {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { Rc::from_raw(Rc::into_raw(std).cast()) }
    }
}

impl<T, const N: usize> ArrayCell<T, N> {
    /// Safety: This function is only used internally, and its output is only passed to `ArrayCell::from_raw_ref` or `SliceCell::from_raw_ref`.
    pub(crate) fn as_raw_ref(&self) -> &[UnsafeCell<T>; N] {
        unsafe { &*(self as *const Self).cast() }
    }
    /// Safety: This function is only used internally, and its output is only passed to `ArrayCell::from_raw_mut` or `SliceCell::from_raw_mut`.
    pub(crate) fn as_raw_mut(&mut self) -> &mut [UnsafeCell<T>; N] {
        unsafe { &mut *(self as *mut Self).cast() }
    }
    /// Safety: This function is only used internally, and its input must have come from `ArrayCell::as_raw_ref` or `SliceCell::as_raw_ref`, and possible slicing, or be zero-sized.
    pub(crate) unsafe fn from_raw_ref(this: &[UnsafeCell<T>; N]) -> &Self {
        unsafe { &*(this as *const [UnsafeCell<T>; N]).cast() }
    }
    /// Safety: This function is only used internally, and its input must have come from `ArrayCell::as_raw_mut` or `SliceCell::as_raw_mut`, and possible slicing, or be zero-sized.
    pub(crate) unsafe fn from_raw_mut(this: &mut [UnsafeCell<T>; N]) -> &mut Self {
        unsafe { &mut *(this as *mut [UnsafeCell<T>; N]).cast() }
    }

    /// Safety: This function is only used internally, and its output is only passed to `ArrayCell::from_raw_boxed` or `SliceCell::from_raw_boxed`.
    #[cfg(feature = "alloc")]
    pub(crate) fn into_raw_boxed(self: Box<Self>) -> Box<[UnsafeCell<T>; N]> {
        unsafe { Box::from_raw(Box::into_raw(self).cast()) }
    }
    /// Safety: This function is only used internally, and its input must have come from `ArrayCell::into_raw_boxed` or `SliceCell::into_raw_boxed`, or be zero-sized.
    #[cfg(feature = "alloc")]
    pub(crate) unsafe fn from_raw_boxed(this: Box<[UnsafeCell<T>; N]>) -> Box<Self> {
        unsafe { Box::from_raw(Box::into_raw(this).cast()) }
    }

    /// Safety: This function is only used internally, and its output is only passed to `ArrayCell::from_raw_rc` or `SliceCell::from_raw_rc`.
    #[cfg(feature = "rc")]
    pub(crate) fn into_raw_rc(self: Rc<Self>) -> Rc<[UnsafeCell<T>; N]> {
        unsafe { Rc::from_raw(Rc::into_raw(self).cast()) }
    }
    /// Safety: This function is only used internally, and its input must have come from `ArrayCell::into_raw_rc` or `SliceCell::into_raw_rc`.
    #[cfg(feature = "rc")]
    pub(crate) unsafe fn from_raw_rc(this: Rc<[UnsafeCell<T>; N]>) -> Rc<Self> {
        unsafe { Rc::from_raw(Rc::into_raw(this).cast()) }
    }

    /// Returns a raw pointer to the underlying data in this [`ArrayCell`].
    pub fn as_ptr(&self) -> *mut [T; N] {
        UnsafeCell::raw_get(&self.inner).cast()
    }

    /// Consumes the [`ArrayCell`], returning the wrapped [array].
    pub fn into_inner(self) -> [T; N] {
        self.inner.into_inner()
    }

    /// Wraps an [array] in an [`ArrayCell`].
    pub fn new(inner: [T; N]) -> Self {
        Self {
            inner: UnsafeCell::new(inner),
        }
    }

    /// Wraps a [boxed](alloc::boxed::Box) [array] in an [`ArrayCell`].
    #[cfg(feature = "alloc")]
    pub fn new_boxed(inner: Box<[T; N]>) -> Box<Self> {
        // SAFETY: `ArrayCell<T, N>` has the same layout as `[T; N]`.
        unsafe { Box::from_raw(Box::into_raw(inner) as *mut _) }
    }

    /// Unwaps a [boxed](alloc::boxed::Box) [ArrayCell] into an [array].
    #[cfg(feature = "alloc")]
    pub fn into_inner_boxed(self: Box<Self>) -> Box<[T; N]> {
        // SAFETY: `ArrayCell<T, N>` has the same layout as `[T; N]`.
        unsafe { Box::from_raw(Box::into_raw(self) as *mut _) }
    }

    /// Wraps a [reference-counted](alloc::rc::Rc) [array] in an `ArrayCell`, if it is uniquely owned.
    #[cfg(feature = "rc")]
    pub fn try_new_rc(mut inner: Rc<[T; N]>) -> Result<Rc<Self>, Rc<[T; N]>> {
        match Rc::get_mut(&mut inner) {
            // SAFETY: `ArrayCell<T, N>` has the same layout as `[T; N]`.
            // And: there are no other `Rc` or `Weak` pointers to this allocation, so it is sound
            // to "add" interior mutability to it
            Some(_unique) => Ok(unsafe { Rc::from_raw(Rc::into_raw(inner).cast()) }),
            None => Err(inner),
        }
    }

    /// Unwraps a [reference-counted](alloc::rc::Rc) [`ArrayCell`] into an [array], if it is uniquely owned.
    #[cfg(feature = "rc")]
    pub fn try_into_inner_rc(mut self: Rc<Self>) -> Result<Rc<[T; N]>, Rc<Self>> {
        match Rc::get_mut(&mut self) {
            // SAFETY: `ArrayCell<T, N>` has the same layout as `[T; N]`.
            // And: there are no other `Rc` or `Weak` pointers to this allocation, so it is sound
            // to "remove" interior mutability from it
            Some(_unique) => Ok(unsafe { Rc::from_raw(Rc::into_raw(self).cast()) }),
            None => Err(self),
        }
    }

    #[cfg(feature = "rc")]
    /// Replacement for `From` impl, since `Rc` is not fundamental.
    pub fn unsize_rc(self: Rc<Self>) -> Rc<SliceCell<T>> {
        // SAFETY: the input to `SliceCell::from_raw_rc` is the result of `ArrayCell::into_raw_rc`.
        unsafe { SliceCell::from_raw_rc(self.into_raw_rc()) }
    }

    /// Unwraps a uniquely borrowed [`ArrayCell`] into an array.
    pub fn get_mut(&mut self) -> &mut [T; N] {
        self.inner.get_mut()
    }

    /// Wraps a uniquely borrowed array in an [`ArrayCell`].
    pub fn from_mut(inner: &mut [T; N]) -> &mut Self {
        unsafe { &mut *(inner as *mut [T; N]).cast() }
    }

    /// Returns the length of the [`ArrayCell`].
    pub const fn len(&self) -> usize {
        N
    }
}

impl<T, const N: usize> AsRef<SliceCell<T>> for ArrayCell<T, N> {
    fn as_ref(&self) -> &SliceCell<T> {
        // SAFETY: the input to `SliceCell::from_raw_ref` is the result of `ArrayCell::as_raw_ref`.
        unsafe { SliceCell::from_raw_ref(self.as_raw_ref()) }
    }
}

impl<T, const N: usize> AsMut<SliceCell<T>> for ArrayCell<T, N> {
    fn as_mut(&mut self) -> &mut SliceCell<T> {
        // SAFETY: the input to `SliceCell::from_raw_mut` is the result of `ArrayCell::as_raw_mut`.
        unsafe { SliceCell::from_raw_mut(self.as_raw_mut()) }
    }
}

impl<T> AsRef<SliceCell<T>> for SliceCell<T> {
    fn as_ref(&self) -> &SliceCell<T> {
        self
    }
}

impl<T> AsMut<SliceCell<T>> for SliceCell<T> {
    fn as_mut(&mut self) -> &mut SliceCell<T> {
        self
    }
}

impl<T, const N: usize> Deref for ArrayCell<T, N> {
    type Target = SliceCell<T>;

    fn deref(&self) -> &Self::Target {
        self.as_ref()
    }
}

impl<T, const N: usize> DerefMut for ArrayCell<T, N> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.as_mut()
    }
}

#[cfg(feature = "assume_cell_layout")]
impl<T> SliceCell<T> {
    /// View this [`SliceCell`] as a [`Cell`] of a [slice].
    pub fn as_std_ref(&self) -> &Cell<[T]> {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { &*(self as *const Self as *const _) }
    }
    /// View this [`SliceCell`] as a [slice] of [`Cell`]s.
    pub fn as_std_transposed_ref(&self) -> &[Cell<T>] {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { &*(self as *const Self as *const _) }
    }
    /// View a [`Cell`] of a [slice] as a [`SliceCell`].
    pub fn from_std_ref(std: &Cell<[T]>) -> &Self {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { &*(std as *const Cell<[T]> as *const _) }
    }
    /// View a [slice] [`Cell`]s as a [`SliceCell`].
    pub fn from_std_transposed_ref(std: &[Cell<T>]) -> &Self {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { &*(std as *const [Cell<T>] as *const _) }
    }
    /// View this [`SliceCell`] as a [`Cell`] of a [slice].
    pub fn as_std_mut(&mut self) -> &mut Cell<[T]> {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { &mut *(self as *mut Self as *mut _) }
    }
    /// View this [`SliceCell`] as a [slice] of [`Cell`]s.
    pub fn as_std_transposed_mut(&mut self) -> &mut [Cell<T>] {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { &mut *(self as *mut Self as *mut _) }
    }
    /// View a [`Cell`] of a [slice] as a [`SliceCell`].
    pub fn from_std_mut(std: &mut Cell<[T]>) -> &mut Self {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { &mut *(std as *mut Cell<[T]> as *mut _) }
    }
    /// View a [slice] of [`Cell`] as a [`SliceCell`].
    pub fn from_std_transposed_mut(std: &mut [Cell<T>]) -> &mut Self {
        // SAFETY: assume_cell_layout feature is enabled, so we are assuming `Cell` upholds the same invariants that we do.
        // See `ArrayCell::as_std_ref` for assumptions.
        unsafe { &mut *(std as *mut [Cell<T>] as *mut _) }
    }
}

#[cfg(all(feature = "assume_cell_layout", feature = "alloc"))]
impl<T> SliceCell<T> {
    /// View this [`SliceCell`] as a [`Cell`] of a [slice].
    pub fn into_std_boxed(self: Box<Self>) -> Box<Cell<[T]>> {
        unsafe { Box::from_raw(Box::into_raw(self) as *mut _) }
    }
    /// View this [`SliceCell`] as a [slice] of [`Cell`]s.
    pub fn into_std_transposed_boxed(self: Box<Self>) -> Box<[Cell<T>]> {
        unsafe { Box::from_raw(Box::into_raw(self) as *mut _) }
    }
    /// View a [`Cell`] of a [slice] as a [`SliceCell`].
    pub fn from_std_boxed(std: Box<Cell<[T]>>) -> Box<Self> {
        unsafe { Box::from_raw(Box::into_raw(std) as *mut _) }
    }
    pub fn from_std_transposed_boxed(std: Box<[Cell<T>]>) -> Box<Self> {
        unsafe { Box::from_raw(Box::into_raw(std) as *mut _) }
    }
}

#[cfg(all(feature = "assume_cell_layout", feature = "rc"))]
impl<T> SliceCell<T> {
    /// View this [`SliceCell`] as a [`Cell`] of a [slice].
    pub fn into_std_rc(self: Rc<Self>) -> Rc<Cell<[T]>> {
        unsafe { Rc::from_raw(Rc::into_raw(self) as *const _) }
    }
    /// View this [`SliceCell`] as a [slice] of [`Cell`]s.
    pub fn into_std_transposed_rc(self: Rc<Self>) -> Rc<[Cell<T>]> {
        unsafe { Rc::from_raw(Rc::into_raw(self) as *const _) }
    }
    /// View a [`Cell`] of a [slice] as a [`SliceCell`].
    pub fn from_std_rc(std: Rc<Cell<[T]>>) -> Rc<Self> {
        unsafe { Rc::from_raw(Rc::into_raw(std) as *const _) }
    }
    pub fn from_std_transposed_rc(std: Rc<[Cell<T>]>) -> Rc<Self> {
        unsafe { Rc::from_raw(Rc::into_raw(std) as *const _) }
    }
}

impl<T> SliceCell<T> {
    /// Safety: This function is only used internally, and its output is only passed to `ArrayCell::from_raw_ref` or `SliceCell::from_raw_ref`.
    pub(crate) fn as_raw_ref(&self) -> &[UnsafeCell<T>] {
        unsafe { &*(self as *const Self as *const _) }
    }
    /// Safety: This function is only used internally, and its input must have come from `ArrayCell::as_raw_ref` or `SliceCell::as_raw_ref`, and possible slicing, or be zero-sized.
    pub(crate) unsafe fn from_raw_ref(this: &[UnsafeCell<T>]) -> &Self {
        unsafe { &*(this as *const _ as *const _) }
    }
    /// Safety: This function is only used internally, and its output is only passed to `ArrayCell::from_raw_mut` or `SliceCell::from_raw_mut`.
    pub(crate) fn as_raw_mut(&mut self) -> &mut [UnsafeCell<T>] {
        unsafe { &mut *(self as *mut Self as *mut _) }
    }
    /// Safety: This function is only used internally, and its input must have come from `ArrayCell::as_raw_mut` or `SliceCell::as_raw_mut`, and possible slicing, or be zero-sized.
    pub(crate) unsafe fn from_raw_mut(this: &mut [UnsafeCell<T>]) -> &mut Self {
        unsafe { &mut *(this as *mut _ as *mut _) }
    }

    /// Safety: This function is only used internally, and its output is only passed to `ArrayCell::from_raw_boxed` or `SliceCell::from_raw_boxed`.
    #[cfg(feature = "alloc")]
    pub(crate) fn into_raw_boxed(self: Box<Self>) -> Box<[UnsafeCell<T>]> {
        unsafe { Box::from_raw(Box::into_raw(self) as *mut _) }
    }
    /// Safety: This function is only used internally, and its input must have come from `ArrayCell::into_raw_boxed` or `SliceCell::into_raw_boxed`, or be zero-sized.
    #[cfg(feature = "alloc")]
    pub(crate) unsafe fn from_raw_boxed(this: Box<[UnsafeCell<T>]>) -> Box<Self> {
        unsafe { Box::from_raw(Box::into_raw(this) as *mut _) }
    }

    /// Safety: This function is only used internally, and its output is only passed to `ArrayCell::from_raw_rc` or `SliceCell::from_raw_rc`.
    #[cfg(feature = "rc")]
    pub(crate) fn into_raw_rc(self: Rc<Self>) -> Rc<[UnsafeCell<T>]> {
        unsafe { Rc::from_raw(Rc::into_raw(self) as *mut _) }
    }
    /// Safety: This function is only used internally, and its input must have come from `ArrayCell::into_raw_rc` or `SliceCell::into_raw_rc`
    #[cfg(feature = "rc")]
    pub(crate) unsafe fn from_raw_rc(this: Rc<[UnsafeCell<T>]>) -> Rc<Self> {
        unsafe { Rc::from_raw(Rc::into_raw(this) as *mut _) }
    }

    /// Returns a raw pointer to the underlying data in this `SliceCell`.
    pub fn as_ptr(&self) -> *mut [T] {
        UnsafeCell::raw_get(&self.inner)
    }

    /// Unwraps a [boxed](alloc::boxed::Box) [`SliceCell`] into a slice.
    #[cfg(feature = "alloc")]
    pub fn into_inner_boxed(self: Box<Self>) -> Box<[T]> {
        // SAFETY: `ArrayCell<T, N>` has the same layout as `[T; N]`.
        unsafe { Box::from_raw(Box::into_raw(self) as *mut _) }
    }

    /// Wraps a [boxed](alloc::boxed::Box) [slice] in a [`SliceCell`].
    #[cfg(feature = "alloc")]
    pub fn new_boxed(inner: Box<[T]>) -> Box<Self> {
        unsafe { Box::from_raw(Box::into_raw(inner) as *mut _) }
    }

    /// Wraps a [reference-counted](alloc::rc::Rc) [slice] in a `SliceCell`, if it is uniquely owned.
    #[cfg(feature = "rc")]
    pub fn try_new_rc(mut inner: Rc<[T]>) -> Result<Rc<Self>, Rc<[T]>> {
        match Rc::get_mut(&mut inner) {
            // SAFETY: `SliceCell<T>` has the same layout as `[T]`.
            // And: there are no other `Rc` or `Weak` pointers to this allocation, so it is sound
            // to "add" interior mutability to it
            Some(_unique) => Ok(unsafe { Rc::from_raw(Rc::into_raw(inner) as *const _) }),
            None => Err(inner),
        }
    }

    /// Unwraps a [reference-counted](alloc::rc::Rc) [`SliceCell`] into an [slice], if it is uniquely owned.
    #[cfg(feature = "rc")]
    pub fn try_into_inner_rc(mut self: Rc<Self>) -> Result<Rc<[T]>, Rc<Self>> {
        match Rc::get_mut(&mut self) {
            // SAFETY: `SliceCell<T>` has the same layout as `[T]`.
            // And: there are no other `Rc` or `Weak` pointers to this allocation, so it is sound
            // to "remove" interior mutability from it
            Some(_unique) => Ok(unsafe { Rc::from_raw(Rc::into_raw(self) as *const _) }),
            None => Err(self),
        }
    }

    /// Unwraps a uniquely borrowed [`SliceCell`] into a slice.
    pub fn get_mut(&mut self) -> &mut [T] {
        self.inner.get_mut()
    }

    /// Wraps a uniquely borrowed slice in a [`SliceCell`].
    pub fn from_mut(inner: &mut [T]) -> &mut Self {
        // SAFETY: `SliceCell<T>` has the same layout as `[T]`.
        // And: the slice is uniquely borrowed, so it is sound to "add" interior mutability for the
        // length of the borrow.
        unsafe { &mut *(inner as *mut [T] as *mut _) }
    }

    /// Returns the length of the [`SliceCell`].
    pub const fn len(&self) -> usize {
        // SAFETY: `SliceCell<T>` has the same layout as `[T]`.
        unsafe { &*(self as *const Self as *const [UnsafeCell<T>]) }.len()
        // TODO: when `slice_ptr_len` becomes stable:
        // `(self as *const Self as *const [UnsafeCell<T>]).len()`
    }

    /// Divide one `SliceCell` into two at an index.
    ///
    /// Panics if `mid > self.len()`.
    ///
    /// See [slice::split_at]
    pub fn split_at(&self, mid: usize) -> (&SliceCell<T>, &SliceCell<T>) {
        let (start, end) = self.as_raw_ref().split_at(mid);
        // SAFETY: the inputs come from the output of `SliceCell::as_raw_ref`, and `.split_at`
        unsafe { (Self::from_raw_ref(start), Self::from_raw_ref(end)) }
    }

    /// Divide one mutable `SliceCell` into two at an index.
    ///
    /// Panics if `mid > self.len()`.
    ///
    /// See [slice::split_at_mut]
    pub fn split_at_mut(&mut self, mid: usize) -> (&mut SliceCell<T>, &mut SliceCell<T>) {
        let (start, end) = self.as_raw_mut().split_at_mut(mid);
        // SAFETY: the inputs come from the output of `SliceCell::as_raw_mut`, and `.split_at_mut`
        unsafe { (Self::from_raw_mut(start), Self::from_raw_mut(end)) }
    }

    /// Returns the first and all the rest of the elements of the `SliceCell`, or None if it is empty.
    ///
    /// See [slice::split_first]
    #[cfg(feature = "assume_cell_layout")]
    pub fn split_first(&self) -> Option<(&Cell<T>, &SliceCell<T>)> {
        let (first, end) = self.as_raw_ref().split_first()?;
        Some((
            // SAFETY: TODO
            unsafe { &*(first as *const UnsafeCell<T> as *const Cell<T>) },
            // SAFETY: the inputs come from the output of `SliceCell::as_raw_mut`, and `.split_first`
            unsafe { Self::from_raw_ref(end) },
        ))
    }

    /// Returns the first and all the rest of the elements of the `SliceCell`, or None if it is empty.
    ///
    /// See [slice::split_first_mut]
    #[cfg(feature = "assume_cell_layout")]
    pub fn split_first_mut(&mut self) -> Option<(&mut T, &mut SliceCell<T>)> {
        let (first, end) = self.as_raw_mut().split_first_mut()?;
        Some((
            // SAFETY: TODO
            UnsafeCell::get_mut(first),
            // SAFETY: the inputs come from the output of `SliceCell::as_raw_mut`, and `.split_first_mut`.
            unsafe { Self::from_raw_mut(end) },
        ))
    }

    /// Returns the last and all the rest of the elements of the `SliceCell`, or None if it is empty.
    ///
    /// See [slice::split_last]
    #[cfg(feature = "assume_cell_layout")]
    pub fn split_last(&self) -> Option<(&Cell<T>, &SliceCell<T>)> {
        let (last, end) = self.as_raw_ref().split_last()?;
        Some((
            unsafe { &*(last as *const UnsafeCell<T> as *const Cell<T>) },
            // SAFETY: the inputs come from the output of `SliceCell::as_raw_ref`, and `.split_last`.
            unsafe { Self::from_raw_ref(end) },
        ))
    }

    /// Returns the last and all the rest of the elements of the `SliceCell`, or None if it is empty.
    ///
    /// See [slice::split_last_mut]
    #[cfg(feature = "assume_cell_layout")]
    pub fn split_last_mut(&mut self) -> Option<(&mut T, &mut SliceCell<T>)> {
        let (last, end) = self.as_raw_mut().split_last_mut()?;
        Some((
            // SAFETY: TODO
            UnsafeCell::get_mut(last),
            // SAFETY: the inputs come from the output of `SliceCell::as_raw_mut`, and `.split_last_mut`.
            unsafe { Self::from_raw_mut(end) },
        ))
    }

    /// Copies all elements from src into self, using a memcpy.
    ///
    /// The length of src must be the same as self.
    ///
    /// See [slice::copy_from_slice].
    pub fn copy_from_slice(&self, src: &[T])
    where
        T: Copy,
    {
        assert_eq!(self.len(), src.len());
        // SAFETY: TODO
        unsafe { &mut *self.as_ptr() }.copy_from_slice(src);
    }

    /// Clones all elements from src into self.
    ///
    /// The length of src must be the same as self.
    ///
    /// See [slice::copy_from_slice].
    #[cfg(feature = "assume_cell_layout")]
    pub fn clone_from_slice(&self, src: &[T])
    where
        T: Clone,
    {
        assert_eq!(self.len(), src.len());
        // T::clone and T::drop are arbitrary user code, so we can't use `self.inner.get().clone_from_slice()`
        for (dst, val) in self.iter().zip(src.iter().cloned()) {
            dst.set(val);
        }
    }

    /// Take all elements from this `SliceCell` into a mutable slice, leaving `T::default()` in each cell
    #[cfg(feature = "assume_cell_layout")]
    pub fn take_into_slice(&self, dst: &mut [T])
    where
        T: Default,
    {
        assert_eq!(self.len(), dst.len());
        // T::default and T::drop are arbitrary user code, so we can't hold a reference into self while it runs.
        for (src, dst) in self.iter().zip(dst.iter_mut()) {
            let val = src.take();
            *dst = val;
        }
    }

    /// Take all elements from this `SliceCell` into a newly allocated `Vec<T>`, leaving `T::default()` in each cell.
    #[cfg(all(feature = "assume_cell_layout", feature = "alloc"))]
    pub fn take_into_vec(&self) -> Vec<T>
    where
        T: Default,
    {
        self.iter().map(Cell::take).collect()
    }

    /// Copy all elements from this `SliceCell` into a mutable slice.
    pub fn copy_into_slice(&self, dst: &mut [T])
    where
        T: Copy,
    {
        assert_eq!(self.len(), dst.len());
        // SAFETY: `slice::copy_from_slice` uses a memcpy to copy the slice, and does nothing else, so it does not access`**self`
        // in an invalid way.
        dst.copy_from_slice(unsafe { &*self.inner.get() });
    }

    /// Copy all elements from this `SliceCell` into a newly allocated `Vec<T>`.
    #[cfg(feature = "alloc")]
    pub fn copy_into_vec(&self) -> Vec<T>
    where
        T: Copy,
    {
        let mut vec = Vec::with_capacity(self.len());
        // SAFETY: TODO
        unsafe {
            let dst = &mut vec.spare_capacity_mut()[..self.len()];
            core::ptr::copy_nonoverlapping(
                self.as_ptr() as *const T,
                dst.as_mut_ptr().cast(),
                self.len(),
            );
            vec.set_len(self.len());
        }
        vec
    }

    #[cfg(feature = "alloc")]
    pub fn replace_with_vec(&self, mut src: Vec<T>) {
        self.swap_with_slice(&mut src);
    }

    pub fn swap_with_slice(&self, val: &mut [T]) {
        assert_eq!(self.len(), val.len());
        // SAFETY: TODO
        unsafe { &mut *self.inner.get() }.swap_with_slice(val);
    }

    #[cfg(any())]
    pub fn swap_with_slicecell(&self, other: &Self) -> Result {
        todo!("decide what to do with overlapping slices. Probably just return an error?")
    }

    /// Swaps two elements in the slice.
    ///
    /// See [`<[T]>::swap`](slice::swap).
    pub fn swap(&self, a: usize, b: usize) {
        if a == b {
            return;
        }
        // SAFETY: TODO
        unsafe { &mut *self.inner.get() }.swap(a, b);
    }

    /// See [`<[T]>::rotate_left`](slice::rotate_left)
    pub fn rotate_left(&self, mid: usize) {
        // SAFETY: TODO
        unsafe { &mut *self.inner.get() }.rotate_left(mid)
    }

    /// See [`<[T]>::rotate_right`](slice::rotate_right)
    pub fn rotate_right(&self, mid: usize) {
        // SAFETY: TODO
        unsafe { &mut *self.inner.get() }.rotate_right(mid)
    }

    /// Fills self with elements by cloning value.
    ///
    /// See also: [`<[T]>::fill`](slice::fill).
    #[cfg(feature = "assume_cell_layout")]
    pub fn fill(&self, val: T)
    where
        T: Clone,
    {
        for dst in self {
            dst.set(val.clone());
        }
    }

    /// Fills self with elements returned by calling a closure repeatedly.
    ///
    /// See also: [`<[T]>::fill_with`](slice::fill_with).
    #[cfg(feature = "assume_cell_layout")]
    pub fn fill_with<F>(&self, mut f: F)
    where
        F: FnMut() -> T,
    {
        for dst in self {
            dst.set(f());
        }
    }

    #[cfg(feature = "rc")]
    /// Replacement for `TryFrom` impl, since `Rc` is not fundamental
    pub fn try_size_rc<const N: usize>(self: Rc<Self>) -> Result<Rc<ArrayCell<T, N>>, Rc<Self>> {
        if self.len() == N {
            Ok({
                let the_rc = self
                    .into_raw_rc()
                    .try_into()
                    .expect("already checked the length");
                // SAFETY: `the_rc` is the result of `SliceCell::into_raw_rc`.
                unsafe { ArrayCell::from_raw_rc(the_rc) }
            })
        } else {
            Err(self)
        }
    }

    /// N should not be 0.
    ///
    /// Splits the slice into a slice of N-element arrays, starting at the beginning of the slice, and a remainder slice with length strictly less than N.
    pub fn as_chunks<const N: usize>(&self) -> (&SliceCell<[T; N]>, &Self) {
        if N == 0 {
            (Default::default(), self)
        } else {
            let chunk_count = self.len() / N;
            let remainder = self.len() % N;
            let (chunks, remainder) = self.split_at(self.len() - remainder);
            let chunks: &[UnsafeCell<T>] = chunks.as_raw_ref();
            // SAFETY: TODO
            let chunks: &[UnsafeCell<[T; N]>] =
                unsafe { core::slice::from_raw_parts(chunks.as_ptr().cast(), chunk_count) };
            // SAFETY: TODO
            let chunks: &SliceCell<[T; N]> = unsafe { SliceCell::from_raw_ref(chunks) };
            (chunks, remainder)
        }
    }

    /// N should not be 0.
    ///
    /// Splits the slice into a slice of N-element arrays, starting at the end of the slice, and a remainder slice with length strictly less than N.
    pub fn as_rchunks<const N: usize>(&self) -> (&Self, &SliceCell<[T; N]>) {
        if N == 0 {
            (self, Default::default())
        } else {
            let chunk_count = self.len() / N;
            let remainder = self.len() % N;
            let (remainder, chunks) = self.split_at(remainder);
            let chunks: &[UnsafeCell<T>] = chunks.as_raw_ref();
            // SAFETY: TODO
            let chunks: &[UnsafeCell<[T; N]>] =
                unsafe { core::slice::from_raw_parts(chunks.as_ptr().cast(), chunk_count) };
            // SAFETY: TODO
            let chunks: &SliceCell<[T; N]> = unsafe { SliceCell::from_raw_ref(chunks) };
            (remainder, chunks)
        }
    }

    /// N should not be 0.
    ///
    /// Splits the slice into a slice of N-element arrays, starting at the beginning of the slice, and a remainder slice with length strictly less than N.
    pub fn as_chunks_mut<const N: usize>(&mut self) -> (&mut SliceCell<[T; N]>, &mut Self) {
        if N == 0 {
            (Default::default(), self)
        } else {
            let chunk_count = self.len() / N;
            let remainder = self.len() % N;
            let (chunks, remainder) = self.split_at_mut(self.len() - remainder);
            let chunks: &mut [UnsafeCell<T>] = chunks.as_raw_mut();
            // SAFETY: TODO
            let chunks: &mut [UnsafeCell<[T; N]>] =
                unsafe { core::slice::from_raw_parts_mut(chunks.as_mut_ptr().cast(), chunk_count) };
            // SAFETY: TODO
            let chunks: &mut SliceCell<[T; N]> = unsafe { SliceCell::from_raw_mut(chunks) };
            (chunks, remainder)
        }
    }

    /// N should not be 0.
    ///
    /// Splits the slice into a slice of N-element arrays, starting at the end of the slice, and a remainder slice with length strictly less than N.
    pub fn as_rchunks_mut<const N: usize>(&mut self) -> (&mut Self, &mut SliceCell<[T; N]>) {
        if N == 0 {
            (self, Default::default())
        } else {
            let chunk_count = self.len() / N;
            let remainder = self.len() % N;
            let (remainder, chunks) = self.split_at_mut(remainder);
            let chunks: &mut [UnsafeCell<T>] = chunks.as_raw_mut();
            // SAFETY: TODO
            let chunks: &mut [UnsafeCell<[T; N]>] =
            // SAFETY: TODO
            unsafe { core::slice::from_raw_parts_mut(chunks.as_mut_ptr().cast(), chunk_count) };
            let chunks: &mut SliceCell<[T; N]> = unsafe { SliceCell::from_raw_mut(chunks) };
            (remainder, chunks)
        }
    }

    #[cfg(feature = "assume_cell_layout")]
    pub fn iter(&self) -> core::slice::Iter<'_, Cell<T>> {
        IntoIterator::into_iter(self)
    }

    pub fn iter_mut(&mut self) -> core::slice::IterMut<'_, T> {
        self.get_mut().iter_mut()
    }
}

impl<T, const N: usize> SliceCell<[T; N]> {
    pub fn flatten(&self) -> &SliceCell<T> {
        let new_len = self.len().checked_mul(N).expect("size overflow");
        let this: &[UnsafeCell<[T; N]>] = self.as_raw_ref();
        // SAFETY: TODO
        let this: &[UnsafeCell<T>] =
            unsafe { core::slice::from_raw_parts(this.as_ptr().cast(), new_len) };
        // SAFETY: TODO
        let this: &SliceCell<T> = unsafe { SliceCell::from_raw_ref(this) };
        this
    }
    pub fn flatten_mut(&mut self) -> &mut SliceCell<T> {
        let new_len = self.len().checked_mul(N).expect("size overflow");
        let this: &mut [UnsafeCell<[T; N]>] = self.as_raw_mut();
        // SAFETY: TODO
        let this: &mut [UnsafeCell<T>] =
            unsafe { core::slice::from_raw_parts_mut(this.as_mut_ptr().cast(), new_len) };
        // SAFETY: TODO
        let this: &mut SliceCell<T> = unsafe { SliceCell::from_raw_mut(this) };
        this
    }
}

impl<T, I: SliceCellIndex<Self>> Index<I> for SliceCell<T> {
    type Output = <I as SliceCellIndex<Self>>::Output;

    fn index(&self, index: I) -> &Self::Output {
        index.get(self).unwrap()
    }
}

impl<T, I: SliceCellIndex<Self>> IndexMut<I> for SliceCell<T> {
    fn index_mut(&mut self, index: I) -> &mut Self::Output {
        index.get_mut(self).unwrap()
    }
}

impl<'a, T: 'a> Default for &'a mut ArrayCell<T, 0> {
    fn default() -> Self {
        // SAFETY: the array is empty
        unsafe { ArrayCell::from_raw_mut(&mut []) }
    }
}

impl<'a, T: 'a> Default for &'a ArrayCell<T, 0> {
    fn default() -> Self {
        // SAFETY: the array is empty
        unsafe { ArrayCell::from_raw_ref(&[]) }
    }
}

impl<'a, T: 'a> Default for &'a mut SliceCell<T> {
    fn default() -> Self {
        &mut <&mut ArrayCell<T, 0>>::default()[..]
    }
}

impl<'a, T: 'a> Default for &'a SliceCell<T> {
    fn default() -> Self {
        &<&ArrayCell<T, 0>>::default()[..]
    }
}

impl<'a, T, const N: usize> From<&'a ArrayCell<T, N>> for &'a SliceCell<T> {
    fn from(value: &'a ArrayCell<T, N>) -> Self {
        // SAFETY: the input to `SliceCell::from_raw_ref` is the result of `ArrayCell::into_raw_ref`.
        unsafe { SliceCell::from_raw_ref(value.as_raw_ref()) }
    }
}

impl<'a, T, const N: usize> From<&'a mut ArrayCell<T, N>> for &'a mut SliceCell<T> {
    fn from(value: &'a mut ArrayCell<T, N>) -> Self {
        // SAFETY: the input to `SliceCell::from_raw_mut` is the result of `ArrayCell::into_raw_mut`.
        unsafe { SliceCell::from_raw_mut(value.as_raw_mut()) }
    }
}

#[cfg(feature = "alloc")]
impl<'a, T, const N: usize> From<Box<ArrayCell<T, N>>> for Box<SliceCell<T>> {
    fn from(value: Box<ArrayCell<T, N>>) -> Self {
        // SAFETY: the input to `SliceCell::from_raw_boxed` is the result of `ArrayCell::into_raw_boxed`.
        unsafe { SliceCell::from_raw_boxed(value.into_raw_boxed()) }
    }
}

impl<'a, T, const N: usize> TryFrom<&'a SliceCell<T>> for &'a ArrayCell<T, N> {
    type Error = &'a SliceCell<T>;

    fn try_from(value: &'a SliceCell<T>) -> Result<Self, Self::Error> {
        if value.len() == N {
            Ok({
                let the_ref = value
                    .as_raw_ref()
                    .try_into()
                    .expect("already checked the length");
                // SAFETY: `the_ref` is the result of `SliceCell::as_raw_ref`.
                unsafe { ArrayCell::from_raw_ref(the_ref) }
            })
        } else {
            Err(value)
        }
    }
}

impl<'a, T, const N: usize> TryFrom<&'a mut SliceCell<T>> for &'a mut ArrayCell<T, N> {
    type Error = &'a mut SliceCell<T>;

    fn try_from(value: &'a mut SliceCell<T>) -> Result<Self, Self::Error> {
        if value.len() == N {
            Ok({
                let the_mut = value
                    .as_raw_mut()
                    .try_into()
                    .expect("already checked the length");
                // SAFETY: `the_mut` is the result of `SliceCell::as_raw_mut`.
                unsafe { ArrayCell::from_raw_mut(the_mut) }
            })
        } else {
            Err(value)
        }
    }
}

#[cfg(feature = "alloc")]
impl<'a, T, const N: usize> TryFrom<Box<SliceCell<T>>> for Box<ArrayCell<T, N>> {
    type Error = Box<SliceCell<T>>;

    fn try_from(value: Box<SliceCell<T>>) -> Result<Self, Self::Error> {
        if value.len() == N {
            Ok({
                let the_box = value
                    .into_raw_boxed()
                    .try_into()
                    .expect("already checked the length");
                // SAFETY: `the_box` is the result of `SliceCell::into_raw_box`.
                unsafe { ArrayCell::from_raw_boxed(the_box) }
            })
        } else {
            Err(value)
        }
    }
}

#[cfg(feature = "assume_cell_layout")]
impl<'a, T> IntoIterator for &'a SliceCell<T> {
    type Item = &'a Cell<T>;

    type IntoIter = core::slice::Iter<'a, Cell<T>>;

    fn into_iter(self) -> Self::IntoIter {
        self.as_std_transposed_ref().iter()
    }
}

#[cfg(feature = "assume_cell_layout")]
impl<'a, T, const N: usize> IntoIterator for &'a ArrayCell<T, N> {
    type Item = &'a Cell<T>;

    type IntoIter = core::slice::Iter<'a, Cell<T>>;

    fn into_iter(self) -> Self::IntoIter {
        self.as_std_transposed_ref().iter()
    }
}

impl<'a, T> IntoIterator for &'a mut SliceCell<T> {
    type Item = &'a mut T;

    type IntoIter = core::slice::IterMut<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.get_mut().iter_mut()
    }
}

impl<'a, T, const N: usize> IntoIterator for &'a mut ArrayCell<T, N> {
    type Item = &'a mut T;

    type IntoIter = core::slice::IterMut<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.get_mut().iter_mut()
    }
}