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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use super::*;
use std::{
borrow::Borrow,
ops,
sync,
rc,
};
pub trait TwoBufferProvider<T: ?Sized>
{
type ControlWrapper: Borrow<T>;
fn as_wrapper(&self) -> &Self::ControlWrapper;
fn from_wrapper_boxed(r: &Self::ControlWrapper) -> Box<Self>;
#[inline(always)]
fn from_wrapper(r: &Self::ControlWrapper) -> Self
where Self: Sized {
*Self::from_wrapper_boxed(r)
}
#[inline(always)]
fn inner(&self) -> &T
{
self.as_wrapper().borrow()
}
fn from_boxed(value: Box<T>) -> Box<Self>;
#[inline(always)]
fn from_value(value: T) -> Self
where T: Sized,
Self: Sized
{
*Self::from_boxed(Box::new(value))
}
}
#[derive(Debug)]
pub struct Shared<T: ?Sized>(sync::Arc<T>);
#[derive(Debug)]
pub struct Private<T: ?Sized>(rc::Rc<T>);
impl<T: ?Sized> TwoBufferProvider<T> for Shared<T> {
type ControlWrapper = sync::Arc<T>;
#[inline(always)]
fn as_wrapper(&self) -> &Self::ControlWrapper {
&self.0
}
#[inline]
fn from_boxed(value: Box<T>) ->Box<Self> {
Box::new(Self(From::from(value)))
}
#[inline(always)]
fn from_value(value: T) -> Self
where T: Sized,
Self: Sized {
Self(sync::Arc::new(value))
}
#[inline]
fn from_wrapper_boxed(r: &Self::ControlWrapper) -> Box<Self> {
Box::new(Self(r.clone()))
}
#[inline(always)]
fn from_wrapper(r: &Self::ControlWrapper) -> Self
where Self: Sized {
Self(r.clone())
}
}
impl<T: ?Sized + AsRawFd> AsRawFd for Shared<T>
{
#[inline(always)]
fn as_raw_fd(&self) -> RawFd {
self.as_wrapper().as_raw_fd()
}
}
impl<T: ?Sized> TwoBufferProvider<T> for Private<T> {
type ControlWrapper = rc::Rc<T>;
#[inline(always)]
fn as_wrapper(&self) -> &Self::ControlWrapper {
&self.0
}
#[inline]
fn from_boxed(value: Box<T>) ->Box<Self> {
Box::new(Self(From::from(value)))
}
#[inline(always)]
fn from_value(value: T) -> Self
where T: Sized,
Self: Sized {
Self(rc::Rc::new(value))
}
#[inline]
fn from_wrapper_boxed(r: &Self::ControlWrapper) -> Box<Self> {
Box::new(Self(r.clone()))
}
#[inline(always)]
fn from_wrapper(r: &Self::ControlWrapper) -> Self
where Self: Sized {
Self(r.clone())
}
}
impl<T: ?Sized + AsRawFd> AsRawFd for Private<T>
{
#[inline(always)]
fn as_raw_fd(&self) -> RawFd {
self.as_wrapper().as_raw_fd()
}
}
impl<T: ?Sized> Shared<T>
{
#[inline]
pub fn is_connected(&self) -> bool
{
sync::Arc::strong_count(&self.0) > 1
}
#[inline]
pub fn into_arc(self) -> sync::Arc<T>
{
self.0
}
#[inline]
pub fn inner(&self) -> &T
{
&self.0
}
}
impl<T: ?Sized> Private<T>
{
#[inline]
pub fn is_connected(&self) -> bool
{
rc::Rc::strong_count(&self.0) > 1
}
#[inline]
pub fn into_rc(self) -> rc::Rc<T>
{
self.0
}
#[inline]
pub fn inner(&self) -> &T
{
&self.0
}
}
pub trait BufferExt<T>
{
fn detach(txrx: Self) -> (MappedFile<T>, MappedFile<T>);
}
impl<B, T> BufferExt<T> for (MappedFile<B>, MappedFile<B>)
where B: TwoBufferProvider<T> + AsRawFd,
T: FromRawFd,
{
#[inline]
fn detach((itx, irx): Self) -> (MappedFile<T>, MappedFile<T>) {
#[cold]
#[inline(never)]
fn _panic_bad_dup(fd: RawFd) -> !
{
panic!("Failed to dup({fd}): {}", io::Error::last_os_error())
}
let tx = itx.file.as_raw_fd();
let rx = irx.file.as_raw_fd();
let (f0, f1) = unsafe {
let fd1 = libc::dup(tx);
if fd1 < 0 {
_panic_bad_dup(tx);
}
let fd2 = libc::dup(rx);
if fd2 < 0 {
_panic_bad_dup(rx);
}
(T::from_raw_fd(fd1), T::from_raw_fd(fd2))
};
(MappedFile {
map: itx.map,
file: f0,
}, MappedFile {
map: irx.map,
file: f1
})
}
}