rust-analyzer/crates/gen_lsp_server/src/stdio.rs

53 lines
1.4 KiB
Rust
Raw Normal View History

2018-09-01 13:18:02 +00:00
use std::{
io::{stdin, stdout},
2018-09-01 13:18:02 +00:00
thread,
};
use crossbeam_channel::{bounded, Receiver, Sender};
2018-12-06 18:16:37 +00:00
use failure::bail;
2018-09-01 13:18:02 +00:00
2018-12-06 18:16:37 +00:00
use crate::{RawMessage, Result};
2018-09-01 13:18:02 +00:00
pub fn stdio_transport() -> (Receiver<RawMessage>, Sender<RawMessage>, Threads) {
let (writer_sender, writer_receiver) = bounded::<RawMessage>(16);
2018-09-01 13:18:02 +00:00
let writer = thread::spawn(move || {
let stdout = stdout();
let mut stdout = stdout.lock();
writer_receiver
.into_iter()
.try_for_each(|it| it.write(&mut stdout))?;
2018-09-01 13:18:02 +00:00
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)? {
if let Err(_) = reader_sender.send(msg) {
break;
}
2018-09-01 13:18:02 +00:00
}
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"),
}
}
}