rust

How to implement bitwise operations on a bitflags enum?

微笑、不失礼 提交于 2021-01-26 04:00:32
问题 I have an enum that looks like this: #[repr(u8)] pub enum PublicFlags { PublicFlagVersion = 0x01, PublicFlagReset = 0x02, NoncePresent = 0x04, IdPresent = 0x08, PktNumLen4 = 0x30, PktNumLen2 = 0x20, PktNumLen1 = 0x10, Multipath = 0x40, } I want to do a bitwise operation on several of the enum values. However, the Rust compiler complains: an implementation of `std::ops::BitAnd` might be missing for `PublicFlags`. 回答1: An enum in Rust is not intended to be used as bit flags. PublicFlags can

How to implement bitwise operations on a bitflags enum?

不羁的心 提交于 2021-01-26 03:59:17
问题 I have an enum that looks like this: #[repr(u8)] pub enum PublicFlags { PublicFlagVersion = 0x01, PublicFlagReset = 0x02, NoncePresent = 0x04, IdPresent = 0x08, PktNumLen4 = 0x30, PktNumLen2 = 0x20, PktNumLen1 = 0x10, Multipath = 0x40, } I want to do a bitwise operation on several of the enum values. However, the Rust compiler complains: an implementation of `std::ops::BitAnd` might be missing for `PublicFlags`. 回答1: An enum in Rust is not intended to be used as bit flags. PublicFlags can

How can we write a generic function for checking Serde serialization and deserialization?

我与影子孤独终老i 提交于 2021-01-26 03:23:43
问题 In a project where custom Serde (1.0) serialization and deserialization methods are involved, I have relied on this test routine to check whether serializing an object and back would yield an equivalent object. // let o: T = ...; let buf: Vec<u8> = to_vec(&o).unwrap(); let o2: T = from_slice(&buf).unwrap(); assert_eq!(o, o2); Doing this inline works pretty well. My next step towards reusability was to make a function check_serde for this purpose. pub fn check_serde<T>(o: T) where T: Debug +

How can we write a generic function for checking Serde serialization and deserialization?

谁说胖子不能爱 提交于 2021-01-26 03:22:57
问题 In a project where custom Serde (1.0) serialization and deserialization methods are involved, I have relied on this test routine to check whether serializing an object and back would yield an equivalent object. // let o: T = ...; let buf: Vec<u8> = to_vec(&o).unwrap(); let o2: T = from_slice(&buf).unwrap(); assert_eq!(o, o2); Doing this inline works pretty well. My next step towards reusability was to make a function check_serde for this purpose. pub fn check_serde<T>(o: T) where T: Debug +

Rust编程进阶:029、使用Box

∥☆過路亽.° 提交于 2021-01-24 14:58:16
示例代码: #[derive(Debug)] enum List { Cons(i32, Box<List>), Nil, } // C语言的定义方式 // struct List { // int value; // struct List *next; // // struct List l; // }; // fn main() { use List::Cons; use List::Nil; //let list = Cons(1, Cons(2, Cons(3, Nil))); let list = Cons(1, Box::new(Cons(2, Box::new(Cons(3, Box::new(Nil)))))); println!("{:?}", list); } 本节全部源代码: https://github.com/anonymousGiga/learn_rust/blob/master/learn_box1/src/main.rs 来源: oschina 链接: https://my.oschina.net/u/943779/blog/4922340

Rust编程进阶:025、crate发布与撤回

余生长醉 提交于 2021-01-24 14:01:52
crate的发布与撤回(此部分没有实际操作过,只讲步骤) (1)创建Crates.io账号:通过Github账户注册,并通过cargo login ****** 来登陆 (2)发布前需要在Cargo.toml中增加描述: [package] name = "package_name" version = "0.1.0" license = "MIT" #Linux 基金会 的 Software Package Data Exchange (SPDX) 列出了可以使用的标识符 authors = ["linghuyichong"] description = "some thing descript the package" 运行cargo publish来发布。 (3)撤回指定版本 cargo yank --vers 0.1.0 来源: oschina 链接: https://my.oschina.net/u/943779/blog/4922281

Changing key of HashMap from child method

北慕城南 提交于 2021-01-24 11:20:50
问题 I'm working on a 2D sandbox game that implements tiles in piston. The basic structure of this is an App struct, which stores a world: World struct, which has a tiles: HashMap property of Box<Tile> s, with the key being an X-Y position of where the tile is. To render this, I loop through the coordinates of every tile space on the screen and if there is a tile in the hashmap with those co-ordinates, I call tile.render(&context, &mut gl); . I now need to implement a Tile.update() method which

Changing key of HashMap from child method

丶灬走出姿态 提交于 2021-01-24 11:19:27
问题 I'm working on a 2D sandbox game that implements tiles in piston. The basic structure of this is an App struct, which stores a world: World struct, which has a tiles: HashMap property of Box<Tile> s, with the key being an X-Y position of where the tile is. To render this, I loop through the coordinates of every tile space on the screen and if there is a tile in the hashmap with those co-ordinates, I call tile.render(&context, &mut gl); . I now need to implement a Tile.update() method which

【Rust日报】2020-11-20 Rust的Pin与Unpin

时光总嘲笑我的痴心妄想 提交于 2021-01-24 08:39:31
Rust的Pin与Unpin 这是小编的个人的私货。😝 今天刚发布的一篇探讨《Rust的Pin与Unpin》的博客文章,我将尝试由浅入深的梳理Pin和Unpin,希望能帮助大家理解这些概念。 博客链接: https://folyd.com/blog/rust-pin-unpin/ 互联网档案馆Archive.org使用Rust存档 Flash 动画和游戏 互联网档案馆发布了一款用Rust开发了 Flash 模拟器 Ruffle (https://ruffle.rs/),无需 Flash 插件就能在浏览器中运行 Flash 动画和游戏作品。互联网档案馆表示它的软件库中开始加入 Flash 作品,目前收藏品已经 超过 1000个 ,用户都可以在浏览器中直接运行。Adobe Flash 将在今年年底结束支持,主流浏览器已逐渐移除了对 Flash 的支持。但 Flash 作为一种创作工具留下了很多出色的作品。 链接: http://blog.archive.org/2020/11/19/flash-animations-live-forever-at-the-internet-archive/ Rewrite in Rust:更现代的命令行工具合集 Rust让命令行工具重新焕发新的生命力,社区用Rust重写了很多旧的命令行工具。这篇文章做了一一例举。 比如: bat替换cat

【Rust日报】2020-11-03 《Rust日报》总第1000期

北战南征 提交于 2021-01-24 07:35:24
今日头版 《Rust日报》第1000期,感谢有你 两年半的时间,我们一期期走来,到了今天发行的第1000期。回想我第一次看《Rust日报》,还是在Rust 2018刚推出的时候。丰富的新闻和思考让我眼前一亮,我慢慢开始喜欢这样的报纸。每天浏览日报,已经成为许多Rust爱好者的生活习惯。 Rust日报社很高兴能和读者们共同进步,也很乐于见到更多的企业、研究团队开始关注这门编程技术。希望在未来的时间里,我们能更好地推广Rust语言,传递更多的社区开发知识,第一时间传播各地Rust开发者的动态和新闻。 加油,Rust爱好者们! 生态圈 内存数据交换格式Apache Arrow发布了v2.0.0版本 Apache Arrow项目定义了基于内存的数据格式,致力于解决系统与系统间的数据传输问题。目前此项目已经发布了重大的更新v2.0.0版本,其中包含的Rust子项目尤为重要。Apache Arrow PMC认为,Rust实现正在缩短与C/C++实现的功能差距,慢慢赶上功能最多的Java、C/C++版本。 本次更新的重点包含很多个模块。核心的“Arrow”模块包含了数据的表示,更新增加了原始类型数组的支持,现在它能从一个迭代器里被加载和转换。实现内部现在使用动态长度的数组,来统一32、64位平台间的差异。Arrow的运算内核也有了较大的改进,添加了大量针对字符串、整数的函数