Skip to content

Commit adb4d2e

Browse files
Rollup merge of #154011 - usamoi:binary_heap_as_mut_slice, r=Mark-Simulacrum
implement `BinaryHeap::as_mut_slice` Tracking issue: #154009
2 parents e8b594b + cab72ae commit adb4d2e

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

  • library/alloc/src/collections/binary_heap

library/alloc/src/collections/binary_heap/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,37 @@ impl<T, A: Allocator> BinaryHeap<T, A> {
13641364
self.data.as_slice()
13651365
}
13661366

1367+
/// Returns a mutable slice of all values in the underlying vector.
1368+
///
1369+
/// # Safety
1370+
///
1371+
/// The caller must ensure that the slice remains a max-heap, i.e. for all indices
1372+
/// `0 < i < slice.len()`, `slice[(i - 1) / 2] >= slice[i]`, before the borrow ends
1373+
/// and the binary heap is used.
1374+
///
1375+
/// # Examples
1376+
///
1377+
/// Basic usage:
1378+
///
1379+
/// ```
1380+
/// #![feature(binary_heap_as_mut_slice)]
1381+
///
1382+
/// use std::collections::BinaryHeap;
1383+
///
1384+
/// let mut heap = BinaryHeap::<u32>::from([1, 2, 3, 4, 5, 6, 7]);
1385+
///
1386+
/// unsafe {
1387+
/// for value in heap.as_mut_slice() {
1388+
/// *value = (*value).saturating_mul(2);
1389+
/// }
1390+
/// }
1391+
/// ```
1392+
#[must_use]
1393+
#[unstable(feature = "binary_heap_as_mut_slice", issue = "154009")]
1394+
pub unsafe fn as_mut_slice(&mut self) -> &mut [T] {
1395+
self.data.as_mut_slice()
1396+
}
1397+
13671398
/// Consumes the `BinaryHeap` and returns the underlying vector
13681399
/// in arbitrary order.
13691400
///

0 commit comments

Comments
 (0)