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