Skip to content

Commit

Permalink
Use ManuallyDrop instead of mem::forget-in-disguise
Browse files Browse the repository at this point in the history
This fixes stacked borrows violation violations.

See tokio-rs/bytes#458 for more.
  • Loading branch information
taiki-e committed Jan 12, 2022
1 parent 6e022e4 commit 43b27c0
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions futures-task/src/future_obj.rs
Expand Up @@ -252,10 +252,9 @@ mod if_alloc {
where
F: Future<Output = T> + 'a,
{
fn into_raw(mut self) -> *mut (dyn Future<Output = T> + 'a) {
let ptr = unsafe { self.as_mut().get_unchecked_mut() as *mut _ };
mem::forget(self);
ptr
fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
let mut this = mem::ManuallyDrop::new(self);
unsafe { this.as_mut().get_unchecked_mut() as *mut _ }
}

unsafe fn drop(ptr: *mut (dyn Future<Output = T> + 'a)) {
Expand All @@ -264,10 +263,9 @@ mod if_alloc {
}

unsafe impl<'a, T: 'a> UnsafeFutureObj<'a, T> for Pin<Box<dyn Future<Output = T> + 'a>> {
fn into_raw(mut self) -> *mut (dyn Future<Output = T> + 'a) {
let ptr = unsafe { self.as_mut().get_unchecked_mut() as *mut _ };
mem::forget(self);
ptr
fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
let mut this = mem::ManuallyDrop::new(self);
unsafe { this.as_mut().get_unchecked_mut() as *mut _ }
}

unsafe fn drop(ptr: *mut (dyn Future<Output = T> + 'a)) {
Expand All @@ -276,10 +274,9 @@ mod if_alloc {
}

unsafe impl<'a, T: 'a> UnsafeFutureObj<'a, T> for Pin<Box<dyn Future<Output = T> + Send + 'a>> {
fn into_raw(mut self) -> *mut (dyn Future<Output = T> + 'a) {
let ptr = unsafe { self.as_mut().get_unchecked_mut() as *mut _ };
mem::forget(self);
ptr
fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
let mut this = mem::ManuallyDrop::new(self);
unsafe { this.as_mut().get_unchecked_mut() as *mut _ }
}

unsafe fn drop(ptr: *mut (dyn Future<Output = T> + 'a)) {
Expand Down

0 comments on commit 43b27c0

Please sign in to comment.