Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ws::Sender doesn't implement std::fmt::Debug [duplicate]

Tags:

rust

I have a connection type Cons and a Subscriber implementation for my Redis server. ws is a websocket library. There's no chance to edit source code either:

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

I get this error:

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`

If I remove the #[derive(Debug)] attribute on Subscriber, it complains about Subscriber. I cannot remove it, and I cannot compile with it.

How can I handle this error and pass connections Cons to this struct?

like image 400
Dennis Avatar asked Jul 31 '18 13:07

Dennis


1 Answers

You can implement Debug as @Shepmaster suggested. You may want to go with a more useful implementation though I'm not sure from context what that would be.

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

playground

like image 117
user25064 Avatar answered Nov 15 '22 08:11

user25064