I'm trying to make a macro that pastes in boilerplate code and I would like to use the variables it pastes in afterwards. Rust's macro hygiene is preventing this; how do I turn this off?
I understand that there are ways to work around it, by passing all the variables into the macro, but that would make the macro useless... I found a crate called unhygienic, which states it does exactly this, but I can't get it to work.
This is how it's supposed to work. This is how you can use the Processing (drawing/graphics framework) in Java or JavaScript (this would show a moving car (defined in a Car class) and a rectangle:
// You need to add a library or use the pde to be able to make this work though
let car;
setup() {
createCanvas(500, 100);
frameRate(60);
car = new Car();
}
draw() {
car.move();
car.show();
// this draws a rectangle at a certain place
rect(100, 200, 200, 100)
}
setup is everything you do once before the main loop, and draw is everything you do inside the loop. This is how I would like to have it look in Rust (definitely not needed, just for aesthetics):
//does all the boilerplate opengl setup.
setup!{
// setting my variables and settings
}
// // adds a loop around the code in it and keeps up the framerate etc
draw!{
// do my updating and drawing commands
}
Try using a trait! This shows the general outline of how you could implement this:
Playground
I know it is a bit burdensome to pass
ctxaround everywhere, but it is well worth it because you don't have to worry about using aRc<RefCell<...>>(which can get even more complicated with multiple threads) in order to ensure unique access to a shared context via slow, runtime-checked memory management. You can run multiple programs at the same time, each with its own context, and you know that at any given time, at most one function is writing to the context.