pub struct PeekMut<'a, T, A = 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, A> PeekMut<'a, T, A>
impl<'a, T, A> PeekMut<'a, T, A>
Sourcepub fn refresh(&mut self) -> bool
🔬This is a nightly-only experimental API. (binary_heap_peek_mut_refresh
)
pub fn refresh(&mut self) -> bool
binary_heap_peek_mut_refresh
)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§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
The archived version of the pointer metadata for this type.
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Converts some archived metadata to the pointer metadata for itself.
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
§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Returns the layout of the type.
§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2where
T: SharedNiching<N1, N2>,
N1: Niching<T>,
N2: Niching<T>,
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2where
T: SharedNiching<N1, N2>,
N1: Niching<T>,
N2: Niching<T>,
§impl<T> Pointee for T
impl<T> Pointee for T
Source§impl<T> Upcastable for T
impl<T> Upcastable for T
Source§fn upcast_any_ref(&self) -> &(dyn Any + 'static)
fn upcast_any_ref(&self) -> &(dyn Any + 'static)
upcast ref
Source§fn upcast_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn upcast_any_mut(&mut self) -> &mut (dyn Any + 'static)
upcast mut ref