zerovec

Macro zeroslice

Source
macro_rules! zeroslice {
    () => { ... };
    ($aligned:ty; $convert:expr; [$($x:expr),+ $(,)?]) => { ... };
}
Expand description

Convenience wrapper for ZeroSlice::from_ule_slice. The value will be created at compile-time, meaning that all arguments must also be constant.

§Arguments

  • $aligned - The type of an element in its canonical, aligned form, e.g., char.
  • $convert - A const function that converts an $aligned into its unaligned equivalent, e.g., const fn from_aligned(a: CanonicalType) -> CanonicalType::ULE.
  • $x - The elements that the ZeroSlice will hold.

§Examples

Using array-conversion functions provided by this crate:

use zerovec::{ZeroSlice, zeroslice, ule::AsULE};
use zerovec::ule::UnvalidatedChar;

const SIGNATURE: &ZeroSlice<char> = zeroslice!(char; <char as AsULE>::ULE::from_aligned; ['b', 'y', 'e', '✌']);
const EMPTY: &ZeroSlice<u32> = zeroslice![];
const UC: &ZeroSlice<UnvalidatedChar> =
    zeroslice!(
        UnvalidatedChar;
        <UnvalidatedChar as AsULE>::ULE::from_unvalidated_char;
        [UnvalidatedChar::from_char('a')]
    );
let empty: &ZeroSlice<u32> = zeroslice![];
let nums = zeroslice!(u32; <u32 as AsULE>::ULE::from_unsigned; [1, 2, 3, 4, 5]);
assert_eq!(nums.last().unwrap(), 5);

Using a custom array-conversion function:

use zerovec::{ule::AsULE, ule::RawBytesULE, zeroslice, ZeroSlice};

const fn be_convert(num: i16) -> <i16 as AsULE>::ULE {
    RawBytesULE(num.to_be_bytes())
}

const NUMBERS_BE: &ZeroSlice<i16> =
    zeroslice!(i16; be_convert; [1, -2, 3, -4, 5]);