From 19d2bda0b8725a65ed49262d9a0b80adb0e78798 Mon Sep 17 00:00:00 2001 From: Dmitry Dodzin Date: Thu, 2 Jan 2025 19:17:00 +0200 Subject: [PATCH] use tokio adapter and wrapper instead of writing one --- Cargo.lock | 1 - mirrord/cli/Cargo.toml | 4 +- mirrord/cli/src/container/sidecar.rs | 55 ++++++---------------------- 3 files changed, 13 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f7b58b8954c..2cfeea353a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4119,7 +4119,6 @@ dependencies = [ "mirrord-sip", "mirrord-vpn", "nix 0.29.0", - "pin-project-lite", "prettytable-rs", "rand", "rcgen", diff --git a/mirrord/cli/Cargo.toml b/mirrord/cli/Cargo.toml index d35e712f99b..aea0b77d7cc 100644 --- a/mirrord/cli/Cargo.toml +++ b/mirrord/cli/Cargo.toml @@ -63,11 +63,11 @@ tempfile.workspace = true rcgen.workspace = true rustls-pemfile.workspace = true tokio-rustls.workspace = true -tokio-stream = { workspace = true, features = ["net"] } +tokio-stream = { workspace = true, features = ["io-util", "net"] } regex.workspace = true mid = "3.0.0" rand.workspace = true -pin-project-lite = "0.2" + [target.'cfg(target_os = "macos")'.dependencies] mirrord-sip = { path = "../sip" } diff --git a/mirrord/cli/src/container/sidecar.rs b/mirrord/cli/src/container/sidecar.rs index 663fb3421a6..28641070058 100644 --- a/mirrord/cli/src/container/sidecar.rs +++ b/mirrord/cli/src/container/sidecar.rs @@ -1,19 +1,11 @@ -use std::{ - net::SocketAddr, - ops::Not, - pin::Pin, - process::Stdio, - task::{Context, Poll}, - time::Duration, -}; +use std::{net::SocketAddr, ops::Not, process::Stdio, time::Duration}; use mirrord_config::{internal_proxy::MIRRORD_INTPROXY_CONTAINER_MODE_ENV, LayerConfig}; -use pin_project_lite::pin_project; use tokio::{ - io::{AsyncBufReadExt, BufReader, Lines}, + io::{AsyncBufReadExt, BufReader}, process::{ChildStderr, ChildStdout, Command}, }; -use tokio_stream::Stream; +use tokio_stream::{wrappers::LinesStream, StreamExt}; use tracing::Level; use crate::{ @@ -121,39 +113,14 @@ impl Sidecar { .parse() .map_err(ContainerError::UnableParseProxySocketAddr)?; - Ok((internal_proxy_addr, SidecarLogs { stdout, stderr })) + Ok(( + internal_proxy_addr, + LinesStream::new(stdout).merge(LinesStream::new(stderr)), + )) } } -// The use pin_project is to have a simple wrapper instead of unpining and repining for accessing -// `Pin<&mut Lines>>` and `Pin<&mut Lines>>` as in -// accordance to their underlying `Stream` impl. -pin_project! { - #[derive(Debug)] - pub(crate) struct SidecarLogs { - #[pin] - pub stdout: Lines>, - #[pin] - pub stderr: Lines>, - } -} - -impl Stream for SidecarLogs { - type Item = Result; - - fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - let this = self.project(); - - if let result @ Poll::Ready(Some(_)) = this - .stdout - .poll_next_line(cx) - .map(|result| result.transpose()) - { - return result; - } - - this.stderr - .poll_next_line(cx) - .map(|result| result.transpose()) - } -} +type SidecarLogs = tokio_stream::adapters::Merge< + LinesStream>, + LinesStream>, +>;