I started with the example for EventLoop from the mio webpage 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 sending thread to a separate function "foo" and started to wonder what type is 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 let the compiler tell me the type:
fn foo(s: &String) { ...
raises the error:
error: mismatched types:
expected `&collections::string::String`,
found `&mio::event_loop::Sender<_>`
Ok, nice but replacing &String
by &mio::event_loop::Sender<u32>
raises the error:
error: struct `Sender` is private
fn foo(s: &mio::event_loop::Sender<u32>) {
^
Hm, looks like a dead end, so I thought passing event_loop instead:
fn foo(s: &mio::event_loop::EventLoop<u32>) {
let sender = s.channel().clone();
...
fn main() { ...
foo(&event_loop); ...
but that raises the 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 confuses me completely.
In e.g. C / C++ I would have just passed a pointer either to EventLop or Sender.
What is Rust trying to tell me here? How to get it working in Rust?
Environment: rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14), mio 0.3.5
The Sender
type is re-exported as mio::Sender
. The compiler knows that the actual type is mio::event_loop::Sender
and reports that. There's currently no way to automatically figure out what type you need in general, but you can look at the documentation of the EventLoop::channel
method and see that it returns a Sender
. If you click on the Sender
type in the documentation you will end up at the documentation of mio::Sender
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With