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

Implement FuturesUnordered::iter_mut #618

Merged

Conversation

srijs
Copy link
Contributor

@srijs srijs commented Oct 22, 2017

Implementing FuturesUnordered::iter_mut as suggested in #605. Generally it should be said that I don't have a lot of experience with unsafe rust yet, so I would appreciate a thorough review!

I did not go for the other suggested ones (into_iter/drain), since I lack the insight into how to implement this safely without leaking memory.

@srijs srijs force-pushed the feat/futures-unordered-iter-mut branch 3 times, most recently from d022efb to f21c2a1 Compare October 22, 2017 05:17
Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The impl looks correct to me. I added some naming related comments.

@@ -427,6 +436,37 @@ impl<T> Drop for FuturesUnordered<T> {
}
}

#[derive(Debug)]
/// Mutable iterator over all futures in the unordered set.
pub struct FuturesUnorderedIterMut<'a, F: 'a> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably just name this IterMut as it already is in the futures_unordered module.

@@ -112,7 +112,7 @@ if_std! {
pub use self::collect::Collect;
pub use self::wait::Wait;
pub use self::split::{SplitStream, SplitSink};
pub use self::futures_unordered::{futures_unordered, FuturesUnordered};
pub use self::futures_unordered::{futures_unordered, FuturesUnordered, FuturesUnorderedIterMut};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably wouldn't re-export this type. Instead, maybe make the futures_unordered module pub (and ensure no priv types are accidentally exported).

@srijs srijs force-pushed the feat/futures-unordered-iter-mut branch from f21c2a1 to 1f8339f Compare October 24, 2017 08:53
@srijs
Copy link
Contributor Author

srijs commented Oct 24, 2017

@carllerche Thanks for the review, addressed all feedback!

@alexcrichton
Copy link
Member

Inside this module there's a public futures_unordered function which if we make the module public will also make that function reachable (but it's not intentional to have that function reachable. I think I'm ok making the module public for the iterator name (albeit it's a bit of a wonky setup) but can we ensure that the free function isn't exposed?

pub struct IterMut<'a, F: 'a> {
node: *const Node<F>,
len: usize,
_marker: PhantomData<&'a F>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this may best be &'a mut FuturesUnordered<F> perhaps?

Copy link
Contributor Author

@srijs srijs Oct 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies -- can you go into more details why that would be better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long long ago in Rust there were subtle distinctions between how &mut T and &T worked, and I forget if any of them are still true today. Basically because this is a mutable iterator it's "better" to have a phantom data of &mut ... instead of &... to ensure this information is conveyed to the compiler.

Additionally I figure it's best to store FuturesUnordered<F> as that's effectively what's happening here (vs just F). This can also cause subtle differences but probably not as important as &mut vs not

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explaining that, changed it!

@carllerche
Copy link
Member

Which release of rust included pub(crate)? I would guess that we should be able to use it soon if not now.

@alexcrichton
Copy link
Member

Unsure, but the function can also just be moved

@srijs
Copy link
Contributor Author

srijs commented Oct 30, 2017

@carllerche According to this, pub(crate) was stabilised in 1.18.

@alexcrichton the futures_unordered function is exported, according to the docs: https://docs.rs/futures/0.1.16/futures/stream/fn.futures_unordered.html

@alexcrichton
Copy link
Member

@srijs right yeah but as is this would also export futures::stream::futures_unordered::futures_unordered (that is, a function in the module). Ideally only futures::stream::futures_unordered would be the exported function.

@srijs srijs force-pushed the feat/futures-unordered-iter-mut branch from 1f8339f to cce3c07 Compare November 1, 2017 10:00
@srijs
Copy link
Contributor Author

srijs commented Nov 1, 2017

@alexcrichton Implemented your feedback.

Now that we have FromIterator for FuturesUnordered, could it be an option to deprecate the free-standing function, or do still see a use-case for it which is not covered by from_iter?

@srijs srijs force-pushed the feat/futures-unordered-iter-mut branch from cce3c07 to 8c7a70b Compare November 1, 2017 10:05
@@ -103,7 +103,7 @@ if_std! {
mod wait;
mod channel;
mod split;
mod futures_unordered;
mod futures_unordered_impl;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I was thinking that we'd still want a futures_unordered module which contained the FuturesUnordered type, for example. I think we'll just want to move the definition of the constructor, futures_unordered, from the module to this stream module

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, changed!

@alexcrichton
Copy link
Member

Ah yeah so for now I personally like the free function for symmetry with the rest of the crate, but eventual deprecation is of course possible!

@srijs srijs force-pushed the feat/futures-unordered-iter-mut branch from 8c7a70b to 16ee1d3 Compare November 2, 2017 09:22
@srijs
Copy link
Contributor Author

srijs commented Nov 2, 2017

@alexcrichton Feedback addressed, let me know if you'd like me to change anything else!

@srijs srijs force-pushed the feat/futures-unordered-iter-mut branch from 16ee1d3 to d59b8a3 Compare November 2, 2017 09:51
@alexcrichton alexcrichton merged commit 5a4bfcb into rust-lang:master Nov 2, 2017
@alexcrichton
Copy link
Member

Awesome, thanks!

@srijs srijs deleted the feat/futures-unordered-iter-mut branch November 3, 2017 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants