pin transport once, use project in write_message (#28)

This commit is contained in:
Marc-Andre Giroux 2025-03-18 00:52:10 -04:00 • committed by GitHub
commit 042299f251
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -105,13 +105,17 @@ where
R: AsyncRead + Unpin, R: AsyncRead + Unpin,
W: AsyncWrite + Unpin, W: AsyncWrite + Unpin,
{ {
pub async fn write_message(&mut self, msg: JsonRpcMessage) -> Result<(), std::io::Error> { pub async fn write_message(
self: &mut Pin<&mut Self>,
msg: JsonRpcMessage,
) -> Result<(), std::io::Error> {
let json = serde_json::to_string(&msg)?; let json = serde_json::to_string(&msg)?;
Pin::new(&mut self.writer)
.write_all(json.as_bytes()) let mut this = self.as_mut().project();
.await?; this.writer.write_all(json.as_bytes()).await?;
Pin::new(&mut self.writer).write_all(b"\n").await?; this.writer.write_all(b"\n").await?;
Pin::new(&mut self.writer).flush().await?; this.writer.flush().await?;
Ok(()) Ok(())
} }
} }
@ -139,6 +143,7 @@ where
{ {
use futures::StreamExt; use futures::StreamExt;
let mut service = self.service; let mut service = self.service;
let mut transport = Pin::new(&mut transport);
tracing::info!("Server started"); tracing::info!("Server started");
while let Some(msg_result) = transport.next().await { while let Some(msg_result) = transport.next().await {