What argument to pass and how to find out its type?

I started with an example for EventLoop from the mio web page and added the main function:

extern crate mio; use std::thread; use mio::{EventLoop, Handler}; struct MyHandler; impl Handler for MyHandler { type Timeout = (); type Message = u32; fn notify(&mut self, event_loop: &mut EventLoop<MyHandler>, msg: u32) { assert_eq!(msg, 123); event_loop.shutdown(); } } fn main() { let mut event_loop = EventLoop::new().unwrap(); let sender = event_loop.channel(); // Send the notification from another thread thread::spawn(move || { let _ = sender.send(123); }); let _ = event_loop.run(&mut MyHandler); } 

Then I had the idea to move the send stream to a separate function "foo" and began to wonder what type was passed:

 extern crate mio; use std::thread; use mio::{EventLoop, Handler}; struct MyHandler; impl Handler for MyHandler { type Timeout = (); type Message = u32; fn notify(&mut self, event_loop: &mut EventLoop<MyHandler>, msg: u32) { assert_eq!(msg, 123); event_loop.shutdown(); } } fn foo(s: &?) { let sender = s.clone(); // Send the notification from another thread thread::spawn(move || { let _ = sender.send(123); }); } fn main() { let mut event_loop = EventLoop::new().unwrap(); let sender = event_loop.channel(); foo(&sender); let _ = event_loop.run(&mut MyHandler); } 

So, I will let the compiler tell me the type:

 fn foo(s: &String) { ... 

causes an error:

 error: mismatched types: expected `&collections::string::String`, found `&mio::event_loop::Sender<_>` 

Ok, nice, but replacing &String with &mio::event_loop::Sender<u32> raises an error:

 error: struct `Sender` is private fn foo(s: &mio::event_loop::Sender<u32>) { ^ 

Hm, it looks like a dead end, so I thought to skip event_loop:

 fn foo(s: &mio::event_loop::EventLoop<u32>) { let sender = s.channel().clone(); ... fn main() { ... foo(&event_loop); ... 

but this causes an error:

  error: the trait `mio::handler::Handler` is not implemented for the type `u32` [E0277] src/main.rs:18 fn foo(s: &mio::event_loop::EventLoop<u32>) { 

which bothers me completely.

In particular. C / C ++ I would just pass a pointer to either EventLop or Sender.

What is Rusta trying to tell me here? How to make it work in Rust?

Environment: rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14), mio ​​0.3.5

+5
source share
1 answer

The Sender type is relayed as mio::Sender . The compiler knows what the actual type is mio::event_loop::Sender and reports it. There is currently no way to automatically determine which type you need at all, but you can look at the documentation of the EventLoop::channel method and see that it returns Sender . If you click on the Sender type in the documentation, you will be taken to the mio::Sender documentation

+5
source

All Articles