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

Add DataPayload::dynamic_cast with example #5467

Merged
merged 1 commit into from
Aug 30, 2024
Merged

Conversation

sffc
Copy link
Member

@sffc sffc commented Aug 30, 2024

I came across this in the context of the transliteration example. Adding this function makes forking providers easier to write.

@sffc sffc merged commit 6b5a69c into unicode-org:main Aug 30, 2024
28 checks passed
@sffc sffc deleted the dynamic-cast branch August 30, 2024 22:17
Copy link
Member

@robertbastian robertbastian left a comment

Choose a reason for hiding this comment

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

This should be applied to the tutorials:

let mut any_payload = (&mut res.payload) as &mut dyn Any;
if let Some(mut decimal_payload) = any_payload.downcast_mut::<DataPayload<DecimalSymbolsV1Marker>>() {

/// {
/// #[inline]
/// fn load(&self, req: DataRequest) -> Result<DataResponse<M>, DataError> {
/// if TypeId::of::<HelloWorldV1Marker>() == TypeId::of::<M>() {
Copy link
Member

Choose a reason for hiding this comment

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

Everywhere we compare data markers types we compare M::INFO. Comparing TypeId::of::<M>() is a new way to do this and we should agree on one.

Copy link
Member Author

@sffc sffc Sep 3, 2024

Choose a reason for hiding this comment

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

Hmm, interesting observation.

If we have access to both marker types, comparing TypeId is probably more efficient. There might still be cases where we have a DynamicDataMarker and we have to compare the INFO.

@sffc
Copy link
Member Author

sffc commented Sep 3, 2024

This should be applied to the tutorials:

downcast_mut and dynamic_cast_mut still exist. This PR adds dymamic_cast, which is behavior that didn't exist in the library before.

@robertbastian
Copy link
Member

Right, the tutorial should use dynamic_cast_mut

@robertbastian
Copy link
Member

#5490

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.

3 participants