*mut T and Send trait

I have the following section of code:

let curr_ptr = self.mytype_.load(Ordering::SeqCst);
let curr_type = unsafe { Box::from_raw(curr_ptr) };
if curr_type.as_ref().eq(old_memtable) {
    let newt_type = MyType::new();
    let new_ptr = Box::into_raw(Box::new(new_type));
    let res = self.mytype_.compare_exchange(
                curr_ptr,
                new_ptr,
                Ordering::Acquire,
                Ordering::Relaxed,
            );
    match res {
                Ok(old) => {
                     // This line does not compile.
                    XXXX::instance().add(*curr_type).await;
                    retval = true;
                }
                Err(_) => {
                    std::mem::forget(curr_type);
                }
        }
} else {
            std::mem::forget(curr_type);
}

The section does not compile because the *mut MyType does not implement Send and it is used later. How am I using it later? How do I fix this w/o trying to make the add() non-async.

Without knowing the exact error you're seeing, it could possibly be fixed by scoping new_ptr to its own block:

let curr_ptr = self.mytype_.load(Ordering::SeqCst);
let curr_type = unsafe { Box::from_raw(curr_ptr) };
if curr_type.as_ref().eq(old_memtable) {
    let new_type = MyType::new();
    let res = {
        let new_ptr = Box::into_raw(Box::new(new_type));
        self.mytype_
            .compare_exchange(curr_ptr, new_ptr, Ordering::Acquire, Ordering::Relaxed)
    };
    match res {
        Ok(old) => {
            // This line does not compile.
            XXXX::instance().add(*curr_type).await;
            retval = true;
        }
        Err(_) => {
            std::mem::forget(curr_type);
        }
    }
} else {
    std::mem::forget(curr_type);
}

The compiler's check for variables held across await points currently isn't very smart, and sometimes it can fail to see that a variable has been moved out of, or that it is Copy and is never used again. The most reliable way to fix this is to place the variable in a separate block from the await expression.

Thanks. That's exactly what I did. Added a method to scope it out. Thanks for the response.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.