Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use zeroslice! macros in ICU4X #3455

Merged
merged 11 commits into from
Jun 2, 2023
14 changes: 5 additions & 9 deletions components/collator/src/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use icu_properties::CanonicalCombiningClass;
use smallvec::SmallVec;
use zerovec::ule::AsULE;
use zerovec::ule::RawBytesULE;
use zerovec::ZeroSlice;
use zerovec::{zeroslice, ZeroSlice};

use crate::provider::CollationDataV1;

Expand Down Expand Up @@ -147,17 +147,13 @@ pub(crate) const FFFD_CE: CollationElement = CollationElement(FFFD_CE_VALUE);
pub(crate) const FFFD_CE32_VALUE: u32 = 0xFFFD0505;
pub(crate) const FFFD_CE32: CollationElement32 = CollationElement32(FFFD_CE32_VALUE);

pub(crate) const EMPTY_U16: &ZeroSlice<u16> =
ZeroSlice::<u16>::from_ule_slice(&<u16 as AsULE>::ULE::from_array([]));
pub(crate) const EMPTY_U16: &ZeroSlice<u16> = zeroslice![];
const SINGLE_REPLACEMENT_CHARACTER_U16: &ZeroSlice<u16> =
ZeroSlice::<u16>::from_ule_slice(&<u16 as AsULE>::ULE::from_array([
REPLACEMENT_CHARACTER as u16
]));

pub(crate) const EMPTY_CHAR: &ZeroSlice<char> = ZeroSlice::new_empty();
zeroslice![u16; <u16 as AsULE>::ULE::from_array; REPLACEMENT_CHARACTER as u16];

pub(crate) const EMPTY_CHAR: &ZeroSlice<char> = zeroslice![];
const SINGLE_REPLACEMENT_CHARACTER_CHAR: &ZeroSlice<char> =
ZeroSlice::<char>::from_ule_slice(&<char as AsULE>::ULE::from_array([REPLACEMENT_CHARACTER]));
zeroslice![char; <char as AsULE>::ULE::from_array; REPLACEMENT_CHARACTER];

/// If `opt` is `Some`, unwrap it. If `None`, panic if debug assertions
/// are enabled and return `default` if debug assertions are not enabled.
Expand Down
7 changes: 3 additions & 4 deletions components/collator/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use icu_collections::char16trie::Char16TrieIterator;
use icu_collections::codepointtrie::CodePointTrie;
use icu_provider::prelude::*;
use zerovec::ule::AsULE;
use zerovec::ZeroSlice;
use zerovec::ZeroVec;
use zerovec::{zeroslice, ZeroSlice};

use crate::elements::CollationElement;
use crate::elements::CollationElement32;
Expand All @@ -39,9 +39,8 @@ use super::CaseFirst;
use super::MaxVariable;

const SINGLE_U32: &ZeroSlice<u32> =
ZeroSlice::<u32>::from_ule_slice(&<u32 as AsULE>::ULE::from_array([FFFD_CE32_VALUE]));
const SINGLE_U64: &ZeroSlice<u64> =
ZeroSlice::<u64>::from_ule_slice(&<u64 as AsULE>::ULE::from_array([FFFD_CE_VALUE]));
zeroslice![u32; <u32 as AsULE>::ULE::from_array; FFFD_CE32_VALUE];
const SINGLE_U64: &ZeroSlice<u64> = zeroslice![u64; <u64 as AsULE>::ULE::from_array; FFFD_CE_VALUE];

