rust - Clippy 冗余分配 lint
问题描述
所以我有这个特征和一个实现它的结构:
trait Trait {
fn foo(&self) -> u64;
}
/// No Copy trait supported because it's expensive
struct Expensive {
id: u64,
}
impl Trait for Expensive {
fn foo(&self) -> u64 {
self.id
}
}
我有另一个我希望成为全球性的结构,它包含以下特征:
struct Cheap {
pub item: Box<dyn Trait>,
}
thread_local! {
static CHEAP: Cheap = Cheap {
item: Box::new(Expensive {
id: 4
})
}
}
fn trait_item() -> Box<dyn Trait> {
CHEAP.with(|c| c.item)
}
这失败了,因为
error[E0507]: cannot move out of `c.item` which is behind a shared reference
--> src/main.rs:35:20
|
35 | CHEAP.with(|c| c.item)
| ^^^^^^ move occurs because `c.item` has type `std::boxed::Box<dyn Trait>`, which does not implement the `Copy` trait
总是在零件内进行处理并不是很可行.with(...)
,因为某些接受零件的功能Trait
并不关心它来自哪里。所以我尝试返回对它的引用:
fn trait_item<'a>() -> &'a Box<dyn Trait> {
CHEAP.with(|c| &c.item)
}
这也失败了,因为我无法在之外发送参考with
:
error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
--> src/main.rs:33:20
|
33 | CHEAP.with(|c| &c.item)
| ^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 33:16...
--> src/main.rs:33:16
|
33 | CHEAP.with(|c| &c.item)
| ^^^^^^^^^^^
note: ...so that reference does not outlive borrowed content
--> src/main.rs:33:20
|
33 | CHEAP.with(|c| &c.item)
| ^^^^^^^
note: but, the lifetime must be valid for the lifetime `'a` as defined on the function body at 32:15...
--> src/main.rs:32:15
|
32 | fn trait_item<'a>() -> &'a Box<dyn Trait> {
| ^^
note: ...so that reference does not outlive borrowed content
--> src/main.rs:33:5
|
33 | CHEAP.with(|c| &c.item)
| ^^^^^^^^^^^^^^^^^^^^^^^
因此,我将整个事情包装成Rc
这样:
struct Cheap {
pub item: Rc<Box<dyn Trait>>,
}
thread_local! {
static CHEAP: Cheap = Cheap {
item: Rc::new(Box::new(Expensive {
id: 4
}))
}
}
fn trait_item() -> Rc<Box<dyn Trait>> {
CHEAP.with(|c| c.item.clone())
}
但是现在clippy抱怨:
warning: usage of `Rc<Box<T>>`
--> src/main.rs:41:15
|
41 | pub item: Rc<Box<dyn Trait>>,
| ^^^^^^^^^^^^^^^^^^ help: try: `Box<dyn Trait>`
|
= note: `#[warn(clippy::redundant_allocation)]` on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_allocation
warning: usage of `Rc<Box<T>>`
--> src/main.rs:53:20
|
53 | fn trait_item() -> Rc<Box<dyn Trait>> {
| ^^^^^^^^^^^^^^^^^^ help: try: `Box<dyn Trait>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_allocation
warning: 2 warnings emitted
我是否遗漏了什么,或者实际上不可能做clippy在这里推荐的事情?
解决方案
您也可以将特征对象包装在 aRc
中,因为Rc
它也是一个指针。
因此,如果您有Rc<Box<T>>
,则您有两个分配:一个用于 the T
,一个用于 the Box
(另一个指针,现在在堆上)。相反,使用Rc<dyn MyTrait>
只有一个分配。
推荐阅读
- java - 处理程序没有实现延迟,而是使应用程序崩溃
- ios - 在 swift 4 中将 Sha256 散列值转换为 NSString
- python - 有效地将列表合并到稀疏列表中
- python - Rstudio 需要 keras 2.0.0,虽然我是通过 Python 安装的
- javascript - 如何在 DotNetBrowser 中获取地理位置信息
- logstash - 如何在logstash中拆分多行日志
- python-3.x - comtypes 与 Python3 兼容吗?
- java - 如何从另一个类调用 if 语句
- php - Laravel 获取每个组的最新记录
- c# - 将事件处理程序设置为参数