Skip to content

Commit

Permalink
Remove callbacks and macros from FFI constructors (#5957)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian authored Jan 7, 2025
1 parent 95507c7 commit ebc88a4
Show file tree
Hide file tree
Showing 29 changed files with 754 additions and 778 deletions.
7 changes: 3 additions & 4 deletions ffi/capi/src/bidi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ 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<Bidi>, DataError> {
Ok(Box::new(Bidi(call_constructor_unstable!(
icu_properties::CodePointMapData::try_new_unstable,
provider,
)?)))
Ok(Box::new(Bidi(
icu_properties::CodePointMapData::try_new_unstable(&provider.get_unstable()?)?,
)))
}
/// Use the data loaded in this object to process a string and calculate bidi information
///
Expand Down
20 changes: 9 additions & 11 deletions ffi/capi/src/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ pub mod ffi {
) -> Result<Box<Calendar>, DataError> {
let prefs = (&locale.0).into();

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

/// Creates a new [`Calendar`] from the specified date and time, using a particular data source.
Expand All @@ -148,14 +148,12 @@ pub mod ffi {
provider: &DataProvider,
kind: AnyCalendarKind,
) -> Result<Box<Calendar>, DataError> {
Ok(Box::new(Calendar(Arc::new(provider.call_constructor(
|provider| {
icu_calendar::AnyCalendar::try_new_for_kind_with_buffer_provider(
provider,
kind.into(),
)
},
)?))))
Ok(Box::new(Calendar(Arc::new(
icu_calendar::AnyCalendar::try_new_for_kind_with_buffer_provider(
provider.get()?,
kind.into(),
)?,
))))
}

/// Returns the kind of this calendar
Expand Down
18 changes: 9 additions & 9 deletions ffi/capi/src/casemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ 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(provider.call_constructor(
icu_casemap::CaseMapper::try_new_with_buffer_provider,
)?)))
Ok(Box::new(CaseMapper(
icu_casemap::CaseMapper::try_new_with_buffer_provider(provider.get()?)?,
)))
}
/// Returns the full lowercase mapping of the given string
#[diplomat::rust_link(icu::casemap::CaseMapper::lowercase, FnInStruct)]
Expand Down Expand Up @@ -236,9 +236,9 @@ pub mod ffi {
pub fn create_with_provider(
provider: &DataProvider,
) -> Result<Box<CaseMapCloser>, DataError> {
Ok(Box::new(CaseMapCloser(provider.call_constructor(
icu_casemap::CaseMapCloser::try_new_with_buffer_provider,
)?)))
Ok(Box::new(CaseMapCloser(
icu_casemap::CaseMapCloser::try_new_with_buffer_provider(provider.get()?)?,
)))
}
/// Adds all simple case mappings and the full case folding for `c` to `builder`.
/// Also adds special case closure mappings.
Expand Down Expand Up @@ -293,9 +293,9 @@ pub mod ffi {
pub fn create_with_provider(
provider: &DataProvider,
) -> Result<Box<TitlecaseMapper>, DataError> {
Ok(Box::new(TitlecaseMapper(provider.call_constructor(
icu_casemap::TitlecaseMapper::try_new_with_buffer_provider,
)?)))
Ok(Box::new(TitlecaseMapper(
icu_casemap::TitlecaseMapper::try_new_with_buffer_provider(provider.get()?)?,
)))
}
/// Returns the full titlecase mapping of the given string
///
Expand Down
16 changes: 7 additions & 9 deletions ffi/capi/src/collator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,13 @@ pub mod ffi {
options: CollatorOptionsV1,
) -> Result<Box<Collator>, DataError> {
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,
)
},
)?)))
Ok(Box::new(Collator(
icu_collator::Collator::try_new_with_buffer_provider(
provider.get()?,
(&locale.0).into(),
options,
)?,
)))
}
/// Compare two strings.
///
Expand Down
50 changes: 25 additions & 25 deletions ffi/capi/src/datetime_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ pub mod ffi {
let options = T::with_length(Length::from(length)).hm();

Ok(Box::new(TimeFormatter(
provider.call_constructor_custom_err(move |provider| {
icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider(
provider, prefs, options,
)
})?,
icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider(
provider.get()?,
prefs,
options,
)?,
)))
}

