7 个版本 (4 个重大更改)
0.5.2 | 2022 年 7 月 15 日 |
---|---|
0.5.1 | 2022 年 6 月 25 日 |
0.5.0 | 2021 年 10 月 16 日 |
0.4.0 | 2021 年 8 月 8 日 |
0.1.0 | 2021 年 6 月 26 日 |
#2 在 #mutable-borrow
每月 355 次下载
在 29 个 crate 中使用 (via resman)
22KB
315 行
🗃️ rt_map
运行时管理的从映射的可变借用。
此库提供了一个允许同时对不同条目进行可变借用的映射。有关此映射实现的示例,请参阅 rt_vec
。
实现是从 shred
中提取并稍作修改的。
使用方法
将以下内容添加到 Cargo.toml
rt_map = "0.5.2" # or
rt_map = { version = "0.5.2", features = ["unsafe_debug"] }
在代码中
use rt_map::RtMap;
struct A(u32);
fn main() {
let mut rt_map = RtMap::new();
rt_map.insert('a', A(1));
rt_map.insert('b', A(2));
// We can validly have two mutable borrows from the `RtMap` map!
let mut a = rt_map.borrow_mut(&'a');
let mut b = rt_map.borrow_mut(&'b');
a.0 = 2;
b.0 = 3;
// We need to explicitly drop the A and B borrows, because they are runtime
// managed borrows, and rustc doesn't know to drop them before the immutable
// borrows after this.
drop(a);
drop(b);
// Multiple immutable borrows to the same value are valid.
let a_0 = rt_map.borrow(&'a');
let _a_1 = rt_map.borrow(&'a');
let b = rt_map.borrow(&'b');
println!("A: {}", a_0.0);
println!("B: {}", b.0);
// Trying to mutably borrow a value that is already borrowed (immutably
// or mutably) returns `Err`.
let a_try_borrow_mut = rt_map.try_borrow_mut(&'a');
let exists = if a_try_borrow_mut.is_some() {
"Ok(..)"
} else {
"Err"
};
println!("a_try_borrow_mut: {}", exists); // prints "Err"
}
特性
"unsafe_debug"
启用 "unsafe_debug"
特性 rt_ref
。
另请参阅
许可证
在以下许可证中选择一项
- Apache 许可证 2.0 版,(LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT 许可证 (LICENSE-MIT 或 https://opensource.org/licenses/MIT)
任选其一。
贡献
除非您明确声明,否则任何有意提交以包含在您的工作中的贡献(根据 Apache-2.0 许可证定义),均应按上述方式双许可,不附加任何额外条款或条件。
依赖项
~39KB