Skip to content
This repository was archived by the owner on Jun 8, 2021. It is now read-only.

Commit c78caaf

Browse files
authored
Merge pull request #495 from sdroege/clone-sender
Manually implement Clone for Sender/SyncSender
2 parents 34b8df4 + ea4c011 commit c78caaf

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

src/main_context_channel.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use get_thread_id;
66
use glib_sys;
77
use std::cell::RefCell;
88
use std::collections::VecDeque;
9+
use std::fmt;
910
use std::mem;
1011
use std::ptr;
1112
use std::sync::mpsc;
@@ -17,7 +18,6 @@ use Priority;
1718
use Source;
1819
use SourceId;
1920

20-
#[derive(Debug)]
2121
enum ChannelSourceState {
2222
NotAttached,
2323
Attached(*mut glib_sys::GSource),
@@ -27,7 +27,6 @@ enum ChannelSourceState {
2727
unsafe impl Send for ChannelSourceState {}
2828
unsafe impl Sync for ChannelSourceState {}
2929

30-
#[derive(Debug)]
3130
struct ChannelInner<T> {
3231
queue: VecDeque<T>,
3332
source: ChannelSourceState,
@@ -71,13 +70,11 @@ impl<T> ChannelInner<T> {
7170
}
7271
}
7372

74-
#[derive(Debug)]
7573
struct ChannelBound {
7674
bound: usize,
7775
cond: Condvar,
7876
}
7977

80-
#[derive(Debug)]
8178
struct Channel<T>(Arc<(Mutex<ChannelInner<T>>, Option<ChannelBound>)>);
8279

8380
impl<T> Clone for Channel<T> {
@@ -321,9 +318,20 @@ unsafe extern "C" fn finalize<T, F: FnMut(T) -> Continue + 'static>(
321318
/// See [`MainContext::channel()`] for how to create such a `Sender`.
322319
///
323320
/// [`MainContext::channel()`]: struct.MainContext.html#method.channel
324-
#[derive(Clone, Debug)]
325321
pub struct Sender<T>(Option<Channel<T>>);
326322

323+
impl<T> fmt::Debug for Sender<T> {
324+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
325+
f.debug_struct("Sender").finish()
326+
}
327+
}
328+
329+
impl<T> Clone for Sender<T> {
330+
fn clone(&self) -> Sender<T> {
331+
Sender(self.0.clone())
332+
}
333+
}
334+
327335
impl<T> Sender<T> {
328336
/// Sends a value to the channel.
329337
pub fn send(&self, t: T) -> Result<(), mpsc::SendError<T>> {
@@ -363,9 +371,20 @@ impl<T> Drop for Sender<T> {
363371
/// See [`MainContext::sync_channel()`] for how to create such a `SyncSender`.
364372
///
365373
/// [`MainContext::sync_channel()`]: struct.MainContext.html#method.sync_channel
366-
#[derive(Clone, Debug)]
367374
pub struct SyncSender<T>(Option<Channel<T>>);
368375

376+
impl<T> fmt::Debug for SyncSender<T> {
377+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
378+
f.debug_struct("SyncSender").finish()
379+
}
380+
}
381+
382+
impl<T> Clone for SyncSender<T> {
383+
fn clone(&self) -> SyncSender<T> {
384+
SyncSender(self.0.clone())
385+
}
386+
}
387+
369388
impl<T> SyncSender<T> {
370389
/// Sends a value to the channel and blocks if the channel is full.
371390
pub fn send(&self, t: T) -> Result<(), mpsc::SendError<T>> {
@@ -411,9 +430,14 @@ impl<T> Drop for SyncSender<T> {
411430
///
412431
/// [`MainContext::channel()`]: struct.MainContext.html#method.channel
413432
/// [`MainContext::sync_channel()`]: struct.MainContext.html#method.sync_channel
414-
#[derive(Debug)]
415433
pub struct Receiver<T>(Option<Channel<T>>, Priority);
416434

435+
impl<T> fmt::Debug for Receiver<T> {
436+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
437+
f.debug_struct("Receiver").finish()
438+
}
439+
}
440+
417441
// It's safe to send the Receiver to other threads for attaching it as
418442
// long as the items to be sent can also be sent between threads.
419443
unsafe impl<T: Send> Send for Receiver<T> {}

src/variant.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,11 @@ unsafe impl Sync for Variant { }
138138

139139
impl fmt::Debug for Variant {
140140
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
141-
f.write_fmt(format_args!("Variant {{ ptr: {:?}, type: \"{}\", value: {} }}",
142-
self.to_glib_none().0, self.type_(), self))
141+
f.debug_struct("Variant")
142+
.field("ptr", &self.to_glib_none().0)
143+
.field("type", &self.type_())
144+
.field("value", &self.to_string())
145+
.finish()
143146
}
144147
}
145148

0 commit comments

Comments
 (0)