-
Notifications
You must be signed in to change notification settings - Fork 213
Add mechanism for linking resource keys with data structs #570
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
Comments
|
Here's the approach I'd like to try. I can add a new field, hidden behind a feature flag, to ResourceKey: However, I am hitting rust-lang/rust#77125, because we always create ResourceKey in a |
CC @Manishearth ^^ see above comment |
No, because there is not a 1-to-1 mapping. A single data struct could have multiple keys. I think it is a good thing that multiple keys could share the same data struct, because it helps unify code and reduce code size. I did a little experimentation and found that duplicating a data struct for two keys results in slightly higher code size than the two keys using the same data struct. I'm willing to share my benchmark if interested. Note to self: it is in the stash named "dup/dry benchmark bin" |
We could still use non-trait associated consts impl FooDataProvider {
const SYMBOLS_V1 = ...;
} |
The problem is there's no impl to put that. To clarify, here is how things currently stand:
Here are things I've considered:
|
I don't understand why this is true. We can write one, or autogenerate one.
If it's many-to-one we don't even need to worry about doing inherent impls, we can make this a part of DataMarker or DataStruct or something. But we can always do inherent impls for this. The goal is simply documentation, right? |
There are many keys that map to a single DataMarker. The problem this ticket is trying to solve is how to enforce that a data request for a particular key results in the correct data struct.
At least documentation, but better would be if we could enforce that keys and structs match each other. For example, if I write |
@sffc we could have a |
Should we consider something like this? trait DataMarker<'data> {
type Yokeable: ...;
type Cart: ...;
const key: DataKey;
}
struct HelloWorldV1Marker {}
impl<'data> DataMarker<'data> for HelloWorldV1Marker {
type Yokeable: HelloWorldV1<'static>;
type Cart: HelloWorldV1<'data>;
const key: icu_resource_key!("core", "hello_world", 1);
}
// Make a new marker struct for each key using the same struct
struct HelloWorldV1AltMarker {}
impl<'data> DataMarker<'data> for HelloWorldV1Marker {
type Yokeable: HelloWorldV1<'static>;
type Cart: HelloWorldV1<'data>;
const key: icu_resource_key!("core", "hello_world_alt", 1);
} and then you have just provider.load_payload::<HelloWorldV1Marker>() and we don't need to carry the key in the request because you can get it from the type parameter. Not sure what this means for FFI, as you said. |
also, what I suggested above basically does not work with ErasedDataProvider the way we have it; ErasedDataProvider would have to become its own trait again, with a signature more similar to DataProvider today. But maybe that's actually fine. |
I think this works. Yes, this won't work well for FFI. |
@sffc actually, what about cases where we want the same data type but multiple key kinds: i think we'll want this for calendar formatting where we'll have a GREGORY and JAPANESE and INDIAN etc keys for the data, even though they're all the same payload type. |
The request should carry runtime data, which is the locale and the variant. GREGORY, JAPANESE, and INDIAN are variants. That doesn't affect the DataKey. So it should work. And for FFI, we don't actually care, because most FFI functions take a concrete type or the ErasedDataProvider type. We would only care if we expose an FFI that directly returns the data structs... which we don't have right now and which I think we can support by other means (such as FFI mutating JSON buffers). |
There is currently no documentation that links resource keys, like
icu_decimal::provider::key::SYMBOLS_V1
, to data structs, likeicu_decimal::provider::DecimalSymbolsV1
. We should figure out a way to document and enforce this mapping.The text was updated successfully, but these errors were encountered: