Description
Hi, I'm looking to using Gluon in a system I'm developing, where I'm using Gluon to mutate a single stateful object. I created this simple example to test things out:
#[macro_use] extern crate gluon;
use gluon::{new_vm, vm::ExternModule, ThreadExt, base::error::AsDiagnostic, import::add_extern_module};
use gluon_codegen::*;
use lazy_static::lazy_static;
use std::sync::{Arc, Mutex};
#[derive(Clone, Debug, Trace, VmType, Userdata)]
#[gluon_trace(skip)]
#[gluon(vm_type = "test.Counter")]
struct Counter(Arc<Mutex<usize>>);
impl Counter {
fn new() -> Counter {
Counter(Arc::new(Mutex::new(0)))
}
}
lazy_static! {
static ref COUNTER: Counter = Counter::new();
}
fn main() {
let vm = new_vm();
let load_mod = |thread: &gluon::Thread| {
thread.register_type::<Counter>("test.Counter", &[])?;
let module = record! {
type Counter => Counter,
get_count => primitive!(0, || *COUNTER.0.lock().unwrap()),
inc_counter => primitive!(0, || {
let mut counter = COUNTER.0.lock().unwrap();
println!("before: {}", *counter);
*counter += 1;
println!("after: {}", *counter);
}),
};
ExternModule::new(thread, module)
};
add_extern_module(&vm, "counter", load_mod);
vm.run_io(true);
let res = vm.run_expr::<()>(
"test",
r#"
let { get_count, inc_counter, ? } = import! counter
inc_counter
"#,
);
if let Err(e) = res {
println!("{}", e.as_diagnostic().message);
}
dbg!(*COUNTER.0.lock().unwrap());
}
However, the dbg!
at the end gave me 0, and the println!
s in the middle are not being printed.
I suspected that I'm not calling the function, so I tried inc_counter ()
(I can only guess, as there's no documentation on how to call a function with no arugments), but I got the following errors:
error: Expected the following types to be equal
Expected: () -> a
Found: ()
1 errors were found during unification:
Types do not match:
Expected: () -> a
Found: ()
- <test>:3:13
|
3 | inc_counter ()
| ^^
|
- Attempted to call a non-function value
I have no idea what this error means, as inc_counter
is for sure defined as a function through primitive!
.
So is the inc_counter
function being optimized out? If so, how can I include the call back? And where can I find the documentation for it, or around which source files should I look into to understand that behaviour?
Thanks a lot for any pointers and suggestions!
Activity