1use std::{
19 io,
20 sync::{Arc, Mutex},
21};
22
23#[derive(Debug)]
24pub(crate) struct TNoopChannel;
25
26impl io::Read for TNoopChannel {
27 fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
28 Ok(0)
29 }
30}
31
32#[derive(Debug, Clone)]
33pub(crate) struct TBufferChannel {
34 inner: Arc<Mutex<Vec<u8>>>,
35}
36
37impl TBufferChannel {
38 pub fn with_capacity(capacity: usize) -> Self {
39 TBufferChannel {
40 inner: Arc::new(Mutex::new(Vec::with_capacity(capacity))),
41 }
42 }
43
44 pub fn take_bytes(&mut self) -> Vec<u8> {
45 self.inner
46 .lock()
47 .map(|mut write| write.split_off(0))
48 .unwrap_or_default()
49 }
50}
51
52impl io::Read for TBufferChannel {
53 fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
54 unreachable!("jaeger protocol never reads")
55 }
56}
57
58impl io::Write for TBufferChannel {
59 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
60 if let Ok(mut inner) = self.inner.lock() {
61 inner.extend_from_slice(buf);
62 }
63 Ok(buf.len())
64 }
65
66 fn flush(&mut self) -> io::Result<()> {
67 Ok(())
68 }
69}
70
71impl thrift::transport::TIoChannel for TBufferChannel {
72 fn split(
73 self,
74 ) -> thrift::Result<(
75 thrift::transport::ReadHalf<Self>,
76 thrift::transport::WriteHalf<Self>,
77 )>
78 where
79 Self: Sized,
80 {
81 Ok((
82 thrift::transport::ReadHalf::new(self.clone()),
83 thrift::transport::WriteHalf::new(self),
84 ))
85 }
86}