Skip to main content

const_join

Macro const_join 

Source
macro_rules! const_join {
    (bytes:) => { ... };
    (bytes: $bytes:expr $(,)?) => { ... };
    (bytes: $($bytes: expr),+ $(,)?) => { ... };
    (bytes_repeated: $bytes:expr, $num:expr $(,)?) => { ... };
    (bytes_separated: $sep:expr; $first:expr, $($rest:expr),+ $(,)?) => { ... };
    (bytes_as_slice: $bytes: expr) => { ... };
    (str: ) => { ... };
    (str: $A:expr $(,)?) => { ... };
    (str: $A:expr, $B:expr, $($rest:expr),+ $(,)?) => { ... };
    (str: $A:expr, $B:expr $(,)?) => { ... };
    (str_repeated: $A:expr, $count:expr $(,)?) => { ... };
    (str_separated: $sep:expr; $first:expr, $($rest:expr),+ $(,)?) => { ... };
}
Expand description

โŒ— Joins multiple byte slices or string slices in compile-time.


๐Ÿ“ sys/mem/view


It leverages the ArrayFrom struct.

Note that these operations are slow and should not be used for fast paths, but mostly for compile-time needs.

ยงExamples

/* string slices */

const SBASE: &str = "path/to";
const SPART1: &str = "/foo";
const SPART2: &str = "/bar";
const SPATH: &str = const_join!(str: SBASE, SPART1, SPART2);
const_assert![eq_str SPATH, "path/to/foo/bar"];

const SREPEATED: &str = const_join!(str_repeated: SPART1, 3);
const_assert![eq_str SREPEATED, "/foo/foo/foo"];

const SPARTS: &str = const_join!(str_separated: "/"; "path", "to", "file");
const_assert!(eq_str SPARTS, "path/to/file");

/* byte slices */

const BBASE: &[u8] = b"path/to";
const BPART1: &[u8] = b"/foo";
const BPART2: &[u8] = b"/bar";
const BPATH: &[u8] = const_join!(bytes: BBASE, BPART1, BPART2);
const_assert![eq_buf BPATH, b"path/to/foo/bar"];

const BREPEATED: &[u8] = const_join!(bytes_repeated: BPART1, 3);
const_assert![eq_buf BREPEATED, b"/foo/foo/foo"];

const BPARTS: &[u8] = const_join!(bytes_separated: b"/"; b"path", b"to", b"file");
const_assert!(eq_buf BPARTS, b"path/to/file");

ยงFeatures

Makes use of the unsafe_str feature if available.