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

[RFC] Make the implementations of FromVoid more generic which gives better ergonomics #143

Closed
wants to merge 8 commits into from

Conversation

@jrmuizel
Copy link
Collaborator

jrmuizel commented Jan 14, 2018

This is probably a breaking change so would be good to include with 0.5 changes.


This change is Reviewable

@nox
Copy link
Member

nox commented Jan 14, 2018

Can you reorder the commits, or at least not put the bumping commit in the middle of the others? Thanks.

@jrmuizel
Copy link
Collaborator Author

jrmuizel commented Jan 14, 2018

All but the last of the commits are from #132

@jrmuizel jrmuizel force-pushed the jrmuizel:cfarray-more-type branch from 4988599 to cc141bb Jan 14, 2018
@jrmuizel
Copy link
Collaborator Author

jrmuizel commented Jan 14, 2018

I needed to drop the re-implementation of get_values() because we don't have a way to implement it only for types that that can be transmuted from const void *

@nox
Copy link
Member

nox commented Jan 14, 2018

@jrmuizel Oh never mind then, didn't realise this was blocked on another PR.

@faern
Copy link
Contributor

faern commented Jan 15, 2018

Would it maybe be possible to remove FromVoid completely and use TCFTypeRef in it's place? They are very similar, but I have not checked if it would be completely compatible, but I think so.

TCFTypeRef was meant as a trait for all the ref types from the sys crate which actually all are *const Foo. And I guess a CFArray is only meant to contain CF types? Then it would not just make type sense, but also meaningful sense to express the generic of the array with that trait.

x
unsafe impl<T> FromVoid for *const T {
unsafe fn from_void(x: *const c_void) -> Self {
mem::transmute(x)

This comment has been minimized.

@faern

faern Jan 15, 2018

Contributor

This can be done without transmute, just with x as *const T, like I do in TCFTypeRef::from_void_ptr.

TCFType::wrap_under_get_rule(mem::transmute(x))
unsafe impl<T: TCFType> FromVoid for T where T::Ref: FromVoid {
unsafe fn from_void(x: *const c_void) -> Self {
TCFType::wrap_under_get_rule(T::Ref::from_void(x))

This comment has been minimized.

@faern

faern Jan 15, 2018

Contributor

Is it maybe a bit risky to assume any void pointer should be wrapped under the get rule? I guess that depend on how one obtain the pointer? I might be mistaken how it's used, but that is my impression.

A possibly safer way would be to force the user to do what I do in downcast: https://github.com/servo/core-foundation-rs/pull/143/files#diff-e49ae9facf583e1f0d73ba355079e544R199
Where they must convert their void pointer to an appropriate ref type and then apply one of the ordinary constructors wrap_under_get_rule or wrap_under_create_rule.

This comment has been minimized.

@jrmuizel

jrmuizel Jan 15, 2018

Author Collaborator

I'm not sure I understand how that would work. from_void is only used internally in array.rs and is unsafe. My understanding is that CFArray contents would always be accessed with wrap_under_get_rule.

This comment has been minimized.

@nox

nox Jan 15, 2018

Member

Is it maybe a bit risky to assume any void pointer should be wrapped under the get rule? I guess that depend on how one obtain the pointer? I might be mistaken how it's used, but that is my impression.

@faern It's never "more" unsafe to wrap anything under the get rule. Worst case, it will retain a value too much and leak it. I wouldn't worry about this too much for now, given we have no notion of returning a borrow of an already-retained value.

This comment has been minimized.

@faern

faern Jan 15, 2018

Contributor

True, it's not unsafe to leak. Bad wording on my part I guess. But maybe the documentation should at least state that the retain count will be incremented on this operation? :)

This comment has been minimized.

@jrmuizel

jrmuizel Jan 15, 2018

Author Collaborator

I wonder if we should actually change CFArray to return an iterator of references instead of values...

@faern
Copy link
Contributor

faern commented Jan 15, 2018

@nox That other PR is, however, not blocked. It's ready for merge. And I just rebased it to make the version bump the last commit :)

@jrmuizel
Copy link
Collaborator Author

jrmuizel commented Jan 15, 2018

CFArrays can be used for types other than CFTypes. For example, CGFontCopyTableTags returns a CFArray of u32.

@jrmuizel jrmuizel force-pushed the jrmuizel:cfarray-more-type branch from cc141bb to 0c2cf57 Jan 15, 2018
@jdm
jdm approved these changes Jan 17, 2018
@bors-servo
Copy link
Contributor

bors-servo commented Jan 18, 2018

The latest upstream changes (presumably #141) made this pull request unmergeable. Please resolve the merge conflicts.

This uses borrowed values from the array to avoid having to
Retain/Release during iteration. It also implements borrowing
for all TCFType.
@jrmuizel jrmuizel force-pushed the jrmuizel:cfarray-more-type branch from 0c2cf57 to 922da73 Jan 24, 2018
@jrmuizel jrmuizel closed this Jan 24, 2018
bors-servo added a commit that referenced this pull request Jan 25, 2018
Add the ability to borrow values from the array.

This is a replacement for #143. It allows iteration while avoiding the reference count churn of the previous approach. It uses a TCFTypeBorrow (could probably use a better name) to hold a sort of reference to the array data. I'm interested in people's thoughts and opinions on this approach.

<!-- 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/147)
<!-- Reviewable:end -->
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

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