Expand Down Expand Up @@ -138,11 +138,11 @@ pub mod ffi {
let options = YMD::with_length(Length::from(length));

Ok(Box::new(GregorianDateFormatter(
provider.call_constructor_custom_err(move |provider| {
icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider(
provider, prefs, options,
)
})?,
icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider(
provider.get()?,
prefs,
options,
)?,
)))
}

Expand Down Expand Up @@ -203,11 +203,11 @@ pub mod ffi {
let options = YMDT::with_length(Length::from(length)).hm();

Ok(Box::new(GregorianDateTimeFormatter(
provider.call_constructor_custom_err(move |provider| {
icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider(
provider, prefs, options,
)
})?,
icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider(
provider.get()?,
prefs,
options,
)?,
)))
}

Expand Down Expand Up @@ -257,11 +257,11 @@ pub mod ffi {
let options = YMD::with_length(Length::from(length));

Ok(Box::new(DateFormatter(
provider.call_constructor_custom_err(move |provider| {
icu_datetime::DateTimeFormatter::try_new_with_buffer_provider(
provider, prefs, options,
)
})?,
icu_datetime::DateTimeFormatter::try_new_with_buffer_provider(
provider.get()?,
prefs,
options,
)?,
)))
}

Expand Down Expand Up @@ -353,11 +353,11 @@ pub mod ffi {
let options = YMDT::with_length(Length::from(length)).hm();

Ok(Box::new(DateTimeFormatter(
provider.call_constructor_custom_err(move |provider| {
icu_datetime::DateTimeFormatter::try_new_with_buffer_provider(
provider, prefs, options,
)
})?,
icu_datetime::DateTimeFormatter::try_new_with_buffer_provider(
provider.get()?,
prefs,
options,
)?,
)))
}
/// Formats a [`DateTime`] to a string.
Expand Down
10 changes: 5 additions & 5 deletions ffi/capi/src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ pub mod ffi {
.map(Into::into)
.unwrap_or(options.grouping_strategy);
Ok(Box::new(FixedDecimalFormatter(
provider.call_constructor_custom_err(move |provider| {
icu_decimal::FixedDecimalFormatter::try_new_with_buffer_provider(
provider, prefs, options,
)
})?,
icu_decimal::FixedDecimalFormatter::try_new_with_buffer_provider(
provider.get()?,
prefs,
options,
)?,
)))
}

Expand Down
14 changes: 9 additions & 5 deletions ffi/capi/src/displaynames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ pub mod ffi {
let options = icu_experimental::displaynames::DisplayNamesOptions::from(options);

Ok(Box::new(LocaleDisplayNamesFormatter(
provider.call_constructor_custom_err(move |provider| icu_experimental::displaynames::LocaleDisplayNamesFormatter::try_new_with_buffer_provider(provider, prefs,
icu_experimental::displaynames::LocaleDisplayNamesFormatter::try_new_with_buffer_provider(provider.get()?, prefs,
options,
))?,
)?,
)))
}

Expand Down Expand Up @@ -145,9 +145,13 @@ pub mod ffi {
) -> Result<Box<RegionDisplayNames>, DataError> {
let prefs = (&locale.0).into();
let options = icu_experimental::displaynames::DisplayNamesOptions::from(options);
Ok(Box::new(RegionDisplayNames(provider.call_constructor_custom_err(move |provider| icu_experimental::displaynames::RegionDisplayNames::try_new_with_buffer_provider(provider, prefs,
options
))?)))
Ok(Box::new(RegionDisplayNames(
icu_experimental::displaynames::RegionDisplayNames::try_new_with_buffer_provider(
provider.get()?,
prefs,
options,
)?,
)))
}

