konst/
slice.rs

1//! `const fn` equivalents of slice methods.
2//!
3//! # Removed in 0.3.0
4//!
5//! These functions were removed in 0.3.0 because there is an equivalent
6//! const fn in the standard library:
7//!
8//! - `first`
9//! - `last`
10//! - `split_first`
11//! - `split_last`
12//!
13//!
14//!
15
16/// `const fn`s for comparing slices for equality and ordering.
17#[cfg(feature = "cmp")]
18#[cfg_attr(feature = "docsrs", doc(cfg(feature = "cmp")))]
19pub mod cmp;
20
21mod bytes_pattern;
22mod slice_as_chunks;
23mod slice_concatenation;
24mod slice_const_methods;
25
26#[cfg(feature = "iter")]
27mod slice_iter_methods;
28
29pub use bytes_pattern::BytesPattern;
30
31pub(crate) use bytes_pattern::PatternNorm;
32
33pub use self::slice_as_chunks::*;
34pub use self::slice_concatenation::*;
35pub use self::slice_const_methods::*;
36
37#[cfg(feature = "iter")]
38pub use slice_iter_methods::*;
39
40__declare_slice_cmp_fns! {
41    import_path = "konst",
42
43    (
44        ///
45        ///  # Example
46        ///
47        ,
48        /// ```rust
49        /// use konst::slice::eq_bytes;
50        ///
51        /// const FOO: &[u8] = b"foo";
52        /// const BAR: &[u8] = b"fooooo";
53        /// const BAZ: &[u8] = b"bar";
54        ///
55        ///
56        /// const FOO_EQ_FOO: bool = eq_bytes(FOO, FOO);
57        /// assert!( FOO_EQ_FOO );
58        ///
59        /// const FOO_EQ_BAR: bool = eq_bytes(FOO, BAR);
60        /// assert!( !FOO_EQ_BAR );
61        ///
62        /// const FOO_EQ_BAZ: bool = eq_bytes(FOO, BAZ);
63        /// assert!( !FOO_EQ_BAZ );
64        ///
65        /// ```
66        ///
67        ,
68        /// ```rust
69        /// use konst::slice::cmp_bytes;
70        ///
71        /// use std::cmp::Ordering;
72        ///
73        /// const FOO: &[u8] = b"foo";
74        /// const BAR: &[u8] = b"fooooo";
75        /// const BAZ: &[u8] = b"bar";
76        ///
77        ///
78        /// const FOO_CMP_FOO: Ordering = cmp_bytes(FOO, FOO);
79        /// assert_eq!(FOO_CMP_FOO, Ordering::Equal);
80        ///
81        /// const FOO_CMP_BAR: Ordering = cmp_bytes(FOO, BAR);
82        /// assert_eq!(FOO_CMP_BAR, Ordering::Less);
83        ///
84        /// const FOO_CMP_BAZ: Ordering = cmp_bytes(FOO, BAZ);
85        /// assert_eq!(FOO_CMP_BAZ, Ordering::Greater);
86        ///
87        /// ```
88        ///
89        ,
90        u8,
91        eq_bytes,
92        cmp_bytes,
93    )
94}
95
96__declare_fns_with_docs! {
97    (Option<&'a [u8]>, (eq_option_bytes, cmp_option_bytes))
98
99    docs(default)
100
101    macro = __impl_option_cmp_fns!(
102        for['a,]
103        params(l, r)
104        eq_comparison = eq_bytes(l, r),
105        cmp_comparison = cmp_bytes(l, r),
106        parameter_copyability = copy,
107    ),
108}
109
110/// The error produced by trying to convert from
111/// `&[T]` to `&[T; N]`, or from `&mut [T]` to `&mut [T; N]`.
112#[doc(inline)]
113pub use konst_kernel::slice::slice_for_konst::TryIntoArrayError;
114
115/// Tries to convert from `&[T]` to `&[T; N]`.
116///
117/// Returns an `Err(TryIntoArrayError{..})` when the slice doesn't match the expected length.
118///
119/// # Example
120///
121/// ```rust
122/// use konst::{
123///     slice::{TryIntoArrayError, try_into_array},
124///     result,
125///     unwrap_ctx,
126/// };
127///
128///
129/// const fn arr_5() -> Option<&'static [u64; 5]> {
130///     let slice: &[u64] = &[1, 10, 100, 1000, 10000];
131///
132///     // Passing the length explicitly to the function
133///     result::ok!(try_into_array::<_, 5>(slice))
134/// }
135///
136/// assert_eq!(arr_5(), Some(&[1, 10, 100, 1000, 10000]));
137///
138///
139/// const fn err() -> Result<&'static [u64; 5], TryIntoArrayError> {
140///     let slice: &[u64] = &[];
141///
142///     // Letting the function infer the length of the array,
143///     try_into_array(slice)
144/// }
145///
146/// assert!(err().is_err());
147///
148///
149/// const fn arr_3() -> &'static [u64; 3] {
150///     let slice: &[u64] = &[3, 5, 8];
151///
152///     let array = unwrap_ctx!(try_into_array(slice));
153///     
154///     // You can destructure the array into its elements like this
155///     let [a, b, c] = *array;
156///     
157///     array
158/// }
159///
160/// assert_eq!(arr_3(), &[3, 5, 8]);
161///
162/// ```
163///
164/// [`try_into_array`]: ./macro.try_into_array.html
165#[doc(inline)]
166pub use konst_kernel::slice::slice_for_konst::try_into_array_func as try_into_array;
167
168/// Tries to convert from `&mut [T]` to `&mut [T; N]`.
169///
170/// Returns an `Err(TryIntoArrayError{..})` when the slice doesn't match the expected length.
171///
172/// # Example
173///
174/// ```rust
175/// use konst::{slice, unwrap_ctx};
176///
177/// const fn mut_array_from<const LEN: usize>(slice: &mut [u8], from: usize) -> &mut [u8; LEN] {
178///     let sliced = slice::slice_range_mut(slice, from, from + LEN);
179///     unwrap_ctx!(slice::try_into_array_mut(sliced))
180/// }
181///
182/// # fn main() {
183///
184/// let slice = &mut [3, 5, 8, 13, 21, 34, 55, 89, 144, 233];
185///
186/// let foo: &mut [u8; 2] = mut_array_from(slice, 0);
187/// assert_eq!(foo, &mut [3, 5]);
188///
189/// let bar: &mut [u8; 3] = mut_array_from(slice, 2);
190/// assert_eq!(bar, &mut [8, 13, 21]);
191///
192/// let baz: &mut [u8; 4] = mut_array_from(slice, 4);
193/// assert_eq!(baz, &mut [21, 34, 55, 89]);
194///
195/// # }
196/// ```
197///
198#[cfg(feature = "rust_1_83")]
199#[cfg_attr(feature = "docsrs", doc(cfg(feature = "rust_1_83")))]
200#[doc(inline)]
201pub use konst_kernel::slice::slice_for_konst::try_into_array_mut_func as try_into_array_mut;