Posso ter uma referência emprestada estática para um objeto de traço?
Existe uma maneira de obter uma referência emprestada estática para a implementação de uma característica de uma estrutura:
trait Trait {}
struct Example;
impl Trait for Example {}
Isso funciona bem:
static instance1: Example = Example;
Isso também funciona bem:
static instance2: &'static Example = &Example;
Mas isso não funciona:
static instance3: &'static Trait = &Example as &'static Trait;
Ele falha assim:
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`
Alternativamente, existe uma maneira de obter um ponteiro estático emprestado para uma característica de um ponteiro estático global emprestado para uma estrutura:
static instance2: &'static Example = &Example;
fn f(i: &'static Trait) {
/* ... */
}
fn main() {
// how do I invoke f passing in instance2?
}