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
//! Common IO primitives.
//!
//! These primitives are closely mirroring the definitions in
//! [`tokio-io`](https://docs.rs/tokio-io).  A big difference is that these definitions are not tied
//! to `std::io::Error`, but instead allow for custom error types, and also don't require
//! allocation.

use core::fmt;
use core::pin;
use core::task;

pub mod read;
pub mod read_exact;
pub mod shutdown;
pub mod write;
pub mod write_all;

pub trait Read: fmt::Debug {
    type Error: ReadError;

    fn poll_read(
        self: pin::Pin<&mut Self>,
        cx: &mut task::Context<'_>,
        buffer: &mut [u8],
    ) -> task::Poll<Result<usize, Self::Error>>;
}

pub trait ReadError: fmt::Debug {
    fn eof() -> Self;
}

pub trait Write: fmt::Debug {
    type Error: WriteError;

    fn poll_write(
        self: pin::Pin<&mut Self>,
        cx: &mut task::Context<'_>,
        bytes: &[u8],
    ) -> task::Poll<Result<usize, Self::Error>>;

    fn poll_shutdown(
        self: pin::Pin<&mut Self>,
        cx: &mut task::Context<'_>,
    ) -> task::Poll<Result<(), Self::Error>>;
}

impl<A: ?Sized + Write + Unpin> Write for &mut A {
    type Error = A::Error;

    fn poll_write(
        mut self: pin::Pin<&mut Self>,
        cx: &mut task::Context<'_>,
        bytes: &[u8],
    ) -> task::Poll<Result<usize, Self::Error>> {
        pin::Pin::new(&mut **self).poll_write(cx, bytes)
    }

    fn poll_shutdown(
        mut self: pin::Pin<&mut Self>,
        cx: &mut task::Context<'_>,
    ) -> task::Poll<Result<(), Self::Error>> {
        pin::Pin::new(&mut **self).poll_shutdown(cx)
    }
}

pub trait WriteError: fmt::Debug {
    fn write_zero() -> Self;
}

pub trait ReadExt: Read {
    fn read<'a>(&'a mut self, buffer: &'a mut [u8]) -> read::Read<'a, Self>
    where
        Self: Unpin,
    {
        read::read(self, buffer)
    }

    fn read_exact<'a>(&'a mut self, buffer: &'a mut [u8]) -> read_exact::ReadExact<'a, Self>
    where
        Self: Unpin,
    {
        read_exact::read_exact(self, buffer)
    }
}

impl<A> ReadExt for A where A: Read {}

pub trait WriteExt: Write {
    fn write<'a>(&'a mut self, bytes: &'a [u8]) -> write::Write<'a, Self>
    where
        Self: Unpin,
    {
        write::write(self, bytes)
    }

    fn write_all<'a>(&'a mut self, bytes: &'a [u8]) -> write_all::WriteAll<'a, Self>
    where
        Self: Unpin,
    {
        write_all::write_all(self, bytes)
    }

    fn shutdown(&mut self) -> shutdown::Shutdown<Self>
    where
        Self: Unpin,
    {
        shutdown::shutdown(self)
    }
}

impl<A> WriteExt for A where A: WriteExt {}