pub struct PeekMut<'a, T: 'a + Ord, A: Allocator = Global> {
heap: &'a mut BinaryHeap<T, A>,
original_len: Option<NonZero<usize>>,
}
Expand description
Structure wrapping a mutable reference to the greatest item on a
BinaryHeap
.
This struct
is created by the peek_mut
method on BinaryHeap
. See
its documentation for more.
Fields§
§heap: &'a mut BinaryHeap<T, A>
§original_len: Option<NonZero<usize>>
Implementations§
Source§impl<'a, T: Ord, A: Allocator> PeekMut<'a, T, A>
impl<'a, T: Ord, A: Allocator> PeekMut<'a, T, A>
Sourcepub fn refresh(&mut self) -> bool
🔬This is a nightly-only experimental API. (binary_heap_peek_mut_refresh
#138355)
pub fn refresh(&mut self) -> bool
binary_heap_peek_mut_refresh
#138355)Sifts the current element to its new position.
Afterwards refers to the new element. Returns if the element changed.
§Examples
The condition can be used to upper bound all elements in the heap. When only few elements are affected, the heap’s sort ensures this is faster than a reconstruction from the raw element list and requires no additional allocation.
#![feature(binary_heap_peek_mut_refresh)]
use std::collections::BinaryHeap;
let mut heap: BinaryHeap<u32> = (0..128).collect();
let mut peek = heap.peek_mut().unwrap();
loop {
*peek = 99;
if !peek.refresh() {
break;
}
}
// Post condition, this is now an upper bound.
assert!(*peek < 100);
When the element remains the maximum after modification, the peek remains unchanged:
#![feature(binary_heap_peek_mut_refresh)]
use std::collections::BinaryHeap;
let mut heap: BinaryHeap<u32> = [1, 2, 3].into();
let mut peek = heap.peek_mut().unwrap();
assert_eq!(*peek, 3);
*peek = 42;
// When we refresh, the peek is updated to the new maximum.
assert!(!peek.refresh(), "42 is even larger than 3");
assert_eq!(*peek, 42);
Trait Implementations§
Auto Trait Implementations§
impl<'a, T, A> Freeze for PeekMut<'a, T, A>
impl<'a, T, A> RefUnwindSafe for PeekMut<'a, T, A>where
A: RefUnwindSafe,
T: RefUnwindSafe,
impl<'a, T, A> Send for PeekMut<'a, T, A>
impl<'a, T, A> Sync for PeekMut<'a, T, A>
impl<'a, T, A> Unpin for PeekMut<'a, T, A>
impl<'a, T, A = Global> !UnwindSafe for PeekMut<'a, T, A>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> SizedTypeProperties for T
impl<T> SizedTypeProperties for T
Source§#[doc(hidden)] const IS_ZST: bool = _
#[doc(hidden)] const IS_ZST: bool = _
🔬This is a nightly-only experimental API. (
sized_type_properties
)Source§#[doc(hidden)] const LAYOUT: Layout = _
#[doc(hidden)] const LAYOUT: Layout = _
🔬This is a nightly-only experimental API. (
sized_type_properties
)Source§#[doc(hidden)] const MAX_SLICE_LEN: usize = _
#[doc(hidden)] const MAX_SLICE_LEN: usize = _
🔬This is a nightly-only experimental API. (
sized_type_properties
)The largest safe length for a
[Self]
. Read more