Skip to content

Commit

Permalink
Use new ULE impl everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Dec 13, 2021
1 parent 1eb37a3 commit d613550
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 52 deletions.
16 changes: 8 additions & 8 deletions components/datetime/src/fields/ule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::fields;
use zerovec::ule::{AsULE, ULE};
use zerovec::ule::{AsULE, ULEError, ULE};

/// `FieldULE` is a type optimized for efficent storing and
/// deserialization of `DateTimeFormat` `Field` elements using
Expand Down Expand Up @@ -70,15 +70,15 @@ impl AsULE for fields::Field {
// 5 The other ULE methods use the default impl.
// 6. FieldULE byte equality is semantic equality.
unsafe impl ULE for FieldULE {
type Error = &'static str;

fn validate_byte_slice(bytes: &[u8]) -> Result<(), Self::Error> {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ULEError> {
let mut chunks = bytes.chunks_exact(2);

if !chunks.all(|c| fields::Field::bytes_in_range(&c[0], &c[1]))
|| !chunks.remainder().is_empty()
{
return Err("Invalid byte sequence");
if !chunks.all(|c| fields::Field::bytes_in_range(&c[0], &c[1])) {
return Err(ULEError::parse::<Self>());
}

if !chunks.remainder().is_empty() {
return Err(ULEError::length::<Self>(bytes.len()));
}
Ok(())
}
Expand Down
30 changes: 15 additions & 15 deletions components/datetime/src/pattern/item/ule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use super::{GenericPatternItem, PatternItem};
use crate::fields;
use core::convert::TryFrom;
use zerovec::ule::{AsULE, ULE};
use zerovec::ule::{AsULE, ULEError, ULE};

/// `PatternItemULE` is a type optimized for efficent storing and
/// deserialization of `DateTimeFormat` `PatternItem` elements using
Expand Down Expand Up @@ -93,15 +93,15 @@ impl PatternItemULE {
// 5. The other ULE methods use the default impl.
// 6. PatternItemULE byte equality is semantic equality.
unsafe impl ULE for PatternItemULE {
type Error = &'static str;

fn validate_byte_slice(bytes: &[u8]) -> Result<(), Self::Error> {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ULEError> {
let mut chunks = bytes.chunks_exact(3);

if !chunks.all(|c| Self::bytes_in_range((&c[0], &c[1], &c[2])))
|| !chunks.remainder().is_empty()
{
return Err("Invalid byte sequence");
if !chunks.all(|c| Self::bytes_in_range((&c[0], &c[1], &c[2]))) {
return Err(ULEError::parse::<Self>());
}

if !chunks.remainder().is_empty() {
return Err(ULEError::length::<Self>(bytes.len()));
}
Ok(())
}
Expand Down Expand Up @@ -228,15 +228,15 @@ impl GenericPatternItemULE {
// 5. The other ULE methods use the default impl.
// 6. GenericPatternItemULE byte equality is semantic equality.
unsafe impl ULE for GenericPatternItemULE {
type Error = &'static str;

fn validate_byte_slice(bytes: &[u8]) -> Result<(), Self::Error> {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ULEError> {
let mut chunks = bytes.chunks_exact(3);

if !chunks.all(|c| Self::bytes_in_range((&c[0], &c[1], &c[2])))
|| !chunks.remainder().is_empty()
{
return Err("Invalid byte sequence");
if !chunks.all(|c| Self::bytes_in_range((&c[0], &c[1], &c[2]))) {
return Err(ULEError::parse::<Self>());
}

if !chunks.remainder().is_empty() {
return Err(ULEError::length::<Self>(bytes.len()));
}
Ok(())
}
Expand Down
15 changes: 6 additions & 9 deletions components/plurals/src/rules/runtime/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use core::{
use icu_provider::yoke::{self, *};
use num_enum::{IntoPrimitive, TryFromPrimitive, UnsafeFromPrimitive};
use zerovec::{
ule::{custom::EncodeAsVarULE, AsULE, PairULE, PlainOldULE, VarULE, ULE},
ule::{custom::EncodeAsVarULE, AsULE, PairULE, PlainOldULE, ULEError, VarULE, ULE},
{VarZeroVec, ZeroVec},
};

Expand Down Expand Up @@ -326,8 +326,8 @@ impl RelationULE {
}

#[inline]
fn validate_andor_polarity_operand(encoded: u8) -> Result<(), &'static str> {
Operand::try_from(encoded & 0b0011_1111).map_err(|_| "Failed to decode operand.")?;
fn validate_andor_polarity_operand(encoded: u8) -> Result<(), ULEError> {
Operand::try_from(encoded & 0b0011_1111).map_err(|_| ULEError::parse::<Self>())?;
Ok(())
}

Expand Down Expand Up @@ -361,8 +361,6 @@ impl RelationULE {
// 6. The other VarULE methods use the default impl.
// 7. RelationULE byte equality is semantic equality.
unsafe impl VarULE for RelationULE {
type Error = &'static str;

#[inline]
unsafe fn from_byte_slice_unchecked(bytes: &[u8]) -> &Self {
let ptr = bytes.as_ptr();
Expand All @@ -387,15 +385,14 @@ unsafe impl VarULE for RelationULE {
}

#[inline]
fn validate_byte_slice(bytes: &[u8]) -> Result<(), Self::Error> {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ULEError> {
RelationULE::validate_andor_polarity_operand(bytes[0])?;
// Skip bytes 1-4 as they're always valid `u32` for `Modulo`.
if bytes.len() < 5 {
return Err("byte slice is too short");
return Err(ULEError::parse::<Self>());
}
let remaining = &bytes[5..];
RangeOrValueULE::validate_byte_slice(remaining)
.map_err(|_| "Invalid list of RangeOrValueULE")?;
RangeOrValueULE::validate_byte_slice(remaining)?;
Ok(())
}
}
Expand Down
9 changes: 3 additions & 6 deletions components/properties/src/ule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use crate::{
};

use core::convert::TryFrom;
use num_enum::TryFromPrimitiveError;
use zerovec::ule::{AsULE, PlainOldULE, ULE};
use zerovec::ule::{AsULE, PlainOldULE, ULEError, ULE};

#[repr(transparent)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
Expand Down Expand Up @@ -42,12 +41,10 @@ impl AsULE for GeneralSubcategory {
// 5. The other ULE methods use the default impl.
// 6. The PartialEq implementation on GeneralSubcategory uses byte equality.
unsafe impl ULE for GeneralSubcategoryULE {
type Error = TryFromPrimitiveError<GeneralSubcategory>;

fn validate_byte_slice(bytes: &[u8]) -> Result<(), Self::Error> {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ULEError> {
// Validate the bytes
for b in bytes {
GeneralSubcategory::try_from(*b)?;
GeneralSubcategory::try_from(*b).map_err(|_| ULEError::parse::<Self>())?;
}
Ok(())
}
Expand Down
8 changes: 2 additions & 6 deletions utils/codepointtrie/src/codepointtrie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use core::num::TryFromIntError;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use yoke::{Yokeable, ZeroCopyFrom};
use zerovec::ULEError;
use zerovec::ZeroVec;

/// The type of trie represents whether the trie has an optimization that
Expand Down Expand Up @@ -331,12 +332,7 @@ impl<'trie, T: TrieValue> CodePointTrie<'trie, T> {
/// let cpt2: CodePointTrie<u32> = cpt1.try_into_converted()
/// .expect("infallible");
/// ```
pub fn try_into_converted<P>(
self,
) -> Result<
CodePointTrie<'trie, P>,
<<P as zerovec::ule::AsULE>::ULE as zerovec::ule::ULE>::Error,
>
pub fn try_into_converted<P>(self) -> Result<CodePointTrie<'trie, P>, ULEError>
where
P: TrieValue,
{
Expand Down
1 change: 0 additions & 1 deletion utils/zerovec/benches/zerovec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ fn get_needles_and_haystack() -> (Vec<u32>, Vec<u32>) {
fn vec_to_unaligned_uvec<'a, T>(vec: &Vec<T>, buffer: &'a mut AlignedBuffer) -> ZeroVec<'a, T>
where
T: EqULE + Copy + PartialEq + fmt::Debug,
<T::ULE as ULE>::Error: fmt::Debug,
{
// Pad with zero to ensure it is not aligned
buffer.0.push(0);
Expand Down
2 changes: 1 addition & 1 deletion utils/zerovec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,6 @@ pub mod zerovec;
mod yoke_impls;

pub use crate::map::ZeroMap;
pub use crate::ule::ULEError;
pub use crate::varzerovec::VarZeroVec;
pub use crate::zerovec::ZeroVec;
pub use crate::ule::ULEError;
9 changes: 4 additions & 5 deletions utils/zerovec/src/ule/custom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,11 @@
//! // 6. The other VarULE methods use the default impl.
//! // 7. FooULE byte equality is semantic equality
//! unsafe impl VarULE for FooULE {
//! type Error = &'static str; // use strings for simplicity
//! fn validate_byte_slice(bytes: &[u8]) -> Result<(), Self::Error> {
//! fn validate_byte_slice(bytes: &[u8]) -> Result<(), ULEError> {
//! // validate each field
//! <char as AsULE>::ULE::validate_byte_slice(&bytes[0..4]).map_err(|_| "validating char failed")?;
//! <char as AsULE>::ULE::validate_byte_slice(&bytes[4..8]).map_err(|_| "validating u32 failed")?;
//! let _ = ZeroVec::<u32>::parse_byte_slice(&bytes[8..]).map_err(|_| "validating ZeroVec failed")?;
//! <char as AsULE>::ULE::validate_byte_slice(&bytes[0..4]).map_err(|_| ULEError::parse::<Self>())?;
//! <char as AsULE>::ULE::validate_byte_slice(&bytes[4..8]).map_err(|_| ULEError::parse::<Self>())?;
//! let _ = ZeroVec::<u32>::parse_byte_slice(&bytes[8..]).map_err(|_| ULEError::parse::<Self>())?;
//! Ok(())
//! }
//! unsafe fn from_byte_slice_unchecked(bytes: &[u8]) -> &Self {
Expand Down
2 changes: 1 addition & 1 deletion utils/zerovec/src/varzerovec/borrowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl<'a, T: VarULE + ?Sized> VarZeroVecBorrowed<'a, T> {
self.things.get(start..end).ok_or(ULEError::FormatError)
})
.chain(last)
.map(|s| s.and_then(|s| T::parse_byte_slice(s).map_err(|e| e.into())))
.map(|s| s.and_then(|s| T::parse_byte_slice(s)))
}

/// Create an iterator over the Ts contained in VarZeroVecBorrowed
Expand Down

0 comments on commit d613550

Please sign in to comment.