Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign up[RFC] Make the implementations of FromVoid more generic which gives better ergonomics #143
Conversation
|
Can you reorder the commits, or at least not put the bumping commit in the middle of the others? Thanks. |
|
All but the last of the commits are from #132 |
|
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 * |
|
@jrmuizel Oh never mind then, didn't realise this was blocked on another PR. |
|
Would it maybe be possible to remove
|
| 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.
This comment has been minimized.
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.
This comment has been minimized.
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.
This comment has been minimized.
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.
This comment has been minimized.
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.
This comment has been minimized.
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.
This comment has been minimized.
jrmuizel
Jan 15, 2018
Author
Collaborator
I wonder if we should actually change CFArray to return an iterator of references instead of values...
|
@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 :) |
|
CFArrays can be used for types other than CFTypes. For example, CGFontCopyTableTags returns a CFArray of u32. |
|
|
This uses borrowed values from the array to avoid having to Retain/Release during iteration. It also implements borrowing for all TCFType.
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 -->
jrmuizel commentedJan 14, 2018
•
edited by larsbergstrom
This is probably a breaking change so would be good to include with 0.5 changes.
This change is