-
Notifications
You must be signed in to change notification settings - Fork 223
Add icu_provider_adapter::delegate module with helper macro #6853
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
base: main
Are you sure you want to change the base?
Conversation
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. |
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request adds a new delegate
module containing a helper macro data_provider_to_field!
. The overall approach is good, with clear documentation and tests. I've identified a significant limitation in the macro's implementation that restricts its usability to only direct fields of a struct. I've provided a suggestion to make the macro more flexible by allowing delegation to nested fields, which would greatly improve its utility.
macro_rules! __data_provider_to_field { | ||
($provider:path, $marker:path, &self.$field:tt.as_deserializing()) => { | ||
impl $crate::icu_provider::DataProvider<$marker> for $provider { | ||
fn load(&self, req: $crate::icu_provider::DataRequest) -> Result<$crate::icu_provider::DataResponse<$marker>, $crate::icu_provider::DataError> { | ||
let provider = $crate::icu_provider::prelude::AsDeserializingBufferProvider::as_deserializing(&self.$field); | ||
$crate::icu_provider::DataProvider::<$marker>::load(&provider, req) | ||
} | ||
} | ||
}; | ||
($provider:path, $marker:path, &self.$field:tt) => { | ||
impl $crate::icu_provider::DataProvider<$marker> for $provider { | ||
fn load(&self, req: $crate::icu_provider::DataRequest) -> Result<$crate::icu_provider::DataResponse<$marker>, $crate::icu_provider::DataError> { | ||
$crate::icu_provider::DataProvider::<$marker>::load(&self.$field, req) | ||
} | ||
} | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current macro implementation is very restrictive as it only allows delegating to a direct field of self
. The use of $field:tt
matches only a single token tree, so it won't work for nested fields like self.foo.bar
.
To make the macro more flexible and support nested field access, you can change $field:tt
to $($field:tt)+
. This will match one or more token trees, allowing for expressions like self.a.b.c
.
macro_rules! __data_provider_to_field { | |
($provider:path, $marker:path, &self.$field:tt.as_deserializing()) => { | |
impl $crate::icu_provider::DataProvider<$marker> for $provider { | |
fn load(&self, req: $crate::icu_provider::DataRequest) -> Result<$crate::icu_provider::DataResponse<$marker>, $crate::icu_provider::DataError> { | |
let provider = $crate::icu_provider::prelude::AsDeserializingBufferProvider::as_deserializing(&self.$field); | |
$crate::icu_provider::DataProvider::<$marker>::load(&provider, req) | |
} | |
} | |
}; | |
($provider:path, $marker:path, &self.$field:tt) => { | |
impl $crate::icu_provider::DataProvider<$marker> for $provider { | |
fn load(&self, req: $crate::icu_provider::DataRequest) -> Result<$crate::icu_provider::DataResponse<$marker>, $crate::icu_provider::DataError> { | |
$crate::icu_provider::DataProvider::<$marker>::load(&self.$field, req) | |
} | |
} | |
}; | |
} | |
macro_rules! __data_provider_to_field { | |
($provider:path, $marker:path, &self.$($field:tt)+.as_deserializing()) => { | |
impl $crate::icu_provider::DataProvider<$marker> for $provider { | |
fn load(&self, req: $crate::icu_provider::DataRequest) -> Result<$crate::icu_provider::DataResponse<$marker>, $crate::icu_provider::DataError> { | |
let provider = $crate::icu_provider::prelude::AsDeserializingBufferProvider::as_deserializing(&self.$($field)+); | |
$crate::icu_provider::DataProvider::<$marker>::load(&provider, req) | |
} | |
} | |
}; | |
($provider:path, $marker:path, &self.$($field:tt)+) => { | |
impl $crate::icu_provider::DataProvider<$marker> for $provider { | |
fn load(&self, req: $crate::icu_provider::DataRequest) -> Result<$crate::icu_provider::DataResponse<$marker>, $crate::icu_provider::DataError> { | |
$crate::icu_provider::DataProvider::<$marker>::load(&self.$($field)+, req) | |
} | |
} | |
}; | |
} |
impl $crate::icu_provider::DataProvider<$marker> for $provider { | ||
fn load(&self, req: $crate::icu_provider::DataRequest) -> Result<$crate::icu_provider::DataResponse<$marker>, $crate::icu_provider::DataError> { | ||
$crate::icu_provider::DataProvider::<$marker>::load(&self.$field, req) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a very small implementation which I'm not sure is worth a macro that has to be special cased for different providers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm ambivalent on this. I think it's fine, but I'm not strongly in favor of adding such a macro.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see it less about the macro and more about blessing this pattern as one we want to encourage, with a docs page, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so just write a docs page? the macro seems brittle and learning how to use the macro is more effort than writing the redirecting impl
See it in use in #6852