From 5f02346ee09779bff97d5e30b79d7aaa44de5d24 Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Tue, 26 Dec 2023 19:13:23 -0800 Subject: [PATCH 1/3] Add as_pattern_item_ule --- components/datetime/src/pattern/item/ule.rs | 37 ++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/components/datetime/src/pattern/item/ule.rs b/components/datetime/src/pattern/item/ule.rs index 0e71cd91e2b..84579ceb8f1 100644 --- a/components/datetime/src/pattern/item/ule.rs +++ b/components/datetime/src/pattern/item/ule.rs @@ -215,6 +215,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): @@ -242,11 +264,9 @@ unsafe impl ULE for GenericPatternItemULE { } } -impl AsULE for GenericPatternItem { - type ULE = GenericPatternItemULE; - +impl GenericPatternItem { #[inline] - fn to_unaligned(self) -> Self::ULE { + pub(crate) const fn to_unaligned_const(self) -> ::ULE { match self { Self::Placeholder(idx) => GenericPatternItemULE([0b1000_0000, 0x00, idx]), Self::Literal(ch) => { @@ -256,6 +276,15 @@ impl AsULE for GenericPatternItem { } } } +} + +impl AsULE for GenericPatternItem { + type ULE = GenericPatternItemULE; + + #[inline] + fn to_unaligned(self) -> Self::ULE { + self.to_unaligned_const() + } #[inline] fn from_unaligned(unaligned: Self::ULE) -> Self { From 54857bbf9f9016b828178aa13548805932a620c6 Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Tue, 26 Dec 2023 19:18:50 -0800 Subject: [PATCH 2/3] Revert unrelated changes --- components/datetime/src/pattern/item/ule.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/components/datetime/src/pattern/item/ule.rs b/components/datetime/src/pattern/item/ule.rs index 84579ceb8f1..f726c26f44f 100644 --- a/components/datetime/src/pattern/item/ule.rs +++ b/components/datetime/src/pattern/item/ule.rs @@ -264,9 +264,11 @@ unsafe impl ULE for GenericPatternItemULE { } } -impl GenericPatternItem { +impl AsULE for GenericPatternItem { + type ULE = GenericPatternItemULE; + #[inline] - pub(crate) const fn to_unaligned_const(self) -> ::ULE { + fn to_unaligned(self) -> Self::ULE { match self { Self::Placeholder(idx) => GenericPatternItemULE([0b1000_0000, 0x00, idx]), Self::Literal(ch) => { @@ -276,15 +278,6 @@ impl GenericPatternItem { } } } -} - -impl AsULE for GenericPatternItem { - type ULE = GenericPatternItemULE; - - #[inline] - fn to_unaligned(self) -> Self::ULE { - self.to_unaligned_const() - } #[inline] fn from_unaligned(unaligned: Self::ULE) -> Self { From cde1a5b4bf9745827827b392d2c964ab76ed38c8 Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Wed, 27 Dec 2023 09:07:41 -0800 Subject: [PATCH 3/3] More docs --- components/datetime/src/pattern/item/ule.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/components/datetime/src/pattern/item/ule.rs b/components/datetime/src/pattern/item/ule.rs index f726c26f44f..96970705d61 100644 --- a/components/datetime/src/pattern/item/ule.rs +++ b/components/datetime/src/pattern/item/ule.rs @@ -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 /// @@ -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 ///