Skip to content

Conversation

@kitcatier
Copy link

@kitcatier kitcatier commented Mar 18, 2023

extreme/src/lib.rs

Lines 24 to 44 in 74e2505

pub fn run<F: std::future::Future>(mut f: F) -> F::Output {
let mut f = unsafe { std::pin::Pin::new_unchecked(&mut f) };
let park = Arc::new(Park::default());
let sender = Arc::into_raw(park.clone());
let raw_waker = RawWaker::new(sender as *const _, &VTABLE);
let waker = unsafe { Waker::from_raw(raw_waker) };
let mut cx = Context::from_waker(&waker);
loop {
match f.as_mut().poll(&mut cx) {
Poll::Pending => {
let mut runnable = park.0.lock().unwrap();
while !*runnable {
runnable = park.1.wait(runnable).unwrap();
}
*runnable = false;
}
Poll::Ready(val) => return val,
}
}
}

Hi, the unsafe function(new_unchecked) called needs to ensure that the parameter must be:
https://doc.rust-lang.org/std/pin/struct.Pin.html#method.new_unchecked

  • This constructor is unsafe because we cannot guarantee that the data pointed to by pointer is pinned, meaning that the data will not be moved or its storage invalidated until it gets dropped. If the constructed Pin<P> does not guarantee that the data P points to is pinned, that is a violation of the API contract and may lead to undefined behavior in later (safe) operations.

and the developer who calls the run function may not notice this safety requirement.
Marking them unsafe also means that callers must make sure they know what they're doing.

@binary-bruce
Copy link

we cannot guarantee that the data pointed to by pointer is pinned

before being polled, future is not necessary to be pinned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants