rust - ws::Sender 没有实现 std::fmt::Debug
问题描述
我的 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() {}
我收到此错误:
error[E0277]: `Sender` doesn't implement `std::fmt::Debug`
--> src/main.rs:58:5
|
58 | connections: Cons,
| ^^^^^^^^^^^^^^^^^ `Sender` cannot be formatted using `{:?}`
|
= help: the trait `std::fmt::Debug` is not implemented for `Sender`
= note: add `#[derive(Debug)]` or manually implement `std::fmt::Debug`
= note: required because of the requirements on the impl of `std::fmt::Debug` for `std::collections::HashMap<u64, Sender>`
= note: required because of the requirements on the impl of `std::fmt::Debug` for `std::sync::Mutex<std::collections::HashMap<u64, Sender>>`
= note: required because of the requirements on the impl of `std::fmt::Debug` for `std::sync::Arc<std::sync::Mutex<std::collections::HashMap<u64, Sender>>>`
= note: required because of the requirements on the impl of `std::fmt::Debug` for `&std::sync::Arc<std::sync::Mutex<std::collections::HashMap<u64, Sender>>>`
= note: required for the cast to the object type `std::fmt::Debug`
如果我删除 上的#[derive(Debug)]
属性Subscriber
,它会抱怨Subscriber
. 我无法删除它,也无法使用它进行编译。
我该如何处理这个错误并将连接传递Cons
给这个结构?
解决方案
您可以Debug
按照@Shepmaster 的建议实施。您可能想要使用更有用的实现,尽管我不确定从上下文中会是什么。
impl fmt::Debug for Subscriber {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Hi")
}
}
推荐阅读
- android - 如何在颤动中延迟 Visibilty 动画?比如等待2秒然后开始动画
- reactjs - 错误:无效的挂钩调用。钩子只能在函数组件的主体内部调用。这可能由于以下原因之一而发生:
- python - sklearn.feature_selection chi2 为不同的标签识别相同的一元和二元
- python - 二维坐标数组的元素叉积
- sql - 加载数据或更新列 Oracle-sql
- javascript - 如何在不允许使用 Yup 进行类型强制的同时允许未定义字段?
- shell - scp 到远程服务器;ssh:连接到主机 server@address.it 端口 22:连接超时
- authorization - MindSphere SDK V2 for Java - 授权失败
- php - 在 POST 数组 php 中没有获得提交按钮?
- javascript - TypeScript 映射对象的键和值