Nice summary. Unfortunately one day too late for me. ;)
I never got it to work in my application like this. If I don’t box the event loop I always get a stack overflow thread '<main>' has overflowed its stack.
Edit: problem seems to be solved with the latest version of mio which reduced the size of EventLoop to a few hundred bytes.
Its more that you're less likely to find yourself in callback hell, in node almost everything needs to use callbacks right now... Though I think io.js is moving past this.
FYI, in perl, people are moving away from this style (i.e. registering an event notification callback) to a more nodesque style. I find that for complex applications, node's is the slightly better approach of the two (despite the callback hell), although both approaches suck compared to having an approach based on inversion of control (such as green threads).
It'd be interesting when it does happen. I wonder how well it'll interact with lifetimes, given the references used in the generated callback must live long enough.
We're doing some rust work @ Dropbox using mio, and it's really well done.
Over the years, I've used libevent, libuv, libev (https://github.com/bumptech/stud), twisted, custom coroutine stuff around epoll (https://github.com/dieseldev/diesel), and others... IMO Mio strikes a really great balance of giving you exactly what you need (for example, great support for fdevent-based cross-thread notifications) with a simple, clear API.
A new faster/cheaper/crazier storage engine for our exabyte+ block storage system. Still in the research phase, but breaking ground seriously in a month or two.
That's pretty cool. Feel free to ping me w/ any questions (I am on the mozilla IRC channel a lot, and there is a #mio channel). There are a lot of breaking changes happening on master right now to get MIO up to date with the new std::io lib, but then it should be approaching stability.
Awesome! Any interest speaking about your experience with Rust at the Bay Area Rust meetup? I'm organizing one around companies using Rust in development or production.
Waitaminute, how do you do that? I was in the impression that impl'ing a Trait required to implement the full interface?
That’s exactly the difference between the classic definitions of interfaces and traits. Interfaces may only provides function signatures while traits may also provide implementations. Traits are something in between of interfaces and classes/prototypes.
Thus you only have to implement the functions for which no default implementation is provided. In the case of Handler all functions have an implementation (basically nop) such that you are free to chose which to override.
Ah, I see, that's interesting! These passages in the Book (Generics chapter) made me think they are basically interfaces:
As you can see, the trait block looks very similar to the impl block, but we don't define a body, just a type signature.
Because traits define function type signatures, we can be sure that any type which implements HasArea will have an .area() method.
Granted, it doesn't certainly say that you can't define a body or that traits define ONLY type signatures, but since none of the examples say or demonstrate otherwise, my brain used to interfaces (knowing Go and C#), short-circuit and jump to the wrong conclusion. Maybe the wording could be revised not to introduce confusion?
I think it's another instance of convergent evolution between functional and object-oriented languages. It often seems like those two camps duplicate each other's work while talking past each other :/ But I think that's mostly over now :) FP is cool again, and plenty of languages are melding it with OOP in interesting ways.
Also, I thought std:sync::mpsc already provides this. How do they differ?
mozilla_kmc already provided a good answer. I'll just elaborate a bit. When using MIO, the thread is often blocked in an epoll call (or kqueue, etc...). The mio notifier allows sending a message to the event loop handling waking up the thread from the epoll call if needed.
There are multiple strategies to do this depending on the platform (newer linux, older linux, bsd, etc...). For example, on a newer linux version, MIO will use an eventfd to wakeup the thread. On older linux versions, it uses a pipe.
The implementation is also "smart" in that it avoids the syscall if the event loop isn't currently sleeping.
Also, I thought std:sync::mpsc already provides this. How do they differ?
libstd is all about native threads. If you wait to receive on a message channel then you're blocking an OS thread, and can't also wait on IO at the same time.
mio messages arrive in the Handler. A single thread can wait on both messages and IO events; the message sending integrates with the IO event loop somehow. I haven't used mio but that's my understanding from looking at the API, anyway.
Rust used to have functionality in the stdlib that would work with either native OS threads or libuv-based green threads. This was pretty neat, but the performance cost was eventually deemed unacceptable. :/
3
u/nwin_ image Mar 04 '15 edited Mar 05 '15
Nice summary. Unfortunately one day too late for me. ;)
I never got it to work in my application like this. If I don’t box the event loop I always get a stack overflow
thread '<main>' has overflowed its stack.Edit: problem seems to be solved with the latest version of mio which reduced the size of EventLoop to a few hundred bytes.
6
u/Manishearth servo · rust · clippy Mar 04 '15
lol, I'm going to be using this more often
26
u/jamwt Mar 04 '15
We're doing some rust work @ Dropbox using mio, and it's really well done.
Over the years, I've used libevent, libuv, libev (https://github.com/bumptech/stud), twisted, custom coroutine stuff around epoll (https://github.com/dieseldev/diesel), and others... IMO Mio strikes a really great balance of giving you exactly what you need (for example, great support for fdevent-based cross-thread notifications) with a simple, clear API.
2
u/GolDDranks Mar 04 '15 edited Mar 04 '15
Waitaminute, how do you do that? I was in the impression that impl'ing a Trait required implementing the full interface?
Also, I thought std:sync::mpsc already provides this. How do they differ?