-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
alloc: Added as_slice
method to BinaryHeap
collection
#82331
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @kennytm (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
This comment has been minimized.
This comment has been minimized.
5dfb4cc
to
05e615b
Compare
This comment has been minimized.
This comment has been minimized.
05e615b
to
6233f3f
Compare
@kennytm I am sending a friendly ping here. Is there something I should improve in this PR? |
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.
cc @rust-lang/libs.
If no one opposes I'll r+ after the tracking issue is filed.
/// println!("{}", x); | ||
/// } | ||
/// ``` | ||
#[unstable(feature = "binary_heap_as_slice", issue = "82331")] |
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.
the number needs to be a tracking issue, not this PR
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.
Can you share a concrete real world use case for this?
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4, 5, 6, 7]); | ||
/// let slice = heap.as_slice(); | ||
/// | ||
/// // Will print in some order | ||
/// for x in slice { | ||
/// println!("{}", x); | ||
/// } |
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.
This is going to need a more realistic example. The point of example code is chiefly to show why you would want to call the method, not how to perform a method call. If someone is reading this documentation, we can assume they know how to make method calls. The example here does nothing to illustrate why as_slice
would be used, since if you needed to do this, you should be writing for x in &heap {...}
instead.
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.
@dtolnay Good question. I had bumped into it when I needed to efficiently serialize BinaryHeap. Some APIs (writer::write_all
in my case) may expect a slice of data, and it is going to be quite inefficient to clone the data or iterate over it item by item (when you only have a reference to the BinaryHeap and cannot use into_vec
)
Related: #82002 |
I could not find a more realistic example in the scope of std library, so I took the example from |
Ping from triage: |
@dtolnay I am kindly pinging you to make the decision |
I'm happy with the example as it is right now, so r=me once a tracking issue is filed and the issue number in the PR is updated. |
r? @Amanieu |
@bors r=Amanieu |
📌 Commit 595f3f2 has been approved by |
Rollup of 7 pull requests Successful merges: - rust-lang#82331 (alloc: Added `as_slice` method to `BinaryHeap` collection) - rust-lang#83130 (escape_ascii take 2) - rust-lang#83374 (unix: Fix feature(unix_socket_ancillary_data) on macos and other BSDs) - rust-lang#83543 (Lint on unknown intra-doc link disambiguators) - rust-lang#83636 (Add a regression test for issue-82792) - rust-lang#83643 (Remove a FIXME resolved by rust-lang#73578) - rust-lang#83644 (:arrow_up: rust-analyzer) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
…rntSushi Stabilize `binary_heap_as_slice` This PR stabilizes `binary_heap_as_slice`: ```rust // std::collections::BinaryHeap impl BinaryHeap<T> { pub fn as_slice(&self) -> &[T] } ``` <br> Tracking issue: rust-lang#83659. Implementation PR: rust-lang#82331. FCPs already completed in the tracking issue. Closes rust-lang#83659. r? libs-api
…rntSushi Stabilize `binary_heap_as_slice` This PR stabilizes `binary_heap_as_slice`: ```rust // std::collections::BinaryHeap impl BinaryHeap<T> { pub fn as_slice(&self) -> &[T] } ``` <br> Tracking issue: rust-lang#83659. Implementation PR: rust-lang#82331. FCPs already completed in the tracking issue. Closes rust-lang#83659. r? libs-api
…rntSushi Stabilize `binary_heap_as_slice` This PR stabilizes `binary_heap_as_slice`: ```rust // std::collections::BinaryHeap impl BinaryHeap<T> { pub fn as_slice(&self) -> &[T] } ``` <br> Tracking issue: rust-lang#83659. Implementation PR: rust-lang#82331. FCPs already completed in the tracking issue. Closes rust-lang#83659. r? libs-api
Rollup merge of rust-lang#124012 - slanterns:as_slice_stabilize, r=BurntSushi Stabilize `binary_heap_as_slice` This PR stabilizes `binary_heap_as_slice`: ```rust // std::collections::BinaryHeap impl BinaryHeap<T> { pub fn as_slice(&self) -> &[T] } ``` <br> Tracking issue: rust-lang#83659. Implementation PR: rust-lang#82331. FCPs already completed in the tracking issue. Closes rust-lang#83659. r? libs-api
I initially asked about whether it is useful addition on https://internals.rust-lang.org/t/should-i-add-as-slice-method-to-binaryheap/13816, and it seems there were no objections, so went ahead with this PR.
Please, guide me through if I need to add tests or something else.
UPD: Tracking issue #83659