From 0a99af0fa4af0d871c5f1789285ad8b1cb83f8e2 Mon Sep 17 00:00:00 2001 From: nils <48135649+Nilstrieb@users.noreply.github.com> Date: Tue, 18 Oct 2022 12:21:14 +0300 Subject: [PATCH] Don't box value in `Fragile` (#28) --- src/fragile.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/fragile.rs b/src/fragile.rs index 1a13d06..8616075 100644 --- a/src/fragile.rs +++ b/src/fragile.rs @@ -16,9 +16,9 @@ use std::mem::ManuallyDrop; /// the destructor will panic. Alternatively you can use `Sticky` which is /// not going to panic but might temporarily leak the value. pub struct Fragile { - // ManuallyDrop is necessary because we need to move out of this `Box` without running the + // ManuallyDrop is necessary because we need to move out of here without running the // Drop code in functions like `into_inner`. - value: ManuallyDrop>, + value: ManuallyDrop, thread_id: NonZeroUsize, } @@ -31,7 +31,7 @@ impl Fragile { /// only the original thread can interact with the value. pub fn new(value: T) -> Self { Fragile { - value: ManuallyDrop::new(Box::new(value)), + value: ManuallyDrop::new(value), thread_id: thread_id::get(), } } @@ -63,7 +63,7 @@ impl Fragile { // SAFETY: `this` is not accessed beyond this point, and because it's in a ManuallyDrop its // destructor is not run. - *unsafe { ManuallyDrop::take(&mut this.value) } + unsafe { ManuallyDrop::take(&mut this.value) } } /// Consumes the `Fragile`, returning the wrapped value if successful. @@ -87,7 +87,7 @@ impl Fragile { /// For a non-panicking variant, use [`try_get`](#method.try_get`). pub fn get(&self) -> &T { self.assert_thread(); - &**self.value + &*self.value } /// Mutably borrows the wrapped value. @@ -98,7 +98,7 @@ impl Fragile { /// For a non-panicking variant, use [`try_get_mut`](#method.try_get_mut`). pub fn get_mut(&mut self) -> &mut T { self.assert_thread(); - &mut **self.value + &mut *self.value } /// Tries to immutably borrow the wrapped value. @@ -106,7 +106,7 @@ impl Fragile { /// Returns `None` if the calling thread is not the one that wrapped the value. pub fn try_get(&self) -> Result<&T, InvalidThreadAccess> { if thread_id::get() == self.thread_id { - Ok(&**self.value) + Ok(&*self.value) } else { Err(InvalidThreadAccess) } @@ -117,7 +117,7 @@ impl Fragile { /// Returns `None` if the calling thread is not the one that wrapped the value. pub fn try_get_mut(&mut self) -> Result<&mut T, InvalidThreadAccess> { if thread_id::get() == self.thread_id { - Ok(&mut **self.value) + Ok(&mut *self.value) } else { Err(InvalidThreadAccess) }