Skip to content

Commit

Permalink
move usages to closures
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Dec 13, 2024
1 parent 16911a0 commit c97ece8
Show file tree
Hide file tree
Showing 18 changed files with 179 additions and 164 deletions.
17 changes: 9 additions & 8 deletions ffi/capi/src/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,8 @@ pub mod ffi {
) -> Result<Box<Calendar>, DataError> {
let prefs = (&locale.0).into();

Ok(Box::new(Calendar(Arc::new(call_constructor!(
icu_calendar::AnyCalendar::try_new_with_buffer_provider,
provider,
prefs
Ok(Box::new(Calendar(Arc::new(provider.call_constructor(
|provider| icu_calendar::AnyCalendar::try_new_with_buffer_provider(provider, prefs),
)?))))
}

Expand All @@ -148,10 +146,13 @@ pub mod ffi {
provider: &DataProvider,
kind: AnyCalendarKind,
) -> Result<Box<Calendar>, DataError> {
Ok(Box::new(Calendar(Arc::new(call_constructor!(
icu_calendar::AnyCalendar::try_new_for_kind_with_buffer_provider,
provider,
kind.into()
Ok(Box::new(Calendar(Arc::new(provider.call_constructor(
|provider| {
icu_calendar::AnyCalendar::try_new_for_kind_with_buffer_provider(
provider,
kind.into(),
)
},
)?))))
}

Expand Down
15 changes: 6 additions & 9 deletions ffi/capi/src/casemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ pub mod ffi {
#[diplomat::attr(supports = fallible_constructors, named_constructor = "with_provider")]
#[cfg(feature = "buffer_provider")]
pub fn create_with_provider(provider: &DataProvider) -> Result<Box<CaseMapper>, DataError> {
Ok(Box::new(CaseMapper(call_constructor!(
icu_casemap::CaseMapper::try_new_with_buffer_provider,
provider,
Ok(Box::new(CaseMapper(provider.call_constructor(
|provider| icu_casemap::CaseMapper::try_new_with_buffer_provider(provider),
)?)))
}
/// Returns the full lowercase mapping of the given string
Expand Down Expand Up @@ -233,9 +232,8 @@ pub mod ffi {
pub fn create_with_provider(
provider: &DataProvider,
) -> Result<Box<CaseMapCloser>, DataError> {
Ok(Box::new(CaseMapCloser(call_constructor!(
icu_casemap::CaseMapCloser::try_new_with_buffer_provider,
provider,
Ok(Box::new(CaseMapCloser(provider.call_constructor(
|provider| icu_casemap::CaseMapCloser::try_new_with_buffer_provider(provider),
)?)))
}
/// Adds all simple case mappings and the full case folding for `c` to `builder`.
Expand Down Expand Up @@ -291,9 +289,8 @@ pub mod ffi {
pub fn create_with_provider(
provider: &DataProvider,
) -> Result<Box<TitlecaseMapper>, DataError> {
Ok(Box::new(TitlecaseMapper(call_constructor!(
icu_casemap::TitlecaseMapper::try_new_with_buffer_provider,
provider,
Ok(Box::new(TitlecaseMapper(provider.call_constructor(
|provider| icu_casemap::TitlecaseMapper::try_new_with_buffer_provider(provider),
)?)))
}
/// Returns the full titlecase mapping of the given string
Expand Down
14 changes: 9 additions & 5 deletions ffi/capi/src/collator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,15 @@ pub mod ffi {
locale: &Locale,
options: CollatorOptionsV1,
) -> Result<Box<Collator>, DataError> {
Ok(Box::new(Collator(call_constructor!(
icu_collator::Collator::try_new_with_buffer_provider,
provider,
icu_collator::CollatorPreferences::from(&locale.0),
icu_collator::CollatorOptions::from(options),
let options = options.into();
Ok(Box::new(Collator(provider.call_constructor(
|provider| {
icu_collator::Collator::try_new_with_buffer_provider(
provider,
(&locale.0).into(),
options,
)
},
)?)))
}
/// Compare two strings.
Expand Down
5 changes: 2 additions & 3 deletions ffi/capi/src/fallbacker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ pub mod ffi {
pub fn create_with_provider(
provider: &DataProvider,
) -> Result<Box<LocaleFallbacker>, DataError> {
Ok(Box::new(LocaleFallbacker(call_constructor!(
icu_locale::LocaleFallbacker::try_new_with_buffer_provider,
provider,
Ok(Box::new(LocaleFallbacker(provider.call_constructor(
|provider| icu_locale::LocaleFallbacker::try_new_with_buffer_provider(provider),
)?)))
}

Expand Down
33 changes: 18 additions & 15 deletions ffi/capi/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ pub mod ffi {
) -> Result<Box<ListFormatter>, DataError> {
let prefs = ListFormatterPreferences::from(&locale.0);
let options = ListFormatterOptions::default().with_length(length.into());
Ok(Box::new(ListFormatter(call_constructor!(
icu_list::ListFormatter::try_new_and_with_buffer_provider,
provider,
prefs,
options,
Ok(Box::new(ListFormatter(provider.call_constructor(
move |provider| {
icu_list::ListFormatter::try_new_and_with_buffer_provider(
provider, prefs, options,
)
},
)?)))
}

Expand Down Expand Up @@ -85,11 +86,12 @@ pub mod ffi {
) -> Result<Box<ListFormatter>, DataError> {
let prefs = ListFormatterPreferences::from(&locale.0);
let options = ListFormatterOptions::default().with_length(length.into());
Ok(Box::new(ListFormatter(call_constructor!(
icu_list::ListFormatter::try_new_or_with_buffer_provider,
provider,
prefs,
options
Ok(Box::new(ListFormatter(provider.call_constructor(
move |provider| {
icu_list::ListFormatter::try_new_or_with_buffer_provider(
provider, prefs, options,
)
},
)?)))
}

Expand Down Expand Up @@ -118,11 +120,12 @@ pub mod ffi {
) -> Result<Box<ListFormatter>, DataError> {
let prefs = ListFormatterPreferences::from(&locale.0);
let options = ListFormatterOptions::default().with_length(length.into());
Ok(Box::new(ListFormatter(call_constructor!(
icu_list::ListFormatter::try_new_unit_with_buffer_provider,
provider,
prefs,
options
Ok(Box::new(ListFormatter(provider.call_constructor(
move |provider| {
icu_list::ListFormatter::try_new_unit_with_buffer_provider(
provider, prefs, options,
)
},
)?)))
}

Expand Down
32 changes: 15 additions & 17 deletions ffi/capi/src/locale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ pub mod ffi {
pub fn create_with_provider(
provider: &DataProvider,
) -> Result<Box<LocaleCanonicalizer>, DataError> {
Ok(Box::new(LocaleCanonicalizer(call_constructor!(
icu_locale::LocaleCanonicalizer::try_new_with_buffer_provider,
provider,
Ok(Box::new(LocaleCanonicalizer(provider.call_constructor(
|provider| icu_locale::LocaleCanonicalizer::try_new_with_buffer_provider(provider),
)?)))
}
/// Create a new [`LocaleCanonicalizer`] with extended data using compiled data.
Expand All @@ -62,14 +61,15 @@ pub mod ffi {
pub fn create_extended_with_provider(
provider: &DataProvider,
) -> Result<Box<LocaleCanonicalizer>, DataError> {
let expander = call_constructor!(
icu_locale::LocaleExpander::try_new_with_buffer_provider,
provider,
)?;
Ok(Box::new(LocaleCanonicalizer(call_constructor!(
icu_locale::LocaleCanonicalizer::try_new_with_expander_with_buffer_provider,
provider,
expander
let expander = provider.call_constructor(|provider| {
icu_locale::LocaleExpander::try_new_with_buffer_provider(provider)
})?;
Ok(Box::new(LocaleCanonicalizer(provider.call_constructor(
move |provider| {
icu_locale::LocaleCanonicalizer::try_new_with_expander_with_buffer_provider(
provider, expander,
)
},
)?)))
}
#[diplomat::rust_link(icu::locale::LocaleCanonicalizer::canonicalize, FnInStruct)]
Expand Down Expand Up @@ -98,9 +98,8 @@ pub mod ffi {
pub fn create_with_provider(
provider: &DataProvider,
) -> Result<Box<LocaleExpander>, DataError> {
Ok(Box::new(LocaleExpander(call_constructor!(
icu_locale::LocaleExpander::try_new_with_buffer_provider,
provider,
Ok(Box::new(LocaleExpander(provider.call_constructor(
|provider| icu_locale::LocaleExpander::try_new_with_buffer_provider(provider),
)?)))
}
/// Create a new [`LocaleExpander`] with extended data using compiled data.
Expand All @@ -117,9 +116,8 @@ pub mod ffi {
pub fn create_extended_with_provider(
provider: &DataProvider,
) -> Result<Box<LocaleExpander>, DataError> {
Ok(Box::new(LocaleExpander(call_constructor!(
icu_locale::LocaleExpander::try_new_with_buffer_provider,
provider,
Ok(Box::new(LocaleExpander(provider.call_constructor(
|provider| icu_locale::LocaleExpander::try_new_with_buffer_provider(provider),
)?)))
}
#[diplomat::rust_link(icu::locale::LocaleExpander::maximize, FnInStruct)]
Expand Down
5 changes: 2 additions & 3 deletions ffi/capi/src/locale_directionality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ pub mod ffi {
pub fn create_with_provider(
provider: &DataProvider,
) -> Result<Box<LocaleDirectionality>, DataError> {
Ok(Box::new(LocaleDirectionality(call_constructor!(
icu_locale::LocaleDirectionality::try_new_with_buffer_provider,
provider,
Ok(Box::new(LocaleDirectionality(provider.call_constructor(
|provider| icu_locale::LocaleDirectionality::try_new_with_buffer_provider(provider),
)?)))
}

Expand Down
32 changes: 20 additions & 12 deletions ffi/capi/src/normalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ pub mod ffi {
pub fn create_nfc_with_provider(
provider: &DataProvider,
) -> Result<Box<ComposingNormalizer>, DataError> {
Ok(Box::new(ComposingNormalizer(call_constructor!(
icu_normalizer::ComposingNormalizer::try_new_nfc_with_buffer_provider,
provider,
Ok(Box::new(ComposingNormalizer(provider.call_constructor(
|provider| {
icu_normalizer::ComposingNormalizer::try_new_nfc_with_buffer_provider(provider)
},
)?)))
}
/// Construct a new ComposingNormalizer instance for NFKC using compiled data.
Expand Down Expand Up @@ -74,9 +75,10 @@ pub mod ffi {
pub fn create_nfkc_with_provider(
provider: &DataProvider,
) -> Result<Box<ComposingNormalizer>, DataError> {
Ok(Box::new(ComposingNormalizer(call_constructor!(
icu_normalizer::ComposingNormalizer::try_new_nfkc_with_buffer_provider,
provider,
Ok(Box::new(ComposingNormalizer(provider.call_constructor(
|provider| {
icu_normalizer::ComposingNormalizer::try_new_nfkc_with_buffer_provider(provider)
},
)?)))
}
/// Normalize a string
Expand Down Expand Up @@ -201,9 +203,12 @@ pub mod ffi {
pub fn create_nfd_with_provider(
provider: &DataProvider,
) -> Result<Box<DecomposingNormalizer>, DataError> {
Ok(Box::new(DecomposingNormalizer(call_constructor!(
icu_normalizer::DecomposingNormalizer::try_new_nfd_with_buffer_provider,
provider,
Ok(Box::new(DecomposingNormalizer(provider.call_constructor(
|provider| {
icu_normalizer::DecomposingNormalizer::try_new_nfd_with_buffer_provider(
provider,
)
},
)?)))
}

Expand Down Expand Up @@ -234,9 +239,12 @@ pub mod ffi {
pub fn create_nfkd_with_provider(
provider: &DataProvider,
) -> Result<Box<DecomposingNormalizer>, DataError> {
Ok(Box::new(DecomposingNormalizer(call_constructor!(
icu_normalizer::DecomposingNormalizer::try_new_nfkd_with_buffer_provider,
provider,
Ok(Box::new(DecomposingNormalizer(provider.call_constructor(
|provider| {
icu_normalizer::DecomposingNormalizer::try_new_nfkd_with_buffer_provider(
provider,
)
},
)?)))
}

Expand Down
19 changes: 8 additions & 11 deletions ffi/capi/src/normalizer_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ pub mod ffi {
pub fn create_with_provider(
provider: &DataProvider,
) -> Result<Box<CanonicalCombiningClassMap>, DataError> {
Ok(Box::new(CanonicalCombiningClassMap(call_constructor!(
icu_normalizer::properties::CanonicalCombiningClassMap::try_new_with_buffer_provider,
provider
)?)))
Ok(Box::new(CanonicalCombiningClassMap(provider.call_constructor(|provider| icu_normalizer::properties::CanonicalCombiningClassMap::try_new_with_buffer_provider(provider))?)))
}

#[diplomat::rust_link(
Expand Down Expand Up @@ -132,9 +129,12 @@ pub mod ffi {
pub fn create_with_provider(
provider: &DataProvider,
) -> Result<Box<CanonicalComposition>, DataError> {
Ok(Box::new(CanonicalComposition(call_constructor!(
icu_normalizer::properties::CanonicalComposition::try_new_with_buffer_provider,
provider,
Ok(Box::new(CanonicalComposition(provider.call_constructor(
|provider| {
icu_normalizer::properties::CanonicalComposition::try_new_with_buffer_provider(
provider,
)
},
)?)))
}

Expand Down Expand Up @@ -203,10 +203,7 @@ pub mod ffi {
pub fn create_with_provider(
provider: &DataProvider,
) -> Result<Box<CanonicalDecomposition>, DataError> {
Ok(Box::new(CanonicalDecomposition(call_constructor!(
icu_normalizer::properties::CanonicalDecomposition::try_new_with_buffer_provider,
provider,
)?)))
Ok(Box::new(CanonicalDecomposition(provider.call_constructor(|provider| icu_normalizer::properties::CanonicalDecomposition::try_new_with_buffer_provider(provider))?)))
}

/// Performs non-recursive canonical decomposition (including for Hangul).
Expand Down
16 changes: 8 additions & 8 deletions ffi/capi/src/pluralrules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ pub mod ffi {
locale: &Locale,
) -> Result<Box<PluralRules>, DataError> {
let prefs = icu_plurals::PluralRulesPreferences::from(&locale.0);
Ok(Box::new(PluralRules(call_constructor!(
icu_plurals::PluralRules::try_new_cardinal_with_buffer_provider,
provider,
prefs
Ok(Box::new(PluralRules(provider.call_constructor(
|provider| {
icu_plurals::PluralRules::try_new_cardinal_with_buffer_provider(provider, prefs)
},
)?)))
}
/// Construct an [`PluralRules`] for the given locale, for ordinal numbers, using compiled data.
Expand All @@ -91,10 +91,10 @@ pub mod ffi {
locale: &Locale,
) -> Result<Box<PluralRules>, DataError> {
let prefs = icu_plurals::PluralRulesPreferences::from(&locale.0);
Ok(Box::new(PluralRules(call_constructor!(
icu_plurals::PluralRules::try_new_ordinal_with_buffer_provider,
provider,
prefs
Ok(Box::new(PluralRules(provider.call_constructor(
|provider| {
icu_plurals::PluralRules::try_new_ordinal_with_buffer_provider(provider, prefs)
},
)?)))
}
/// Get the category for a given number represented as operands
Expand Down
9 changes: 6 additions & 3 deletions ffi/capi/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ pub mod ffi {
pub fn create_with_provider(
provider: &DataProvider,
) -> Result<Box<ScriptWithExtensions>, DataError> {
Ok(Box::new(ScriptWithExtensions(call_constructor!(
icu_properties::script::ScriptWithExtensions::try_new_with_buffer_provider,
provider
Ok(Box::new(ScriptWithExtensions(provider.call_constructor(
|provider| {
icu_properties::script::ScriptWithExtensions::try_new_with_buffer_provider(
provider,
)
},
)?)))
}

Expand Down
9 changes: 5 additions & 4 deletions ffi/capi/src/segmenter_grapheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ pub mod ffi {
pub fn create_with_provider(
provider: &DataProvider,
) -> Result<Box<GraphemeClusterSegmenter>, DataError> {
Ok(Box::new(GraphemeClusterSegmenter(call_constructor!(
icu_segmenter::GraphemeClusterSegmenter::try_new_with_buffer_provider,
provider,
)?)))
Ok(Box::new(GraphemeClusterSegmenter(
provider.call_constructor(|provider| {
icu_segmenter::GraphemeClusterSegmenter::try_new_with_buffer_provider(provider)
})?,
)))
}
/// Segments a string.
///
Expand Down
Loading

0 comments on commit c97ece8

Please sign in to comment.