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

Replace clang.rs iterator with generic boxed Map<Range<>> #213

Closed
wants to merge 1 commit into from

Conversation

jeanphilippeD
Copy link
Contributor

@jeanphilippeD jeanphilippeD commented Nov 4, 2016

Add functions:
-ffi_call_index_iterator -> Box
for foreign function with unsigned length and index

-ffi_call_index_iterator_check_positive -> Option<Box>
for foreign function with signed length and index that can be -1

These function take two closures as parameters.
This interface should guide the correct usage of having thin
closures arround the actual two relevant ffi functions.

The ad hoc iterator where basically implementing
(0..clang_getNum()).map( |id| clang_getItem( self.x, id ) )
with some additional complexity if clang_getNum() could be -1.

Using these new function greatly improve maintainability
since now everything is in the function getting a specific iterator,
and all iterators implement ExactSizeIterator consistently.

The trade off is we now have dynamic dispatch, but that should not
be too problematic since we are calling a foreign function to get
the item.
Eventually rust will support impl trait (experimental feature
conservative_impl_trait which has a very close syntax) at which
point the Box can be replaced if needed and appropriate.

@highfive
Copy link

highfive commented Nov 4, 2016

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!

@jeanphilippeD
Copy link
Contributor Author

This is a simpler & cleaner alternative to PR #189

@emilio
Copy link
Contributor

emilio commented Nov 6, 2016

I'm still not totally sure I like this more than the status quo or #189. In general, I think the common things that have these iterators are:

  • They're ranged.
  • There are specialized functions that allow fetching an item and the count from that range, and getting the count may fail.

I think #189 cleaned up would end up being simpler and more idiomatic than this PR, but I also don't dislike this... Opinions, @fitzgen?

@fitzgen
Copy link
Member

fitzgen commented Nov 7, 2016

I prefer #189 -- we shouldn't need to box here.

@jeanphilippeD
Copy link
Contributor Author

Thank you for your feedback.

I agree, we should not need to box here.
My understanding is that is what the rust feature "unboxed_closure" should allow one day for the code in this PR.

My understanding is there is little performance gain to be have considering it redirect to ffi calls.
Is there a maintenance benefit to having unboxed iterator here, because the unboxed version seems more verbose and less local (and also seems to have a more complex implementation)?

@jeanphilippeD
Copy link
Contributor Author

I'll re-open and clean up PR #189

@fitzgen
Copy link
Member

fitzgen commented Nov 8, 2016

My understanding is that is what the rust feature "unboxed_closure" should allow one day for the code in this PR.

We have unboxed closures in Rust already, I think the "return impl trait" (or -> impl Trait) is what you're looking for.

@emilio
Copy link
Contributor

emilio commented Nov 10, 2016

Yep, in general you want something like -> impl ExactSizeIterator<Item=Foo>, but I'm not sure it's stable, and we need to keep bindgen in stable if we ever want to run it in mozilla-central...

@bors-servo
Copy link

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

Add functions:
-ffi_call_index_iterator -> Box<ExactSizeIterator>
 for foreign function with unsigned length and index

-ffi_call_index_iterator_check_positive -> Option<Box<ExactSizeIterator>>
 for foreign function with signed length and index that can be -1

These function take two closures as parameters.
This interface should guide the correct usage of having thin
closures arround the actual two relevant ffi functions.

The ad hoc iterator where basically implementing
(0..clang_getNum()).map( |id| clang_getItem( self.x, id ) )
with some additional complexity if clang_getNum() could be -1.

Using these new function greatly improve maintainability
since now everything is in the function getting a specific iterator,
and all iterators implement ExactSizeIterator consistently.

The trade off is we now have dynamic dispatch, but that should not
be too problematic since we are calling a foreign function to get
the item.
Eventually rust will support impl trait (experimental feature
conservative_impl_trait which has a very close syntax)  at which
point the Box can be replaced if needed and appropriate.
@jeanphilippeD
Copy link
Contributor Author

Hi @fitzgen ,

Thank you for your comment.
Finally got around to try your suggestion, using impl trait (it is in unstable feature(conservative_impl_trait)).
Also allowed me to understand life time better and I updated this PR accordingly.

The commit 0c769db shows the migration path.
Tested locally with rustc 1.14.0-nightly (cae6ab1c4 2016-11-05)

impl trait was implemented in rust 1.12, but will take some time before it is stable.
https://github.com/rust-lang/rfcs/blob/master/text/1522-conservative-impl-trait.md
rust-lang/rust#34511

This version seems to be dealing with lifetime in a better way than what we have in master, it seems before we were copying CXComment and CXType, and the iterator may have outlived the parent Comment and Type (is that an issue here?).

I still feel it make sense to use standard iterator types but if there are more important things to consider, I'll improve PR #189

Thanks,
JP

@bors-servo
Copy link

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

@emilio
Copy link
Contributor

emilio commented Feb 13, 2017

I believe this can be closed for now, due to the lack of activity. Thanks for the PR and the discussion!

@emilio emilio closed this Feb 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants