如何关闭宏观卫生?

问题描述

我正在尝试制作一个粘贴在样板代码中的宏,我想使用此后粘贴的变量。 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

解决方法

尝试使用特征!这显示了如何实现此目标的概述:

Playground

// 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*会变得更加复杂多线程),以确保通过缓慢的,经过运行时检查的内存管理来唯一访问共享上下文。您可以同时运行多个程序,每个程序都有自己的上下文,并且您知道在任何给定时间,最多有一个函数正在写入上下文。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...