1#![no_std]
2#![cfg_attr(feature = "nightly_docs", feature(doc_cfg))]
3
4#[cfg(feature = "alloc")]
5extern crate alloc;
6#[cfg(feature = "std")]
7extern crate std;
8
9#[cfg(feature = "rc")]
10use alloc::rc::Rc;
11#[cfg(feature = "alloc")]
12use alloc::{boxed::Box, vec::Vec};
13use core::cell::Cell;
14use core::cell::UnsafeCell;
15use core::ops::Index;
16use core::ops::IndexMut;
17use index::SliceCellIndex;
18
19mod index;
28#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "std")))]
29#[cfg(feature = "std")]
30pub mod io;
31
32#[repr(transparent)]
38pub struct ArrayCell<T, const N: usize> {
39 inner: UnsafeCell<[T; N]>,
40}
41
42unsafe impl<T: Send, const N: usize> Send for ArrayCell<T, N> {}
45
46#[repr(transparent)]
52pub struct SliceCell<T> {
53 inner: UnsafeCell<[T]>,
54}
55
56unsafe impl<T: Send> Send for SliceCell<T> {}
59
60impl<T, const N: usize> ArrayCell<T, N> {
61 pub const fn as_std_ref(&self) -> &Cell<[T; N]> {
63 unsafe { &*(self as *const Self).cast() }
73 }
74 pub const fn as_std_transposed_ref(&self) -> &[Cell<T>; N] {
76 unsafe { &*(self as *const Self).cast() }
78 }
79 pub const fn from_std_ref(std: &Cell<[T; N]>) -> &Self {
81 unsafe { &*(std as *const Cell<[T; N]>).cast() }
83 }
84 pub const fn from_std_transposed_ref(std: &[Cell<T>; N]) -> &Self {
86 unsafe { &*(std as *const [Cell<T>; N]).cast() }
88 }
89 pub fn as_std_mut(&mut self) -> &mut Cell<[T; N]> {
91 unsafe { &mut *(self as *mut Self).cast() }
93 }
94 pub fn as_std_transposed_mut(&mut self) -> &mut [Cell<T>; N] {
96 unsafe { &mut *(self as *mut Self).cast() }
98 }
99 pub fn from_std_mut(std: &mut Cell<[T; N]>) -> &mut Self {
101 unsafe { &mut *(std as *mut Cell<[T; N]>).cast() }
103 }
104 pub fn from_std_transposed_mut(std: &mut [Cell<T>; N]) -> &mut Self {
106 unsafe { &mut *(std as *mut [Cell<T>; N]).cast() }
108 }
109}
110
111#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "alloc")))]
112#[cfg(feature = "alloc")]
113impl<T, const N: usize> ArrayCell<T, N> {
114 pub fn into_std_boxed(self: Box<Self>) -> Box<Cell<[T; N]>> {
116 unsafe { Box::from_raw(Box::into_raw(self).cast()) }
118 }
119 pub fn into_std_transposed_boxed(self: Box<Self>) -> Box<[Cell<T>; N]> {
121 unsafe { Box::from_raw(Box::into_raw(self).cast()) }
123 }
124 pub fn from_std_boxed(std: Box<Cell<[T; N]>>) -> Box<Self> {
126 unsafe { Box::from_raw(Box::into_raw(std).cast()) }
128 }
129 pub fn from_std_transposed_boxed(std: Box<[Cell<T>; N]>) -> Box<Self> {
131 unsafe { Box::from_raw(Box::into_raw(std).cast()) }
133 }
134
135 pub fn new_boxed(inner: Box<[T; N]>) -> Box<Self> {
137 unsafe { Box::from_raw(Box::into_raw(inner).cast()) }
139 }
140
141 pub fn into_inner_boxed(self: Box<Self>) -> Box<[T; N]> {
143 unsafe { Box::from_raw(Box::into_raw(self).cast()) }
145 }
146}
147
148#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "rc")))]
149#[cfg(feature = "rc")]
150impl<T, const N: usize> ArrayCell<T, N> {
151 pub fn into_std_rc(self: Rc<Self>) -> Rc<Cell<[T; N]>> {
153 unsafe { Rc::from_raw(Rc::into_raw(self).cast()) }
155 }
156 pub fn into_std_transposed_rc(self: Rc<Self>) -> Rc<[Cell<T>; N]> {
158 unsafe { Rc::from_raw(Rc::into_raw(self).cast()) }
160 }
161 pub fn from_std_rc(std: Rc<Cell<[T; N]>>) -> Rc<Self> {
163 unsafe { Rc::from_raw(Rc::into_raw(std).cast()) }
165 }
166 pub fn from_std_transposed_rc(std: Rc<[Cell<T>; N]>) -> Rc<Self> {
168 unsafe { Rc::from_raw(Rc::into_raw(std).cast()) }
170 }
171
172 pub fn try_new_rc(mut inner: Rc<[T; N]>) -> Result<Rc<Self>, Rc<[T; N]>> {
174 match Rc::get_mut(&mut inner) {
175 Some(_unique) => Ok(unsafe { Rc::from_raw(Rc::into_raw(inner).cast()) }),
179 None => Err(inner),
180 }
181 }
182
183 pub fn try_into_inner_rc(mut self: Rc<Self>) -> Result<Rc<[T; N]>, Rc<Self>> {
185 match Rc::get_mut(&mut self) {
186 Some(_unique) => Ok(unsafe { Rc::from_raw(Rc::into_raw(self).cast()) }),
190 None => Err(self),
191 }
192 }
193
194 pub fn unsize_rc(self: Rc<Self>) -> Rc<SliceCell<T>> {
198 SliceCell::from_std_transposed_rc(ArrayCell::into_std_transposed_rc(self))
199 }
200}
201
202impl<T, const N: usize> ArrayCell<T, N> {
203 pub fn as_ptr(&self) -> *mut [T; N] {
205 UnsafeCell::raw_get(&self.inner).cast()
206 }
207
208 pub fn into_inner(self) -> [T; N] {
210 self.inner.into_inner()
211 }
212
213 pub fn new(inner: [T; N]) -> Self {
215 Self {
216 inner: UnsafeCell::new(inner),
217 }
218 }
219
220 #[doc(alias = "get_mut")]
222 pub fn as_mut(&mut self) -> &mut [T; N] {
223 self.inner.get_mut()
224 }
225
226 pub fn from_mut(inner: &mut [T; N]) -> &mut Self {
228 unsafe { &mut *(inner as *mut [T; N]).cast() }
229 }
230
231 pub fn get<I: SliceCellIndex<Self>>(&self, idx: I) -> Option<&I::Output> {
235 idx.get(self)
236 }
237
238 pub fn get_mut<I: SliceCellIndex<Self>>(&mut self, idx: I) -> Option<&mut I::Output> {
242 idx.get_mut(self)
243 }
244
245 pub const fn len(&self) -> usize {
247 N
248 }
249}
250
251impl<T, const N: usize> AsRef<SliceCell<T>> for ArrayCell<T, N> {
252 fn as_ref(&self) -> &SliceCell<T> {
253 SliceCell::from_std_ref(self.as_std_ref())
254 }
255}
256
257impl<T, const N: usize> AsMut<SliceCell<T>> for ArrayCell<T, N> {
258 fn as_mut(&mut self) -> &mut SliceCell<T> {
259 SliceCell::from_mut(self.as_mut())
260 }
261}
262
263impl<T> AsRef<SliceCell<T>> for SliceCell<T> {
264 fn as_ref(&self) -> &SliceCell<T> {
265 self
266 }
267}
268
269impl<T> AsMut<SliceCell<T>> for SliceCell<T> {
270 fn as_mut(&mut self) -> &mut SliceCell<T> {
271 self
272 }
273}
274
275impl<T> SliceCell<T> {
276 pub const fn as_std_ref(&self) -> &Cell<[T]> {
278 unsafe { &*(self as *const Self as *const _) }
280 }
281 pub const fn as_std_transposed_ref(&self) -> &[Cell<T>] {
283 unsafe { &*(self as *const Self as *const _) }
285 }
286 pub const fn from_std_ref(std: &Cell<[T]>) -> &Self {
288 unsafe { &*(std as *const Cell<[T]> as *const _) }
290 }
291 pub const fn from_std_transposed_ref(std: &[Cell<T>]) -> &Self {
293 unsafe { &*(std as *const [Cell<T>] as *const _) }
295 }
296 pub fn as_std_mut(&mut self) -> &mut Cell<[T]> {
298 unsafe { &mut *(self as *mut Self as *mut _) }
300 }
301 pub fn as_std_transposed_mut(&mut self) -> &mut [Cell<T>] {
303 unsafe { &mut *(self as *mut Self as *mut _) }
305 }
306 pub fn from_std_mut(std: &mut Cell<[T]>) -> &mut Self {
308 unsafe { &mut *(std as *mut Cell<[T]> as *mut _) }
310 }
311 pub fn from_std_transposed_mut(std: &mut [Cell<T>]) -> &mut Self {
313 unsafe { &mut *(std as *mut [Cell<T>] as *mut _) }
315 }
316}
317
318#[cfg(feature = "alloc")]
319impl<T> SliceCell<T> {
320 pub fn into_std_boxed(self: Box<Self>) -> Box<Cell<[T]>> {
322 unsafe { Box::from_raw(Box::into_raw(self) as *mut _) }
324 }
325 pub fn into_std_transposed_boxed(self: Box<Self>) -> Box<[Cell<T>]> {
327 unsafe { Box::from_raw(Box::into_raw(self) as *mut _) }
329 }
330 pub fn from_std_boxed(std: Box<Cell<[T]>>) -> Box<Self> {
332 unsafe { Box::from_raw(Box::into_raw(std) as *mut _) }
334 }
335 pub fn from_std_transposed_boxed(std: Box<[Cell<T>]>) -> Box<Self> {
337 unsafe { Box::from_raw(Box::into_raw(std) as *mut _) }
339 }
340
341 pub fn into_inner_boxed(self: Box<Self>) -> Box<[T]> {
343 unsafe { Box::from_raw(Box::into_raw(self) as *mut _) }
345 }
346
347 pub fn new_boxed(inner: Box<[T]>) -> Box<Self> {
349 unsafe { Box::from_raw(Box::into_raw(inner) as *mut _) }
350 }
351}
352
353#[cfg(feature = "rc")]
354impl<T> SliceCell<T> {
355 pub fn into_std_rc(self: Rc<Self>) -> Rc<Cell<[T]>> {
357 unsafe { Rc::from_raw(Rc::into_raw(self) as *const _) }
359 }
360 pub fn into_std_transposed_rc(self: Rc<Self>) -> Rc<[Cell<T>]> {
362 unsafe { Rc::from_raw(Rc::into_raw(self) as *const _) }
364 }
365 pub fn from_std_rc(std: Rc<Cell<[T]>>) -> Rc<Self> {
367 unsafe { Rc::from_raw(Rc::into_raw(std) as *const _) }
369 }
370 pub fn from_std_transposed_rc(std: Rc<[Cell<T>]>) -> Rc<Self> {
371 unsafe { Rc::from_raw(Rc::into_raw(std) as *const _) }
373 }
374
375 pub fn try_new_rc(mut inner: Rc<[T]>) -> Result<Rc<Self>, Rc<[T]>> {
377 match Rc::get_mut(&mut inner) {
378 Some(_unique) => Ok(unsafe { Rc::from_raw(Rc::into_raw(inner) as *const _) }),
382 None => Err(inner),
383 }
384 }
385
386 pub fn try_into_inner_rc(mut self: Rc<Self>) -> Result<Rc<[T]>, Rc<Self>> {
388 match Rc::get_mut(&mut self) {
389 Some(_unique) => Ok(unsafe { Rc::from_raw(Rc::into_raw(self) as *const _) }),
393 None => Err(self),
394 }
395 }
396
397 pub fn try_size_rc<const N: usize>(self: Rc<Self>) -> Result<Rc<ArrayCell<T, N>>, Rc<Self>> {
399 if self.len() == N {
400 Ok({
401 let the_rc = self
402 .into_std_transposed_rc()
403 .try_into()
404 .ok()
405 .expect("already checked the length");
406 ArrayCell::from_std_transposed_rc(the_rc)
407 })
408 } else {
409 Err(self)
410 }
411 }
412}
413
414impl<T> SliceCell<T> {
415 pub fn as_ptr(&self) -> *mut [T] {
417 UnsafeCell::raw_get(&self.inner)
418 }
419
420 #[doc(alias = "get_mut")]
422 pub fn as_mut(&mut self) -> &mut [T] {
423 self.inner.get_mut()
424 }
425
426 pub fn from_mut(inner: &mut [T]) -> &mut Self {
428 unsafe { &mut *(inner as *mut [T] as *mut _) }
432 }
433
434 pub fn get<I: SliceCellIndex<Self>>(&self, idx: I) -> Option<&I::Output> {
438 idx.get(self)
439 }
440
441 pub fn get_mut<I: SliceCellIndex<Self>>(&mut self, idx: I) -> Option<&mut I::Output> {
445 idx.get_mut(self)
446 }
447
448 pub const fn len(&self) -> usize {
450 self.as_std_transposed_ref().len()
451 }
452
453 pub fn split_at(&self, mid: usize) -> (&SliceCell<T>, &SliceCell<T>) {
459 let (start, end) = self.as_std_transposed_ref().split_at(mid);
460 (
461 Self::from_std_transposed_ref(start),
462 Self::from_std_transposed_ref(end),
463 )
464 }
465
466 pub fn split_at_mut(&mut self, mid: usize) -> (&mut SliceCell<T>, &mut SliceCell<T>) {
472 let (start, end) = self.as_mut().split_at_mut(mid);
473 (Self::from_mut(start), Self::from_mut(end))
474 }
475
476 pub fn split_first(&self) -> Option<(&Cell<T>, &SliceCell<T>)> {
480 let (first, end) = self.as_std_transposed_ref().split_first()?;
481 Some((first, Self::from_std_transposed_ref(end)))
482 }
483
484 pub fn split_first_mut(&mut self) -> Option<(&mut T, &mut SliceCell<T>)> {
488 let (first, end) = self.as_mut().split_first_mut()?;
489 Some((first, Self::from_mut(end)))
490 }
491
492 pub fn split_last(&self) -> Option<(&Cell<T>, &SliceCell<T>)> {
496 let (last, start) = self.as_std_transposed_ref().split_last()?;
497 Some((last, Self::from_std_transposed_ref(start)))
498 }
499
500 pub fn split_last_mut(&mut self) -> Option<(&mut T, &mut SliceCell<T>)> {
504 let (last, start) = self.as_mut().split_last_mut()?;
505 Some((last, Self::from_mut(start)))
506 }
507
508 pub fn copy_from_slice(&self, src: &[T])
514 where
515 T: Copy,
516 {
517 assert!(self.len() == src.len());
518 self.iter().zip(src.iter()).for_each(|(dst, src)| {
520 dst.set(*src);
521 });
522 }
523
524 pub fn copy_from(&self, src: &SliceCell<T>)
530 where
531 T: Copy,
532 {
533 assert!(self.len() == src.len());
534 unsafe {
538 std::ptr::copy(
539 src.as_ptr() as *const T,
540 self.as_ptr() as *mut T,
541 self.len(),
542 );
543 }
544 }
545
546 pub fn clone_from_slice(&self, src: &[T])
552 where
553 T: Clone,
554 {
555 assert!(self.len() == src.len());
556 for (dst, val) in self.iter().zip(src.iter().cloned()) {
558 dst.set(val);
559 }
560 }
561
562 pub fn take_into_slice(&self, dst: &mut [T])
564 where
565 T: Default,
566 {
567 assert!(self.len() == dst.len());
568 for (src, dst) in self.iter().zip(dst.iter_mut()) {
570 let val = src.take();
571 *dst = val;
572 }
573 }
574
575 #[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "alloc")))]
577 #[cfg(feature = "alloc")]
578 pub fn take_into_vec(&self) -> Vec<T>
579 where
580 T: Default,
581 {
582 self.iter().map(Cell::take).collect()
583 }
584
585 pub fn copy_into_slice(&self, dst: &mut [T])
587 where
588 T: Copy,
589 {
590 assert!(self.len() == dst.len());
591 self.iter().zip(dst.iter_mut()).for_each(|(src, dst)| {
593 *dst = src.get();
594 });
595 }
596
597 #[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "alloc")))]
599 #[cfg(feature = "alloc")]
600 pub fn copy_into_vec(&self) -> Vec<T>
601 where
602 T: Copy,
603 {
604 self.iter().map(Cell::get).collect()
606 }
607
608 #[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "alloc")))]
609 #[cfg(feature = "alloc")]
610 pub fn replace_with_vec(&self, mut src: Vec<T>) {
611 self.swap_with_slice(&mut src);
612 }
613
614 pub fn swap_with_slice(&self, val: &mut [T]) {
615 assert_eq!(self.len(), val.len());
616 unsafe { &mut *self.inner.get() }.swap_with_slice(val);
619 }
620
621 #[cfg(any())]
622 pub fn swap_with_slicecell(&self, other: &Self) -> Result {
623 todo!("decide what to do with overlapping slices. Probably just return an error?")
624 }
625
626 pub fn swap(&self, a: usize, b: usize) {
630 if a == b {
631 return;
632 }
633 let a = &self[a];
634 let b = &self[b];
635 Cell::swap(a, b)
636 }
637
638 pub fn rotate_left(&self, mid: usize) {
640 unsafe { &mut *self.inner.get() }.rotate_left(mid)
643 }
644
645 pub fn rotate_right(&self, mid: usize) {
647 unsafe { &mut *self.inner.get() }.rotate_right(mid)
650 }
651
652 pub fn fill(&self, val: T)
656 where
657 T: Clone,
658 {
659 for dst in self {
660 dst.set(val.clone());
661 }
662 }
663
664 pub fn fill_with<F>(&self, mut f: F)
668 where
669 F: FnMut() -> T,
670 {
671 for dst in self {
672 dst.set(f());
673 }
674 }
675
676 pub fn as_chunks<const N: usize>(&self) -> (&SliceCell<[T; N]>, &Self) {
680 if N == 0 {
681 (Default::default(), self)
682 } else {
683 let chunk_count = self.len() / N;
684 let remainder = self.len() % N;
685 let (chunks, remainder) = self.split_at(self.len() - remainder);
686 let chunks: &[Cell<T>] = chunks.as_std_transposed_ref();
687 let chunks: &[Cell<[T; N]>] =
691 unsafe { core::slice::from_raw_parts(chunks.as_ptr().cast(), chunk_count) };
692 let chunks: &SliceCell<[T; N]> = SliceCell::from_std_transposed_ref(chunks);
693 (chunks, remainder)
694 }
695 }
696
697 pub fn as_rchunks<const N: usize>(&self) -> (&Self, &SliceCell<[T; N]>) {
701 if N == 0 {
702 (self, Default::default())
703 } else {
704 let chunk_count = self.len() / N;
705 let remainder = self.len() % N;
706 let (remainder, chunks) = self.split_at(remainder);
707 let chunks: &[Cell<T>] = chunks.as_std_transposed_ref();
708 let chunks: &[Cell<[T; N]>] =
712 unsafe { core::slice::from_raw_parts(chunks.as_ptr().cast(), chunk_count) };
713 let chunks: &SliceCell<[T; N]> = SliceCell::from_std_transposed_ref(chunks);
714 (remainder, chunks)
715 }
716 }
717
718 pub fn as_chunks_mut<const N: usize>(&mut self) -> (&mut SliceCell<[T; N]>, &mut Self) {
722 if N == 0 {
723 (Default::default(), self)
724 } else {
725 let chunk_count = self.len() / N;
726 let remainder = self.len() % N;
727 let (chunks, remainder) = self.split_at_mut(self.len() - remainder);
728 let chunks: &mut [T] = chunks.as_mut();
729 let chunks: &mut [[T; N]] =
731 unsafe { core::slice::from_raw_parts_mut(chunks.as_mut_ptr().cast(), chunk_count) };
732 let chunks: &mut SliceCell<[T; N]> = SliceCell::from_mut(chunks);
733 (chunks, remainder)
734 }
735 }
736
737 pub fn as_rchunks_mut<const N: usize>(&mut self) -> (&mut Self, &mut SliceCell<[T; N]>) {
741 if N == 0 {
742 (self, Default::default())
743 } else {
744 let chunk_count = self.len() / N;
745 let remainder = self.len() % N;
746 let (remainder, chunks) = self.split_at_mut(remainder);
747 let chunks: &mut [T] = chunks.as_mut();
748 let chunks: &mut [[T; N]] =
750 unsafe { core::slice::from_raw_parts_mut(chunks.as_mut_ptr().cast(), chunk_count) };
751 let chunks: &mut SliceCell<[T; N]> = SliceCell::from_mut(chunks);
752 (remainder, chunks)
753 }
754 }
755
756 pub fn iter(&self) -> core::slice::Iter<'_, Cell<T>> {
757 IntoIterator::into_iter(self)
758 }
759
760 pub fn iter_mut(&mut self) -> core::slice::IterMut<'_, T> {
761 self.as_mut().iter_mut()
762 }
763}
764
765impl<T, const N: usize> SliceCell<[T; N]> {
766 pub fn flatten(&self) -> &SliceCell<T> {
774 let new_len = self.len().checked_mul(N).expect("length overflow");
775 let this: &[Cell<[T; N]>] = self.as_std_transposed_ref();
776 let this: &[Cell<T>] =
780 unsafe { core::slice::from_raw_parts(this.as_ptr().cast(), new_len) };
781 let this: &SliceCell<T> = SliceCell::from_std_transposed_ref(this);
782 this
783 }
784
785 pub fn flatten_mut(&mut self) -> &mut SliceCell<T> {
793 let new_len = self.len().checked_mul(N).expect("length overflow");
794 let this: &mut [[T; N]] = self.as_mut();
795 let this: &mut [T] =
797 unsafe { core::slice::from_raw_parts_mut(this.as_mut_ptr().cast(), new_len) };
798 let this: &mut SliceCell<T> = SliceCell::from_mut(this);
799 this
800 }
801
802 #[cfg(any())]
804 pub fn as_slice_of_arraycells(&self) -> &[ArrayCell<T, N>] {
805 todo!()
806 }
807}
808
809impl<T, I: SliceCellIndex<Self>> Index<I> for SliceCell<T> {
810 type Output = <I as SliceCellIndex<Self>>::Output;
811
812 fn index(&self, index: I) -> &Self::Output {
813 index.get(self).unwrap()
814 }
815}
816
817impl<T, I: SliceCellIndex<Self>> IndexMut<I> for SliceCell<T> {
818 fn index_mut(&mut self, index: I) -> &mut Self::Output {
819 index.get_mut(self).unwrap()
820 }
821}
822
823impl<'a, T: 'a> Default for &'a mut ArrayCell<T, 0> {
824 fn default() -> Self {
825 ArrayCell::from_mut(&mut [])
826 }
827}
828
829impl<'a, T: 'a> Default for &'a ArrayCell<T, 0> {
830 fn default() -> Self {
831 ArrayCell::from_mut(&mut [])
832 }
833}
834
835impl<'a, T: 'a> Default for &'a mut SliceCell<T> {
836 fn default() -> Self {
837 SliceCell::from_mut(&mut [])
838 }
839}
840
841impl<'a, T: 'a> Default for &'a SliceCell<T> {
842 fn default() -> Self {
843 SliceCell::from_mut(&mut [])
844 }
845}
846
847impl<'a, T, const N: usize> From<&'a ArrayCell<T, N>> for &'a SliceCell<T> {
848 fn from(value: &'a ArrayCell<T, N>) -> Self {
849 SliceCell::from_std_ref(value.as_std_ref())
850 }
851}
852
853impl<'a, T, const N: usize> From<&'a mut ArrayCell<T, N>> for &'a mut SliceCell<T> {
854 fn from(value: &'a mut ArrayCell<T, N>) -> Self {
855 SliceCell::from_mut(value.as_mut())
856 }
857}
858
859#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "alloc")))]
860#[cfg(feature = "alloc")]
861impl<'a, T, const N: usize> From<Box<ArrayCell<T, N>>> for Box<SliceCell<T>> {
862 fn from(value: Box<ArrayCell<T, N>>) -> Self {
863 SliceCell::from_std_boxed(value.into_std_boxed())
864 }
865}
866
867impl<'a, T, const N: usize> TryFrom<&'a SliceCell<T>> for &'a ArrayCell<T, N> {
868 type Error = &'a SliceCell<T>;
869
870 fn try_from(value: &'a SliceCell<T>) -> Result<Self, Self::Error> {
871 if value.len() == N {
872 Ok({
873 let the_ref = value
874 .as_std_transposed_ref()
875 .try_into()
876 .expect("already checked the length");
877 ArrayCell::from_std_transposed_ref(the_ref)
878 })
879 } else {
880 Err(value)
881 }
882 }
883}
884
885impl<'a, T, const N: usize> TryFrom<&'a mut SliceCell<T>> for &'a mut ArrayCell<T, N> {
886 type Error = &'a mut SliceCell<T>;
887
888 fn try_from(value: &'a mut SliceCell<T>) -> Result<Self, Self::Error> {
889 if value.len() == N {
890 Ok({
891 let the_mut = value
892 .as_mut()
893 .try_into()
894 .expect("already checked the length");
895 ArrayCell::from_mut(the_mut)
896 })
897 } else {
898 Err(value)
899 }
900 }
901}
902
903#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "alloc")))]
904#[cfg(feature = "alloc")]
905impl<'a, T, const N: usize> TryFrom<Box<SliceCell<T>>> for Box<ArrayCell<T, N>> {
906 type Error = Box<SliceCell<T>>;
907
908 fn try_from(value: Box<SliceCell<T>>) -> Result<Self, Self::Error> {
909 if value.len() == N {
910 Ok({
911 let the_box = value
912 .into_std_transposed_boxed()
913 .try_into()
914 .ok()
915 .expect("already checked the length");
916 ArrayCell::from_std_transposed_boxed(the_box)
917 })
918 } else {
919 Err(value)
920 }
921 }
922}
923
924impl<'a, T> IntoIterator for &'a SliceCell<T> {
925 type Item = &'a Cell<T>;
926
927 type IntoIter = core::slice::Iter<'a, Cell<T>>;
928
929 fn into_iter(self) -> Self::IntoIter {
930 self.as_std_transposed_ref().iter()
931 }
932}
933
934impl<'a, T, const N: usize> IntoIterator for &'a ArrayCell<T, N> {
935 type Item = &'a Cell<T>;
936
937 type IntoIter = core::slice::Iter<'a, Cell<T>>;
938
939 fn into_iter(self) -> Self::IntoIter {
940 self.as_std_transposed_ref().iter()
941 }
942}
943
944impl<'a, T> IntoIterator for &'a mut SliceCell<T> {
945 type Item = &'a mut T;
946
947 type IntoIter = core::slice::IterMut<'a, T>;
948
949 fn into_iter(self) -> Self::IntoIter {
950 self.as_mut().iter_mut()
951 }
952}
953
954impl<'a, T, const N: usize> IntoIterator for &'a mut ArrayCell<T, N> {
955 type Item = &'a mut T;
956
957 type IntoIter = core::slice::IterMut<'a, T>;
958
959 fn into_iter(self) -> Self::IntoIter {
960 self.as_mut().iter_mut()
961 }
962}