Skip to content

Commit

Permalink
validate_byte_slice -> validate_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Nov 13, 2024
1 parent e36bd3e commit afc957d
Show file tree
Hide file tree
Showing 36 changed files with 167 additions and 167 deletions.
8 changes: 4 additions & 4 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,16 +357,16 @@ 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> {
fn validate_bytes(bytes: &[u8]) -> Result<(), UleError> {
let sixteens = RawBytesULE::<2>::parse_bytes(bytes)?;

for sixteen in sixteens {
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
2 changes: 1 addition & 1 deletion components/datetime/src/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ mod test {
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
14 changes: 7 additions & 7 deletions components/datetime/src/provider/pattern/item/ule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -384,7 +384,7 @@ mod test {
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 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
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
6 changes: 3 additions & 3 deletions components/locale_core/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,13 @@ macro_rules! impl_tinystr_subtag {
//
// 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.
// 5. All other methods must be left with their default impl.
// 6. Byte equality is semantic equality.
#[cfg(feature = "zerovec")]
unsafe impl zerovec::ule::ULE for $name {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), zerovec::ule::UleError> {
fn validate_bytes(bytes: &[u8]) -> Result<(), zerovec::ule::UleError> {
let it = bytes.chunks_exact(core::mem::size_of::<Self>());
if !it.remainder().is_empty() {
return Err(zerovec::ule::UleError::length::<Self>(bytes.len()));
Expand Down
8 changes: 4 additions & 4 deletions components/pattern/src/implementations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ where
///
/// 1. `Pattern<B>` does not include any uninitialized or padding bytes.
/// 2. `Pattern<B>` is aligned to 1 byte.
/// 3. The implementation of `validate_byte_slice()` returns an error
/// 3. The implementation of `validate_bytes()` returns an error
/// if any byte is not valid.
/// 4. The implementation of `validate_byte_slice()` returns an error
/// 4. The implementation of `validate_bytes()` returns an error
/// if the slice cannot be used to build a `Pattern<B>` in its entirety.
/// 5. The implementation of `from_bytes_unchecked()` returns a reference to the same data.
/// 6. `parse_bytes()` is equivalent to `validate_byte_slice()` followed by `from_bytes_unchecked()`.
/// 6. `parse_bytes()` is equivalent to `validate_bytes()` followed by `from_bytes_unchecked()`.
/// 7. `Pattern<B>` byte equality is semantic equality.
unsafe impl<B, S: ?Sized + VarULE> VarULE for Pattern<B>
where
B: PatternBackend<Store = S>,
{
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
fn validate_bytes(bytes: &[u8]) -> Result<(), UleError> {
let store = S::parse_bytes(bytes)?;
B::validate_store(store).map_err(|_| UleError::parse::<Self>())
}
Expand Down
14 changes: 7 additions & 7 deletions components/plurals/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ unsafe impl<V> VarULE for PluralElementsPackedULE<V>
where
V: VarULE + ?Sized,
{
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
fn validate_bytes(bytes: &[u8]) -> Result<(), UleError> {
let unpacked_bytes =
Self::unpack_bytes(bytes).ok_or_else(|| UleError::length::<Self>(bytes.len()))?;
// The high bit of lead_byte was read in unpack_bytes.
Expand All @@ -409,9 +409,9 @@ where
return Err(UleError::parse::<Self>());
}
// Now validate the two variable-length slices.
V::validate_byte_slice(unpacked_bytes.v_bytes)?;
V::validate_bytes(unpacked_bytes.v_bytes)?;
if let Some(specials_bytes) = unpacked_bytes.specials_bytes {
PluralElementsTupleSliceVarULE::<V>::validate_byte_slice(specials_bytes)?;
PluralElementsTupleSliceVarULE::<V>::validate_bytes(specials_bytes)?;
}
Ok(())
}
Expand All @@ -430,7 +430,7 @@ where
///
/// # Safety
///
/// The bytes must be valid according to [`PluralElementsPackedULE::validate_byte_slice`].
/// The bytes must be valid according to [`PluralElementsPackedULE::validate_bytes`].
pub const unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self {
// Safety: the bytes are valid by trait invariant, and we are transparent over bytes
core::mem::transmute(bytes)
Expand Down Expand Up @@ -654,12 +654,12 @@ impl From<PluralCategoryAndMetadata> for PluralCategoryAndMetadataPackedULE {
//
// 1. The type is a single byte, not padding.
// 2. The type is a single byte, so it has align(1).
// 3. `validate_byte_slice` checks the validity of every byte.
// 4. `validate_byte_slice` checks the validity of every byte.
// 3. `validate_bytes` checks the validity of every byte.
// 4. `validate_bytes` checks the validity of every byte.
// 5. All other methods are be left with their default impl.
// 6. The represented enums implement Eq by byte equality.
unsafe impl ULE for PluralCategoryAndMetadataPackedULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), zerovec::ule::UleError> {
fn validate_bytes(bytes: &[u8]) -> Result<(), zerovec::ule::UleError> {
bytes
.iter()
.all(|byte| {
Expand Down
6 changes: 3 additions & 3 deletions components/plurals/src/rules/runtime/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,13 @@ pub(crate) struct AndOrPolarityOperandULE(u8);
// (achieved by `#[repr(transparent)]` on a type that satisfies this invariant)
/// 2. AndOrPolarityOperandULE 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
// (impossible since it is of size 1 byte)
// 5 The other ULE methods use the default impl.
// 6. AndOrPolarityOperandULE byte equality is semantic equality.
unsafe impl ULE for AndOrPolarityOperandULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
fn validate_bytes(bytes: &[u8]) -> Result<(), UleError> {
for byte in bytes {
Operand::new_from_u8(byte & 0b0011_1111).ok_or_else(UleError::parse::<Self>)?;
}
Expand Down
6 changes: 3 additions & 3 deletions utils/potential_utf/src/ustr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,16 @@ impl<'a> zerovec::maps::ZeroMapKV<'a> for PotentialUtf8 {
// Safety (based on the safety checklist on the VarULE trait):
// 1. PotentialUtf8 does not include any uninitialized or padding bytes (transparent over a ULE)
// 2. PotentialUtf8 is aligned to 1 byte (transparent over a ULE)
// 3. The impl of `validate_byte_slice()` returns an error if any byte is not valid (impossible)
// 4. The impl of `validate_byte_slice()` returns an error if the slice cannot be used in its entirety (impossible)
// 3. The impl of `validate_bytes()` returns an error if any byte is not valid (impossible)
// 4. The impl of `validate_bytes()` returns an error if the slice cannot be used in its entirety (impossible)
// 5. The impl of `from_bytes_unchecked()` returns a reference to the same data (returns the argument directly)
// 6. All other methods are defaulted
// 7. `[T]` byte equality is semantic equality (transparent over a ULE)
/// This impl requires enabling the optional `zerovec` Cargo feature
#[cfg(feature = "zerovec")]
unsafe impl zerovec::ule::VarULE for PotentialUtf8 {
#[inline]
fn validate_byte_slice(_: &[u8]) -> Result<(), zerovec::ule::UleError> {
fn validate_bytes(_: &[u8]) -> Result<(), zerovec::ule::UleError> {
Ok(())
}
#[inline]
Expand Down
12 changes: 6 additions & 6 deletions utils/tinystr/src/ule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ use zerovec::{ZeroSlice, ZeroVec};
// (achieved by `#[repr(transparent)]` on a type that satisfies this invariant)
// 2. TinyAsciiStr 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. TinyAsciiStr byte equality is semantic equality
unsafe impl<const N: usize> ULE for TinyAsciiStr<N> {
#[inline]
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
fn validate_bytes(bytes: &[u8]) -> Result<(), UleError> {
if bytes.len() % N != 0 {
return Err(UleError::length::<Self>(bytes.len()));
}
Expand Down Expand Up @@ -62,13 +62,13 @@ impl<'a, const N: usize> ZeroMapKV<'a> for TinyAsciiStr<N> {
// (achieved by `#[repr(transparent)]` on a type that satisfies this invariant)
// 2. UnvalidatedTinyAsciiStr 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. UnvalidatedTinyAsciiStr byte equality is semantic equality
unsafe impl<const N: usize> ULE for UnvalidatedTinyAsciiStr<N> {
#[inline]
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
fn validate_bytes(bytes: &[u8]) -> Result<(), UleError> {
if bytes.len() % N != 0 {
return Err(UleError::length::<Self>(bytes.len()));
}
Expand Down
Loading

0 comments on commit afc957d

Please sign in to comment.