Raw pointers not playing well with futures

This is a hyper server program. In the main function I create a read-only vector. Every connection handler that is created receives a raw pointer to this vector. They are supposed to dereference this pointer and use the common data, but the compilation fails with an error saying:

the trait `std::marker::Send` is not implemented for `*const Vec<String>`
note: future is not `Send` as this value is used across an await

But I am not using the raw pointer across an await. I'm converting it into a reference and then using that across the await (line 28). How do I fix this issue?

You should wrap the unsafe code in helper functions.

impl S {
    pub fn get_vec(&self) -> &Vec<String> {
        unsafe {
            &*self.ptr
        }
    }
}

However you really should not be using raw pointers for this. Either use Box::leak or wrap it in an Arc.

5 Likes

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.