fn data_ce_to_primary(data_ce: u64, c: char) -> u32 {
// Collation::getThreeBytePrimaryForOffsetData
Expand Down
9 changes: 3 additions & 6 deletions components/collections/src/codepointinvlist/cpinvlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use alloc::vec::Vec;
use core::{char, ops::RangeBounds, ops::RangeInclusive};
use yoke::Yokeable;
use zerofrom::ZeroFrom;
use zerovec::{ule::AsULE, ZeroSlice, ZeroVec};
use zerovec::{ule::AsULE, zeroslice, ZeroSlice, ZeroVec};

use super::CodePointInversionListError;
use crate::codepointinvlist::utils::{deconstruct_range, is_valid_zv};
Expand All @@ -18,14 +18,11 @@ const BMP_MAX: u32 = 0xFFFF;

/// Represents the inversion list for a set of all code points in the Basic Multilingual Plane.
const BMP_INV_LIST_SLICE: &ZeroSlice<u32> =
ZeroSlice::<u32>::from_ule_slice(&<u32 as AsULE>::ULE::from_array([0x0, BMP_MAX + 1]));
zeroslice![u32; <u32 as AsULE>::ULE::from_array; 0x0, BMP_MAX + 1];
skius marked this conversation as resolved.
Show resolved Hide resolved

/// Represents the inversion list for all of the code points in the Unicode range.
const ALL_SLICE: &ZeroSlice<u32> =
ZeroSlice::<u32>::from_ule_slice(&<u32 as AsULE>::ULE::from_array([
0x0,
(char::MAX as u32) + 1,
]));
zeroslice![u32; <u32 as AsULE>::ULE::from_array; 0x0, (char::MAX as u32) + 1];

/// A membership wrapper for [`CodePointInversionList`].
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use icu_collections::codepointtrie::{CodePointTrieHeader, TrieType};
use zerovec::{ule::RawBytesULE, ZeroSlice};
use zerovec::ule::AsULE;
use zerovec::{zeroslice, ZeroSlice};

#[rustfmt::skip]
pub static INDEX: &ZeroSlice<u16> = ZeroSlice::<u16>::from_ule_slice(&RawBytesULE::<2>::from_array([
pub static INDEX: &ZeroSlice<u16> = zeroslice![u16; <u16 as AsULE>::ULE::from_array;
skius marked this conversation as resolved.
Show resolved Hide resolved
0,0x40,0x7f,0xbf,0xff,0x12e,0x16d,0x1ad,0x1e5,0x224,0x250,0x28e,0x2ce,0x2de,0x31e,0x34f,
0x38c,0x3bc,0x3fa,0x43a,0x44a,0x47b,0x4b2,0x4f2,0x532,0x572,0x5a3,0x5cf,0x60f,0x644,0x65e,0x69e,
0x6de,0x71e,0x756,0x78d,0x7ca,0x809,0x848,0x887,0x8c6,0x905,0x944,0x983,0x9c3,0xa01,0xa3f,0xa7f,
Expand Down Expand Up @@ -198,10 +199,10 @@ pub static INDEX: &ZeroSlice<u16> = ZeroSlice::<u16>::from_ule_slice(&RawBytesUL
0x66a,0x66a,0x66a,0x66a,0x66a,0x66a,0x66a,0x66a,0x66a,0x66a,0x66a,0x66a,0x66a,0x66a,0x380,0x380,
0x380,0x380,0x380,0x380,0x380,0x380,0x380,0x380,0x380,0x380,0x380,0x380,0x380,0x380,0x380,0x380,
0x380,0x380,0x380,0x380,0x380,0x380,0x380,0x380,0x380,0x380,0x380,0x380,0x380,0x380,0xaa0
]));
];

#[rustfmt::skip]
pub static DATA: &ZeroSlice<u8> = ZeroSlice::<u8>::from_ule_slice(&[
pub static DATA: &ZeroSlice<u8> = zeroslice![u8; core::convert::identity;
0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,
0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,
0xc,0x17,0x17,0x17,0x19,0x17,0x17,0x17,0x14,0x15,0x17,0x18,0x17,0x13,0x17,0x17,
Expand Down Expand Up @@ -1111,7 +1112,7 @@ pub static DATA: &ZeroSlice<u8> = ZeroSlice::<u8>::from_ule_slice(&[
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0x10,0x10,
0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0,0
]);
];

pub static HEADER: CodePointTrieHeader = CodePointTrieHeader {
high_start: 0x110000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use icu_collections::codepointtrie::{CodePointTrieHeader, TrieType};
use zerovec::{ule::RawBytesULE, ZeroSlice};
use zerovec::ule::AsULE;
use zerovec::{zeroslice, ZeroSlice};

#[rustfmt::skip]
pub static INDEX: &ZeroSlice<u16> = ZeroSlice::<u16>::from_ule_slice(&RawBytesULE::<2>::from_array([
pub static INDEX: &ZeroSlice<u16> = zeroslice![u16; <u16 as AsULE>::ULE::from_array;
0,0x40,0x7f,0xbf,0xff,0x12e,0x16d,0x1ad,0x1e5,0x224,0x250,0x28e,0x2ce,0x2de,0x31e,0x34f,
0x38c,0x3bc,0x3fa,0x43a,0x44a,0x47b,0x4b2,0x4f2,0x532,0x572,0x5a3,0x5cf,0x60f,0x644,0x65e,0x69e,
0x6de,0x71e,0x756,0x78d,0x7ca,0x809,0x848,0x887,0x8c6,0x905,0x944,0x983,0x9c3,0xa01,0xa3f,0xa7f,
Expand Down Expand Up @@ -220,10 +221,10 @@ pub static INDEX: &ZeroSlice<u16> = ZeroSlice::<u16>::from_ule_slice(&RawBytesUL
0x767,0x767,0x767,0x767,0x767,0x767,0x767,0x767,0x767,0x767,0x767,0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,
0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,
0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,0x4b5,0xb9d
]));
];

#[rustfmt::skip]
pub static DATA: &ZeroSlice<u8> = ZeroSlice::<u8>::from_ule_slice(&[
pub static DATA: &ZeroSlice<u8> = zeroslice![u8; core::convert::identity;
0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,
0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,
0xc,0x17,0x17,0x17,0x19,0x17,0x17,0x17,0x14,0x15,0x17,0x18,0x17,0x13,0x17,0x17,
Expand Down Expand Up @@ -848,7 +849,7 @@ pub static DATA: &ZeroSlice<u8> = ZeroSlice::<u8>::from_ule_slice(&[
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0x10,0x10,
0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0,0,0,0
]);
];

pub static HEADER: CodePointTrieHeader = CodePointTrieHeader {
high_start: 0x110000,
Expand Down
8 changes: 3 additions & 5 deletions components/normalizer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ use utf16_iter::Utf16CharsEx;
use utf8_iter::Utf8CharsEx;
use write16::Write16;
use zerofrom::ZeroFrom;
use zerovec::ule::AsULE;
use zerovec::ZeroSlice;
use zerovec::{zeroslice, ZeroSlice};

#[derive(Debug)]
enum SupplementPayloadHolder {
Expand Down Expand Up @@ -244,10 +243,9 @@ fn char_from_u16(u: u16) -> char {
char_from_u32(u32::from(u))
}

const EMPTY_U16: &ZeroSlice<u16> =
ZeroSlice::<u16>::from_ule_slice(&<u16 as AsULE>::ULE::from_array([]));
const EMPTY_U16: &ZeroSlice<u16> = zeroslice![];

const EMPTY_CHAR: &ZeroSlice<char> = ZeroSlice::new_empty();
const EMPTY_CHAR: &ZeroSlice<char> = zeroslice![];

#[inline(always)]
fn in_inclusive_range(c: char, start: char, end: char) -> bool {
Expand Down
81 changes: 81 additions & 0 deletions utils/zerovec/src/zerovec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,87 @@ impl<T: AsULE> FromIterator<T> for ZeroVec<'_, T> {
}
}

/// Convenience wrapper for [`ZeroSlice::from_ule_slice`]. The value will be created at compile-time.
///
/// # Arguments
///
/// * `$aligned` - The type of an element in its canonical, aligned form, e.g., `char`.
/// * `$array_fn` - A const function that converts an array of `$aligned` elements into an array
/// of their unaligned equivalents, e.g.,
/// `const fn from_array<const N: usize>(arr: [char; N]) -> [<char as AsULE>::ULE; N]`.
/// * `$x` - The elements that the `ZeroSlice` will hold.
///
/// # Examples
///
/// Using array-conversion functions provided by this crate:
///
/// ```
/// use zerovec::{ZeroSlice, zeroslice, ule::AsULE};
///
/// const SIGNATURE: &ZeroSlice<char> = zeroslice![char; <char as AsULE>::ULE::from_array; 'b', 'y', 'e', '✌'];
/// const EMPTY: &ZeroSlice<u32> = zeroslice![];
/// let empty: &ZeroSlice<u32> = zeroslice![];
/// let nums = zeroslice![u32; <u32 as AsULE>::ULE::from_array; 1, 2, 3, 4, 5];
/// assert_eq!(nums.last().unwrap(), 5);
/// ```
///
/// Using a custom array-conversion function:
///
/// ```
/// use zerovec::{ZeroSlice, zeroslice};
///
/// mod conversion {
/// use zerovec::ule::RawBytesULE;
/// pub(super) const fn i16_array_to_be_array<const N: usize>(arr: [i16; N]) -> [RawBytesULE<2>; N] {
/// let mut result = [RawBytesULE([0; 2]); N];
/// let mut i = 0;
/// while i < N {
/// result[i] = RawBytesULE(arr[i].to_be_bytes());
/// i += 1;
/// }
/// result
/// }
/// }
///
/// const NUMBERS: &ZeroSlice<i16> = zeroslice![i16; conversion::i16_array_to_be_array; 1, -2, 3, -4, 5];
/// ```
#[macro_export]
macro_rules! zeroslice {
() => (
$crate::ZeroSlice::new_empty()
);
($aligned:ty; $array_fn:expr; $($x:expr),+ $(,)?) => (
$crate::ZeroSlice::<$aligned>::from_ule_slice(
{const X: &[<$aligned as $crate::ule::AsULE>::ULE] = &$array_fn([$($x),+]); X}
)
);
}

/// Creates a borrowed `ZeroVec`. Convenience wrapper for `zeroslice![...].as_zerovec()`.
///
/// See [`zeroslice!`](crate::zeroslice) for more information.
///
/// # Examples
///
/// ```
/// use zerovec::{ZeroVec, zerovec, ule::AsULE};
///
/// const SIGNATURE: ZeroVec<char> = zerovec![char; <char as AsULE>::ULE::from_array; 'a', 'y', 'e', '✌'];
/// assert!(!SIGNATURE.is_owned());
///
/// const EMPTY: ZeroVec<u32> = zerovec![];
/// assert!(!EMPTY.is_owned());
/// ```
#[macro_export]
macro_rules! zerovec {
() => (
$crate::ZeroVec::new()
);
($aligned:ty; $array_fn:expr; $($x:expr),+ $(,)?) => (
$crate::zeroslice![$aligned; $array_fn; $($x),+].as_zerovec()
);
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
14 changes: 5 additions & 9 deletions utils/zerovec/src/zerovec/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ where
#[cfg(test)]
mod test {
use super::*;
use crate::zeroslice;

#[test]
fn test_split_first() {
Expand All @@ -576,21 +577,16 @@ mod test {
}
{
// single element slice
const DATA: &ZeroSlice<u16> =
ZeroSlice::<u16>::from_ule_slice(&<u16 as AsULE>::ULE::from_array([211]));
assert_eq!((211, ZeroSlice::new_empty()), DATA.split_first().unwrap());
const DATA: &ZeroSlice<u16> = zeroslice![u16; <u16 as AsULE>::ULE::from_array; 211];
assert_eq!((211, zeroslice![]), DATA.split_first().unwrap());
}
{
// slice with many elements.
const DATA: &ZeroSlice<u16> =
ZeroSlice::<u16>::from_ule_slice(&<u16 as AsULE>::ULE::from_array([
211, 281, 421, 32973,
]));
zeroslice![u16; <u16 as AsULE>::ULE::from_array; 211, 281, 421, 32973];
const EXPECTED_VALUE: (u16, &ZeroSlice<u16>) = (
211,
ZeroSlice::<u16>::from_ule_slice(&<u16 as AsULE>::ULE::from_array([
281, 421, 32973,
])),
zeroslice![u16; <u16 as AsULE>::ULE::from_array; 281, 421, 32973],
);

assert_eq!(EXPECTED_VALUE, DATA.split_first().unwrap());
Expand Down