Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Use IndexSet instead of Vec for codepoints #283

Merged
merged 2 commits into from
Dec 5, 2022
Merged

Conversation

cmyr
Copy link
Member

@cmyr cmyr commented Nov 30, 2022

This ensures that codepoints are unique, but also respects the original order in which they appear in the file.

This is an alternative to #268. Although it brings in a dependency, it leads to a much simpler and more efficient implementation overall.

Thoughts @madig ?

This ensures that codepoints are unique, but also respects the original
order in which they appear in the file.
@RickyDaMa
Copy link
Collaborator

indexmap is already pulled in as a transitive dependency, so this seems like a cheap solution to the problem while rocking the boat as little as possible

All it may end up doing is meaning users of the library have to pull in indexmap themselves non-transitively to be able to interact with the data type, depending on what they're doing with it

This creates a case for keeping codepoints private and exposing methods to interact with it, as then crate consumers don't have to care about the underlying data types being used

@madig
Copy link
Collaborator

madig commented Dec 2, 2022

The readme says:

Preserves insertion order as long as you don't call .remove().

Hm :) Are we gonna let that stop our OCD?

@madig
Copy link
Collaborator

madig commented Dec 2, 2022

Otherwise, I'm ok with pulling it in and exposing it directly in the API. I don't like making delegation functions...

@RickyDaMa
Copy link
Collaborator

I'm ok with pulling it in and exposing it directly in the API

Personally I'd rather not, however it is a common dependency so definitely not the end of the world.

As far as I can tell from their documentation, only remove changes item order, not something like retain

@cmyr
Copy link
Member Author

cmyr commented Dec 2, 2022

So I'm okay going either way with this. Fwiw it would be easy enough to modify the contents without needing to have the crate in scope, e.g.

my_glyph.codepoints.clear();
my_glyph.codepoints.extend(['a', 'b', 'c']);

but I think maybe not having it be public API is a strictly better design, so I'll go ahead and do that.

@cmyr
Copy link
Member Author

cmyr commented Dec 2, 2022

Okay, I've updated this with a new custom Codepoints type, wrapping an indexset.

self.0.is_empty()
}

/// Set the codepoints. See [Codepoints::new][] for useage.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd remove the [] here, at best they go through to the rustdoc as is, or at worst they break your link.
While I'm on this line of code, it's "usage" 😉

@RickyDaMa
Copy link
Collaborator

Looks good to me! The only this to bear in mind is that we are still exposing the iterator type from indexmap, but I don't anticipate that causing any issues given that you're always going to be interacting with it through standard APIs

You could also trivially make a borrowing iterator that returns owned chars, since char is Copy, e.g. by doing:

fn iter(&self) -> impl Iterator<Item = char> {
    self.0.iter().copied()
}

@cmyr
Copy link
Member Author

cmyr commented Dec 5, 2022

@RickyDaMa unfortunately I can't use impl Iterator with the IntoIterator trait. :(

@RickyDaMa
Copy link
Collaborator

Yeah in IntoIterator I knew you wouldn't be able to, hence not proposing a solution there, more just making the observation. I would expect it to be fine though

Maybe in some future version of the Rust compiler 😄

@cmyr cmyr force-pushed the codepoints-indexset branch from 70a2b3c to a91f187 Compare December 5, 2022 18:36
@cmyr cmyr merged commit 1ef6f20 into master Dec 5, 2022
@cmyr cmyr deleted the codepoints-indexset branch December 5, 2022 20:10
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants