- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
use once_cell::sync::OnceCell;
pub trait SomeTrait {}
pub struct Impl1 {}
impl SomeTrait for Impl1 {}
pub static GLOBAL_THING: OnceCell<Box<dyn SomeTrait>> = OnceCell::new();
pub fn main() {
GLOBAL_THING.set(Box::new(Impl1 {})).unwrap();
}
我收到此错误,但不确定如何解释它的含义或修复它。
error[E0599]: the method `unwrap` exists for enum `Result<(), Box<(dyn SomeTrait + 'static)>>`, but its trait bounds were not satisfied
--> src/main.rs:11:42
|
11 | GLOBAL_THING.set(Box::new(Impl1 {})).unwrap();
| ^^^^^^ method cannot be called on `Result<(), Box<(dyn SomeTrait + 'static)>>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`Box<dyn SomeTrait>: Debug`
一个简单的解决方法是只执行 if let Err() = GLOBAL_THING.set() {panic!(...)}
而不是使用 unwrap()
,但我想了解这里发生了什么,并在可能的情况下进行修复。
最佳答案
如果你看一下 unwrap
method文档你会看到它没有为所有 Result
定义s,但只有那些 E: Debug
:
impl<T, E> Result<T, E>
where
E: Debug,
{
pub fn unwrap(self) -> T;
}
它需要错误类型 E
实现Debug
以便在解包失败时打印错误。
报错提示结果类型为Result<(), Box<(dyn SomeTrait + 'static)>>
, 所以 E = Box<(dyn SomeTrait + 'static)>
.您可以通过 SomeTrait: Debug
使此错误类型可调试,这要求任何实现 SomeTrait
的类型还必须实现 Debug
.
pub trait SomeTrait: Debug {}
#[derive(Debug)]
pub struct Impl1 {}
一旦你这样做了,你会遇到下一个错误:
error[E0277]: `dyn SomeTrait` cannot be shared between threads safely
--> src/main.rs:11:1
|
11 | pub static GLOBAL_THING: OnceCell<Box<dyn SomeTrait>> = OnceCell::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `dyn SomeTrait` cannot be shared between threads safely
|
= help: the trait `Sync` is not implemented for `dyn SomeTrait`
= note: required because of the requirements on the impl of `Sync` for `Unique<dyn SomeTrait>`
= note: required because it appears within the type `Box<dyn SomeTrait>`
= note: required because of the requirements on the impl of `Sync` for `once_cell::imp::OnceCell<Box<dyn SomeTrait>>`
= note: required because it appears within the type `once_cell::sync::OnceCell<Box<dyn SomeTrait>>`
= note: shared static variables must have a type that implements `Sync`
要解决此问题,您还需要制作盒装特征对象 Send + Sync
以便它可以跨线程共享。
pub static GLOBAL_THING: OnceCell<Box<dyn SomeTrait + Send + Sync>> = OnceCell::new();
您可以在 Playground 上看到最终结果.
关于rust - Trait Bound 不满足 Result<(), Box<(dyn SomeTrait + 'static)>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69592445/
我对下面的生命周期发生了什么感到困惑: struct Foo{} impl Foo { fn foo(&self, _s: &str) {} } fn main() { let foo
这个问题在这里已经有了答案: How to convert a boxed trait into a trait reference? (2 个答案) 关闭去年。 我正在尝试获取 &dyn T来自B
这个问题在这里已经有了答案: Why doesn't Rust support trait object upcasting? (4 个回答) 1年前关闭。 我有一个库需要实现特定特征( TQDisp
我有一个接收 Box 的函数并需要将其转换为 Rc在线程内共享只读所有权。 用Box一些T: Sized ,我们可以做到Rc::new(*my_box) ,但不幸的是that doesn't work
我可以接受 Box 吗?在接受 Box 的地方?如果是,如何?如果不是,为什么,还有比以下示例更优雅的方法吗? #![allow(unused)] use std::error::Error as S
这个问题在这里已经有了答案: Why doesn't Rust support trait object upcasting? (4 个回答) 2年前关闭。 如果我有 Box , 我可以返回 &dyn
我无法将参数传递给 fn。 trait T {} struct S { others: Vec>> } impl S { fn bar(&self) { for o i
在我的程序中,在辅助线程上执行了一些操作及其结果:Result>被发送回主线程。这是非常合理的错误有 Send要求,所以实际类型是Result> .我还加了 Sync能够使用 Box from方法(仅
我有一个 Vec>作为输入,我想将其元素存储在 Vec>> .最好的方法是什么? 我试过: use std::cell::RefCell; use std::rc::Rc; trait Trait {
use std::sync::Arc; pub type A = Arc Result + Send + Sync>; pub struct B { a: A, } 给 Compilin
这个问题在这里已经有了答案: Why doesn't Rust support trait object upcasting? (4 个回答) Can I cast between two trait
读完the subtyping chapter of the Nomicon ,我无法理解类型参数的协方差。特别是对于 Box类型,描述为:T is covariant . 但是,如果我写这段代码:
我尝试在 vs 包管理器中安装第三方包时反复出现此错误, Unable to resolve dependency 'openssl.v120.windesktop.msvcstl.dyn.rt-dy
我有以下简化代码。 use async_trait::async_trait; // 0.1.36 use std::error::Error; #[async_trait] trait Metric
我有以下简化的代码。 use async_trait::async_trait; // 0.1.36 use std::error::Error; #[async_trait] trait Metri
我正在尝试在线程之间共享一个函数,可以用另一个函数调用它: fn main() { let on_virtual_tun_write = std::sync::Arc::new(|f: &dy
我最近看过使用dyn关键字的代码: fn foo(arg: &dyn Display) {} fn bar() -> Box {} 这个语法是什么意思? 最佳答案 TL; DR:这是用于指定特征对象类
我正在创建HashMap>。我可以创建HashMap并插入一个实现MyTrait的结构,但是当我检索MyTrait并尝试使用它时,编译器向我提示: error[E0161]: cannot move
我正在阅读一些代码,它有一个 consume使我可以通过自己的函数 f 的函数. fn consume(self, _timestamp: Instant, len: usize, f: F) ->
我最近看到使用 dyn 关键字的代码: fn foo(arg: &dyn Display) {} fn bar() -> Box {} 这个语法是什么意思? 最佳答案 TL;DR:这是一种用于指定tr
我是一名优秀的程序员,十分优秀!