mitsein/
array1.rs

1//! Non-empty [arrays][`prim@array`].
2
3use core::borrow::{Borrow, BorrowMut};
4use core::num::NonZeroUsize;
5
6use crate::iter1::IntoIterator1;
7use crate::slice1::Slice1;
8#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
9use crate::sync1::ArcSlice1;
10#[cfg(feature = "alloc")]
11use crate::vec1::Vec1;
12
13#[diagnostic::on_unimplemented(
14    note = "a non-empty size, length, or capacity parameter may be zero",
15    note = "`Array1` is not yet implemented for all non-empty array types and non-zero constants"
16)]
17pub trait Array1:
18    AsMut<Slice1<Self::Item>>
19    + AsRef<Slice1<Self::Item>>
20    + Borrow<Slice1<Self::Item>>
21    + BorrowMut<Slice1<Self::Item>>
22    + IntoIterator1
23    + Sized
24{
25    const N: NonZeroUsize;
26
27    #[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
28    #[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", target_has_atomic = "ptr"))))]
29    fn into_arc_slice1(self) -> ArcSlice1<Self::Item>;
30
31    #[cfg(feature = "alloc")]
32    #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
33    fn into_vec1(self) -> Vec1<Self::Item> {
34        self.into_iter1().collect1()
35    }
36
37    fn as_slice1(&self) -> &Slice1<Self::Item>;
38
39    fn as_mut_slice1(&mut self) -> &mut Slice1<Self::Item>;
40}
41
42macro_rules! with_non_zero_array_size_literals {
43    ($f:ident$(,)?) => {
44        $crate::with_literals!(
45            $f,
46            [
47                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
48                24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
49                45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
50            ],
51        );
52    };
53}
54pub(crate) use with_non_zero_array_size_literals;
55
56/// # Safety
57///
58/// The literal `$N` must be non-zero.
59macro_rules! impl_array1_for_array {
60    ($N:literal) => {
61        impl<T> $crate::array1::Array1 for [T; $N] {
62            // SAFETY: The literal `$N` must non-zero.
63            const N: ::core::num::NonZeroUsize =
64                unsafe { ::core::num::NonZeroUsize::new_unchecked($N) };
65
66            #[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
67            #[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", target_has_atomic = "ptr"))))]
68            fn into_arc_slice1(self) -> $crate::sync1::ArcSlice1<Self::Item> {
69                use $crate::sync1::ArcSlice1Ext as _;
70
71                $crate::sync1::ArcSlice1::from_array1(self)
72            }
73
74            fn as_slice1(&self) -> &$crate::slice1::Slice1<T> {
75                self.as_ref()
76            }
77
78            fn as_mut_slice1(&mut self) -> &mut $crate::slice1::Slice1<T> {
79                self.as_mut()
80            }
81        }
82    };
83}
84with_non_zero_array_size_literals!(impl_array1_for_array);
85
86/// # Safety
87///
88/// The literal `$N` must be non-zero.
89macro_rules! impl_as_mut_for_array {
90    ($N:literal) => {
91        impl<T> ::core::convert::AsMut<$crate::slice1::Slice1<T>> for [T; $N] {
92            fn as_mut(&mut self) -> &mut $crate::slice1::Slice1<T> {
93                // SAFETY: `self` must be non-empty.
94                unsafe { $crate::slice1::Slice1::from_mut_slice_unchecked(self.as_mut_slice()) }
95            }
96        }
97    };
98}
99with_non_zero_array_size_literals!(impl_as_mut_for_array);
100
101/// # Safety
102///
103/// The literal `$N` must be non-zero.
104macro_rules! impl_as_ref_for_array {
105    ($N:literal) => {
106        impl<T> ::core::convert::AsRef<$crate::slice1::Slice1<T>> for [T; $N] {
107            fn as_ref(&self) -> &$crate::slice1::Slice1<T> {
108                // SAFETY: `self` must be non-empty.
109                unsafe { $crate::slice1::Slice1::from_slice_unchecked(self.as_slice()) }
110            }
111        }
112    };
113}
114with_non_zero_array_size_literals!(impl_as_ref_for_array);
115
116/// # Safety
117///
118/// The literal `$N` must be non-zero.
119macro_rules! impl_borrow_for_array {
120    ($N:literal) => {
121        impl<T> ::core::borrow::Borrow<$crate::slice1::Slice1<T>> for [T; $N] {
122            fn borrow(&self) -> &$crate::slice1::Slice1<T> {
123                // SAFETY: `self` must be non-empty.
124                unsafe { $crate::slice1::Slice1::from_slice_unchecked(self.as_slice()) }
125            }
126        }
127    };
128}
129with_non_zero_array_size_literals!(impl_borrow_for_array);
130
131/// # Safety
132///
133/// The literal `$N` must be non-zero.
134macro_rules! impl_borrow_mut_for_array {
135    ($N:literal) => {
136        impl<T> ::core::borrow::BorrowMut<$crate::slice1::Slice1<T>> for [T; $N] {
137            fn borrow_mut(&mut self) -> &mut $crate::slice1::Slice1<T> {
138                // SAFETY: `self` must be non-empty.
139                unsafe { $crate::slice1::Slice1::from_mut_slice_unchecked(self.as_mut_slice()) }
140            }
141        }
142    };
143}
144with_non_zero_array_size_literals!(impl_borrow_mut_for_array);
145
146/// # Safety
147///
148/// The literal `$N` must be non-zero.
149macro_rules! impl_into_iterator1_for_array {
150    ($N:literal) => {
151        impl<T> $crate::iter1::IntoIterator1 for [T; $N] {
152            fn into_iter1(self) -> $crate::iter1::Iterator1<Self::IntoIter> {
153                // SAFETY: `self` must be non-empty.
154                unsafe { $crate::iter1::Iterator1::from_iter_unchecked(self) }
155            }
156        }
157
158        impl<T> $crate::iter1::IntoIterator1 for &'_ [T; $N] {
159            fn into_iter1(self) -> $crate::iter1::Iterator1<Self::IntoIter> {
160                self.as_slice1().iter1()
161            }
162        }
163
164        impl<T> $crate::iter1::IntoIterator1 for &'_ mut [T; $N] {
165            fn into_iter1(self) -> $crate::iter1::Iterator1<Self::IntoIter> {
166                self.as_mut_slice1().iter1_mut()
167            }
168        }
169    };
170}
171with_non_zero_array_size_literals!(impl_into_iterator1_for_array);