const_util/
concat.rs

1//! Functions for concatenating slices
2
3use crate::Const;
4
5struct TwoValues<A, B>(A, B);
6impl<'a, T: ?Sized + 'a, A: Const<Type = &'a T>, B: Const<Type = &'a T>> Const for TwoValues<A, B> {
7    type Type = &'a [&'a T];
8    const VALUE: Self::Type = &[A::VALUE, B::VALUE];
9}
10/// Concats two `str`s into a single `&'static str` at compile time
11pub const fn concat_strs2<'a, Lhs: Const<Type = &'a str>, Rhs: Const<Type = &'a str>>(
12) -> &'static str {
13    concat_strs::<TwoValues<Lhs, Rhs>>()
14}
15
16/// Concats two `[u8]`s into a single `&'static [u8]` at compile time
17pub const fn concat_bytes2<'a, Lhs: Const<Type = &'a [u8]>, Rhs: Const<Type = &'a [u8]>>(
18) -> &'static [u8] {
19    concat_bytes::<TwoValues<Lhs, Rhs>>()
20}
21
22/// Concats a collection of `&str`s into a single `&'static str` at compile time.
23///
24/// # Example
25/// ```
26/// use const_util::{Const, concat::concat_strs};
27/// struct MyStrings;
28/// impl Const for MyStrings {
29///     type Type = &'static [&'static str];
30///     const VALUE: Self::Type = &[
31///         "Odd",
32///         "Even",
33///         "Schmeven",
34///     ];
35/// }
36/// assert_eq!(
37///     concat_strs::<MyStrings>(),
38///     "OddEvenSchmeven",
39/// )
40/// ```
41pub const fn concat_strs<'a, Strs: Const<Type = &'a [&'a str]>>() -> &'static str {
42    struct ToBytes<C>(C);
43    impl<'a, C: Const<Type = &'a [&'a str]>> Const for ToBytes<C> {
44        type Type = &'a [&'a [u8]];
45        // SAFETY: https://doc.rust-lang.org/reference/type-layout.html#r-layout.str
46        const VALUE: Self::Type = unsafe {
47            let strs = crate::value_of::<C>();
48            core::slice::from_raw_parts(strs.as_ptr().cast(), strs.len())
49        };
50    }
51    match core::str::from_utf8(concat_bytes::<ToBytes<Strs>>()) {
52        Ok(s) => s,
53        Err(_) => unreachable!(),
54    }
55}
56
57#[rustversion::since(1.87)]
58const fn copy_from_slice<T: Copy>(src: &[T], dst: &mut [T]) {
59    dst.copy_from_slice(src);
60}
61#[rustversion::before(1.87)]
62const fn copy_from_slice<T: Copy>(src: &[T], dst: &mut [T]) {
63    assert!(src.len() == dst.len());
64    // SAFETY: T: Copy. This is literally how copy_from_slice is implemented.
65    unsafe {
66        core::ptr::copy_nonoverlapping(src.as_ptr(), dst.as_mut_ptr(), src.len());
67    }
68}
69
70macro_rules! generate_slice_concat {
71    ($T:ty, $input:ty, $default:expr) => {{
72        use generic_upper_bound as gub;
73        struct Concat<C>(C);
74        gub::impl_accept_upper_bound! {
75            impl{'a, C: Const<Type = &'a [&'a [$T]]>} Concat<C>;
76            const DESIRED_GENERIC: usize = {
77                let mut slices = crate::value_of::<C>();
78                let mut out = 0;
79                while let [first, rest @ ..] = slices {
80                    out += first.len();
81                    slices = rest;
82                }
83                out
84            };
85            const EVAL<const N: usize>: &'static [$T] = &{
86                let mut out = [const { $default }; N];
87                let mut out_slice: &mut [_] = &mut out;
88                let mut slices = crate::value_of::<C>();
89                while let [first, rest @ ..] = slices {
90                    let lhs;
91                    (lhs, out_slice) = out_slice.split_at_mut(first.len());
92                    copy_from_slice(first, lhs);
93                    slices = rest;
94                }
95                out
96            };
97        }
98        gub::eval_with_upper_bound::<Concat<$input>>()
99            .split_at(gub::desired_generic::<Concat<$input>>())
100            .0
101    }};
102}
103
104/// Concats a collection of `&[u8]`s into a single `&'static [u8]` at compile time.
105///
106/// # Example
107/// Analogous to [`concat_strs`].
108pub const fn concat_bytes<'a, Bytes: Const<Type = &'a [&'a [u8]]>>() -> &'static [u8] {
109    generate_slice_concat!(u8, Bytes, 0)
110}