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

ZeroTrie: change map_store to pub convert_store #4022

Merged
merged 4 commits into from
Sep 12, 2023

Conversation

sffc
Copy link
Member

@sffc sffc commented Sep 11, 2023

Toward #2909

I think @robertbastian did not like the map_store function, but I needed it again, and this time outside the crate. I made a new function named cast_store that requires a From impl and made it public. Happy to choose some other name.

Although this logic is possible to write outside this crate, I prefer to keep it in this crate. Here is how one could write this logic without this function:

ZeroTrieSimpleAscii::from(NewStore::from(trie.take_store()))

Note that the trie type needs to be named. With this PR, you can simply do:

trie.cast_store::<NewStore>()

@sffc sffc marked this pull request as ready for review September 11, 2023 09:53
@sffc sffc requested a review from a team as a code owner September 11, 2023 09:53
@sffc sffc requested a review from robertbastian September 11, 2023 09:53
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.

For context: #2722 (comment)

Doesn't NewStore::from(trie.take_store()).into() work without naming the type?

_ => return Err(D::Error::custom("invalid ZeroTrie tag")),
};
let zerotrie = match *tag {
tags::SIMPLE_ASCII => ZeroTrieSimpleAscii::from_store(trie_bytes)
Copy link
Member

Choose a reason for hiding this comment

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

Does this need to use cast_store? We have a store, so why not convert and then create the trie, instead of create and then convert.

@sffc
Copy link
Member Author

sffc commented Sep 11, 2023

Doesn't NewStore::from(trie.take_store()).into() work without naming the type?

An extra type inference is needed when calling Into. cast_store() carries the type from trie directly to the caller. This is needed for example when chaining a call to .into_zerotrie().

@sffc sffc requested a review from robertbastian September 11, 2023 17:36
Manishearth
Manishearth previously approved these changes Sep 11, 2023
Copy link
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

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

defer to rob on review but drive-by review: i like this

@sffc
Copy link
Member Author

sffc commented Sep 11, 2023

defer to rob on review but drive-by review: i like this

Are you happy with the method name?

It's not really a "cast" in the sense that it runs actual code, not just a type system inference (static cast) or type id check (dynamic cast). This is a converting cast invoking the From impl.

Bikeshed:

  • into_store (but the return value is not a store)
  • remap_store (new word with new meaning?)
  • map_store (but the mapping function is implicit)
  • change_store
  • transform_store
  • recreate_store
  • convert_store
  • transfigure_store

Maybe convert_store is the best. It's not an entirely new term to ICU4X but it's fairly specific to the circumstance when used.

@Manishearth
Copy link
Member

a From impl is actual code. I don't mind as long as it's well documented

robertbastian
robertbastian previously approved these changes Sep 12, 2023
@sffc sffc changed the title ZeroTrie: change map_store to pub cast_store ZeroTrie: change map_store to pub convert_store Sep 12, 2023
@sffc
Copy link
Member Author

sffc commented Sep 12, 2023

cargo fmt. :(

autosubmit = on

@robertbastian robertbastian merged commit 8aad29e into unicode-org:main Sep 12, 2023
@sffc sffc deleted the ztrie2 branch September 12, 2023 16:30
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