Skip to content

Commit 8ec7985

Browse files
committed
BinaryHeap: fix missing safety doc
1 parent 2e71c18 commit 8ec7985

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/binary_heap.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,25 @@ where
389389

390390
/// Removes the *top* (greatest if max-heap, smallest if min-heap) item from the binary heap and
391391
/// returns it, without checking if the binary heap is empty.
392-
#[allow(clippy::missing_safety_doc)] // TODO
392+
///
393+
/// # Safety
394+
///
395+
/// The binary heap must not be empty.
396+
///
397+
/// # Example
398+
///
399+
/// ```
400+
/// use heapless::binary_heap::{BinaryHeap, Max};
401+
///
402+
/// let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
403+
/// heap.push(42).unwrap();
404+
///
405+
/// if !heap.is_empty() {
406+
/// // SAFETY: the heap is not empty
407+
/// let val = unsafe { heap.pop_unchecked() };
408+
/// assert_eq!(val, 42);
409+
/// }
410+
/// ```
393411
pub unsafe fn pop_unchecked(&mut self) -> T {
394412
let mut item = self.data.pop_unchecked();
395413

@@ -423,7 +441,25 @@ where
423441
}
424442

425443
/// Pushes an item onto the binary heap without first checking if it's full.
426-
#[allow(clippy::missing_safety_doc)] // TODO
444+
///
445+
/// # Safety
446+
///
447+
/// The binary heap must not be full.
448+
///
449+
/// # Example
450+
///
451+
/// ```
452+
/// use heapless::binary_heap::{BinaryHeap, Max};
453+
///
454+
/// let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
455+
///
456+
/// if !heap.is_full() {
457+
/// // SAFETY: the heap is not full
458+
/// unsafe { heap.push_unchecked(42) };
459+
/// }
460+
/// assert_eq!(heap.len(), 1);
461+
/// assert_eq!(heap.peek(), Some(&42));
462+
/// ```
427463
pub unsafe fn push_unchecked(&mut self, item: T) {
428464
let old_len = self.len();
429465
self.data.push_unchecked(item);

0 commit comments

Comments
 (0)