Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit impl Clone for tx to avoid T: Clone #865

Merged
merged 1 commit into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion tokio-sync/src/mpsc/bounded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ use std::fmt;
/// Send values to the associated `Receiver`.
///
/// Instances are created by the [`channel`](fn.channel.html) function.
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct Sender<T> {
chan: chan::Tx<T, Semaphore>,
}

impl<T> Clone for Sender<T> {
fn clone(&self) -> Self {
Sender { chan: self.chan.clone() }
}
}

/// Receive values from the associated `Sender`.
///
/// Instances are created by the [`channel`](fn.channel.html) function.
Expand Down
8 changes: 7 additions & 1 deletion tokio-sync/src/mpsc/unbounded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ use std::fmt;
///
/// Instances are created by the
/// [`unbounded_channel`](fn.unbounded_channel.html) function.
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct UnboundedSender<T> {
chan: chan::Tx<T, Semaphore>,
}

impl<T> Clone for UnboundedSender<T> {
fn clone(&self) -> Self {
UnboundedSender { chan: self.chan.clone() }
}
}

/// Receive values from the associated `UnboundedSender`.
///
/// Instances are created by the
Expand Down
30 changes: 30 additions & 0 deletions tokio-sync/tests/mpsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,36 @@ fn send_recv_unbounded() {
assert!(val.is_none());
}

#[test]
fn clone_sender_no_t_clone_buffer() {
#[derive(Debug, PartialEq, Eq)]
struct NotClone;
let (mut tx, mut rx) = mpsc::channel(100);
tx.try_send(NotClone).unwrap();
tx.clone().try_send(NotClone).unwrap();

let val = assert_ready!(rx.poll());
assert_eq!(val, Some(NotClone));

let val = assert_ready!(rx.poll());
assert_eq!(val, Some(NotClone));
}

#[test]
fn clone_sender_no_t_clone_unbounded() {
#[derive(Debug, PartialEq, Eq)]
struct NotClone;
let (mut tx, mut rx) = mpsc::unbounded_channel();
tx.try_send(NotClone).unwrap();
tx.clone().try_send(NotClone).unwrap();

let val = assert_ready!(rx.poll());
assert_eq!(val, Some(NotClone));

let val = assert_ready!(rx.poll());
assert_eq!(val, Some(NotClone));
}

#[test]
fn send_recv_buffer_limited() {
let (mut tx, mut rx) = mpsc::channel::<i32>(1);
Expand Down