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
//! Generic constants for types from the [`alloc`] crate, including `String` and `Vec`.
//!
//! [`alloc`]: https://doc.rust-lang.org/alloc/
//!
//! # Removed in 0.4.0
//!
//! - `VEC_NEW`: removed because the stable equivalent is `const { Vec::new() }`
//! - `STRING_NEW`: removed because the stable equivalent is `const { String::new()}`
//! - `COW_STR_NEW`: removed because the stable equivalent is `const { Cow::Borrowed("") }`
//!
//!
use alloc::borrow::Cow;
declare_generic_const! {
/// An empty `Cow<'_, [T]>`. Usable to construct a `[Cow<'_, [T]>; N]`.
///
/// As of Rust 1.89.0, `[Cow::Borrowed(&[][..]); LEN]` is not valid,
/// because `Cow<'_, [T]>` isn't copy,
/// but `[CONST; LEN]` does work, like in the example below.
///
/// # Example
///
/// ```rust
/// use konst::alloc_type::COW_SLICE_NEW;
///
/// use std::borrow::Cow;
///
/// const SLICES: [Cow<'_, [u64]>; 6] = [COW_SLICE_NEW::<u64>::V; 6];
///
/// let mut cows = SLICES;
///
/// [3, 5, 8, 13, 21, 34].iter().copied()
/// .enumerate()
/// .filter(|(i, _)| i % 2 != 0 )
/// .for_each(|(i, v)|{
/// cows[i].to_mut().push(v)
/// });
///
/// assert_eq!(cows, [&[][..], &[5], &[], &[13], &[], &[34]])
///
/// ```
///
for['a, T: Clone + 'a]
pub const COW_SLICE_NEW['a, T]: Cow<'a, [T]> = Cow::Borrowed(&[]);
}