Skip to content

Commit f98d0a1

Browse files
committed
const
1 parent 41ccd85 commit f98d0a1

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ pub struct WordBreakOptions<'a> {
6464
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
6565
pub struct WordBreakInvariantOptions {}
6666

67+
impl WordBreakInvariantOptions {
68+
/// `const` version of [`Default::default`]
69+
pub const fn default() -> Self {
70+
Self {}
71+
}
72+
}
73+
6774
/// Implements the [`Iterator`] trait over the word boundaries of the given string.
6875
///
6976
/// Lifetimes:
@@ -521,7 +528,9 @@ impl WordSegmenter {
521528
///
522529
/// [📚 Help choosing a constructor](icu_provider::constructors)
523530
#[cfg(feature = "compiled_data")]
524-
pub fn new_non_complex(_options: WordBreakInvariantOptions) -> WordSegmenterBorrowed<'static> {
531+
pub const fn new_non_complex(
532+
_options: WordBreakInvariantOptions,
533+
) -> WordSegmenterBorrowed<'static> {
525534
WordSegmenterBorrowed {
526535
data: crate::provider::Baked::SINGLETON_SEGMENTER_BREAK_WORD_V1,
527536
complex: ComplexPayloadsBorrowed::empty(),

0 commit comments

Comments
 (0)