diff --git a/framework/base/src/types/managed/basic/big_float.rs b/framework/base/src/types/managed/basic/big_float.rs index 81c5c25c53..9306769130 100644 --- a/framework/base/src/types/managed/basic/big_float.rs +++ b/framework/base/src/types/managed/basic/big_float.rs @@ -35,12 +35,9 @@ impl ManagedType for BigFloat { self.handle.clone() } - unsafe fn forget_into_handle(mut self) -> Self::OwnHandle { + unsafe fn forget_into_handle(self) -> Self::OwnHandle { unsafe { - let handle = core::mem::replace( - &mut self.handle, - core::mem::MaybeUninit::uninit().assume_init(), - ); + let handle = core::ptr::read(&self.handle); core::mem::forget(self); handle } diff --git a/framework/base/src/types/managed/basic/big_int.rs b/framework/base/src/types/managed/basic/big_int.rs index e981f6a9ec..464bc4150c 100644 --- a/framework/base/src/types/managed/basic/big_int.rs +++ b/framework/base/src/types/managed/basic/big_int.rs @@ -38,12 +38,9 @@ impl ManagedType for BigInt { self.handle.clone() } - unsafe fn forget_into_handle(mut self) -> Self::OwnHandle { + unsafe fn forget_into_handle(self) -> Self::OwnHandle { unsafe { - let handle = core::mem::replace( - &mut self.handle, - core::mem::MaybeUninit::uninit().assume_init(), - ); + let handle = core::ptr::read(&self.handle); core::mem::forget(self); handle } diff --git a/framework/base/src/types/managed/basic/elliptic_curve.rs b/framework/base/src/types/managed/basic/elliptic_curve.rs index 7252bd8b90..9b86dba5d1 100644 --- a/framework/base/src/types/managed/basic/elliptic_curve.rs +++ b/framework/base/src/types/managed/basic/elliptic_curve.rs @@ -43,12 +43,9 @@ impl ManagedType for EllipticCurve { self.handle.clone() } - unsafe fn forget_into_handle(mut self) -> Self::OwnHandle { + unsafe fn forget_into_handle(self) -> Self::OwnHandle { unsafe { - let handle = core::mem::replace( - &mut self.handle, - core::mem::MaybeUninit::uninit().assume_init(), - ); + let handle = core::ptr::read(&self.handle); core::mem::forget(self); handle } diff --git a/framework/base/src/types/managed/basic/managed_buffer.rs b/framework/base/src/types/managed/basic/managed_buffer.rs index 17ee49a639..cafa32a4d2 100644 --- a/framework/base/src/types/managed/basic/managed_buffer.rs +++ b/framework/base/src/types/managed/basic/managed_buffer.rs @@ -37,12 +37,9 @@ impl ManagedType for ManagedBuffer { self.handle.clone() } - unsafe fn forget_into_handle(mut self) -> Self::OwnHandle { + unsafe fn forget_into_handle(self) -> Self::OwnHandle { unsafe { - let handle = core::mem::replace( - &mut self.handle, - core::mem::MaybeUninit::uninit().assume_init(), - ); + let handle = core::ptr::read(&self.handle); core::mem::forget(self); handle } diff --git a/framework/base/src/types/managed/basic/managed_map.rs b/framework/base/src/types/managed/basic/managed_map.rs index e0d593a883..2fce9deb76 100644 --- a/framework/base/src/types/managed/basic/managed_map.rs +++ b/framework/base/src/types/managed/basic/managed_map.rs @@ -23,12 +23,9 @@ impl ManagedType for ManagedMap { self.handle.clone() } - unsafe fn forget_into_handle(mut self) -> Self::OwnHandle { + unsafe fn forget_into_handle(self) -> Self::OwnHandle { unsafe { - let handle = core::mem::replace( - &mut self.handle, - core::mem::MaybeUninit::uninit().assume_init(), - ); + let handle = core::ptr::read(&self.handle); core::mem::forget(self); handle } diff --git a/framework/base/src/types/managed/managed_type_trait.rs b/framework/base/src/types/managed/managed_type_trait.rs index f7df30288f..8b30fb8400 100644 --- a/framework/base/src/types/managed/managed_type_trait.rs +++ b/framework/base/src/types/managed/managed_type_trait.rs @@ -12,8 +12,18 @@ pub trait ManagedType: Sized { fn get_handle(&self) -> Self::OwnHandle; /// Forgets current object (does not run destructor), but extracts the handle. - /// + /// /// The handle remains an owned object, so the handle's destructor will run later, when dropped. + /// + /// ## Safety + /// + /// Destructures the object, without running a constructor. + /// + /// To avoid a memory leak, it is necessary for the object to be later + /// reconstructed from handle and its destructor run. + /// + /// It is designed to be used ManagedVec and ManagedOption, + /// where items are dropped later, together with their container. unsafe fn forget_into_handle(self) -> Self::OwnHandle; #[doc(hidden)]