Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make FFI DataProvider only support BufferProvider #5895

Merged
merged 13 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions components/datetime/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ pub enum DateTimeFormatterLoadError {
Data(DataError),
}

impl From<DataError> for DateTimeFormatterLoadError {
Copy link
Member

Choose a reason for hiding this comment

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

I'm okay with this, but @robertbastian pushed back on adding the From impls for the fine-grained error types in this crate.

Copy link
Member Author

Choose a reason for hiding this comment

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

hm. we kind of need it to use the closure but we could also not use the closure and revive the macro instead for these few call sites.

Copy link
Member Author

Choose a reason for hiding this comment

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

hm. we kind of need it to use the closure but we could also not use the closure and revive the macro instead for these few call sites.

fn from(error: DataError) -> Self {
Self::Data(error)
}
}

/// An error from mixing calendar types in a formatter.
#[derive(Display, Debug, Copy, Clone, PartialEq)]
#[displaydoc("DateTimeFormatter for {this_kind} calendar was given a {date_kind:?} calendar")]
Expand Down
4 changes: 0 additions & 4 deletions ffi/capi/bindings/c/DataProvider.h

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

4 changes: 0 additions & 4 deletions ffi/capi/bindings/cpp/icu4x/DataProvider.d.hpp

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

14 changes: 0 additions & 14 deletions ffi/capi/bindings/cpp/icu4x/DataProvider.hpp

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

29 changes: 0 additions & 29 deletions ffi/capi/bindings/dart/DataProvider.g.dart

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

4 changes: 0 additions & 4 deletions ffi/capi/bindings/js/DataProvider.d.ts

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

20 changes: 0 additions & 20 deletions ffi/capi/bindings/js/DataProvider.mjs

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

6 changes: 3 additions & 3 deletions ffi/capi/src/bidi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub mod ffi {
use alloc::vec::Vec;
use core::fmt::Write;

use crate::errors::ffi::DataError;
use crate::provider::ffi::DataProvider;
#[cfg(feature = "buffer_provider")]
use crate::{errors::ffi::DataError, provider::ffi::DataProvider};

pub enum BidiDirection {
Ltr,
Expand Down Expand Up @@ -39,9 +39,9 @@ pub mod ffi {
/// Creates a new [`Bidi`] from locale data, and a particular data source.
#[diplomat::rust_link(icu::properties::bidi::BidiClassAdapter::new, FnInStruct)]
#[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::new [m => Ok(m.static_to_owned())],
icu_properties::CodePointMapData::try_new_unstable,
provider,
)?)))
Expand Down
25 changes: 13 additions & 12 deletions ffi/capi/src/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ pub mod ffi {
use alloc::sync::Arc;
use core::fmt::Write;

#[cfg(any(feature = "compiled_data", feature = "buffer_provider"))]
use crate::errors::ffi::DataError;
use crate::locale_core::ffi::Locale;
#[cfg(feature = "buffer_provider")]
use crate::provider::ffi::DataProvider;

/// The various calendar types currently supported by [`Calendar`]
Expand Down Expand Up @@ -126,34 +128,33 @@ pub mod ffi {
#[diplomat::rust_link(icu::calendar::AnyCalendar::try_new, FnInEnum)]
#[diplomat::attr(supports = fallible_constructors, named_constructor = "for_locale_with_provider")]
#[diplomat::demo(default_constructor)]
#[cfg(feature = "buffer_provider")]
pub fn create_for_locale_with_provider(
provider: &DataProvider,
locale: &Locale,
) -> Result<Box<Calendar>, DataError> {
let prefs = (&locale.0).into();

Ok(Box::new(Calendar(Arc::new(call_constructor!(
icu_calendar::AnyCalendar::try_new,
icu_calendar::AnyCalendar::try_new_with_any_provider,
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),
)?))))
}

/// Creates a new [`Calendar`] from the specified date and time, using a particular data source.
#[diplomat::rust_link(icu::calendar::AnyCalendar::new_for_kind, FnInEnum)]
#[diplomat::attr(supports = fallible_constructors, named_constructor = "for_kind_with_provider")]
#[cfg(feature = "buffer_provider")]
pub fn create_for_kind_with_provider(
provider: &DataProvider,
kind: AnyCalendarKind,
) -> Result<Box<Calendar>, DataError> {
Ok(Box::new(Calendar(Arc::new(call_constructor!(
icu_calendar::AnyCalendar::new_for_kind [r => Ok(r)],
icu_calendar::AnyCalendar::try_new_for_kind_with_any_provider,
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(),
)
},
)?))))
Comment on lines +151 to 158
Copy link
Member

Choose a reason for hiding this comment

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

I don't specifically recall a discussion on changing the macro to a closure. It seems like it would be harder in the future to change the behavior back to supporting try_new_unstable and similar. But it's fine.

Copy link
Member Author

