Repository: actix/actix (language: rust)

Issue:
Implement Debug for actix structs
Currently it's not possible to autogenerate debug instances for structs containing `Addr` as `Debug` is not implemented for it. For other use-cases it would probably be helpful to implement for other structures as well.


Discussion / hints:
Good idea. Would you provide PR?
I'll try.

Fix patch:
diff --git a/src/address/channel.rs b/src/address/channel.rs
index 3a74a2073..858cceab4 100644
--- a/src/address/channel.rs
+++ b/src/address/channel.rs
@@ -3,6 +3,7 @@ use std::hash::{Hash, Hasher};
 use std::sync::atomic::Ordering::{Relaxed, SeqCst};
 use std::sync::atomic::{AtomicBool, AtomicUsize};
 use std::sync::Arc;
+use std::fmt;
 use std::{thread, usize};
 
 use futures::sync::oneshot::{channel as sync_channel, Receiver};
@@ -51,6 +52,15 @@ pub struct AddressSender<A: Actor> {
     maybe_parked: Arc<AtomicBool>,
 }
 
+impl<A: Actor> fmt::Debug for AddressSender<A> {
+    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+        fmt.debug_struct("AddressSender")
+            .field("sender_task", &self.sender_task)
+            .field("maybe_parked", &self.maybe_parked)
+            .finish()
+    }
+}
+
 trait AssertKinds: Send + Sync + Clone {}
 
 /// The receiving end of a channel which implements the `Stream` trait.
diff --git a/src/address/mod.rs b/src/address/mod.rs
index 2c3d821ce..72cd4d33c 100644
--- a/src/address/mod.rs
+++ b/src/address/mod.rs
@@ -67,6 +67,7 @@ impl fmt::Debug for MailboxError {
 }
 
 /// Address of the actor
+#[derive(Debug)]
 pub struct Addr<A: Actor> {
     tx: AddressSender<A>,
 }
diff --git a/src/arbiter.rs b/src/arbiter.rs
index 4a26f1cfb..93e03ba9d 100644
--- a/src/arbiter.rs
+++ b/src/arbiter.rs
@@ -36,6 +36,7 @@ thread_local!(
 /// unless the panic is in the System actor. Users of Arbiter can opt into
 /// shutting down the system on panic by using `Arbiter::builder()` and enabling
 /// `stop_system_on_panic`.
+#[derive(Debug)]
 pub struct Arbiter {
     stop: Option<Sender<i32>>,
     stop_system_on_panic: bool,
diff --git a/src/context.rs b/src/context.rs
index 57de7b1bb..dff7983a8 100644
--- a/src/context.rs
+++ b/src/context.rs
@@ -2,6 +2,7 @@ use actor::{Actor, ActorContext, ActorState, AsyncContext, SpawnHandle};
 use address::{Addr, AddressReceiver};
 use arbiter::Arbiter;
 use fut::ActorFuture;
+use std::fmt;
 
 use contextimpl::{AsyncContextParts, ContextFut, ContextParts};
 use mailbox::Mailbox;
@@ -15,6 +16,15 @@ where
     mb: Option<Mailbox<A>>,
 }
 
+impl<A: Actor<Context = Context<A>>> fmt::Debug for Context<A> {
+    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+        fmt.debug_struct("Context")
+            .field("parts", &self.parts)
+            .field("mb", &self.mb)
+            .finish()
+    }
+}
+
 impl<A> ActorContext for Context<A>
 where
     A: Actor<Context = Self>,
diff --git a/src/contextimpl.rs b/src/contextimpl.rs
index 2b6a2fd71..f13432806 100644
--- a/src/contextimpl.rs
+++ b/src/contextimpl.rs
@@ -1,5 +1,6 @@
 use futures::{Async, Future, Poll};
 use smallvec::SmallVec;
+use std::fmt;
 
 use actor::{
     Actor, ActorContext, ActorState, AsyncContext, Running, SpawnHandle, Supervised,
@@ -44,6 +45,18 @@ where
     handles: SmallVec<[SpawnHandle; 2]>,
 }
 
+impl<A> fmt::Debug for ContextParts<A>
+where
+    A: Actor,
+    A::Context: AsyncContext<A>,
+{
+    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+        fmt.debug_struct("ContextParts")
+            .field("flags", &self.flags)
+            .finish()
+    }
+}
+
 impl<A> ContextParts<A>
 where
     A: Actor,
@@ -185,6 +198,16 @@ where
     items: SmallVec<[Item<A>; 3]>,
 }
 
