注意:无法满足"_:解码器可运行<u8>"



在我制作的这个简单草图上,我尝试使用一个名为Runnable的特征来运行Arc<dyn LockableOption<T>>

use std::sync::{Arc, LockResult, Mutex, MutexGuard, PoisonError};
pub type LockableArc<T> = Arc<Mutex<Option<T>>>;
pub struct MutexGuardOptionRef<'a, T: ?Sized> {
pub mutex_guard: MutexGuard<'a, Option<Box<T>>>,
}
pub trait LockableOption<T: ?Sized>: Send + Sync {
fn lock(&self) -> LockResult<MutexGuardOptionRef<T>>;
}
impl<T: ?Sized + Send> LockableOption<T> for LockableArc<Box<T>> {
fn lock(&self) -> LockResult<MutexGuardOptionRef<T>> {
unimplemented!()
}
}
pub trait Decoder<T>: Send {
}
pub struct FfmpegDecoder<T> {
x: T,
}
impl<T: 'static + Send> Decoder<T> for FfmpegDecoder<T> {
}
trait DecoderRunnable<T> {
fn run(s: Arc<dyn LockableOption<dyn Decoder<T>>>);
}
impl<T: 'static + Send> DecoderRunnable<T> for FfmpegDecoder<T> {
fn run(s_lockable: Arc<dyn LockableOption<dyn Decoder<T>>>) {
unimplemented!()
}
}
fn main() {
let r: LockableArc<Box<dyn Decoder<u8>>> = Arc::new(Mutex::new(Some(Box::new(FfmpegDecoder{x: 0u8}))));
let rr: Arc<dyn LockableOption<dyn Decoder<u8>>> = Arc::new(r);
DecoderRunnable::<u8>::run(rr.clone());
}

操场

我收到错误:

error[E0283]: type annotations needed
--> src/main.rs:42:5
|
30 |     fn run(s: Arc<dyn LockableOption<dyn Decoder<T>>>);
|     --------------------------------------------------- required by `DecoderRunnable::run`
...
42 |     DecoderRunnable::<u8>::run(rr.clone());
|     ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
|
= note: cannot satisfy `_: DecoderRunnable<u8>`

我没有得到。为什么这里的任何东西都应该满足DecoderRunnableDecoderRunnable是一个具有期望Arc<dyn LockableOption<dyn Decoder<T>>>run函数的特征,我正在传递rr这正是这样。

特征必须是实现类型,以便编译器可以确定要运行的实现。在您的示例中,DecoderRunnable仅针对FfmpegDecoder<T>实现,并且您尝试在没有实现的Arc<dyn LockableOption<dyn Decoder<u8>>>上调用它。

始终可以使用以下语法指定需要调用的实现:

<FfmpegDecoder<u8> as DecoderRunnable::<u8>>::run(rr);

虽然这似乎不像你想做的.目前尚不清楚您要抽象的内容,因为您还将解码器深深嵌套在LockableArc<T>中。

如果你只想给LockableArc<Box<dyn Decoder<u8>>>添加方便的方法,你可以为Arc<dyn ...>添加一个 impl 块,并使run方法以&self而不是Arc<dyn ...>作为它的第一个参数。

相关内容

最新更新