Skip to content

Commit 2f5653e

Browse files
committed
const
1 parent 41ccd85 commit 2f5653e

File tree

5 files changed

+61
-21
lines changed

5 files changed

+61
-21
lines changed

components/segmenter/src/complex/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ impl ComplexPayloadsBorrowed<'static> {
257257
}
258258

259259
#[cfg(feature = "compiled_data")]
260-
pub(crate) fn empty() -> Self {
260+
pub(crate) const fn empty() -> Self {
261261
Self {
262262
grapheme: GraphemeClusterSegmenter::new(),
263263
my: None,

components/segmenter/src/line.rs

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use alloc::string::String;
1010
use alloc::vec;
1111
use alloc::vec::Vec;
1212
use core::char;
13-
use icu_locale_core::subtags::language;
13+
use icu_locale_core::subtags::{language, Language};
1414
use icu_locale_core::LanguageIdentifier;
1515
use icu_provider::prelude::*;
1616
use utf8_iter::Utf8CharIndices;
@@ -208,24 +208,42 @@ pub struct LineBreakOptions<'a> {
208208
pub content_locale: Option<&'a LanguageIdentifier>,
209209
}
210210

211+
impl LineBreakOptions<'_> {
212+
/// `const` version of [`Default::default`]
213+
pub const fn default() -> Self {
214+
Self {
215+
strictness: None,
216+
word_option: None,
217+
content_locale: None,
218+
}
219+
}
220+
}
221+
211222
#[derive(Debug, Clone, Copy)]
212223
struct ResolvedLineBreakOptions {
213224
strictness: LineBreakStrictness,
214225
word_option: LineBreakWordOption,
215226
ja_zh: bool,
216227
}
217228

218-
impl From<LineBreakOptions<'_>> for ResolvedLineBreakOptions {
219-
fn from(options: LineBreakOptions<'_>) -> Self {
220-
let ja_zh = if let Some(content_locale) = options.content_locale.as_ref() {
221-
content_locale.language == language!("ja") || content_locale.language == language!("zh")
222-
} else {
223-
false
224-
};
225-
Self {
226-
strictness: options.strictness.unwrap_or_default(),
227-
word_option: options.word_option.unwrap_or_default(),
228-
ja_zh,
229+
impl LineBreakOptions<'_> {
230+
const fn resolve(self) -> ResolvedLineBreakOptions {
231+
ResolvedLineBreakOptions {
232+
strictness: match self.strictness {
233+
Some(s) => s,
234+
None => LineBreakStrictness::Strict,
235+
},
236+
word_option: match self.word_option {
237+
Some(s) => s,
238+
None => LineBreakWordOption::Normal,
239+
},
240+
ja_zh: if let Some(content_locale) = self.content_locale.as_ref() {
241+
const JA: Language = language!("ja");
242+
const ZH: Language = language!("zh");
243+
matches!(content_locale.language, JA | ZH)
244+
} else {
245+
false
246+
},
229247
}
230248
}
231249
}
@@ -426,7 +444,7 @@ impl LineSegmenter {
426444
#[cfg(feature = "compiled_data")]
427445
pub fn new_lstm(options: LineBreakOptions) -> LineSegmenterBorrowed<'static> {
428446
LineSegmenterBorrowed {
429-
options: options.into(),
447+
options: options.resolve(),
430448
data: crate::provider::Baked::SINGLETON_SEGMENTER_BREAK_LINE_V1,
431449
complex: ComplexPayloadsBorrowed::new_lstm(),
432450
}
@@ -456,7 +474,7 @@ impl LineSegmenter {
456474
+ ?Sized,
457475
{
458476
Ok(Self {
459-
options: options.into(),
477+
options: options.resolve(),
460478
payload: provider.load(Default::default())?.payload,
461479
complex: ComplexPayloads::try_new_lstm(provider)?,
462480
})
@@ -474,7 +492,7 @@ impl LineSegmenter {
474492
#[cfg(feature = "compiled_data")]
475493
pub fn new_dictionary(options: LineBreakOptions) -> LineSegmenterBorrowed<'static> {
476494
LineSegmenterBorrowed {
477-
options: options.into(),
495+
options: options.resolve(),
478496
data: crate::provider::Baked::SINGLETON_SEGMENTER_BREAK_LINE_V1,
479497
// Line segmenter doesn't need to load CJ dictionary because UAX 14 rules handles CJK
480498
// characters [1]. Southeast Asian languages however require complex context analysis
@@ -508,7 +526,7 @@ impl LineSegmenter {
508526
+ ?Sized,
509527
{
510528
Ok(Self {
511-
options: options.into(),
529+
options: options.resolve(),
512530
payload: provider.load(Default::default())?.payload,
513531
// Line segmenter doesn't need to load CJ dictionary because UAX 14 rules handles CJK
514532
// characters [1]. Southeast Asian languages however require complex context analysis
@@ -527,9 +545,9 @@ impl LineSegmenter {
527545
///
528546
/// [📚 Help choosing a constructor](icu_provider::constructors)
529547
#[cfg(feature = "compiled_data")]
530-
pub fn new_non_complex(options: LineBreakOptions) -> LineSegmenterBorrowed<'static> {
548+
pub const fn new_non_complex(options: LineBreakOptions) -> LineSegmenterBorrowed<'static> {
531549
LineSegmenterBorrowed {
532-
options: options.into(),
550+
options: options.resolve(),
533551
data: crate::provider::Baked::SINGLETON_SEGMENTER_BREAK_LINE_V1,
534552
complex: ComplexPayloadsBorrowed::empty(),
535553
}
@@ -556,7 +574,7 @@ impl LineSegmenter {
556574
+ ?Sized,
557575
{
558576
Ok(Self {
559-
options: options.into(),
577+
options: options.resolve(),
560578
payload: provider.load(Default::default())?.payload,
561579
complex: ComplexPayloads::try_new_empty(provider)?,
562580
})

components/segmenter/src/word.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,30 @@ pub struct WordBreakOptions<'a> {
5757
pub invariant_options: WordBreakInvariantOptions,
5858
}
5959

60+
impl WordBreakOptions<'_> {
61+
/// `const` version of [`Default::default`]
62+
pub const fn default() -> Self {
63+
Self {
64+
content_locale: None,
65+
invariant_options: WordBreakInvariantOptions::default()
66+
}
67+
}
68+
}
69+
6070
/// Locale-independent options to tailor word breaking behavior
6171
///
6272
/// Currently empty but may grow in the future
6373
#[non_exhaustive]
6474
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
6575
pub struct WordBreakInvariantOptions {}
6676

77+
impl WordBreakInvariantOptions {
78+
/// `const` version of [`Default::default`]
79+
pub const fn default() -> Self {
80+
Self {}
81+
}
82+
}
83+
6784
/// Implements the [`Iterator`] trait over the word boundaries of the given string.
6885
///
6986
/// Lifetimes:
@@ -521,7 +538,9 @@ impl WordSegmenter {
521538
///
522539
/// [📚 Help choosing a constructor](icu_provider::constructors)
523540
#[cfg(feature = "compiled_data")]
524-
pub fn new_non_complex(_options: WordBreakInvariantOptions) -> WordSegmenterBorrowed<'static> {
541+
pub const fn new_non_complex(
542+
_options: WordBreakInvariantOptions,
543+
) -> WordSegmenterBorrowed<'static> {
525544
WordSegmenterBorrowed {
526545
data: crate::provider::Baked::SINGLETON_SEGMENTER_BREAK_WORD_V1,
527546
complex: ComplexPayloadsBorrowed::empty(),

ffi/capi/src/segmenter_line.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub mod ffi {
4444
}
4545

4646
#[diplomat::rust_link(icu::segmenter::options::LineBreakOptions, Struct)]
47+
#[diplomat::rust_link(icu::segmenter::options::LineBreakOptions::default, FnInStruct, hidden)]
4748
#[diplomat::attr(supports = non_exhaustive_structs, rename = "LineBreakOptions")]
4849
#[diplomat::attr(kotlin, disable)] // option support (https://github.com/rust-diplomat/diplomat/issues/989)
4950
pub struct LineBreakOptionsV2 {

ffi/capi/src/segmenter_word.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pub mod ffi {
6767
/// Khmer, Lao, and Thai.
6868
#[diplomat::rust_link(icu::segmenter::WordSegmenter::new_auto, FnInStruct)]
6969
#[diplomat::rust_link(icu::segmenter::options::WordBreakInvariantOptions, Struct, hidden)]
70+
#[diplomat::rust_link(icu::segmenter::options::WordBreakInvariantOptions::default, FnInStruct, hidden)]
7071
#[diplomat::attr(auto, named_constructor = "auto")]
7172
#[cfg(feature = "compiled_data")]
7273
pub fn create_auto() -> Box<WordSegmenter> {
@@ -82,6 +83,7 @@ pub mod ffi {
8283
/// Khmer, Lao, and Thai.
8384
#[diplomat::rust_link(icu::segmenter::WordSegmenter::try_new_auto, FnInStruct)]
8485
#[diplomat::rust_link(icu::segmenter::options::WordBreakOptions, Struct, hidden)]
86+
#[diplomat::rust_link(icu::segmenter::options::WordBreakOptions::default, FnInStruct, hidden)]
8587
#[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "auto_with_content_locale")]
8688
#[cfg(feature = "compiled_data")]
8789
pub fn create_auto_with_content_locale(

0 commit comments

Comments
 (0)