/// Returns the locale specific display name of a region.
Expand Down
55 changes: 30 additions & 25 deletions ffi/capi/src/exemplar_chars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ pub mod ffi {
locale: &Locale,
) -> Result<Box<ExemplarCharacters>, DataError> {
let locale = locale.to_datalocale();
Ok(Box::new(ExemplarCharacters(call_constructor_unstable!(
icu_locale::exemplar_chars::ExemplarCharacters::try_new_main_unstable,
provider,
&locale
)?)))
Ok(Box::new(ExemplarCharacters(
icu_locale::exemplar_chars::ExemplarCharacters::try_new_main_unstable(
&provider.get_unstable()?,
&locale,
)?,
)))
}

/// Create an [`ExemplarCharacters`] for the "auxiliary" set of exemplar characters for a given locale, using compiled data.
Expand Down Expand Up @@ -107,11 +108,12 @@ pub mod ffi {
locale: &Locale,
) -> Result<Box<ExemplarCharacters>, DataError> {
let locale = locale.to_datalocale();
Ok(Box::new(ExemplarCharacters(call_constructor_unstable!(
icu_locale::exemplar_chars::ExemplarCharacters::try_new_auxiliary_unstable,
provider,
&locale
)?)))
Ok(Box::new(ExemplarCharacters(
icu_locale::exemplar_chars::ExemplarCharacters::try_new_auxiliary_unstable(
&provider.get_unstable()?,
&locale,
)?,
)))
}

/// Create an [`ExemplarCharacters`] for the "punctuation" set of exemplar characters for a given locale, using compiled data.
Expand Down Expand Up @@ -140,11 +142,12 @@ pub mod ffi {
locale: &Locale,
) -> Result<Box<ExemplarCharacters>, DataError> {
let locale = locale.to_datalocale();
Ok(Box::new(ExemplarCharacters(call_constructor_unstable!(
icu_locale::exemplar_chars::ExemplarCharacters::try_new_punctuation_unstable,
provider,
&locale
)?)))
Ok(Box::new(ExemplarCharacters(
icu_locale::exemplar_chars::ExemplarCharacters::try_new_punctuation_unstable(
&provider.get_unstable()?,
&locale,
)?,
)))
}

/// Create an [`ExemplarCharacters`] for the "index" set of exemplar characters for a given locale, using compiled data.
Expand Down Expand Up @@ -174,11 +177,12 @@ pub mod ffi {
locale: &Locale,
) -> Result<Box<ExemplarCharacters>, DataError> {
let locale = locale.to_datalocale();
Ok(Box::new(ExemplarCharacters(call_constructor_unstable!(
icu_locale::exemplar_chars::ExemplarCharacters::try_new_numbers_unstable,
provider,
&locale
)?)))
Ok(Box::new(ExemplarCharacters(
icu_locale::exemplar_chars::ExemplarCharacters::try_new_numbers_unstable(
&provider.get_unstable()?,
&locale,
)?,
)))
}

/// Create an [`ExemplarCharacters`] for the "main" set of exemplar characters for a given locale, using compiled data.
Expand Down Expand Up @@ -208,11 +212,12 @@ pub mod ffi {
locale: &Locale,
) -> Result<Box<ExemplarCharacters>, DataError> {
let locale = locale.to_datalocale();
Ok(Box::new(ExemplarCharacters(call_constructor_unstable!(
icu_locale::exemplar_chars::ExemplarCharacters::try_new_index_unstable,
provider,
&locale
)?)))
Ok(Box::new(ExemplarCharacters(
icu_locale::exemplar_chars::ExemplarCharacters::try_new_index_unstable(
&provider.get_unstable()?,
&locale,
)?,
)))
}
}
}
6 changes: 3 additions & 3 deletions ffi/capi/src/fallbacker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ pub mod ffi {
pub fn create_with_provider(
provider: &DataProvider,
) -> Result<Box<LocaleFallbacker>, DataError> {
Ok(Box::new(LocaleFallbacker(provider.call_constructor(
icu_locale::LocaleFallbacker::try_new_with_buffer_provider,
)?)))
Ok(Box::new(LocaleFallbacker(
icu_locale::LocaleFallbacker::try_new_with_buffer_provider(provider.get()?)?,
)))
}

/// Creates a new `LocaleFallbacker` without data for limited functionality.
Expand Down
Loading

0 comments on commit ebc88a4

Please sign in to comment.