Skip to content

Commit ef1d292

Browse files
committed
Impl Drop for PyAtomicRef
1 parent dd4f0c3 commit ef1d292

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

vm/src/object/ext.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,19 @@ pub struct PyAtomicRef<T> {
245245
_phantom: PhantomData<T>,
246246
}
247247

248+
impl<T> Drop for PyAtomicRef<T> {
249+
fn drop(&mut self) {
250+
// SAFETY: We are dropping the atomic reference, so we can safely
251+
// release the pointer.
252+
unsafe {
253+
let ptr = Radium::swap(&self.inner, null_mut(), Ordering::Relaxed);
254+
if let Some(ptr) = NonNull::<PyObject>::new(ptr.cast()) {
255+
let _: PyObjectRef = PyObjectRef::from_raw(ptr);
256+
}
257+
}
258+
}
259+
}
260+
248261
cfg_if::cfg_if! {
249262
if #[cfg(feature = "threading")] {
250263
unsafe impl<T: Send + PyObjectPayload> Send for PyAtomicRef<T> {}

0 commit comments

Comments
 (0)