Skip to content

Commit f738cfd

Browse files
authored
Use pin macro instead of new_unchecked (#40)
Addresses concerns that the variable was not being properly pinned.
1 parent 2232df7 commit f738cfd

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ macro_rules! ready {
2323
}};
2424
}
2525

26+
/// Pins a variable on the stack.
27+
///
28+
/// TODO: Drop in favor of `core::pin::pin`, once MSRV is bumped to 1.68.
29+
macro_rules! pin {
30+
($($x:ident),* $(,)?) => {
31+
$(
32+
let mut $x = $x;
33+
#[allow(unused_mut)]
34+
let mut $x = unsafe {
35+
std::pin::Pin::new_unchecked(&mut $x)
36+
};
37+
)*
38+
}
39+
}
40+
2641
mod barrier;
2742
mod mutex;
2843
mod once_cell;

src/once_cell.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::convert::Infallible;
33
use std::fmt;
44
use std::future::Future;
55
use std::mem::{forget, MaybeUninit};
6-
use std::pin::Pin;
76
use std::ptr;
87
use std::sync::atomic::{AtomicUsize, Ordering};
98
use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
@@ -750,7 +749,7 @@ impl<T> Drop for OnceCell<T> {
750749
}
751750

752751
/// Either return the result of a future now, or panic.
753-
fn now_or_never<T>(mut f: impl Future<Output = T>) -> T {
752+
fn now_or_never<T>(f: impl Future<Output = T>) -> T {
754753
const NOOP_WAKER: RawWakerVTable = RawWakerVTable::new(clone, wake, wake_by_ref, drop);
755754

756755
unsafe fn wake(_: *const ()) {}
@@ -760,15 +759,14 @@ fn now_or_never<T>(mut f: impl Future<Output = T>) -> T {
760759
}
761760
unsafe fn drop(_: *const ()) {}
762761

763-
// SAFETY: We don't move the future after we pin it here.
764-
let future = unsafe { Pin::new_unchecked(&mut f) };
762+
pin!(f);
765763

766764
let waker = unsafe { Waker::from_raw(RawWaker::new(ptr::null(), &NOOP_WAKER)) };
767765

768766
// Poll the future exactly once.
769767
let mut cx = Context::from_waker(&waker);
770768

771-
match future.poll(&mut cx) {
769+
match f.poll(&mut cx) {
772770
Poll::Ready(value) => value,
773771
Poll::Pending => unreachable!("future not ready"),
774772
}

0 commit comments

Comments
 (0)