diff options
Diffstat (limited to 'src/event_loop.rs')
-rw-r--r-- | src/event_loop.rs | 72 |
1 files changed, 33 insertions, 39 deletions
diff --git a/src/event_loop.rs b/src/event_loop.rs index 31e803de..4941b479 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -1,10 +1,10 @@ //! The main event loop which performs I/O on the pseudoterminal use std::borrow::Cow; use std::collections::VecDeque; -use std::io::{self, ErrorKind, Read, Write}; use std::fs::File; -use std::sync::Arc; +use std::io::{self, ErrorKind, Read, Write}; use std::marker::Send; +use std::sync::Arc; use mio::{self, Events, PollOpt, Ready}; use mio_extras::channel::{self, Receiver, Sender}; @@ -15,10 +15,10 @@ use mio::unix::UnixReady; use crate::ansi; use crate::display; use crate::event; -use crate::tty; +use crate::sync::FairMutex; use crate::term::Term; +use crate::tty; use crate::util::thread; -use crate::sync::FairMutex; /// Messages that may be sent to the `EventLoop` #[derive(Debug)] @@ -58,14 +58,14 @@ enum DrainResult { /// Nothing was available to receive Empty, /// A shutdown message was received - Shutdown + Shutdown, } impl DrainResult { pub fn is_shutdown(&self) -> bool { match *self { DrainResult::Shutdown => true, - _ => false + _ => false, } } } @@ -84,12 +84,13 @@ pub struct Notifier(pub Sender<Msg>); impl event::Notify for Notifier { fn notify<B>(&mut self, bytes: B) - where B: Into<Cow<'static, [u8]>>, + where + B: Into<Cow<'static, [u8]>>, { let bytes = bytes.into(); // terminal hangs if we send 0 bytes through. if bytes.len() == 0 { - return + return; } if self.0.send(Msg::Input(bytes)).is_err() { panic!("expected send event loop msg"); @@ -99,11 +100,7 @@ impl event::Notify for Notifier { impl Default for State { fn default() -> State { - State { - write_list: VecDeque::new(), - parser: ansi::Processor::new(), - writing: None, - } + State { write_list: VecDeque::new(), parser: ansi::Processor::new(), writing: None } } } @@ -117,9 +114,7 @@ impl State { #[inline] fn goto_next(&mut self) { - self.writing = self.write_list - .pop_front() - .map(Writing::new); + self.writing = self.write_list.pop_front().map(Writing::new); } #[inline] @@ -161,8 +156,8 @@ impl Writing { } impl<T> EventLoop<T> - where - T: tty::EventedPty + Send + 'static, +where + T: tty::EventedPty + Send + 'static, { /// Create a new event loop pub fn new( @@ -198,10 +193,10 @@ impl<T> EventLoop<T> match msg { Msg::Input(input) => { state.write_list.push_back(input); - } + }, Msg::Shutdown => { return DrainResult::Shutdown; - } + }, } } @@ -233,8 +228,8 @@ impl<T> EventLoop<T> buf: &mut [u8], mut writer: Option<&mut X>, ) -> io::Result<()> - where - X: Write, + where + X: Write, { const MAX_READ: usize = 0x1_0000; let mut processed = 0; @@ -271,20 +266,18 @@ impl<T> EventLoop<T> // Run the parser for byte in &buf[..got] { - state - .parser - .advance(&mut **terminal, *byte, &mut self.pty.writer()); + state.parser.advance(&mut **terminal, *byte, &mut self.pty.writer()); } // Exit if we've processed enough bytes if processed > MAX_READ { break; } - } + }, Err(err) => match err.kind() { ErrorKind::Interrupted | ErrorKind::WouldBlock => { break; - } + }, _ => return Err(err), }, } @@ -311,21 +304,21 @@ impl<T> EventLoop<T> Ok(0) => { state.set_current(Some(current)); break 'write_many; - } + }, Ok(n) => { current.advance(n); if current.finished() { state.goto_next(); break 'write_one; } - } + }, Err(err) => { state.set_current(Some(current)); match err.kind() { ErrorKind::Interrupted | ErrorKind::WouldBlock => break 'write_many, _ => return Err(err), } - } + }, } } } @@ -343,14 +336,10 @@ impl<T> EventLoop<T> let poll_opts = PollOpt::edge() | PollOpt::oneshot(); let channel_token = tokens.next().unwrap(); - self.poll - .register(&self.rx, channel_token, Ready::readable(), poll_opts) - .unwrap(); + self.poll.register(&self.rx, channel_token, Ready::readable(), poll_opts).unwrap(); // Register TTY through EventedRW interface - self.pty - .register(&self.poll, &mut tokens, Ready::readable(), poll_opts) - .unwrap(); + self.pty.register(&self.poll, &mut tokens, Ready::readable(), poll_opts).unwrap(); let mut events = Events::with_capacity(1024); @@ -385,8 +374,12 @@ impl<T> EventLoop<T> } }, - token if token == self.pty.read_token() || token == self.pty.write_token() => { - #[cfg(unix)] { + token + if token == self.pty.read_token() + || token == self.pty.write_token() => + { + #[cfg(unix)] + { if UnixReady::from(event.readiness()).is_hup() { // don't try to do I/O on a dead PTY continue; @@ -395,7 +388,8 @@ impl<T> EventLoop<T> if event.readiness().is_readable() { if let Err(e) = self.pty_read(&mut state, &mut buf, pipe.as_mut()) { - #[cfg(target_os = "linux")] { + #[cfg(target_os = "linux")] + { // On Linux, a `read` on the master side of a PTY can fail // with `EIO` if the client side hangs up. In that case, // just loop back round for the inevitable `Exited` event. |