rust - ws::Sender 没有实现 std::fmt::Debug

标签 rust

<分区>

我的 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() {}

Playground

我收到这个错误:

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`

如果我删除 Subscriber 上的 #[derive(Debug)] 属性,它会提示 Subscriber。我无法删除它,也无法使用它进行编译。

如何处理此错误并将连接 Cons 传递给此结构?

最佳答案

您可以按照@Shepmaster 的建议实现Debug。您可能希望采用更有用的实现方式,但我不确定从上下文来看那会是什么。

impl fmt::Debug for Subscriber {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Hi")
    }
}

playground

关于rust - ws::Sender 没有实现 std::fmt::Debug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51615131/

相关文章:

rust - 将嵌套结构字段路径作为宏参数传递

winapi - Windows SYSTEMTIME 结构与系统时钟不同吗?

rust - 如何在rust中返回带有引用的结构?

exception - 如何使用 ?并捕获使用rust ?

rust - 如何在 `macro_rules!` 中声明一个变量?

rust - 是否可以有条件地启用 `derive` 之类的属性?

rust - 如何创建返回借用值的函数?

rust - 有没有办法在 futures for_each 流中继续?

rust - 函数返回 serde 反序列化类型时如何修复生命周期错误?

rust - 是否有等同于 C++ 中时钟函数的 Rust?