Merge #878
878: Minor simplifications for `Forever` r=Dirbaio a=GrantM11235 Co-authored-by: Grant Miller <GrantM11235@gmail.com>
This commit is contained in:
commit
c0496f8bbe
@ -52,20 +52,7 @@ impl<T> Forever<T> {
|
|||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
#[allow(clippy::mut_from_ref)]
|
#[allow(clippy::mut_from_ref)]
|
||||||
pub fn put(&'static self, val: T) -> &'static mut T {
|
pub fn put(&'static self, val: T) -> &'static mut T {
|
||||||
if self
|
self.put_with(|| val)
|
||||||
.used
|
|
||||||
.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
|
|
||||||
.is_err()
|
|
||||||
{
|
|
||||||
panic!("Forever::put() called multiple times");
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe {
|
|
||||||
let p = self.t.get();
|
|
||||||
let p = (&mut *p).as_mut_ptr();
|
|
||||||
p.write(val);
|
|
||||||
&mut *p
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Store the closure return value in this `Forever`, returning a mutable reference to it.
|
/// Store the closure return value in this `Forever`, returning a mutable reference to it.
|
||||||
@ -87,12 +74,8 @@ impl<T> Forever<T> {
|
|||||||
panic!("Forever.put() called multiple times");
|
panic!("Forever.put() called multiple times");
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe {
|
let p: &'static mut MaybeUninit<T> = unsafe { &mut *self.t.get() };
|
||||||
let p = self.t.get();
|
p.write(val())
|
||||||
let p = (&mut *p).as_mut_ptr();
|
|
||||||
p.write(val());
|
|
||||||
&mut *p
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Unsafely get a mutable reference to the contents of this Forever.
|
/// Unsafely get a mutable reference to the contents of this Forever.
|
||||||
@ -106,8 +89,7 @@ impl<T> Forever<T> {
|
|||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
#[allow(clippy::mut_from_ref)]
|
#[allow(clippy::mut_from_ref)]
|
||||||
pub unsafe fn steal(&self) -> &mut T {
|
pub unsafe fn steal(&self) -> &mut T {
|
||||||
let p = self.t.get();
|
let p: &mut MaybeUninit<T> = &mut *self.t.get();
|
||||||
let p = (&mut *p).as_mut_ptr();
|
p.assume_init_mut()
|
||||||
&mut *p
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user