Skip to content

Commit 150cf01

Browse files
committed
BinaryHeap: fix missing safety doc
1 parent 4e31d3e commit 150cf01

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

src/binary_heap.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,26 @@ where
386386

387387
/// Removes the *top* (greatest if max-heap, smallest if min-heap) item from the binary heap and
388388
/// returns it, without checking if the binary heap is empty.
389-
#[allow(clippy::missing_safety_doc)] // TODO
389+
///
390+
/// # Safety
391+
///
392+
/// The binary heap must not be empty.
393+
///
394+
/// # Example
395+
///
396+
/// ```
397+
/// use heapless::binary_heap::{BinaryHeap, Max};
398+
///
399+
/// let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
400+
/// heap.push(42)?;
401+
///
402+
/// if !heap.is_empty() {
403+
/// // SAFETY: We just pushed a number onto the heap, so it cannot be empty.
404+
/// let val = unsafe { heap.pop_unchecked() };
405+
/// assert_eq!(val, 42);
406+
/// }
407+
/// # Ok::<(), u8>(())
408+
/// ```
390409
pub unsafe fn pop_unchecked(&mut self) -> T {
391410
let mut item = self.data.pop_unchecked();
392411

@@ -420,7 +439,25 @@ where
420439
}
421440

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

0 commit comments

Comments
 (0)