本文设计了一个比较巧妙的 Events 模式, 不由地让人想起 Qt
的 signal
.下面是核心代码以及使用例子. 具体更多细节可以参阅原文.
trait Sig { type Data; type Receiver: Rec; fn emit(&self, data: Self::Data); fn conn(&mut self) -> Self::Receiver; fn disc(&mut self, i: usize); } trait Rec { type Data; fn on_emit(self, data: Self::Data); fn get_id(&self) -> usize; } macro_rules! def_signal{ // ... } // 使用例子 struct MySigData { num: i32, } fn main() { def_signal!( MySig, // Signal 名字 MyRec, // Receiver 名字 NySigData, // 预定义的数据类型 |this: MyRec, data: MySigData| { // 逻辑 println!("MySig receriver R{} - num: {}", this.id, data.num); } ) let mut ms2 = MySig::new(); let r1 = ms2.conn(); let r2 = ms2.conn(); ms2.emit(MySigData{ num: 3}); ms2.disc(r1.id); ms2.emit(MySigData{ num: 9}); ms2.disc(r2.id); }
输出结果
MySig receriver R1 - num: 3 MySig receriver R2 - num: 3 Removing Signal R1 MySig receriver R2 - num: 9 Removing Signal R2
原文链接:https://rossketeer.medium.com/custom-events-in-rust-c4e534b6b8cb
ECS (entity-component-system)
是一种广泛应用于游戏引擎中的设计理念.本文主要描述的是 ECS
相关的概念中的 Scheduler
.
bevy_ecs
, yaks
等.原文链接:https://ratysz.github.io/article/scheduling-1/
这是 Crust of Rust
最新的一期 Rust 视频: Sorting Algorithms
.Crust of Rust
是一系列质量比较高的 Rust 直播编码视频. 强烈推荐给各位小伙伴.
需要科学上网.油管连接:https://www.youtube.com/watch?v=h4RkCyJyXmM&feature=youtu.be
t-rec
是一个 Rust 编写的超快的命令行录屏器, 可以生成 .gif
图片.github地址:https://github.com/sassman/t-rec-rs
--
From 日报小组 BobQin,FBI小白