unicode-org / icu4x

Solving i18n for client-side and resource-constrained environments.
https://icu4x.unicode.org
Other
1.34k stars 174 forks source link

Leverage `zeroslice!` macro to clean up ZeroVec API #1935

Open sffc opened 2 years ago

sffc commented 2 years ago

With Rust 1.61 and https://github.com/unicode-org/icu4x/pull/1926, we will now be able to do the following:

  1. Add a general-purpose zeroslice! macro
  2. Remove the old type-specific const constructors
  3. Migrate users of the problematic EqULE trait to use the new convenience macro, and then remove EqULE

For the macro, you should be able to write

// NOTE: The syntax should be bikeshed
static FOO: &ZeroSlice<u16> = zeroslice![<cb>; 222, 333, 444, 555];

which desugars to

static FOO: &ZeroSlice<u16> = $crate::ZeroSlice::from_ule_slice(
    &<cb>([222, 333, 444, 555])
);

where <cb> is a const function that converts from an aligned to unaligned array (i.e. AsULE::to_unaligned); it would be nice to have a trait for this but we still can't use trait functions in a const context. We can probably write it such that the function only needs to convert from a single T to a single T::ULE and call it in a loop. The big thing is that we can write ZeroSlice::from_ule_slice in a const context now.

sffc commented 2 years ago

Example of using the const function to build a ZeroSlice: https://github.com/unicode-org/icu4x/blob/269b807d9f0eda2905efab6c9e6bd30d8c243a80/utils/codepointtrie/benches/tries/gc_small.rs

The first step for this issue is to migrate all users of from_slice_or_alloc to use the const function if possible.