如何使用 Arc<Mutex<...>> 投射特征对象?
How do you cast trait objects with an Arc<Mutex<...>>?
我正在尝试调用一个函数,该函数接受指向某个特征对象的 Mutex
的指针,并且我希望能够实现该特征对象的 Mutex
的特征允许将 Mutex
作为特征对象的实例进行抽象。
举个例子,想象一个 Event
侦听器设置:
use std::sync::{Arc, Mutex, Weak};
// Define a simple event
trait Event: Send + Sync + 'static {}
impl Event for String {}
// Define the listener interface
trait Listener<E: Event> {
fn notify(&self, event: &E);
}
// Extend the listener interface to listenrs wrapped by a mutex
impl<E: Event> Listener<E> for Mutex<Listener<E>> {
fn notify(&self, event: &E) {
self.lock().unwrap().notify(event);
}
}
// Contrived thing to listen for messages
struct Console;
impl Listener<String> for Console {
fn notify(&self, event: &String) {
println!("{}", event);
}
}
// Simple function which may be called asynchronously and then sends a message
// when it is complete
fn do_stuff(l: Arc<Listener<String>>) {
// Would normally cast to a Weak<...> and then store in a list of listneners
// For some sort of object
let m = String::from("I did stuff!");
l.notify(&m);
}
fn main() {
let l: Arc<Mutex<Console>> = Arc::new(Mutex::new(Console));
let t1 = Arc::clone(&l) as Arc<Mutex<Listener<String>>>; //this part is ok
// Here is where we run into issues... This *should* be equvlient to
// do_stuff(t1), but with the corercion explicit
let t2 = Arc::clone(&t1) as Arc<Listener<String>>;
do_stuff(t2);
// This is a simple, working example of it interpreting a Mutex<Listener<E>>
// as just a Listener<E>
let m = String::from("Somthing else...");
(l as Arc<Mutex<Listener<String>>>).notify(&m);
}
问题是:
error[E0277]: the trait bound `Listener<std::string::String>: std::marker::Sized` is not satisfied in `std::sync::Mutex<Listener<std::string::String>>`
--> src/main.rs:45:14
|
45 | let t2 = Arc::clone(&t1) as Arc<Listener<String>>;
| ^^^^^^^^^^^^^^^ `Listener<std::string::String>` does not have a constant size known at compile-time
|
= help: within `std::sync::Mutex<Listener<std::string::String>>`, the trait `std::marker::Sized` is not implemented for `Listener<std::string::String>`
= note: required because it appears within the type `std::sync::Mutex<Listener<std::string::String>>`
= note: required for the cast to the object type `Listener<std::string::String>`
为什么会这样?由于 Arc
是指向数据的指针,根据我的理解,它应该能够指向 Listener<String>
而恰好是 Listener<Mutex<String>>
.
我看到至少有两种方法可以避免这种情况,第一种是简单地 impl Listener<String> for Mutex<Listener<String>>
,但是,在实际代码中,这可能需要相互依赖,应该避免这种情况,因为特征只能在特征所在的地方实现或结构已定义(并且 Mutex
未在我的代码中定义)。
第二个是将 Mutex
移动到 Listener
对象中,因此调用者根本不需要转换它。这会起作用,并且可能是更好的解决方案。尽管如此,我还是很好奇为什么建议的转换不起作用,或者可以更改什么使其起作用。
Since an Arc
is a pointer to data, from my understanding, it should be able to point to a Listener<String>
是的,没错。我相信你的问题是你(不小心?)要求你在某个时候有一个 Mutex<Listener<String>>
。这是 not 有效的,因为 Mutex
中的值不在指针后面,因此使整个类型未调整大小。
虽然 Arc<Mutex<Listener<String>>>
很好。
相反,我会为实现相同特征的 任何 种类的 Mutex
实现特征。我也会对特征的引用和 Box
ed 特征对象做同样的事情。在所有情况下,我都会删除 Sized
绑定以允许特征对象:
use std::sync::{Arc, Mutex};
trait Event: Send + Sync + 'static {}
impl Event for String {}
trait Listener<E: Event> {
fn notify(&self, event: &E);
}
impl<L, E> Listener<E> for Mutex<L>
where
L: ?Sized + Listener<E>,
E: Event,
{
fn notify(&self, event: &E) {
self.lock().unwrap().notify(event);
}
}
impl<'a, L, E> Listener<E> for &'a L
where
L: ?Sized + Listener<E>,
E: Event,
{
fn notify(&self, event: &E) {
(**self).notify(event);
}
}
struct Console;
impl Listener<String> for Console {
fn notify(&self, event: &String) {
println!("{}", event);
}
}
fn do_stuff(l: Arc<Listener<String>>) {
let m = String::from("I did stuff!");
l.notify(&m);
}
fn main() {
let l: Arc<Mutex<Console>> = Arc::new(Mutex::new(Console));
let l2 = Arc::clone(&l) as Arc<Listener<String>>;
let l3 = Arc::clone(&l) as Arc<Listener<String>>;
do_stuff(l);
do_stuff(l2);
let m = String::from("Something else...");
l3.notify(&m);
}
我正在尝试调用一个函数,该函数接受指向某个特征对象的 Mutex
的指针,并且我希望能够实现该特征对象的 Mutex
的特征允许将 Mutex
作为特征对象的实例进行抽象。
举个例子,想象一个 Event
侦听器设置:
use std::sync::{Arc, Mutex, Weak};
// Define a simple event
trait Event: Send + Sync + 'static {}
impl Event for String {}
// Define the listener interface
trait Listener<E: Event> {
fn notify(&self, event: &E);
}
// Extend the listener interface to listenrs wrapped by a mutex
impl<E: Event> Listener<E> for Mutex<Listener<E>> {
fn notify(&self, event: &E) {
self.lock().unwrap().notify(event);
}
}
// Contrived thing to listen for messages
struct Console;
impl Listener<String> for Console {
fn notify(&self, event: &String) {
println!("{}", event);
}
}
// Simple function which may be called asynchronously and then sends a message
// when it is complete
fn do_stuff(l: Arc<Listener<String>>) {
// Would normally cast to a Weak<...> and then store in a list of listneners
// For some sort of object
let m = String::from("I did stuff!");
l.notify(&m);
}
fn main() {
let l: Arc<Mutex<Console>> = Arc::new(Mutex::new(Console));
let t1 = Arc::clone(&l) as Arc<Mutex<Listener<String>>>; //this part is ok
// Here is where we run into issues... This *should* be equvlient to
// do_stuff(t1), but with the corercion explicit
let t2 = Arc::clone(&t1) as Arc<Listener<String>>;
do_stuff(t2);
// This is a simple, working example of it interpreting a Mutex<Listener<E>>
// as just a Listener<E>
let m = String::from("Somthing else...");
(l as Arc<Mutex<Listener<String>>>).notify(&m);
}
问题是:
error[E0277]: the trait bound `Listener<std::string::String>: std::marker::Sized` is not satisfied in `std::sync::Mutex<Listener<std::string::String>>`
--> src/main.rs:45:14
|
45 | let t2 = Arc::clone(&t1) as Arc<Listener<String>>;
| ^^^^^^^^^^^^^^^ `Listener<std::string::String>` does not have a constant size known at compile-time
|
= help: within `std::sync::Mutex<Listener<std::string::String>>`, the trait `std::marker::Sized` is not implemented for `Listener<std::string::String>`
= note: required because it appears within the type `std::sync::Mutex<Listener<std::string::String>>`
= note: required for the cast to the object type `Listener<std::string::String>`
为什么会这样?由于 Arc
是指向数据的指针,根据我的理解,它应该能够指向 Listener<String>
而恰好是 Listener<Mutex<String>>
.
我看到至少有两种方法可以避免这种情况,第一种是简单地 impl Listener<String> for Mutex<Listener<String>>
,但是,在实际代码中,这可能需要相互依赖,应该避免这种情况,因为特征只能在特征所在的地方实现或结构已定义(并且 Mutex
未在我的代码中定义)。
第二个是将 Mutex
移动到 Listener
对象中,因此调用者根本不需要转换它。这会起作用,并且可能是更好的解决方案。尽管如此,我还是很好奇为什么建议的转换不起作用,或者可以更改什么使其起作用。
Since an
Arc
is a pointer to data, from my understanding, it should be able to point to aListener<String>
是的,没错。我相信你的问题是你(不小心?)要求你在某个时候有一个 Mutex<Listener<String>>
。这是 not 有效的,因为 Mutex
中的值不在指针后面,因此使整个类型未调整大小。
虽然 Arc<Mutex<Listener<String>>>
很好。
相反,我会为实现相同特征的 任何 种类的 Mutex
实现特征。我也会对特征的引用和 Box
ed 特征对象做同样的事情。在所有情况下,我都会删除 Sized
绑定以允许特征对象:
use std::sync::{Arc, Mutex};
trait Event: Send + Sync + 'static {}
impl Event for String {}
trait Listener<E: Event> {
fn notify(&self, event: &E);
}
impl<L, E> Listener<E> for Mutex<L>
where
L: ?Sized + Listener<E>,
E: Event,
{
fn notify(&self, event: &E) {
self.lock().unwrap().notify(event);
}
}
impl<'a, L, E> Listener<E> for &'a L
where
L: ?Sized + Listener<E>,
E: Event,
{
fn notify(&self, event: &E) {
(**self).notify(event);
}
}
struct Console;
impl Listener<String> for Console {
fn notify(&self, event: &String) {
println!("{}", event);
}
}
fn do_stuff(l: Arc<Listener<String>>) {
let m = String::from("I did stuff!");
l.notify(&m);
}
fn main() {
let l: Arc<Mutex<Console>> = Arc::new(Mutex::new(Console));
let l2 = Arc::clone(&l) as Arc<Listener<String>>;
let l3 = Arc::clone(&l) as Arc<Listener<String>>;
do_stuff(l);
do_stuff(l2);
let m = String::from("Something else...");
l3.notify(&m);
}