Skip to content

Commit addb676

Browse files
Replace unsafe with Option::take
1 parent edf7482 commit addb676

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/semaphore.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::fmt;
2+
use core::mem;
23
use core::pin::Pin;
3-
use core::ptr::addr_of_mut;
44
use core::sync::atomic::{AtomicUsize, Ordering};
55
use core::task::Poll;
66

@@ -342,7 +342,7 @@ impl SemaphoreGuard<'_> {
342342
/// Drops the guard _without_ releasing the acquired permit.
343343
#[inline]
344344
pub fn forget(self) {
345-
core::mem::forget(self);
345+
mem::forget(self);
346346
}
347347
}
348348

@@ -356,24 +356,24 @@ impl Drop for SemaphoreGuard<'_> {
356356
/// An owned guard that releases the acquired permit.
357357
#[clippy::has_significant_drop]
358358
#[derive(Debug)]
359-
pub struct SemaphoreGuardArc(Arc<Semaphore>);
359+
pub struct SemaphoreGuardArc(Option<Arc<Semaphore>>);
360360

361361
impl SemaphoreGuardArc {
362362
/// Drops the guard _without_ releasing the acquired permit.
363363
/// (Will still decrement the `Arc` reference count.)
364364
#[inline]
365-
pub fn forget(self) {
366-
let mut manual = core::mem::ManuallyDrop::new(self);
367-
365+
pub fn forget(mut self) {
368366
// Drop the inner `Arc` in order to decrement the reference count.
369-
// SAFETY: `manual` not used after this
370-
let _arc = unsafe { addr_of_mut!(manual.0).read() };
367+
// FIXME: get rid of the `Option` once RFC 3466 or equivalent is merged.
368+
drop(self.0.take());
369+
mem::forget(self);
371370
}
372371
}
373372

374373
impl Drop for SemaphoreGuardArc {
375374
fn drop(&mut self) {
376-
self.0.count.fetch_add(1, Ordering::AcqRel);
377-
self.0.event.notify(1);
375+
let opt = self.0.take().unwrap();
376+
opt.count.fetch_add(1, Ordering::AcqRel);
377+
opt.event.notify(1);
378378
}
379379
}

0 commit comments

Comments
 (0)