Mio run EventLoop in new-function


How can i run the EventLoop in the new-function with a mut reference to the object of MyHandler i just created?

pub struct MyHandler {
    token_counter: usize,
    clients: HashMap<Token, TcpStream>,
    ev_loop: EventLoop<MyHandler>

impl MyHandler {
    pub fn new() -> MyHandler {       
         MyHandler {
            token_counter: 0,
            clients: HashMap::new(),
            ev_loop: EventLoop::new().unwrap()


The way I’ve done this in the past is to hold the event loop outside of the handler. The handler can still have access to it wherever is needed; the loop just needs to be passed as a mutable argument.


Is there a reason you want to do it this way? I believe you are going to get into a bit of a mess with mutable references.

When you implement mio::Handler, the ready() function has & mut EventLoop<MyHandler> as a parameter. This gives you access to the event loop so you can perform necessary operations like register(). You can check out my blog post for an example on how this all fits together.


Yeah, i know that you can do it pretty nice and simple that way.
But is there a way to encapsulate the EventLoop inside of the Handler?
Or is this impossible?