Hi, I'm new to Rust and trying to use Inkwell (LLVM safe wrapper) . I'm trying to make a struct for code emitting that contains multiple Inkwell objects: context, module, builder, execution_engine. Notably, the latter three contain a reference to the first and I want to make a constructor that returns them together.
The problem is, I'm trying to move context into the emitter struct, after creating references to it. I wonder what is the idiomatic rust solution to this.
I want to avoid:
- using Rc
- using options to half initialize the struct first
- using hacks in general
- cloning anything
here is the code:
use inkwell::builder::Builder;
use inkwell::context::Context;
use inkwell::execution_engine::{ExecutionEngine, JitFunction};
use inkwell::module::Module;
use inkwell::OptimizationLevel;
use std::error::Error;
struct Emitter<'ctx> {
context: Box<Context>,
module: Module<'ctx>,
builder: Builder<'ctx>,
execution_engine: ExecutionEngine<'ctx>,
}
impl<'ctx> Emitter<'ctx> {
pub fn emit(&mut self) {}
}
pub fn new_emitter<'a>() -> Option<Emitter<'a>> {
let context = Box::new(Context::create());
let module = context.create_module("main");
let builder = context.create_builder();
let execution_engine = module
.create_jit_execution_engine(OptimizationLevel::None)
.unwrap();
Some(Emitter {
context,
module,
builder,
execution_engine,
})
}
Help is much appreciated ^^