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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
use std::future::Future;
use std::io::{self, IoSlice};
use std::mem::MaybeUninit;
use std::pin::Pin;
use std::process::{ChildStderr, ChildStdin, ChildStdout};
use std::task::{self, Poll};
use heph::actor;
use mio::unix::pipe;
use mio::Interest;
use crate::bytes::{Bytes, BytesVectored, MaybeUninitSlice};
use crate::{self as rt, Bound};
pub fn new<M, RT>(ctx: &mut actor::Context<M, RT>) -> io::Result<(Sender, Receiver)>
where
RT: rt::Access,
{
let (mut sender, mut receiver) = pipe::new()?;
let rt = ctx.runtime();
rt.register(&mut sender, Interest::WRITABLE)?;
rt.register(&mut receiver, Interest::READABLE)?;
Ok((Sender { inner: sender }, Receiver { inner: receiver }))
}
#[derive(Debug)]
pub struct Sender {
inner: pipe::Sender,
}
impl Sender {
pub fn from_child_stdin<M, RT>(
ctx: &mut actor::Context<M, RT>,
stdin: ChildStdin,
) -> io::Result<Sender>
where
RT: rt::Access,
{
let mut sender = pipe::Sender::from(stdin);
sender.set_nonblocking(true)?;
ctx.runtime().register(&mut sender, Interest::WRITABLE)?;
Ok(Sender { inner: sender })
}
pub fn try_write(&mut self, buf: &[u8]) -> io::Result<usize> {
io::Write::write(&mut self.inner, buf)
}
pub fn write<'a, 'b>(&'a mut self, buf: &'b [u8]) -> Write<'a, 'b> {
Write { sender: self, buf }
}
pub fn write_all<'a, 'b>(&'a mut self, buf: &'b [u8]) -> WriteAll<'a, 'b> {
WriteAll { sender: self, buf }
}
pub fn try_write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
io::Write::write_vectored(&mut self.inner, bufs)
}
pub fn write_vectored<'a, 'b>(
&'a mut self,
bufs: &'b mut [IoSlice<'b>],
) -> WriteVectored<'a, 'b> {
WriteVectored { sender: self, bufs }
}
pub fn write_vectored_all<'a, 'b>(
&'a mut self,
bufs: &'b mut [IoSlice<'b>],
) -> WriteVectoredAll<'a, 'b> {
WriteVectoredAll { sender: self, bufs }
}
}
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Write<'a, 'b> {
sender: &'a mut Sender,
buf: &'b [u8],
}
impl<'a, 'b> Future for Write<'a, 'b> {
type Output = io::Result<usize>;
fn poll(self: Pin<&mut Self>, _: &mut task::Context<'_>) -> Poll<Self::Output> {
let Write { sender, buf } = Pin::into_inner(self);
try_io!(sender.try_write(*buf))
}
}
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct WriteAll<'a, 'b> {
sender: &'a mut Sender,
buf: &'b [u8],
}
impl<'a, 'b> Future for WriteAll<'a, 'b> {
type Output = io::Result<()>;
fn poll(self: Pin<&mut Self>, _: &mut task::Context<'_>) -> Poll<Self::Output> {
let WriteAll { sender, buf } = Pin::into_inner(self);
loop {
match sender.try_write(*buf) {
Ok(0) => return Poll::Ready(Err(io::ErrorKind::WriteZero.into())),
Ok(n) if buf.len() <= n => return Poll::Ready(Ok(())),
Ok(n) => {
*buf = &buf[n..];
continue;
}
Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => break Poll::Pending,
Err(ref err) if err.kind() == io::ErrorKind::Interrupted => continue,
Err(err) => break Poll::Ready(Err(err)),
}
}
}
}
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct WriteVectored<'a, 'b> {
sender: &'a mut Sender,
bufs: &'b mut [IoSlice<'b>],
}
impl<'a, 'b> Future for WriteVectored<'a, 'b> {
type Output = io::Result<usize>;
fn poll(self: Pin<&mut Self>, _: &mut task::Context<'_>) -> Poll<Self::Output> {
let WriteVectored { sender, bufs } = Pin::into_inner(self);
try_io!(sender.try_write_vectored(*bufs))
}
}
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct WriteVectoredAll<'a, 'b> {
sender: &'a mut Sender,
bufs: &'b mut [IoSlice<'b>],
}
impl<'a, 'b> Future for WriteVectoredAll<'a, 'b> {
type Output = io::Result<()>;
fn poll(self: Pin<&mut Self>, _: &mut task::Context<'_>) -> Poll<Self::Output> {
let WriteVectoredAll { sender, bufs } = Pin::into_inner(self);
while !bufs.is_empty() {
match sender.try_write_vectored(*bufs) {
Ok(0) => return Poll::Ready(Err(io::ErrorKind::WriteZero.into())),
Ok(n) => IoSlice::advance_slices(bufs, n),
Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => return Poll::Pending,
Err(ref err) if err.kind() == io::ErrorKind::Interrupted => continue,
Err(err) => return Poll::Ready(Err(err)),
}
}
Poll::Ready(Ok(()))
}
}
impl<RT: rt::Access> Bound<RT> for Sender {
type Error = io::Error;
fn bind_to<M>(&mut self, ctx: &mut actor::Context<M, RT>) -> io::Result<()> {
ctx.runtime()
.reregister(&mut self.inner, Interest::WRITABLE)
}
}
#[derive(Debug)]
pub struct Receiver {
inner: pipe::Receiver,
}
impl Receiver {
pub fn from_child_stdout<M, RT>(
ctx: &mut actor::Context<M, RT>,
stdout: ChildStdout,
) -> io::Result<Receiver>
where
RT: rt::Access,
{
let mut receiver = pipe::Receiver::from(stdout);
receiver.set_nonblocking(true)?;
ctx.runtime().register(&mut receiver, Interest::READABLE)?;
Ok(Receiver { inner: receiver })
}
pub fn from_child_stderr<M, RT>(
ctx: &mut actor::Context<M, RT>,
stderr: ChildStderr,
) -> io::Result<Receiver>
where
RT: rt::Access,
{
let mut receiver = pipe::Receiver::from(stderr);
receiver.set_nonblocking(true)?;
ctx.runtime().register(&mut receiver, Interest::READABLE)?;
Ok(Receiver { inner: receiver })
}
pub fn try_read<B>(&mut self, mut buf: B) -> io::Result<usize>
where
B: Bytes,
{
debug_assert!(
buf.has_spare_capacity(),
"called `Receiver::try_read` with an empty buffer"
);
let buf_bytes = unsafe { &mut *(buf.as_bytes() as *mut [MaybeUninit<u8>] as *mut [u8]) };
io::Read::read(&mut self.inner, buf_bytes).map(|read| {
unsafe { buf.update_length(read) }
read
})
}
pub fn read<'a, B>(&'a mut self, buf: B) -> Read<'a, B>
where
B: Bytes,
{
Read {
receiver: self,
buf,
}
}
pub fn read_n<'a, B>(&'a mut self, buf: B, n: usize) -> ReadN<'a, B>
where
B: Bytes,
{
debug_assert!(
buf.spare_capacity() >= n,
"called `Reader::read_n` with a buffer smaller then `n`",
);
ReadN {
receiver: self,
buf,
left: n,
}
}
pub fn try_read_vectored<B>(&mut self, mut bufs: B) -> io::Result<usize>
where
B: BytesVectored,
{
debug_assert!(
bufs.has_spare_capacity(),
"called `Receiver::try_read_vectored` with empty buffers"
);
let mut buffers = bufs.as_bufs();
let bufs_bytes = unsafe { MaybeUninitSlice::as_io(buffers.as_mut()) };
match io::Read::read_vectored(&mut self.inner, bufs_bytes) {
Ok(read) => {
drop(buffers);
unsafe { bufs.update_lengths(read) }
Ok(read)
}
Err(err) => Err(err),
}
}
pub fn read_vectored<B>(&mut self, bufs: B) -> ReadVectored<'_, B>
where
B: BytesVectored,
{
debug_assert!(
bufs.has_spare_capacity(),
"called `Receiver::read_vectored` with empty buffers"
);
ReadVectored {
receiver: self,
bufs,
}
}
pub fn read_n_vectored<B>(&mut self, bufs: B, n: usize) -> ReadNVectored<'_, B>
where
B: BytesVectored,
{
debug_assert!(
bufs.spare_capacity() >= n,
"called `Receiver::read_n_vectored` with a buffer smaller then `n`"
);
ReadNVectored {
receiver: self,
bufs,
left: n,
}
}
}
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Read<'b, B> {
receiver: &'b mut Receiver,
buf: B,
}
impl<'b, B> Future for Read<'b, B>
where
B: Bytes + Unpin,
{
type Output = io::Result<usize>;
fn poll(self: Pin<&mut Self>, _: &mut task::Context<'_>) -> Poll<Self::Output> {
let Read { receiver, buf } = Pin::into_inner(self);
try_io!(receiver.try_read(&mut *buf))
}
}
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct ReadN<'b, B> {
receiver: &'b mut Receiver,
buf: B,
left: usize,
}
impl<'b, B> Future for ReadN<'b, B>
where
B: Bytes + Unpin,
{
type Output = io::Result<()>;
fn poll(self: Pin<&mut Self>, _: &mut task::Context<'_>) -> Poll<Self::Output> {
let ReadN {
receiver,
buf,
left,
} = Pin::into_inner(self);
loop {
match receiver.try_read(&mut *buf) {
Ok(0) => return Poll::Ready(Err(io::ErrorKind::UnexpectedEof.into())),
Ok(n) if n >= *left => return Poll::Ready(Ok(())),
Ok(n) => {
*left -= n;
continue;
}
Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => break Poll::Pending,
Err(ref err) if err.kind() == io::ErrorKind::Interrupted => continue,
Err(err) => break Poll::Ready(Err(err)),
}
}
}
}
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct ReadVectored<'b, B> {
receiver: &'b mut Receiver,
bufs: B,
}
impl<'b, B> Future for ReadVectored<'b, B>
where
B: BytesVectored + Unpin,
{
type Output = io::Result<usize>;
fn poll(self: Pin<&mut Self>, _: &mut task::Context<'_>) -> Poll<Self::Output> {
let ReadVectored { receiver, bufs } = Pin::into_inner(self);
try_io!(receiver.try_read_vectored(&mut *bufs))
}
}
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct ReadNVectored<'b, B> {
receiver: &'b mut Receiver,
bufs: B,
left: usize,
}
impl<'b, B> Future for ReadNVectored<'b, B>
where
B: BytesVectored + Unpin,
{
type Output = io::Result<()>;
fn poll(self: Pin<&mut Self>, _: &mut task::Context<'_>) -> Poll<Self::Output> {
let ReadNVectored {
receiver,
bufs,
left,
} = Pin::into_inner(self);
loop {
match receiver.try_read_vectored(&mut *bufs) {
Ok(0) => return Poll::Ready(Err(io::ErrorKind::UnexpectedEof.into())),
Ok(n) if n >= *left => return Poll::Ready(Ok(())),
Ok(n) => {
*left -= n;
continue;
}
Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => break Poll::Pending,
Err(ref err) if err.kind() == io::ErrorKind::Interrupted => continue,
Err(err) => break Poll::Ready(Err(err)),
}
}
}
}
impl<RT: rt::Access> Bound<RT> for Receiver {
type Error = io::Error;
fn bind_to<M>(&mut self, ctx: &mut actor::Context<M, RT>) -> io::Result<()> {
ctx.runtime()
.reregister(&mut self.inner, Interest::READABLE)
}
}