如何解决如何关闭宏观卫生?
我正在尝试制作一个粘贴在样板代码中的宏,我想使用此后粘贴的变量。 Rust的宏观卫生状况正在阻止这种情况;如何关闭此功能?
我知道可以通过将所有变量传递到宏中来解决该问题,但这会使宏无用……我发现了一个名为不卫生的板条箱,它说的确实如此,但是我可以没用。
这就是它应该如何工作的。这是您可以在Java或JavaScript中使用“处理”(图形/图形框架)的方式(这将显示正在移动的汽车(在<hgroup class="header__title--content">
<h1 class="header__title"><span class="header__title--element-one">Story </span><span class="header__title--element-two">of the </span><span class="header__title--element-three">Grandmaster</span></h1>
<h2 class="header__subtitle">bobby fisher,the some content goes here</h2>
</hgroup>
类中定义)和一个矩形:
Car
// 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,100)
}
是您在主循环之前执行的所有操作,而setup
是您在循环内部执行的所有操作。这就是我希望在Rust中使用它的方式(绝对不需要,仅出于美观目的):
draw
解决方法
尝试使用特征!这显示了如何实现此目标的概述:
// Library:
mod library {
pub trait Program {
fn setup(ctx: &mut Context) -> Self;
fn draw(&mut self,ctx: &mut Context);
}
pub struct Context;
impl Context {
pub fn create_canvas(&mut self,width: usize,height: usize) {
// ...
}
pub fn frame_rate(&mut self,fps: usize) {
// ...
}
pub fn rect(&mut self,x: usize,y: usize,w: usize,h: usize) {
// ...
}
}
pub fn run_program<P: Program>() {
let ctx = Context;
let program = P::setup(&mut ctx);
loop {
program.draw(&mut ctx);
}
}
}
// User:
use library::{Context,Program};
struct Car;
impl Car {
// `move` is a reserved keyword
fn move_pos(&mut self,ctx: &mut Context) {
// ...
}
fn show(&mut self,ctx: &mut Context) {
// ...
}
}
struct MyProgram {
car: Car,}
impl Program for MyProgram {
fn setup(ctx: &mut Context) -> Self {
ctx.create_canvas(500,100);
ctx.frame_rate(60);
Self { car: Car }
}
fn draw(&mut self,ctx: &mut Context) {
self.car.move_pos(ctx);
self.car.show(ctx);
// this draws a rectangle at a certain place
ctx.rect(100,200,100)
}
}
fn main() {
library::run_program::<MyProgram>();
}
我知道在任何地方传递ctx
有点麻烦,但这是很值得的,因为您不必担心使用Rc<RefCell<...>>
(使用char*
会变得更加复杂多线程),以确保通过缓慢的,经过运行时检查的内存管理来唯一访问共享上下文。您可以同时运行多个程序,每个程序都有自己的上下文,并且您知道在任何给定时间,最多有一个函数正在写入上下文。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。