Is it possible to transfer &[u8] to another thread without copying?

Hi, I have a variable of type &[u8] that get from Rust binding to a C library:

fn on_paint(&mut self, buffer: &[u8], width: usize, height: usize) {
    // Here I pass buffer to another thread using std::sync:mpsc
}

I need to send this buffer to another thread. Currently, I use std::sync::mpsc but I have to make a copy of it let buffer = buffer.to_vec(). Is it possible to send it without copying?

Where does the other thread come from? If you spawn it in on_paint via thread::spawn, you could use thread::scope instead, which allows capturing non-'static data. Otherwise I don't see a way around copying the buffer, either by message-passing the copy to the other thread (as you currently do) or by sharing the buffer in memory via Arc<[u8]> or such. Thread execution order isn't deterministic, so the thread that owns the buffer might deallocate it, making the reference the other thread has to the buffer dangle.

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.