[英]Multible trait objects of the same instance wrapped by Arc<Mutex<_>>
The goal is to have an object ( callback_handler
) implementing the traits A
, B
and C
that can be passed to different functions that expect a trait object of type A
or B
, for instance.目标是让 object (
callback_handler
) 实现特征A
、 B
和C
,这些特征可以传递给不同的函数,例如期望类型为A
或B
的特征 object 。
This requires callback_handler
, and the trait objects to be protected by the same Mutex
(since, eg, trait A
expects &mut self
).这需要
callback_handler
和 trait 对象由同一个Mutex
保护(因为,例如,trait A
期望&mut self
)。 Furthermore, for this setup to work, this Mutex
must be wrapped in an Arc
or Rc
:此外,要使此设置生效,必须将此
Mutex
包装在Arc
或Rc
中:
use std::sync::{Arc, Mutex};
trait A {
fn a(&mut self) {}
}
trait B {}
trait C: A + B {}
struct Foo {}
impl A for Foo {}
impl B for Foo {}
impl C for Foo {}
fn register_for_A(callback_handler: Arc<Mutex<Box<dyn A>>>) {
// Register for callbacks defined in `A`.
// Store the passed handler somewhere.
}
fn register_for_B(callback_handler: Arc<Mutex<Box<dyn B>>>) {
// Register for callbacks defined in `B`.
// Store the passed handler somewhere.
}
fn main() {
let callback_handler = Arc::new(Mutex::new(Box::new(Foo{})));
// Register for callbacks using different trait objects of the same "origin" object.
// For this to be safe, all objects must be protected by the same ("shared") mutex.
// !!! This will not work since there must be some steps to cast this to the right type. !!!
register_for_A(Arc::clone(&callback_handler));
register_for_B(Arc::clone(&callback_handler));
// We can still use `callback_handler` to call methods on Foo ourself
}
Now, the question is, how can the origin object callback_hanlder
of type Arc<Mutex<Box<Foo>>>
be casted/converted to an Arc<Mutex<Box<dyn A>>>
and Arc<Mutex<Box<dyn B>>>
?现在,问题是,如何将
Arc<Mutex<Box<Foo>>>
类型的原点 object callback_hanlder
转换/转换为Arc<Mutex<Box<dyn A>>>
和Arc<Mutex<Box<dyn B>>>
? While I don't see a technical reason why this should not be possible, I don't know how to perform such a conversion and whether it is doable.虽然我没有看到为什么这不可能的技术原因,但我不知道如何执行这样的转换以及它是否可行。 While one solution would be using the
Any
trait, I was hoping that there is a solution that retains compile-time type safety for functions register_for_A
and register_for_B
.虽然一种解决方案是使用
Any
特性,但我希望有一种解决方案可以保留函数register_for_A
和register_for_B
的编译时类型安全性。
You're getting tripped up by your double use of smart pointers Box
+ Arc
you can simply cast an Arc<Mutex<Foo>>
to an Arc<Mutex<dyn A>>
using as
, there is really no need to additionally Box
it:你被智能指针
Box
+ Arc
的双重使用绊倒了你可以简单地将Arc<Mutex<Foo>>
转换为Arc<Mutex<dyn A>>
使用as
,真的没有必要另外将它Box
:
use std::sync::{Arc, Mutex};
trait A {
fn a(&mut self) {}
}
trait B {}
struct Foo {}
impl A for Foo {}
impl B for Foo {}
fn register_for_A(callback_handler: Arc<Mutex<dyn A>>) {
// Register for callbacks defined in `A`.
// Store the passed handler somewhere.
}
fn register_for_B(callback_handler: Arc<Mutex<dyn B>>) {
// Register for callbacks defined in `B`.
// Store the passed handler somewhere.
}
fn main() {
let callback_handler = Arc::new(Mutex::new(Foo{}));
// Register for callbacks using different trait objects of the same "origin" object.
// For this to be safe, all objects must be protected by the same ("shared") mutex.
// !!! This will not work since there must be some steps to cast this to the right type. !!!
register_for_A(Arc::clone(&callback_handler) as _);
register_for_B(Arc::clone(&callback_handler) as _);
// We can still use `callback_handler` to call methods on Foo ourself
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.