-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
(Rust implementation) Add idiomatic rust iterator for Vector #5166
Conversation
…Follow (get implementation). Immutable iterator that respects lifetimes. This allows for more idiomatic combinator/iterator access of vector types. Small fix for old rust version. I didn't realize flatbuffers TravisCI is targeting such an old version of rust
70b13ac
to
89e65ad
Compare
@jaynus LGTM! Would you add a no-op commit to get CI to run again? Looks like Appveyor had an error. |
Done and passed. |
@jaynus Would you please add a test for this? Perhaps as a sub- |
@jaynus Or, minimally, by adding a vector iterator assert to the |
This would be a great change! In this commit that was just merged, I had to add some code in a few spots to
Those might be good candidates to replace with code that tests out this new functionality. Not exactly sure, but maybe something like this:
|
…he prop function. this means the iterator is tested for every prop generation test cycle
As per the commit note, added a small test addition to roundtrip_vectors, which is implemented in the prop function which creates and tests a vector of a given type. this means the iterator is tested for every prop generation test cycle (every time used in roundtrip vectors) |
@jaynus Cool! Let's add your vector
|
I don't think this works as expected with regards to lifetimes. Specifically, the items returned by the iterator has a lifetime limited by the borrowing of the vector, instead of by the lifetime argument in the vector itself. I would expect all four of these functions to compile (and give the same result): https://gist.github.com/TethysSvensson/2615a8a1aff7f243fda78c3c1e1ced51 In the current implementation, only the first and last one compiles. I think part of it is that |
I think this PR should include something along the lines of this to work as expected: (This commit comes from a branch we use at my work. It contains a cherry-pick of your commit, but also adds additional derives to the generated enums that we don't expect to get upstreamed.) |
@TethysSvensson Thanks for thinking hard about the lifetimes! @jaynus WDYT? |
…ator clone+copy, and fixes lifetime issue with iterator references to vectors
I've updated this PR with multiple cases. I apologize for the delay, I just got wrapped up in other work for a while.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated for all these requested changes.
Ping |
@rw Deriving Clone an Copy is not possible in this case since it puts a Copy / Clone bound on T. In this case we only need to be able to copy the marker to T (PhantomData in this case), not the actual T type. |
@jean-airoldie @jaynus Is this ready to go in? I know there was the other PR @jean-airoldie worked on, so I want to make sure nothing is missed. Otherwise this LGTM. |
@rw Yes its pretty much ready. The only thing I found is that some bounds could be less strict. |
I am here. This has just fallen through the cracks of my github alerts. What is needed to get this finalized and merged? |
|
Editing in browser as I'm afk for the weekend.
rust/flatbuffers/src/vector.rs
Outdated
@@ -52,7 +52,7 @@ impl<'a, T: 'a> Vector<'a, T> { | |||
} | |||
} | |||
|
|||
impl<'a, T: 'a> Vector<'a, T> { | |||
impl<'a, T: 'a> Vector<'a, T: Follow<'a> + 'a> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be impl<'a, T: 'a + Follow<'a>> Vector<'a, T> {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Woops What I got for editing on the road :)
@@ -28,6 +28,14 @@ use primitives::*; | |||
#[derive(Debug)] | |||
pub struct Vector<'a, T: 'a>(&'a [u8], usize, PhantomData<T>); | |||
|
|||
impl<'a, T: 'a> Clone for Vector<'a, T> { | |||
fn clone(&self) -> Self { | |||
Vector(self.0, self.1, self.2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use the Copy
instance to implement the Clone
instance. This makes it more clear that Copy == Clone
for this type.
Specifically you can replace the body with *self
.
However it might be late enough in the process that this should be changed in a later commit.
} | ||
} | ||
|
||
impl<'a, T: Follow<'a> + 'a> Iterator for VectorIter<'a, T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jaynus Ping. |
@jaynus @jean-airoldie @TethysSvensson Looks like both PRs went stagnant. What do you think about merging one of them? |
@rw: I have been managing a fork of this repo for my work and my fork includes a more efficient version of this PR. I would be happy if I could get some or all of it upstreamed. So far I have created #5577, #5579 and #5580 which contains most of the changes in my fork. My version of this PR is in #5579. |
Implemented by #5579. Thanks @jaynus @jean-airoldie @TethysSvensson for working on this! |
Implemented for T: Follow (which provides get accessor).
In rust, it is more idiomatic to use iterators and combinators on 'collections' of data instead of a len()/get() set. The Flatbuffers implementation currently requires:
Immutable iterator that respects lifetimes. This allows for more idiomatic combinator/iterator access of vector types in flatbuffers. Iterator respects inner vector lifetimes, and allocates single iterator object for tracking (reference + iterator location track).