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

alloc: Added as_slice method to BinaryHeap collection #82331

Merged
merged 3 commits into from
Mar 30, 2021

Conversation

frol
Copy link
Contributor

@frol frol commented Feb 20, 2021

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.

There is BinaryHeap::into_vec, but it consumes the value. I wonder if there is API design limitation that should be taken into account. Implementation-wise, the inner buffer is just a Vec, so it is trivial to expose as_slice from it.

Please, guide me through if I need to add tests or something else.

UPD: Tracking issue #83659

@rust-highfive
Copy link
Collaborator

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.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 20, 2021
@rust-log-analyzer

This comment has been minimized.

@frol frol force-pushed the feat/std-binary-heap-as-slice branch from 5dfb4cc to 05e615b Compare February 20, 2021 13:32
@rust-log-analyzer

This comment has been minimized.

@frol frol force-pushed the feat/std-binary-heap-as-slice branch from 05e615b to 6233f3f Compare February 20, 2021 18:46
@frol
Copy link
Contributor Author

frol commented Feb 25, 2021

@kennytm I am sending a friendly ping here. Is there something I should improve in this PR?

Copy link
Member

@kennytm kennytm left a 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")]
Copy link
Member

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

Copy link
Member

@dtolnay dtolnay left a 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?

Comment on lines 902 to 908
/// 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);
/// }
Copy link
Member

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.

Copy link
Contributor Author

@frol frol Mar 1, 2021

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)

@Dylan-DPC-zz Dylan-DPC-zz added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 27, 2021
@m-ou-se
Copy link
Member

m-ou-se commented Mar 3, 2021

Related: #82002

@frol
Copy link
Contributor Author

frol commented Mar 13, 2021

I could not find a more realistic example in the scope of std library, so I took the example from Vec::as_slice. @dtolnay If you feel that it is not worth adding to the standard library, I am fine to close this PR.

@JohnCSimon
Copy link
Member

Ping from triage:
@frol looks like dtolnay hasn't responded to this... do you want to close this PR?

@frol
Copy link
Contributor Author

frol commented Mar 29, 2021

@dtolnay I am kindly pinging you to make the decision

@Amanieu
Copy link
Member

Amanieu commented Mar 29, 2021

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.

@m-ou-se m-ou-se assigned Amanieu and unassigned kennytm Mar 29, 2021
@m-ou-se m-ou-se added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Mar 29, 2021
@frol
Copy link
Contributor Author

frol commented Mar 29, 2021

r? @Amanieu

@dtolnay
Copy link
Member

dtolnay commented Mar 29, 2021

@bors r=Amanieu

@bors
Copy link
Contributor

bors commented Mar 29, 2021

📌 Commit 595f3f2 has been approved by Amanieu

@bors bors removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Mar 29, 2021
@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Mar 29, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 30, 2021
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
@bors bors merged commit 2843baa into rust-lang:master Mar 30, 2021
@rustbot rustbot added this to the 1.53.0 milestone Mar 30, 2021
@dtolnay dtolnay self-assigned this Mar 24, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 7, 2024
…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
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 7, 2024
…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
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 7, 2024
…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
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jun 7, 2024
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.