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

Example of baked data crate with macros #2992

Closed
wants to merge 7 commits into from

Conversation

sffc
Copy link
Member

@sffc sffc commented Jan 14, 2023

See #2945

@sffc
Copy link
Member Author

sffc commented Jan 14, 2023

It works across crate boundaries if I change the super-absolute imports like ::icu_provider to normal ones like icu_provider so that I can use crate as icu_provider at the point I invoke the macro.

@@ -77,6 +78,7 @@ lto = true
[profile.release-opt-size]
inherits = "release"
opt-level = "s"
debug = true
Copy link
Member Author

Choose a reason for hiding this comment

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

(reminder to remove this line)

type DataStruct =
<icu_provider::hello_world::HelloWorldV1Marker as icu_provider::DataMarker>::Yokeable;
static KEYS: &[&str] = &["bn", "en", "en-US", "ja", "ru"];
static BN: DataStruct = $crate::baked::core_helloworld_v1::data_core_helloworld_v1_bn!();
Copy link
Member

Choose a reason for hiding this comment

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

This works, but it will be hard to generate because you're using the absolute path of where this will be included, e.g. it has to be included at $crate::baked. If we can't find a way to use relative addressing for the data file (either using mod or include), we should inline the data to step around this problem.

Copy link
Member Author

@sffc sffc Jan 25, 2023

Choose a reason for hiding this comment

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

The path is relative to the globaldata crate, which is under our control. We may need to tell databake-datagen what that path is, but this is totally an internal thing.

Copy link
Member

Choose a reason for hiding this comment

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

It would be useful if the logic we add here is also usable by datagen clients, not only for this crate.

include!("ru.data.rs");

#[macro_export]
macro_rules! impl_core_helloworld_v1 {
Copy link
Member

Choose a reason for hiding this comment

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

What's the plan for AnyProvider? Having one macro per key doesn't work, the keys should be arguments (plural) to the macro.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it should "just work"

struct LocalBakedProvider;
impl_core_helloworld_v1!(LocalBakedProvider);
impl_decimal_symbols_v1!(LocalBakedProvider);
impl_any_provider!(LocalBakedProvider, [
  icu_provider::hello_world::HelloWorldV1Marker,
  icu_decimal::provider::SymbolsV1Marker,
]);

@@ -0,0 +1,10 @@
#[macro_export]
Copy link
Member

Choose a reason for hiding this comment

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

Wrapping each data expression in a macro is a lot of cognitive overhead, and probably also compile time overhead.

Copy link
Member Author

Choose a reason for hiding this comment

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

The cognitive overhead is being moved from the call site (people need to learn how data providers work) to an internal crate (that only us and power users will see).

pub fn try_new(locale: &DataLocale) -> Result<Self, DataError> {
struct LocalBakedProvider;
use crate as icu_provider;
globaldata::impl_core_helloworld_v1!(LocalBakedProvider);
Copy link
Member Author

Choose a reason for hiding this comment

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

An open question is how to enable vertical fallback. An easy way, which is probably fine, is

let provider = globaldata::with_vertical_fallback!(LocalBakedProvider);

and the macro has two versions: one that is a no-op, and one that is enabled with the globaldata/fallback feature that actually enables the fallback

@sffc sffc mentioned this pull request Feb 22, 2023
@sffc sffc closed this Aug 8, 2023
@sffc sffc deleted the macroinclude branch August 8, 2023 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants