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
use prelude::IoControl;
use ffi::{RawFd, AsRawFd, ioctl};
use core::{IoContext, AsIoContext, AsyncFd};
use async::Handler;
use streams::Stream;
use reactive_io::{AsAsyncFd, read, async_read, write, async_write, cancel,
                  getnonblock, setnonblock};

use std::io;

/// Typedef for the typical usage of a stream-oriented descriptor.
pub struct StreamDescriptor {
    fd: AsyncFd,
}

impl StreamDescriptor {
    pub unsafe fn from_raw_fd(ctx: &IoContext, fd: RawFd) -> StreamDescriptor {
        StreamDescriptor {
            fd: AsyncFd::new::<Self>(fd, ctx),
        }
    }

    pub fn cancel(&self) {
        cancel(self)
    }

    pub fn get_non_blocking(&self) -> io::Result<bool> {
        getnonblock(self)
    }

    pub fn io_control<C>(&self, cmd: &mut C) -> io::Result<()>
        where C: IoControl,
    {
        ioctl(self, cmd)
    }

    pub fn set_non_blocking(&self, on: bool) -> io::Result<()> {
        setnonblock(self, on)
    }
}

impl Stream<io::Error> for StreamDescriptor {
    fn async_read_some<F>(&self, buf: &mut [u8], handler: F) -> F::Output
        where F: Handler<usize, io::Error>,
    {
        async_read(self, buf, handler)
    }

    fn async_write_some<F>(&self, buf: &[u8], handler: F) -> F::Output
        where F: Handler<usize, io::Error>
    {
        async_write(self, buf, handler)
    }

    fn read_some(&self, buf: &mut [u8]) -> io::Result<usize> {
        read(self, buf)
    }

    fn write_some(&self, buf: &[u8]) -> io::Result<usize> {
        write(self, buf)
    }
}

impl AsRawFd for StreamDescriptor {
    fn as_raw_fd(&self) -> RawFd {
        self.fd.as_raw_fd()
    }
}

unsafe impl Send for StreamDescriptor {
}

unsafe impl AsIoContext for StreamDescriptor {
    fn as_ctx(&self) -> &IoContext {
        self.fd.as_ctx()
    }
}

impl AsAsyncFd for StreamDescriptor {
    fn as_fd(&self) -> &AsyncFd {
        &self.fd
    }
}