@Manishearth Manishearth Dec 17, 2024

Choose a reason for hiding this comment

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

Nope, this wasn't discussed before: this PR was the discussion opportunity, and I can remove the last N commits if we don't want this.

But it should be easy enough to bring back when we need it.

I tend to consider macro use to be suboptimal, so if they can be removed without sacrificing anything I try to remove them. Here the complexity has not increased and the main downside is wanting to refactor again in the medium term.

}

Expand Down
24 changes: 11 additions & 13 deletions ffi/capi/src/casemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ use icu_casemap::titlecase::TitlecaseOptions;
pub mod ffi {
use alloc::boxed::Box;

use crate::{errors::ffi::DataError, locale_core::ffi::Locale, provider::ffi::DataProvider};
#[cfg(any(feature = "compiled_data", feature = "buffer_provider"))]
use crate::errors::ffi::DataError;
use crate::locale_core::ffi::Locale;
#[cfg(feature = "buffer_provider")]
use crate::provider::ffi::DataProvider;
use diplomat_runtime::DiplomatOption;

use writeable::Writeable;
Expand Down Expand Up @@ -65,12 +69,10 @@ pub mod ffi {
/// Construct a new CaseMapper instance using a particular data source.
#[diplomat::rust_link(icu::casemap::CaseMapper::new, FnInStruct)]
#[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::new [r => Ok(r)],
icu_casemap::CaseMapper::try_new_with_any_provider,
Ok(Box::new(CaseMapper(provider.call_constructor(
icu_casemap::CaseMapper::try_new_with_buffer_provider,
provider,
)?)))
}
/// Returns the full lowercase mapping of the given string
Expand Down Expand Up @@ -230,14 +232,12 @@ pub mod ffi {
#[diplomat::rust_link(icu::casemap::CaseMapCloser::new, FnInStruct)]
#[diplomat::rust_link(icu::casemap::CaseMapCloser::new_with_mapper, FnInStruct, hidden)]
#[diplomat::attr(supports = fallible_constructors, named_constructor = "with_provider")]
#[cfg(feature = "buffer_provider")]
pub fn create_with_provider(
provider: &DataProvider,
) -> Result<Box<CaseMapCloser>, DataError> {
Ok(Box::new(CaseMapCloser(call_constructor!(
icu_casemap::CaseMapCloser::new [r => Ok(r)],
icu_casemap::CaseMapCloser::try_new_with_any_provider,
Ok(Box::new(CaseMapCloser(provider.call_constructor(
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 @@ -289,14 +289,12 @@ pub mod ffi {
#[diplomat::rust_link(icu::casemap::TitlecaseMapper::new, FnInStruct)]
#[diplomat::rust_link(icu::casemap::TitlecaseMapper::new_with_mapper, FnInStruct, hidden)]
#[diplomat::attr(supports = fallible_constructors, named_constructor = "with_provider")]
#[cfg(feature = "buffer_provider")]
pub fn create_with_provider(
provider: &DataProvider,
) -> Result<Box<TitlecaseMapper>, DataError> {
Ok(Box::new(TitlecaseMapper(call_constructor!(
icu_casemap::TitlecaseMapper::new [r => Ok(r)],
icu_casemap::TitlecaseMapper::try_new_with_any_provider,
Ok(Box::new(TitlecaseMapper(provider.call_constructor(
icu_casemap::TitlecaseMapper::try_new_with_buffer_provider,
provider,
)?)))
}
/// Returns the full titlecase mapping of the given string
Expand Down
22 changes: 14 additions & 8 deletions ffi/capi/src/collator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
pub mod ffi {
use alloc::boxed::Box;

use crate::{errors::ffi::DataError, locale_core::ffi::Locale, provider::ffi::DataProvider};
#[cfg(feature = "buffer_provider")]
use crate::provider::ffi::DataProvider;
#[cfg(any(feature = "compiled_data", feature = "buffer_provider"))]
use crate::{errors::ffi::DataError, locale_core::ffi::Locale};
use diplomat_runtime::DiplomatOption;

#[diplomat::opaque]
Expand Down Expand Up @@ -131,18 +134,21 @@ pub mod ffi {
#[diplomat::rust_link(icu::collator::CollatorPreferences, Struct, hidden)]
#[diplomat::attr(supports = fallible_constructors, constructor)]
#[diplomat::attr(supports = non_exhaustive_structs, rename = "create_with_provider")]
#[cfg(feature = "buffer_provider")]
pub fn create_v1_with_provider(
provider: &DataProvider,
locale: &Locale,
options: CollatorOptionsV1,
) -> Result<Box<Collator>, DataError> {
Ok(Box::new(Collator(call_constructor!(
icu_collator::Collator::try_new [r => Ok(r?.static_to_owned())],
icu_collator::Collator::try_new_with_any_provider,
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
Loading
Loading