Unexpected behavior within FFI procedural macro

⚓ Rust    📅 2026-01-12    👤 surdeus    👁️ 2      

surdeus

I am a little confused by the following code. I am attempting to create a procedural macro for my FFI code. My problem is that I want to provide a mutable buffer to the caller. The following code compiles ONLY when the assert_is_send() code is uncommented, otherwise I get the following error:
`*mut u8` cannot be sent between threads safely the trait `std::marker::Send` is not implemented for `*mut u8` required by a bound in `SessionGuard::execute.

pub trait SessionGuard: Sized {
    fn execute<F, Fut>(session: *const c_char, action: F) -> i32
    where
        F: FnOnce(Self) -> Fut + Send + 'static,
        Fut: Future<Output = Result<(), Error>> + Send + 'static;
}

pub struct OutBuffer(pub *mut u8);

unsafe impl Send for OutBuffer {} 

#[session_endpoint]
async fn session(user: SessionUser, buffer: OutBuffer) -> Result<(), Error> {

    // fn assert_is_send<T: Send>(_t: &T) {}
    // assert_is_send(&buffer);
    let outbuffer = unsafe { std::slice::from_raw_parts_mut(buffer.0, 10) };
    ...    
    Ok(())
}

2 posts - 2 participants

Read full topic

🏷️ Rust_feed