我的 Redis 服务器有一个连接类型Cons和一个Subscriber实现。ws 是一个 websocket 库。也没有机会编辑源代码:
use std::collections::HashMap;
use std::fmt::Debug;
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, Mutex};
#[derive(Debug)]
pub struct Consumer {
tag: String,
no_local: bool,
no_ack: bool,
exclusive: bool,
nowait: bool,
subscriber: Box<ConsumerSubscriber>,
pub current_message: Option<bool>,
}
impl Consumer {
pub fn new(
tag: String,
no_local: bool,
no_ack: bool,
exclusive: bool,
nowait: bool,
subscriber: Box<ConsumerSubscriber>,
) -> Consumer {
Consumer {
tag,
no_local,
no_ack,
exclusive,
nowait,
subscriber,
current_message: None,
}
}
pub fn new_delivery_complete(&mut self) {
if let Some(delivery) = self.current_message.take() {
self.subscriber.new_delivery(delivery);
}
}
}
pub trait ConsumerSubscriber: Debug + Send + Sync {
fn new_delivery(&mut self, delivery: bool);
}
#[derive(Clone)]
pub struct Sender {
connection_id: u32,
}
// Above code is out of my source code and I cannot edit it.
// Below is my own code.
type Cons = Arc<Mutex<HashMap<u64, Sender>>>;
#[derive(Debug)]
struct Subscriber {
messager: Arc<AtomicBool>,
connections: Cons,
}
impl ConsumerSubscriber for Subscriber {
fn new_delivery(&mut self, delivery: bool) {
println!("received correctly: {:?}", delivery)
}
}
fn main() {}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
use std::collections::HashMap;
use std::fmt::Debug;
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, Mutex};
#[derive(Debug)]
pub struct Consumer {
tag: String,
no_local: bool,
no_ack: bool,
exclusive: bool,
nowait: bool,
subscriber: Box<ConsumerSubscriber>,
pub current_message: Option<bool>,
}
impl Consumer {
pub fn new(
tag: String,
no_local: bool,
no_ack: bool,
exclusive: bool,
nowait: bool,
subscriber: Box<ConsumerSubscriber>,
) -> Consumer {
Consumer {
tag,
no_local,
no_ack,
exclusive,
nowait,
subscriber,
current_message: None,
}
}
pub fn new_delivery_complete(&mut self) {
if let Some(delivery) = self.current_message.take() {
self.subscriber.new_delivery(delivery);
}
}
}
pub trait ConsumerSubscriber: Debug + Send + Sync {
fn new_delivery(&mut self, delivery: bool);
}
#[derive(Clone)]
pub struct Sender {
connection_id: u32,
}
// Above code is out of my source code and I cannot edit it.
// Below is my own code.
type Cons = Arc<Mutex<HashMap<u64, Sender>>>;
#[derive(Debug)]
struct Subscriber {
messager: Arc<AtomicBool>,
connections: Cons,
}
impl ConsumerSubscriber for Subscriber {
fn new_delivery(&mut self, delivery: bool) {
println!("received correctly: {:?}", delivery)
}
}
fn main() {}
Run Code Online (Sandbox Code Playgroud)
如果我删除#[derive(Debug)]上的属性Subscriber,它会抱怨Subscriber. 我无法删除它,也无法使用它进行编译。
如何处理此错误并将连接传递Cons到此结构?
您可以Debug按照@Shepmaster的建议实施。您可能想要采用更有用的实现,尽管我从上下文中不确定那会是什么。
impl fmt::Debug for Subscriber {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Hi")
}
}
Run Code Online (Sandbox Code Playgroud)