Skip to content

Commit ee67d71

Browse files
authored
Merge pull request #536 from newAM/binary-heap-safety
BinaryHeap: fix missing safety doc
2 parents 64fee10 + 7f2cdee commit ee67d71

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/binary_heap.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,24 @@ 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+
/// // SAFETY: We just pushed a number onto the heap, so it cannot be empty.
403+
/// let val = unsafe { heap.pop_unchecked() };
404+
/// assert_eq!(val, 42);
405+
/// # Ok::<(), u8>(())
406+
/// ```
390407
pub unsafe fn pop_unchecked(&mut self) -> T {
391408
let mut item = self.data.pop_unchecked();
392409

@@ -420,7 +437,23 @@ where
420437
}
421438

422439
/// Pushes an item onto the binary heap without first checking if it's full.
423-
#[allow(clippy::missing_safety_doc)] // TODO
440+
///
441+
/// # Safety
442+
///
443+
/// The binary heap must not be full.
444+
///
445+
/// # Example
446+
///
447+
/// ```
448+
/// use heapless::binary_heap::{BinaryHeap, Max};
449+
///
450+
/// let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
451+
///
452+
/// // SAFETY: We just created an empty heap of size 8, so it cannot be full.
453+
/// unsafe { heap.push_unchecked(42) };
454+
/// assert_eq!(heap.len(), 1);
455+
/// assert_eq!(heap.peek(), Some(&42));
456+
/// ```
424457
pub unsafe fn push_unchecked(&mut self, item: T) {
425458
let old_len = self.len();
426459
self.data.push_unchecked(item);

0 commit comments

Comments
 (0)