mirror of
https://github.com/rust-embedded/heapless.git
synced 2025-10-02 06:50:32 +00:00
Hole: switch from Option to ManuallyDrop to avoid a panicking branch
This commit is contained in:
parent
08c76f34c7
commit
8121700a76
@ -10,6 +10,7 @@
|
||||
|
||||
use core::cmp::Ordering;
|
||||
use core::marker::PhantomData;
|
||||
use core::mem::ManuallyDrop;
|
||||
use core::{mem, ptr, slice};
|
||||
|
||||
use generic_array::ArrayLength;
|
||||
@ -333,7 +334,7 @@ where
|
||||
struct Hole<'a, T: 'a> {
|
||||
data: &'a mut [T],
|
||||
/// `elt` is always `Some` from new until drop.
|
||||
elt: Option<T>,
|
||||
elt: ManuallyDrop<T>,
|
||||
pos: usize,
|
||||
}
|
||||
|
||||
@ -347,7 +348,7 @@ impl<'a, T> Hole<'a, T> {
|
||||
let elt = ptr::read(data.get_unchecked(pos));
|
||||
Hole {
|
||||
data,
|
||||
elt: Some(elt),
|
||||
elt: ManuallyDrop::new(elt),
|
||||
pos,
|
||||
}
|
||||
}
|
||||
@ -360,7 +361,7 @@ impl<'a, T> Hole<'a, T> {
|
||||
/// Returns a reference to the element removed.
|
||||
#[inline]
|
||||
fn element(&self) -> &T {
|
||||
self.elt.as_ref().unwrap()
|
||||
&self.elt
|
||||
}
|
||||
|
||||
/// Returns a reference to the element at `index`.
|
||||
@ -393,7 +394,7 @@ impl<'a, T> Drop for Hole<'a, T> {
|
||||
// fill the hole again
|
||||
unsafe {
|
||||
let pos = self.pos;
|
||||
ptr::write(self.data.get_unchecked_mut(pos), self.elt.take().unwrap());
|
||||
ptr::write(self.data.get_unchecked_mut(pos), ptr::read(&*self.elt));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user