So I have this trait and a struct that implements it:
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
}
}
I have another structure that I wish to be global, that contains the trait:
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)
}
This fails because
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
It's not really feasible to always do the processing within the .with(...)
part, because some functions that take in the Trait
don't care where it comes from. So I try to return a reference to it instead:
fn trait_item<'a>() -> &'a Box<dyn Trait> {
CHEAP.with(|c| &c.item)
}
This also fails because I cannot send a reference outside of the 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)
| ^^^^^^^^^^^^^^^^^^^^^^^
So instead I wrap the whole thing in an Rc
like so:
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())
}
But now clippy complains:
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
Am I missing something, or is it not actually possible to do what clippy recommends here?
You can wrap a trait object in a Rc
as well, since Rc
is also a pointer.
Therefore if you have Rc<Box<T>>
, you have two allocations: One for the T
and one for the Box
(another pointer, that now is on the heap). Instead, use Rc<dyn MyTrait>
to only have one allocation.