These are chat archives for rust-lang/rust

28th
Apr 2017
Martell Malone
@martell
Apr 28 2017 15:02
Hey I'm looking to find out what the default type rx for channel is in let (tx, rx) = channel();
from std::sync::mpsc::channel
It is defined as Receiver<T> but what is rx when you don't define what the generic like above?
Looking at the std lib here nothing references T apart from the signature
#[stable(feature = "rust1", since = "1.0.0")]
pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
    let a = Arc::new(UnsafeCell::new(oneshot::Packet::new()));
    (Sender::new(Flavor::Oneshot(a.clone())), Receiver::new(Flavor::Oneshot(a)))
}
Martell Malone
@martell
Apr 28 2017 15:11
I'm trying to store the receiver in a struct like this rx: Receiver<_>
but '_' is not allowed in type signatures
So I'm trying to find the correct way to define the type
Sergey Noskov
@Albibek
Apr 28 2017 15:11
@martell the only requirement is that they have to be same. Sender::newis supposed to work on Sender<T> so the generic is hidden there.
you can specify type in the channel call, for example channel::<String>, but it's usually inferred later - when you start using the channel sending/receiving something of a particular type
Martell Malone
@martell
Apr 28 2017 15:15
Okay that makes a lot of sense, thanks for taking the time to reply.
Is there a way to store it without defining it so that it can infer it later where I don't need to know what it is?
Sergey Noskov
@Albibek
Apr 28 2017 15:17
At some stage you'll need the concrete type anyways, for code generation to work
In most cases compiler will infer it automatically, but sometimes it needs help
Martell Malone
@martell
Apr 28 2017 15:19
I think I got what it was supposed to be based on your help. :)
let (tx, rx) = channel();
let mut watcher: RecommendedWatcher = try!(Watcher::new(tx, Duration::from_secs(2)));
the Watcher::new expected argument defines what tx<T> it is in this case and in turn makes rx the same :shipit: