@@ -649,8 +649,8 @@ pub(crate) struct CollationSpecialPrimariesValidated<'data> {
649649 pub compressible_bytes : & ' data [ <u16 as AsULE >:: ULE ; 16 ] ,
650650}
651651
652- impl CollationSpecialPrimariesValidated < ' static > {
653- pub ( crate ) const HARDCODED_COMPRESSIBLE_BYTES_FALLBACK : & ' static [ <u16 as AsULE >:: ULE ; 16 ] = & [
652+ impl < ' a > CollationSpecialPrimaries < ' a > {
653+ const HARDCODED_COMPRESSIBLE_BYTES_FALLBACK : & ' static [ <u16 as AsULE >:: ULE ; 16 ] = & [
654654 <u16 as AsULE >:: ULE :: from_unsigned ( 0b0000_0000_0000_0000 ) ,
655655 <u16 as AsULE >:: ULE :: from_unsigned ( 0b0000_0000_0000_0000 ) ,
656656 <u16 as AsULE >:: ULE :: from_unsigned ( 0b0000_0000_0000_0000 ) ,
@@ -668,6 +668,63 @@ impl CollationSpecialPrimariesValidated<'static> {
668668 <u16 as AsULE >:: ULE :: from_unsigned ( 0b0000_0000_0000_0000 ) ,
669669 <u16 as AsULE >:: ULE :: from_unsigned ( 0b0100_0000_0000_0000 ) ,
670670 ] ;
671+
672+ pub ( crate ) fn validated ( self ) -> CollationSpecialPrimariesValidated < ' a > {
673+ let ( last_primaries, compressible_bytes) =
674+ if let Some ( borrowed) = self . last_primaries . as_maybe_borrowed ( ) {
675+ let ( l, c) = borrowed
676+ . as_ule_slice ( )
677+ // by invariant
678+ . split_at ( MaxVariable :: Currency as usize ) ;
679+ (
680+ l,
681+ c. try_into ( )
682+ . unwrap_or ( Self :: HARDCODED_COMPRESSIBLE_BYTES_FALLBACK ) ,
683+ )
684+ } else {
685+ (
686+ self . last_primaries . as_slice ( ) . as_ule_slice ( ) ,
687+ Self :: HARDCODED_COMPRESSIBLE_BYTES_FALLBACK ,
688+ )
689+ } ;
690+
691+ let last_primaries_truncate_len = last_primaries. len ( ) ;
692+ CollationSpecialPrimariesValidated {
693+ last_primaries : self . last_primaries . truncated ( last_primaries_truncate_len) ,
694+ numeric_primary : self . numeric_primary ,
695+ compressible_bytes,
696+ }
697+ }
698+
699+ pub ( crate ) const fn const_validated ( & ' static self ) -> CollationSpecialPrimariesValidated < ' a > {
700+ let borrowed = self . last_primaries . as_slice ( ) ;
701+ let ( last_primaries, compressible_bytes) = borrowed
702+ . as_ule_slice ( )
703+ // by invariant
704+ . split_at ( MaxVariable :: Currency as usize ) ;
705+ // TODO: use c.as_array() on MSRV 1.93
706+ let compressible_bytes = if compressible_bytes. len ( ) == 16 {
707+ unsafe { & * ( compressible_bytes. as_ptr ( ) as * const [ <u16 as AsULE >:: ULE ; 16 ] ) }
708+ } else {
709+ Self :: HARDCODED_COMPRESSIBLE_BYTES_FALLBACK
710+ } ;
711+
712+ CollationSpecialPrimariesValidated {
713+ last_primaries : ZeroSlice :: from_ule_slice ( last_primaries) . as_zerovec ( ) ,
714+ numeric_primary : self . numeric_primary ,
715+ compressible_bytes,
716+ }
717+ }
718+ }
719+
720+ #[ test]
721+ fn compressible_bytes ( ) {
722+ assert_eq ! (
723+ Baked :: SINGLETON_COLLATION_SPECIAL_PRIMARIES_V1
724+ . clone( )
725+ . validated( ) ,
726+ Baked :: SINGLETON_COLLATION_SPECIAL_PRIMARIES_V1 . const_validated( ) ,
727+ ) ;
671728}
672729
673730icu_provider:: data_struct!(
0 commit comments