Skip to content
Closed
81 changes: 81 additions & 0 deletions components/collator/src/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,87 @@ impl CollatorBorrowed<'static> {
})
}

/// This creates a root collator using baked data only.
///
/// ✨ *Enabled with the `unstable` Cargo feature.*
#[cfg(feature = "compiled_data")]
#[cfg(feature = "unstable")]
pub const fn new_root() -> Self {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: this should take CollatorOptions

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CollatorOptionsBitField::from(CollatorOptions) is not const fn, so accepting CollatorOptions in new_root() would require either making the conversion const or dropping the const fn. how should i proceed?

const _: () = assert!(
crate::provider::Baked::SINGLETON_COLLATION_JAMO_V1
.ce32s
.as_slice()
.len()
== JAMO_COUNT
);
const _: () = assert!(
crate::provider::Baked::SINGLETON_COLLATION_SPECIAL_PRIMARIES_V1
.last_primaries
.as_slice()
.len()
> (MaxVariable::Currency as usize)
);

Self {
special_primaries: const {
&CollationSpecialPrimariesValidated {
last_primaries: zerovec::ZeroSlice::from_ule_slice(
crate::provider::Baked::SINGLETON_COLLATION_SPECIAL_PRIMARIES_V1
.last_primaries
.as_slice()
.as_ule_slice()
.split_at(MaxVariable::Currency as usize)
.0,
)
.as_zerovec(),
numeric_primary:
crate::provider::Baked::SINGLETON_COLLATION_SPECIAL_PRIMARIES_V1
.numeric_primary,
compressible_bytes: {
const C: &[<u16 as AsULE>::ULE] =
crate::provider::Baked::SINGLETON_COLLATION_SPECIAL_PRIMARIES_V1
.last_primaries
.as_slice()
.as_ule_slice();
if C.len() == MaxVariable::Currency as usize + 16 {
let i = MaxVariable::Currency as usize;
#[allow(clippy::indexing_slicing)]
&[
C[i],
C[i + 1],
C[i + 2],
C[i + 3],
C[i + 4],
Comment on lines +892 to +897
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: This code is heavily duplicated from try_new and seems like it has a lot of funny invariants to uphold. Please refactor these constructors so that they share more code paths.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#7872 refactors this

C[i + 5],
C[i + 6],
C[i + 7],
C[i + 8],
C[i + 9],
C[i + 10],
C[i + 11],
C[i + 12],
C[i + 13],
C[i + 14],
C[i + 15],
]
} else {
CollationSpecialPrimariesValidated::HARDCODED_COMPRESSIBLE_BYTES_FALLBACK
}
},
}
},
root: crate::provider::Baked::SINGLETON_COLLATION_ROOT_V1,
tailoring: None,
jamo: crate::provider::Baked::SINGLETON_COLLATION_JAMO_V1,
options: CollatorOptionsBitField::default(),
diacritics: crate::provider::Baked::COLLATION_DIACRITICS_V1_UND,
reordering: None,
decompositions: icu_normalizer::provider::Baked::SINGLETON_NORMALIZER_NFD_DATA_V1,
tables: icu_normalizer::provider::Baked::SINGLETON_NORMALIZER_NFD_TABLES_V1,
lithuanian_dot_above: false,
}
}

/// Cheaply converts a [`CollatorBorrowed<'static>`] into a [`Collator`].
///
/// Note: Due to branching and indirection, using [`Collator`] might inhibit some
Expand Down
3 changes: 3 additions & 0 deletions components/collator/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub struct Baked;

#[cfg(feature = "compiled_data")]
#[allow(unused_imports)]
#[allow(missing_docs)]
const _: () = {
use icu_collator_data::*;
pub mod icu {
Expand Down Expand Up @@ -98,6 +99,8 @@ icu_provider::data_marker!(
fallback_config = SCRIPT_FALLBACK,
#[cfg(feature = "datagen")]
attributes_domain = "collator",
#[cfg(feature = "datagen")]
expose_baked_consts = true,
);
icu_provider::data_marker!(
/// Data marker for collation jamo data.
Expand Down
1 change: 1 addition & 0 deletions ffi/capi/tests/missing_apis.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ icu::collator::CollationKeySink::write#FnInTrait
icu::collator::CollationKeySink::write_byte#FnInTrait
icu::collator::CollatorBorrowed::compare_latin1#FnInStruct
icu::collator::CollatorBorrowed::compare_latin1_utf16#FnInStruct
icu::collator::CollatorBorrowed::new_root#FnInStruct
icu::collator::CollatorBorrowed::write_sort_key_to#FnInStruct
icu::collator::CollatorBorrowed::write_sort_key_utf16_to#FnInStruct
icu::collator::CollatorBorrowed::write_sort_key_utf8_to#FnInStruct
Expand Down
14 changes: 9 additions & 5 deletions provider/data/collator/data/collation_diacritics_v1.rs.data

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 22 additions & 24 deletions provider/data/collator/stubdata/collation_diacritics_v1.rs.data

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions provider/data/collator/stubdata/mod.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading