< 返回版块

苦瓜小仔 发表于 2025-05-22 07:50

Tags:日报

const Trait 设计文档《Effectively constant or constantly effective》

作者:Niko

时间:2025 年 3 月

目前 Rust effects 的焦点是 const Trait,它是一个实现了很久功能,中途完全重写,导致大范围的夜间代码破坏。但目前已经有正式的 RFC 了:

RFC#3762 Make trait methods callable in const contexts:https://github.com/rust-lang/rfcs/pull/3762

而 Niko 的这篇记录整理了效果的形式化模型 (Formality model) 思考,以及 const Trait 的设计和语法。

We define an effect E as a set of runtime and associated effects ::Effect:

E = panic | loop | runtime | await | throw | yield | <P0 as Trait<P1...>>::Effect | union(E0, ...., En)

Rust "keywords" and funtion declarations can be translated to sets of these primitive effects:

  • a const fn can have the effects union(panic, loop)
  • a fn can have the union(panic, loop, runtime) (we also call this set of effects "default")
  • an async fn can have the effects union(await, default)
  • a gen fn, if we had that, would have the effect union(yield, default)

文档:https://nikomatsakis.github.io/constantly-effective/

讨论:https://rust-lang.zulipchat.com/#narrow/channel/328082-t-lang.2Feffects/topic/.22constantly.20effective.22/with/508272260

文章《Tarpaulins Week Of Speed Pt2》

作者:xd009642

本文介绍了作者使用samply工具对代码进行性能优化的过程。通过分析火焰图,作者发现HashMap::get在报告生成中占用大量时间,通过调整迭代顺序和优化文件读取方式,显著提升了性能。

同时,作者还尝试了链接时优化(LTO)来进一步提高性能。最终,这些优化在多个项目中取得了良好的效果,降低了运行时间。

阅读:https://xd009642.github.io/2025/05/20/Tarpaulins-Week-of-Speed-pt2.html

Reddit:https://www.reddit.com/r/rust/comments/1krqt6s/tarpaulins_week_of_speed_part_2/

文章《Rust turns 10: How a broken elevator changed software forever》

作者:Steven Vaughan-Nichols

文章讲述了 Rust 编程语言诞生 10 年的发展历程。Rust 由 Mozilla 软件开发者 Graydon Hoare 于 2006 年因电梯故障引发的思考而开始设计,旨在创建一种小型、快速且无内存错误的编程语言。它通过独特的所有权系统确保内存安全,防止常见错误。

2015 年 Rust 1.0 发布后,迅速发展,其包注册中心从 2000 个包增长到如今的 18 万多个,被众多科技巨头采用,尽管学习曲线较陡,但因其安全性和高效性受到开发者青睐。

阅读:https://www.zdnet.com/article/rust-turns-10-how-a-broken-elevator-changed-software-forever/

Reddit:https://www.reddit.com/r/rust/comments/1kraum8/rust_turns_10_how_a_broken_elevator_changed/

iddqd - 借用结构作为键的 map

iddqd 是一个 Rust 语言的 crate,提供多种 ID 映射类型,包括基于 B-Tree 的 IdOrdMap、哈希映射的 IdHashMap、双向映射的 BiHashMap 和三向映射的 TriHashMap

这些映射类型允许从值中借用键,解决了传统映射中键值一致性维护的痛点。

使用时需为值类型实现特定 trait,如 IdOrdItemIdHashItem 等。它还支持通过 insert_overwriteinsert_unique 插入值,并且 serde 实现会拒绝重复键。

该 crate 适用于需要复杂键值映射的场景,且通过多种测试确保可靠性,支持 no-std 环境(部分类型除外),并提供可选功能扩展。

use iddqd::{IdHashMap, IdHashItem, id_upcast};

#[derive(Debug)]
struct Artifact {
    name: String,
    version: String,
    data: Vec<u8>,
}

// The key type is a borrowed form of the name and version. It needs to
// implement `Hash + Eq`.
#[derive(Hash, PartialEq, Eq)]
struct ArtifactKey<'a> {
    name: &'a str,
    version: &'a str,
}

impl IdHashItem for Artifact {
    // The key type can borrow from the value.
    type Key<'a> = ArtifactKey<'a>;

    fn key(&self) -> Self::Key<'_> {
        ArtifactKey {
            name: &self.name,
            version: &self.version,
        }
    }

    id_upcast!();
}

let mut artifacts = IdHashMap::<Artifact>::new();

// Add artifacts to the map.
artifacts.insert_unique(Artifact {
    name: "artifact1".to_owned(),
    version: "1.0".to_owned(),
    data: b"data1".to_vec(),
})
.unwrap();
artifacts.insert_unique(Artifact {
    name: "artifact2".to_owned(),
    version: "1.0".to_owned(),
    data: b"data2".to_vec(),
})
.unwrap();

// Look up artifacts by name and version.
assert_eq!(
    artifacts
        .get(&ArtifactKey { name: "artifact1", version: "1.0" })
        .unwrap()
        .data,
    b"data1",
);

仓库:https://github.com/oxidecomputer/iddqd

Reddit:https://www.reddit.com/r/rust/comments/1ksagxg/announcing_iddqd_maps_where_keys_are_borrowed/

roto - 适用于 Rust 的静态类型、JIT 编译、可热重载的嵌入式脚本语言

Roto 是一种强类型、编译型嵌入式脚本语言,主要用于 Rotonda(一个可组合、可编程的 BGP 路由引擎)来创建路由过滤器。

它可嵌入任何 Rust 应用,支持 Rust 类型和函数注册。其特点包括静态类型检查、编译为机器码以实现快速运行、支持热重载等。

但也存在一些限制,如所有注册的 Rust 类型必须实现 CloneCopy,不支持循环构造等。

仓库:https://github.com/NLnetLabs/roto

Reddit:https://www.reddit.com/r/rust/comments/1kry3ct/statically_typed_jit_compiled_hotreloadable/

crabapple - 读取、检查和提取加密 iOS 备份数据的库

crabapple 是一个 Rust 库,用于读取、检查和提取由 Finder、Apple 设备或 iTunes 创建的加密 iOS 备份中的数据。

它支持加载和解析 Manifest.plist 以获取元数据、设备信息和加密参数,能够解密和查询 AES-256 加密的 Manifest.db,并按保护类别检索和解密单个文件。

该库跨平台支持 macOS、Windows 和 Linux,适用于需要访问 iOS 备份数据的项目。

仓库:https://github.com/ReagentX/crabapple

Reddit:https://www.reddit.com/r/rust/comments/1ks04rt/announcing_crabapple_library_for_reading/

ml-rf-wasm - 将随机森林分类器编译为 WASM 的示例

ml-rf-wasm 是一个将 Rust 编写的随机森林分类器编译为 WebAssembly(WASM)的项目,用于在本地 HTML 页面中运行机器学习模型。

它基于 smartcore 库实现随机森林算法,并通过 wasm-bindgen 将 Rust 函数暴露给 JavaScript,以便在浏览器中进行模型训练和预测。

该项目旨在为其他编程语言的开发者提供一个 Rust 实践示例,尽管代码可能存在错误,但它展示了如何将 Rust 与 WebAssembly 结合用于机器学习任务。

仓库:https://github.com/jb-stats/ml-rf-wasm

Reddit:https://www.reddit.com/r/rust/comments/1ksbp2l/rust_compiled_to_webassembly_wasm_for_running/

--

From 日报小组 苦瓜小仔

社区学习交流平台订阅:

评论区

写评论

还没有评论

1 共 0 条评论, 1 页