The inner field value of a struct is not being changed, requiring extraction and reinsertion.
Minimal example:
pub fn main(player) {
dbg!(player.position.x);
player.position.x = 0;
dbg!(player.position.x);
}
use rune::alloc::clone::TryClone;
use rune::termcolor::{ColorChoice, StandardStream};
use rune::{Any, ContextError, Diagnostics, Module, Vm};
use std::sync::Arc;
#[derive(Any, TryClone, Debug)]
pub struct Position {
#[rune(get, set)]
pub x: u16,
#[rune(get, set)]
pub y: u16,
}
#[derive(Any, Debug)]
pub struct Player {
#[rune(get, set)]
pub position: Position,
}
pub fn module() -> Result<Module, ContextError> {
let mut module = Module::new();
module.ty::<Position>()?;
module.ty::<Player>()?;
Ok(module)
}
fn main() -> rune::support::Result<()> {
let mut context = rune_modules::default_context()?;
context.install(module()?)?;
let mut sources = rune::sources!(
entry => {
pub fn main(player) {
dbg!(player.position.x);
player.position.x = 0;
dbg!(player.position.x);
}
}
);
let mut diagnostics = Diagnostics::new();
let result = rune::prepare(&mut sources)
.with_context(&context)
.with_diagnostics(&mut diagnostics)
.build();
if !diagnostics.is_empty() {
let mut writer = StandardStream::stderr(ColorChoice::Always);
diagnostics.emit(&mut writer, &sources)?;
}
let mut vm = Vm::new(Arc::new(context.runtime()?), Arc::new(result?));
let mut player = Player {
position: Position { x: 10, y: 10 },
};
vm.call(["main"], (&mut player,))?;
Ok(())
}
Output
Expected result
Note: when the field is extracted, it works.
pub fn main(player) {
let position = player.position;
dbg!(position.x);
position.x = 0;
dbg!(position.x);
player.position = position;
dbg!(player.position.x);
}
Ouput
The inner field value of a struct is not being changed, requiring extraction and reinsertion.
Minimal example:
Output
Expected result
Note: when the field is extracted, it works.
Ouput