Thank you for offering to look. I hesitate to supply the example because I have tried so many things and get so many different errors depending on what approach I try to take.
I don't want to waste your time. What I'm doing now is just trying to make global variables with builtin data types like String. This takes away some of the options for error. My idea is to get this working before I try to do it with my own data structure:
So with a simpler case where I'm making a global string instead
// ....
static mut HEAP_INSTANCE : OnceLock<String> = OnceLock::new();
#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
HEAP_INSTANCE.set("blah".to_string());
^^^^^^^^^^^^^ use of mutable static
let _rocket = rocket::build()
.mount("/", routes![min,create_heap])
.launch()
.await?;
Ok(())
}
As you can see it's an error to use a static mut. I realise that this thing isn't safe in a multi-threaded program but I had hoped that I might ignore that in my single-threaded program or add some kind of locking container that would make it acceptable to the compiler.If I try to use my heap data structure then I start having a multitude of issues, the most common being that I create something which is owned by a scope that disappears too soon. IOW I need to initialise the global with a structure that has a 'static lifetime and I'm doing that in a context which definitely isn't static. i.e. I get "creates a temporary value which is freed while still in use" or something similar
While writing this reply I might have solved my issue:
//static mut HEAP_INSTANCE : OnceLock<String> = OnceLock::new();
static HEAP_INSTANCE : OnceLock<Box<Heap<String>>> = OnceLock::new();
async fn main() -> Result<(), rocket::Error> {
let heap = Box::new(Heap::new(100));
let _ = HEAP_INSTANCE.set(heap);
// ....
}
I don't fully understand why I don't need the mut and I don't know if I really need a Box or not but at least this compiles.Thank you for helping me to duck debug! :-D