Is there a way for me to obtain a static borrowed reference to a struct's implementation of a trait:
trait Trait {}
struct Example;
impl Trait for Example {}
This works fine:
static instance1: Example = Example;
This also works fine:
static instance2: &'static Example = &Example;
But this doesn't work:
static instance3: &'static Trait = &Example as &'static Trait;
It fails thus:
error[E0277]: the trait bound `Trait + 'static: std::marker::Sync` is not satisfied in `&'static Trait + 'static`
--> src/main.rs:10:1
|
10 | static instance3: &'static Trait = &Example as &'static Trait;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Trait + 'static` cannot be shared between threads safely
|
= help: within `&'static Trait + 'static`, the trait `std::marker::Sync` is not implemented for `Trait + 'static`
= note: required because it appears within the type `&'static Trait + 'static`
= note: shared static variables must have a type that implements `Sync`
Alternatively, is there a way to obtain a borrowed static pointer to a trait from a global borrowed static pointer to a struct:
static instance2: &'static Example = &Example;
fn f(i: &'static Trait) {
/* ... */
}
fn main() {
// how do I invoke f passing in instance2?
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…