Skip to content

Commit

Permalink
Add GenericPatternItemULE::as_pattern_item_ule (#4497)
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc authored Dec 27, 2023
1 parent 84d16e9 commit 3f50b42
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions components/datetime/src/pattern/item/ule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ use zerovec::ule::{AsULE, ZeroVecError, ULE};
///
/// If the discriminant is not set, the bottom three bits of the first byte,
/// together with the next two bytes, contain all 21 bits required to encode
/// any [`Unicode Code Point`].
/// any [`Unicode Code Point`]. By design, the representation of a code point
/// is the same between [`PatternItemULE`] and [`GenericPatternItemULE`].
///
/// # Diagram
///
Expand Down Expand Up @@ -160,7 +161,8 @@ impl AsULE for PatternItem {
///
/// If the discriminant is not set, the bottom three bits of the first byte,
/// together with the next two bytes, contain all 21 bits required to encode
/// any [`Unicode Code Point`].
/// any [`Unicode Code Point`]. By design, the representation of a code point
/// is the same between [`PatternItemULE`] and [`GenericPatternItemULE`].
///
/// # Diagram
///
Expand Down Expand Up @@ -215,6 +217,28 @@ impl GenericPatternItemULE {
char::try_from(u).is_ok()
}
}

/// Converts this [`GenericPatternItemULE`] to a [`PatternItemULE`]
/// (if a Literal) or returns the placeholder value.
#[allow(dead_code)] // #4415
#[inline]
pub(crate) fn as_pattern_item_ule(&self) -> Result<&PatternItemULE, u8> {
if Self::determine_field_from_u8(self.0[0]) {
Err(self.0[2])
} else {
if cfg!(debug_assertions) {
let GenericPatternItem::Literal(c) = GenericPatternItem::from_unaligned(*self)
else {
unreachable!("expected a literal!")
};
let pattern_item_ule = PatternItem::Literal(c).to_unaligned();
debug_assert_eq!(self.0, pattern_item_ule.0);
}
// Safety: when a Literal, the two ULEs have the same repr,
// as shown in the above assertion (and the class docs).
Ok(unsafe { core::mem::transmute(self) })
}
}
}

// Safety (based on the safety checklist on the ULE trait):
Expand Down

0 comments on commit 3f50b42

Please sign in to comment.