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

Consistently use bytes not byte_slice #5816

Merged
merged 16 commits into from
Nov 15, 2024
Merged
10 changes: 5 additions & 5 deletions components/casemap/src/provider/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ impl CaseMapData {
#[cfg(any(feature = "datagen", test))]
pub(crate) fn try_from_icu_integer(int: u16) -> Result<Self, UleError> {
let raw = int.to_unaligned();
CaseMapDataULE::validate_byte_slice(raw.as_bytes())?;
CaseMapDataULE::validate_bytes(raw.as_bytes())?;

let this = Self::from_unaligned(CaseMapDataULE(raw));
Ok(this)
Expand Down Expand Up @@ -357,17 +357,17 @@ impl CaseMapDataULE {
/// 1. The type *must not* include any uninitialized or padding bytes: repr(transparent)
/// wrapper around ULE type
/// 2. The type must have an alignment of 1 byte: repr(transparent) wrapper around ULE type
/// 3. The impl of [`ULE::validate_byte_slice()`] *must* return an error if the given byte slice
/// 3. The impl of [`ULE::validate_bytes()`] *must* return an error if the given byte slice
/// would not represent a valid slice of this type: It does
/// 4. The impl of [`ULE::validate_byte_slice()`] *must* return an error if the given byte slice
/// 4. The impl of [`ULE::validate_bytes()`] *must* return an error if the given byte slice
/// cannot be used in its entirety (if its length is not a multiple of `size_of::<Self>()`):
/// it does, due to the RawBytesULE parse call
/// 5. All other methods *must* be left with their default impl, or else implemented according to
/// their respective safety guidelines: They have been
/// 6. The equality invariant is satisfied
unsafe impl ULE for CaseMapDataULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
let sixteens = RawBytesULE::<2>::parse_byte_slice(bytes)?;
fn validate_bytes(bytes: &[u8]) -> Result<(), UleError> {
let sixteens = RawBytesULE::<2>::parse_bytes_to_slice(bytes)?;

for sixteen in sixteens {
let sixteen = sixteen.as_unsigned_int();
Expand Down
6 changes: 3 additions & 3 deletions components/collections/codepointtrie_builder/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ where
let data_ptr = wasm.get_data_ptr(&ucptrie_ptr);
let data_length = wasm.get_data_length(&ucptrie_ptr);

let index = ZeroSlice::<u16>::parse_byte_slice(
let index = ZeroSlice::<u16>::parse_bytes(
wasm.get_bytes_at_ptr(&index_ptr, index_length * core::mem::size_of::<u16>()),
)
.unwrap()
Expand All @@ -260,7 +260,7 @@ where

let data = if core::mem::size_of::<T::ULE>() == 3 {
// need to reallocate 32-bit trie as 24-bit zerovec
ZeroVec::<T>::parse_byte_slice(
ZeroVec::<T>::parse_bytes(
&wasm
.get_bytes_at_ptr(&data_ptr, data_length * 4)
.iter()
Expand All @@ -272,7 +272,7 @@ where
.unwrap()
.into_owned()
} else {
ZeroVec::<T>::parse_byte_slice(
ZeroVec::<T>::parse_bytes(
wasm.get_bytes_at_ptr(&data_ptr, data_length * core::mem::size_of::<T::ULE>()),
)
.unwrap()
Expand Down
6 changes: 3 additions & 3 deletions components/collections/src/codepointtrie/planes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ pub fn get_planes_trie() -> CodePointTrie<'static, u8> {
0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x10, 0x10, 0x10, 0,
];
#[allow(clippy::unwrap_used)] // valid bytes
let index: ZeroVec<u16> = ZeroVec::parse_byte_slice(index_array_as_bytes).unwrap();
let index: ZeroVec<u16> = ZeroVec::parse_bytes(index_array_as_bytes).unwrap();
#[allow(clippy::unwrap_used)] // valid bytes
let data: ZeroVec<u8> = ZeroVec::parse_byte_slice(data_8_array).unwrap();
let data: ZeroVec<u8> = ZeroVec::parse_bytes(data_8_array).unwrap();
let high_start = 0x100000;
let shifted12_high_start = 0x100;
let index3_null_offset = 0x2;
Expand Down Expand Up @@ -289,7 +289,7 @@ mod tests {
fn test_index_byte_array_literal() {
let index_array_as_bytes: &[u8] = super::INDEX_ARRAY_AS_BYTES;
let index_zv_bytes: ZeroVec<u16> =
ZeroVec::parse_byte_slice(index_array_as_bytes).expect("infallible");
ZeroVec::parse_bytes(index_array_as_bytes).expect("infallible");
let index_zv_aligned: ZeroVec<u16> = ZeroVec::from_slice_or_alloc(INDEX_ARRAY);
assert_eq!(index_zv_bytes, index_zv_aligned);
}
Expand Down
6 changes: 3 additions & 3 deletions components/datetime/src/fields/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ impl FieldLengthULE {
//
// 1. Must not include any uninitialized or padding bytes (true since transparent over a ULE).
// 2. Must have an alignment of 1 byte (true since transparent over a ULE).
// 3. ULE::validate_byte_slice() checks that the given byte slice represents a valid slice.
// 4. ULE::validate_byte_slice() checks that the given byte slice has a valid length
// 3. ULE::validate_bytes() checks that the given byte slice represents a valid slice.
// 4. ULE::validate_bytes() checks that the given byte slice has a valid length
// (true since transparent over a type of size 1).
// 5. All other methods must be left with their default impl.
// 6. Byte equality is semantic equality.
unsafe impl ULE for FieldLengthULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
fn validate_bytes(bytes: &[u8]) -> Result<(), UleError> {
for byte in bytes {
Self::validate_byte(*byte)?;
}
Expand Down
8 changes: 4 additions & 4 deletions components/datetime/src/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Field {

impl FieldULE {
#[inline]
pub(crate) fn validate_bytes(bytes: (u8, u8)) -> Result<(), zerovec::ule::UleError> {
pub(crate) fn validate_byte_pair(bytes: (u8, u8)) -> Result<(), zerovec::ule::UleError> {
symbols::FieldSymbolULE::validate_byte(bytes.0)?;
length::FieldLengthULE::validate_byte(bytes.1)?;
Ok(())
Expand Down Expand Up @@ -141,7 +141,7 @@ mod test {

for (ref_field, ref_bytes) in samples {
let ule = ref_field.to_unaligned();
assert_eq!(ULE::as_byte_slice(&[ule]), ref_bytes);
assert_eq!(ULE::slice_as_bytes(&[ule]), ref_bytes);
let field = Field::from_unaligned(ule);
assert_eq!(field, ref_field);
}
Expand Down Expand Up @@ -170,15 +170,15 @@ mod test {
let mut bytes: Vec<u8> = vec![];
for item in ref_field.iter() {
let ule = item.to_unaligned();
bytes.extend(ULE::as_byte_slice(&[ule]));
bytes.extend(ULE::slice_as_bytes(&[ule]));
}

let mut bytes2: Vec<u8> = vec![];
for seq in ref_bytes.iter() {
bytes2.extend_from_slice(seq);
}

assert!(FieldULE::validate_byte_slice(&bytes).is_ok());
assert!(FieldULE::validate_bytes(&bytes).is_ok());
assert_eq!(bytes, bytes2);
}
}
Expand Down
6 changes: 3 additions & 3 deletions components/datetime/src/fields/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,13 @@ impl FieldSymbolULE {
//
// 1. Must not include any uninitialized or padding bytes (true since transparent over a ULE).
// 2. Must have an alignment of 1 byte (true since transparent over a ULE).
// 3. ULE::validate_byte_slice() checks that the given byte slice represents a valid slice.
// 4. ULE::validate_byte_slice() checks that the given byte slice has a valid length
// 3. ULE::validate_bytes() checks that the given byte slice represents a valid slice.
// 4. ULE::validate_bytes() checks that the given byte slice has a valid length
// (true since transparent over a type of size 1).
// 5. All other methods must be left with their default impl.
// 6. Byte equality is semantic equality.
unsafe impl ULE for FieldSymbolULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
fn validate_bytes(bytes: &[u8]) -> Result<(), UleError> {
for byte in bytes {
Self::validate_byte(*byte)?;
}
Expand Down
22 changes: 11 additions & 11 deletions components/datetime/src/provider/pattern/item/ule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl PatternItemULE {
fn bytes_in_range(value: (&u8, &u8, &u8)) -> bool {
if Self::determine_field_from_u8(*value.0) {
// ensure that unused bytes are all zero
fields::FieldULE::validate_bytes((*value.1, *value.2)).is_ok()
fields::FieldULE::validate_byte_pair((*value.1, *value.2)).is_ok()
&& *value.0 == 0b1000_0000
} else {
char::try_from(u32::from_be_bytes([0x00, *value.0, *value.1, *value.2])).is_ok()
Expand All @@ -88,12 +88,12 @@ impl PatternItemULE {
// (achieved by `#[repr(transparent)]` on a ULE type)
// 2. PatternItemULE is aligned to 1 byte.
// (achieved by `#[repr(transparent)]` on a ULE type)
// 3. The impl of validate_byte_slice() returns an error if any byte is not valid.
// 4. The impl of validate_byte_slice() returns an error if there are extra bytes.
// 3. The impl of validate_bytes() returns an error if any byte is not valid.
// 4. The impl of validate_bytes() returns an error if there are extra bytes.
// 5. The other ULE methods use the default impl.
// 6. PatternItemULE byte equality is semantic equality.
unsafe impl ULE for PatternItemULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
fn validate_bytes(bytes: &[u8]) -> Result<(), UleError> {
if bytes.len() % 3 != 0 {
return Err(UleError::length::<Self>(bytes.len()));
}
Expand Down Expand Up @@ -245,12 +245,12 @@ impl GenericPatternItemULE {
// (achieved by `#[repr(transparent)]` on a type that satisfies this invariant)
// 2. GenericPatternItemULE is aligned to 1 byte.
// (achieved by `#[repr(transparent)]` on a type that satisfies this invariant)
// 3. The impl of validate_byte_slice() returns an error if any byte is not valid.
// 4. The impl of validate_byte_slice() returns an error if there are extra bytes.
// 3. The impl of validate_bytes() returns an error if any byte is not valid.
// 4. The impl of validate_bytes() returns an error if there are extra bytes.
// 5. The other ULE methods use the default impl.
// 6. GenericPatternItemULE byte equality is semantic equality.
unsafe impl ULE for GenericPatternItemULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
fn validate_bytes(bytes: &[u8]) -> Result<(), UleError> {
if bytes.len() % 3 != 0 {
return Err(UleError::length::<Self>(bytes.len()));
}
Expand Down Expand Up @@ -343,7 +343,7 @@ mod test {

for (ref_pattern, ref_bytes) in samples {
let ule = ref_pattern.to_unaligned();
assert_eq!(ULE::as_byte_slice(&[ule]), ref_bytes);
assert_eq!(ULE::slice_as_bytes(&[ule]), ref_bytes);
let pattern = PatternItem::from_unaligned(ule);
assert_eq!(pattern, ref_pattern);
}
Expand Down Expand Up @@ -376,15 +376,15 @@ mod test {
let mut bytes: Vec<u8> = vec![];
for item in ref_pattern.iter() {
let ule = item.to_unaligned();
bytes.extend(ULE::as_byte_slice(&[ule]));
bytes.extend(ULE::slice_as_bytes(&[ule]));
}

let mut bytes2: Vec<u8> = vec![];
for seq in ref_bytes.iter() {
bytes2.extend_from_slice(seq);
}

assert!(PatternItemULE::validate_byte_slice(&bytes).is_ok());
assert!(PatternItemULE::validate_bytes(&bytes).is_ok());
assert_eq!(bytes, bytes2);
}
}
Expand All @@ -399,7 +399,7 @@ mod test {

for (ref_pattern, ref_bytes) in samples {
let ule = ref_pattern.to_unaligned();
assert_eq!(ULE::as_byte_slice(&[ule]), ref_bytes);
assert_eq!(ULE::slice_as_bytes(&[ule]), ref_bytes);
let pattern = GenericPatternItem::from_unaligned(ule);
assert_eq!(pattern, ref_pattern);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ pub struct CompactCountULE(u8);
// (achieved by `#[repr(transparent)]` on a ULE type)
// 2. CompactCountULE is aligned to 1 byte.
// (achieved by `#[repr(transparent)]` on a ULE type)
// 3. The impl of validate_byte_slice() returns an error if any byte is not valid.
// 4. The impl of validate_byte_slice() returns an error if there are extra bytes.
// 3. The impl of validate_bytes() returns an error if any byte is not valid.
// 4. The impl of validate_bytes() returns an error if there are extra bytes.
// 5. The other ULE methods use the default impl.
// 6. CompactCountULE byte equality is semantic equality.
unsafe impl ULE for CompactCountULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
fn validate_bytes(bytes: &[u8]) -> Result<(), UleError> {
for byte in bytes {
if byte & 0b0111_1000 != 0 {
return Err(UleError::parse::<Self>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,13 @@ impl<'data> CurrencyExtendedDataV1<'data> {
/// # Safety
///
/// The bytes must represent a valid [`icu_plurals::provider::PluralElementsPackedULE`]
pub const unsafe fn from_byte_slice_unchecked(bytes: &'data [u8]) -> Self {
pub const unsafe fn from_bytes_unchecked(bytes: &'data [u8]) -> Self {
Self {
display_names: icu_plurals::provider::PluralElementsPackedCow {
elements: alloc::borrow::Cow::Borrowed(
// Safety: this function's safety invariant guarantees that the bytes
// represent a valid `PluralElementsPackedULE`
icu_plurals::provider::PluralElementsPackedULE::from_byte_slice_unchecked(
bytes,
),
icu_plurals::provider::PluralElementsPackedULE::from_bytes_unchecked(bytes),
),
},
}
Expand All @@ -71,10 +69,10 @@ impl databake::Bake for CurrencyExtendedDataV1<'_> {
fn bake(&self, ctx: &databake::CrateEnv) -> databake::TokenStream {
use zerovec::ule::VarULE;
ctx.insert("icu_experimental::dimension::provider::extended_currency");
let bytes = self.display_names.elements.as_byte_slice().bake(ctx);
// Safety: The bytes are returned by `PluralElementsPackedULE::as_byte_slice`.
let bytes = self.display_names.elements.as_bytes().bake(ctx);
// Safety: The bytes are returned by `PluralElementsPackedULE::slice_as_bytes`.
Manishearth marked this conversation as resolved.
Show resolved Hide resolved
databake::quote! { unsafe {
icu_experimental::dimension::provider::extended_currency::CurrencyExtendedDataV1::from_byte_slice_unchecked(#bytes)
icu_experimental::dimension::provider::extended_currency::CurrencyExtendedDataV1::from_bytes_unchecked(#bytes)
}}
}
}
Expand Down
18 changes: 9 additions & 9 deletions components/experimental/src/dimension/provider/pattern_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ pub struct PatternKeyULE(u8);
// (achieved by `#[repr(transparent)]` on a ULE type)
// 2. PatternKeyULE is aligned to 1 byte.
// (achieved by `#[repr(transparent)]` on a ULE type)
// 3. The impl of validate_byte_slice() returns an error if any byte is not valid.
// 4. The impl of validate_byte_slice() returns an error if there are extra bytes.
// 3. The impl of validate_bytes() returns an error if any byte is not valid.
// 4. The impl of validate_bytes() returns an error if there are extra bytes.
// 5. The other ULE methods use the default impl.
// 6. PatternKeyULE byte equality is semantic equality.
unsafe impl ULE for PatternKeyULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), zerovec::ule::UleError> {
fn validate_bytes(bytes: &[u8]) -> Result<(), zerovec::ule::UleError> {
for &byte in bytes.iter() {
// Ensure the first two bits (b7 & b6) are not 11.
if (byte & 0b1100_0000) == 0b1100_0000 {
Expand Down Expand Up @@ -176,28 +176,28 @@ fn test_pattern_key_ule() {

let binary = PatternKey::Binary(0b0000_1111);
let binary_ule = binary.to_unaligned();
PatternKeyULE::validate_byte_slice(&[binary_ule.0]).unwrap();
PatternKeyULE::validate_bytes(&[binary_ule.0]).unwrap();
assert_eq!(binary_ule.0, 0b0000_1111);

let decimal = PatternKey::Decimal(0b0000_1111);
let decimal_ule = decimal.to_unaligned();
PatternKeyULE::validate_byte_slice(&[decimal_ule.0]).unwrap();
PatternKeyULE::validate_bytes(&[decimal_ule.0]).unwrap();
assert_eq!(decimal_ule.0, 0b0100_1111);

let power2 = PatternKey::Power {
power: Two,
count: CompoundCount::Two,
};
let power2_ule = power2.to_unaligned();
PatternKeyULE::validate_byte_slice(&[power2_ule.0]).unwrap();
PatternKeyULE::validate_bytes(&[power2_ule.0]).unwrap();
assert_eq!(power2_ule.0, 0b1010_0010);

let power3 = PatternKey::Power {
power: Three,
count: CompoundCount::Two,
};
let power3_ule = power3.to_unaligned();
PatternKeyULE::validate_byte_slice(&[power3_ule.0]).unwrap();
PatternKeyULE::validate_bytes(&[power3_ule.0]).unwrap();
assert_eq!(power3_ule.0, 0b1011_0010);

let binary = PatternKey::from_unaligned(binary_ule);
Expand Down Expand Up @@ -234,13 +234,13 @@ fn test_pattern_key_ule() {
// Test invalid bytes
let unvalidated_bytes = [0b1100_0000];
assert_eq!(
PatternKeyULE::validate_byte_slice(&unvalidated_bytes),
PatternKeyULE::validate_bytes(&unvalidated_bytes),
Err(UleError::parse::<PatternKeyULE>())
);

let unvalidated_bytes = [0b1000_0000];
assert_eq!(
PatternKeyULE::validate_byte_slice(&unvalidated_bytes),
PatternKeyULE::validate_bytes(&unvalidated_bytes),
Err(UleError::parse::<PatternKeyULE>())
);
}
6 changes: 3 additions & 3 deletions components/experimental/src/dimension/provider/ule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ pub struct CurrencyPatternConfigULE([u8; 3]);
// (achieved by `#[repr(transparent)]` on a ULE type)
// 2. CurrencyPatternConfigULE is aligned to 1 byte.
// (achieved by `#[repr(transparent)]` on a ULE type)
// 3. The impl of validate_byte_slice() returns an error if any byte is not valid.
// 4. The impl of validate_byte_slice() returns an error if there are extra bytes.
// 3. The impl of validate_bytes() returns an error if any byte is not valid.
// 4. The impl of validate_bytes() returns an error if there are extra bytes.
// 5. The other ULE methods use the default impl.
// 6. CurrencyPatternConfigULE byte equality is semantic equality.
unsafe impl ULE for CurrencyPatternConfigULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
fn validate_bytes(bytes: &[u8]) -> Result<(), UleError> {
if bytes.len() % 3 != 0 {
return Err(UleError::length::<Self>(bytes.len()));
}
Expand Down
12 changes: 5 additions & 7 deletions components/experimental/src/dimension/provider/units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,13 @@ impl<'data> UnitsDisplayNameV1<'data> {
/// # Safety
///
/// The bytes must represent a valid [`icu_plurals::provider::PluralElementsPackedULE`]
pub const unsafe fn from_byte_slice_unchecked(bytes: &'data [u8]) -> Self {
pub const unsafe fn from_bytes_unchecked(bytes: &'data [u8]) -> Self {
Self {
patterns: icu_plurals::provider::PluralElementsPackedCow {
elements: alloc::borrow::Cow::Borrowed(
// Safety: this function's safety invariant guarantees that the bytes
// represent a valid `PluralElementsPackedULE`
icu_plurals::provider::PluralElementsPackedULE::from_byte_slice_unchecked(
bytes,
),
icu_plurals::provider::PluralElementsPackedULE::from_bytes_unchecked(bytes),
),
},
}
Expand All @@ -55,10 +53,10 @@ impl databake::Bake for UnitsDisplayNameV1<'_> {
fn bake(&self, ctx: &databake::CrateEnv) -> databake::TokenStream {
use zerovec::ule::VarULE;
ctx.insert("icu_experimental::dimension::provider::units");
let bytes = self.patterns.elements.as_byte_slice().bake(ctx);
// Safety: The bytes are returned by `PluralElementsPackedULE::as_byte_slice`.
let bytes = self.patterns.elements.as_bytes().bake(ctx);
// Safety: The bytes are returned by `PluralElementsPackedULE::slice_as_bytes`.
databake::quote! { unsafe {
icu_experimental::dimension::provider::units::UnitsDisplayNameV1::from_byte_slice_unchecked(#bytes)
icu_experimental::dimension::provider::units::UnitsDisplayNameV1::from_bytes_unchecked(#bytes)
}}
}
}
Expand Down
Loading