+impl<A, C> fmt::Debug for ContextFut<A, C>
+where
+    C: AsyncContextParts<A>,
+    A: Actor<Context = C>,
+{
+    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+        write!(fmt, "ContextFut {{ /* omitted */ }}")
+    }
+}
+
 impl<A, C> ContextFut<A, C>
 where
     C: AsyncContextParts<A>,
diff --git a/src/handler.rs b/src/handler.rs
index b72012903..61fd5d9af 100644
--- a/src/handler.rs
+++ b/src/handler.rs
@@ -1,5 +1,6 @@
 use futures::sync::oneshot::Sender as SyncSender;
 use futures::Future;
+use std::fmt;
 
 use actor::{Actor, AsyncContext};
 use address::Addr;
@@ -166,6 +167,16 @@ pub struct Response<I, E> {
     item: ResponseTypeItem<I, E>,
 }
 
+impl<I, E> fmt::Debug for Response<I, E> {
+    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+        let mut fmt = fmt.debug_struct("Response");
+        match self.item {
+                ResponseTypeItem::Result(_) => fmt.field("item", &"Result(_)".to_string()),
+                ResponseTypeItem::Fut(_) => fmt.field("item", &"Fut(_)".to_string()),
+        }.finish()
+    }
+}
+
 impl<I, E> Response<I, E> {
     /// Create async response
     pub fn async<T>(fut: T) -> Self
@@ -220,6 +231,16 @@ pub struct ActorResponse<A, I, E> {
     item: ActorResponseTypeItem<A, I, E>,
 }
 
+impl<A, I, E> fmt::Debug for ActorResponse<A, I, E> {
+    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+        let mut fmt = fmt.debug_struct("ActorResponse");
+        match self.item {
+                ActorResponseTypeItem::Result(_) => fmt.field("item", &"Result(_)".to_string()),
+                ActorResponseTypeItem::Fut(_) => fmt.field("item", &"Fut(_)".to_string()),
+        }.finish()
+    }
+}
+
 impl<A: Actor, I, E> ActorResponse<A, I, E> {
     /// Create response
     pub fn reply(val: Result<I, E>) -> Self {
diff --git a/src/mailbox.rs b/src/mailbox.rs
index 57b92c9a6..8a998f108 100644
--- a/src/mailbox.rs
+++ b/src/mailbox.rs
@@ -1,4 +1,5 @@
 use futures::{Async, Stream};
+use std::fmt;
 
 use actor::{Actor, AsyncContext};
 use address::EnvelopeProxy;
@@ -18,6 +19,17 @@ where
     msgs: AddressReceiver<A>,
 }
 
+impl<A> fmt::Debug for Mailbox<A> where
+    A: Actor,
+    A::Context: AsyncContext<A>,
+{
+    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+        fmt.debug_struct("Mailbox")
+            .field("capacity", &self.capacity())
+            .finish()
+    }
+}
+
 impl<A> Default for Mailbox<A>
 where
     A: Actor,
diff --git a/src/registry.rs b/src/registry.rs
index 02dcded87..16cdcfeab 100644
--- a/src/registry.rs
+++ b/src/registry.rs
@@ -222,6 +222,7 @@ impl Registry {
 ///     });
 /// }
 /// ```
+#[derive(Debug)]
 pub struct SystemRegistry {
     system: Addr<Arbiter>,
     registry: InnerRegistry,
diff --git a/src/supervisor.rs b/src/supervisor.rs
index 6fac8d500..402ba3f83 100644
--- a/src/supervisor.rs
+++ b/src/supervisor.rs
@@ -61,6 +61,7 @@ use msgs::Execute;
 ///     });
 /// }
 /// ```
+#[derive(Debug)]
 pub struct Supervisor<A>
 where
     A: Supervised + Actor<Context = Context<A>>,
diff --git a/src/system.rs b/src/system.rs
index 33128652d..41eb5199f 100644
--- a/src/system.rs
+++ b/src/system.rs
@@ -58,7 +58,7 @@ use registry::SystemRegistry;
 ///     std::process::exit(code);
 /// }
 /// ```
-#[derive(Clone)]
+#[derive(Clone, Debug)]
 pub struct System {
     sys: Addr<SystemArbiter>,
     arbiter: Addr<Arbiter>,
@@ -154,6 +154,7 @@ impl System {
 
 /// Helper object that runs System's event loop
 #[must_use = "SystemRunner must be run"]
+#[derive(Debug)]
 pub struct SystemRunner {
     rt: Runtime,
     stop: Receiver<i32>,
@@ -196,6 +197,7 @@ impl SystemRunner {
     }
 }
 
+#[derive(Debug)]
 pub(crate) struct SystemArbiter {
     stop: Option<Sender<i32>>,
     arbiters: HashMap<String, Addr<Arbiter>>,


Test patch: