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

Higher level dictionary find/contains_key #129

Merged
merged 3 commits into from Nov 23, 2017

Conversation

@faern
Copy link
Contributor

faern commented Nov 22, 2017

I'm not very familiar with Core Foundation yet, so tell me if this is not a very helpful addition.

Adding versions of find and contains_key that can take any TCFType and automatically converts it to the raw pointer of its corresponding ref type. It looks like dictionaries will mostly contain types implementing this trait, and thus these versions of the methods can make it a bit more ergonomic to use the dictionary. The best would of course be if we could return the correct type as well, but I guess that is not possible with the available information.

Better suggestions for the names are welcome :)


This change is Reviewable

let value = self.find(key);
if value.is_none() {
panic!("No entry found for key {:p}", key);
match self.find(key) {

This comment has been minimized.

@KiChjang

KiChjang Nov 22, 2017

Member

This looks like an expanded version of expect.

This comment has been minimized.

@faern

faern Nov 22, 2017

Author Contributor

Indeed. I just kept the panic! existing before this PR. With expect I can't do the formatting as easily (will need an extra format!) and I did not want to remove the printing of the pointer. I don't see the point in printing the pointer but maybe there is one so I kept it. If not, the entire implementation can simply be

self.find(key).expect("No entry found for key")

To be honest I don't see the reason to have this get at all. Much more explicit to use find and have to unwrap yourself if you want panicking behavior.

I guess it would break some stuff, but I think it would be cleaner to get rid of this panicking version altogether and rename find to get. From my experience get is a more common name with dictionaries, plus it would match better with the underlying CFDictionaryGetValueIfPresent.

This comment has been minimized.

@KiChjang

KiChjang Nov 22, 2017

Member

Ah, so there's the format! macro that you can use if formatting is the problem you're facing.

This comment has been minimized.

@faern

faern Nov 22, 2017

Author Contributor

Yes. So under the conditions that get should remain and that it should panic with the pointer in the message, now it probably has the simplest implementation.

This comment has been minimized.

@faern

faern Nov 22, 2017

Author Contributor

However, this version is forced to always allocate and format the string, even in the happy case. So the previous solution probably had better performance.

@jdm
Copy link
Member

jdm commented Nov 23, 2017

@bors-servo
Copy link
Contributor

bors-servo commented Nov 23, 2017

📌 Commit c445b6e has been approved by jdm

@bors-servo
Copy link
Contributor

bors-servo commented Nov 23, 2017

Testing commit c445b6e with merge 40165e7...

bors-servo added a commit that referenced this pull request Nov 23, 2017
Higher level dictionary find/contains_key

I'm not very familiar with Core Foundation yet, so tell me if this is not a very helpful addition.

Adding versions of find and contains_key that can take any `TCFType` and automatically converts it to the raw pointer of its corresponding ref type. It looks like dictionaries will mostly contain types implementing this trait, and thus these versions of the methods can make it a bit more ergonomic to use the dictionary. The best would of course be if we could return the correct type as well, but I guess that is not possible with the available information.

Better suggestions for the names are welcome :)

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/129)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

bors-servo commented Nov 23, 2017

☀️ Test successful - status-travis
Approved by: jdm
Pushing 40165e7 to master...

@bors-servo bors-servo merged commit c445b6e into servo:master Nov 23, 2017
2 checks passed
2 checks passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
homu Test successful
Details
@faern faern deleted the faern:higher-level-dictionary-find branch Nov 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants
You can’t perform that action at this time.