I am using specs that require some lifetime types to keep reference of the dispatcher. (specs::Dispatcher - Rust)
I have some context struct that hold things together.
pub struct Context<'a, 'b> {
pub world: World,
pub dispatcher: Dispatcher<'a, 'b>,
...
}
I am trying to use it through FFI by returning a handler pointer to the host language:
#[no_mangle]
pub extern "C" fn init() -> *mut Context {
let mut context = Context::new(value);
Box::into_raw(Box::new(context))
}
The the host language send data providing the handler
fn from_ptr(ptr: *mut Context) -> &'c mut Context {
assert!(!ptr.is_null());
unsafe {
&mut *ptr
}
}
#[no_mangle]
pub extern fn update(ctx_ptr: *mut Context, delta: u32) -> u32 {
let ctx = from_ptr(ctx_ptr);
ctx.update(delta)
0
}
From what I understand, there is no black magic to represent the lifecycle when casting it from raw pointers.
Any suggestion how to deal with it?
For now what I found:
A) Pray for success of encapsulation of private lifetimes (Pre-RFC: Encapsulating private lifetimes - language design - Rust Internals).
Too much hope?
B) Get rid of Dispatcher reference that require the lifecycles by dynamically creating each tick.
It will works, but this means whatever I use from now one can not have lifecycles.
Too much optimism?
C) Keep a unsafe global variable with Context and just share some KEY through FFI.
I am not complete even sure if it works.
Too much lack of knowledge?