I am writing a plugin for program written in C (wireshark) and wireshark initializes some structures in the plugin. So I do need mutable static structs in Rust. I made it work by creating a vector and doing mem::transmute
but I wonder if it is possible to do it without copying to Vec
and the way I would do it in C: just declare static array and passing pointer to array to Wireshark for initialization.
Here is minimized example of what is needed. I do not understand, why move happen. I am taking a reference to INSTANCE
when creating array, so array should be initialized with pointer to INSTANCE
, not with its copy?
extern "C" {
pub fn register(hf: *mut ApiInitialized<i32>);
}
struct ApiInitialized<T>(T);
static mut INSTANCE: ApiInitialized<i32> = ApiInitialized(-1);
static mut LIST: [&ApiInitialized<i32>; 1] = [& unsafe {INSTANCE}];
fn init() {
unsafe { register(LIST.as_mut_ptr() as *mut _); }
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0507]: cannot move out of a raw pointer
--> src/lib.rs:6:57
|
6 | static mut LIST: [&ApiInitialized<i32>; 1] = [& unsafe {INSTANCE}];
| ^^^^^^^^ move occurs because value has type `ApiInitialized<i32>`, which does not implement the `Copy` trait
For more information about this error, try `rustc --explain E0507`.
error: could not compile `playground` due to previous error