rust-analyzer/crates/gen_lsp_server/src/stdio.rs
Jeremy A. Kolb 61f3a438d3 Cargo Format
Run `cargo fmt` and ignore generated files
2018-10-16 09:41:10 -04:00

47 lines
1.3 KiB
Rust

use std::{
io::{stdin, stdout},
thread,
};
use crossbeam_channel::{bounded, Receiver, Sender};
use {RawMessage, Result};
pub fn stdio_transport() -> (Receiver<RawMessage>, Sender<RawMessage>, Threads) {
let (writer_sender, mut writer_receiver) = bounded::<RawMessage>(16);
let writer = thread::spawn(move || {
let stdout = stdout();
let mut stdout = stdout.lock();
writer_receiver.try_for_each(|it| it.write(&mut stdout))?;
Ok(())
});
let (reader_sender, reader_receiver) = bounded::<RawMessage>(16);
let reader = thread::spawn(move || {
let stdin = stdin();
let mut stdin = stdin.lock();
while let Some(msg) = RawMessage::read(&mut stdin)? {
reader_sender.send(msg);
}
Ok(())
});
let threads = Threads { reader, writer };
(reader_receiver, writer_sender, threads)
}
pub struct Threads {
reader: thread::JoinHandle<Result<()>>,
writer: thread::JoinHandle<Result<()>>,
}
impl Threads {
pub fn join(self) -> Result<()> {
match self.reader.join() {
Ok(r) => r?,
Err(_) => bail!("reader panicked"),
}
match self.writer.join() {
Ok(r) => r,
Err(_) => bail!("writer panicked"),
}
}
}