tick/
lib.rs

1//! # Tick
2//!
3//! An implementation of Transports, Protocols, and Streams over mio.
4//!
5//! # Example
6//!
7//! ```rust
8//! use tick::{Tick, Protocol, Transfer};
9//!
10//! struct Echo(Transfer);
11//! impl Protocol<Tcp> for Echo {
12//!     fn on_data(&mut self, data: &[u8]) {
13//!         println!("data received: {:?}", data);
14//!         self.0.write(data);
15//!     }
16//! }
17//!
18//! let mut tick = Tick::new(Echo);
19//! tick.accept(listener);
20//! tick.run();
21//! ```
22
23#![cfg_attr(test, deny(warnings))]
24#![cfg_attr(test, deny(missing_docs))]
25
26#[macro_use] extern crate log;
27extern crate mio;
28extern crate slab;
29
30pub use mio::Evented;
31pub use tick::{Tick, Notify};
32pub use protocol::{Protocol, Interest};
33pub use protocol::Factory as ProtocolFactory;
34pub use transfer::Transfer;
35pub use transport::Transport;
36
37mod handler;
38mod protocol;
39mod stream;
40mod tick;
41mod transfer;
42mod transport;
43
44#[derive(Clone, Copy, PartialEq, Debug)]
45enum Action {
46    Register(mio::EventSet),
47    Wait,
48    Remove,
49}
50
51enum Message {
52    Interest(mio::Token, Interest),
53    Timeout(Box<FnMut() + Send + 'static>, u64),
54    Shutdown,
55}
56
57#[derive(Debug)]
58pub enum Error {
59    TooManySockets,
60    Timeout,
61    Io(::std::io::Error)
62}
63
64impl From<::std::io::Error> for Error {
65    fn from(e: ::std::io::Error) -> Error {
66        Error::Io(e)
67    }
68}
69
70pub type Result<T> = std::result::Result<T, Error>;
71
72/// Opaque ID returned when adding listeners and streams to the loop.
73#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
74pub struct Id(::mio::Token);
75
76impl ::std::fmt::Debug for Id {
77    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
78        f.debug_tuple("Id")
79            .field(&(self.0).0)
80            .finish()
81    }
82}
83
84
85impl slab::Index for Id {
86    fn from_usize(i: usize) -> Id {
87        Id(::mio::Token(i))
88    }
89
90    fn as_usize(&self) -> usize {
91        (self.0).0
92    }
93}
94
95pub type Slab<T> = slab::Slab<T, Id>;