有没有办法让我获得对结构的特征实现的静态借用引用:
trait Trait {}
struct Example;
impl Trait for Example {}
这工作正常:
static instance1: Example = Example;
这也可以正常工作:
static instance2: &'static Example = &Example;
但这不起作用:
static instance3: &'static Trait = &Example as &'static Trait;
它因此失败:
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`
或者,有没有一种方法可以从全局借用静态指针获取指向 trait 的借用静态指针:
static instance2: &'static Example = &Example;
fn f(i: &'static Trait) {
/* ... */
}
fn main() {
// how do I invoke f passing in instance2?
}