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
use {Take, Retry};

/// Base trait for generic stream operations
///
/// Most of these function are generalizations of functions already present in [`std::io::Read`]
/// and/or [`std::io::Write`]:
///
/// - `by_ref` ← [`std::io::Read::by_ref`] and [`std::io::Write::by_ref`]
/// - `take` ← [`std::io::Read::take`]
///
/// New methods without counterpart in `std::io`
///
/// - `retry`
///
/// [`std::io::Read`]: https://doc.rust-lang.org/nightly/std/io/trait.Read.html
/// [`std::io::Write`]: https://doc.rust-lang.org/nightly/std/io/trait.Write.html
/// [`std::io::Read::by_ref`]:
///     https://doc.rust-lang.org/nightly/std/io/trait.Read.html#method.by_ref
/// [`std::io::Read::take`]: https://doc.rust-lang.org/nightly/std/io/trait.Read.html#method.take
/// [`std::io::Write::by_ref`]:
///     https://doc.rust-lang.org/nightly/std/io/trait.Write.html#method.by_ref
pub trait Stream {
    /// Creates a "by reference" adaptor for this stream.
    ///
    /// This method is equivalent to [`std::io::Read::by_ref`] and [`std::io::Write::by_ref`]
    ///
    /// [`std::io::Read::by_ref`]:
    ///     https://doc.rust-lang.org/nightly/std/io/trait.Read.html#method.by_ref
    /// [`std::io::Write::by_ref`]:
    ///     https://doc.rust-lang.org/nightly/std/io/trait.Write.html#method.by_ref
    #[inline]
    fn by_ref(&mut self) -> &mut Self
        where Self: Sized
    {
        self
    }

    /// Creates an adapter which will limits the total number of bytes of this stream.
    ///
    /// This function returns a new stream instance of which will read write at most
    /// `limit` bytes, after which it will always return EOF (`Ok(0)`). Any
    /// errors will not count towards the number of bytes read and future
    /// calls to `read` or `write` may succeed.
    ///
    /// Differences to `std::io::Read::take`
    /// ====================================
    /// This function is equivalent to [`std::io::Read::take`] except that the returned
    /// adapter implements [`BufReadGrow`] and [`std::io::Write`] if possible.
    ///
    /// [`BufReadGrow`]: ./trait.BufReadGrow.html
    /// [`std::io::Read::take`]:
    ///     https://doc.rust-lang.org/nightly/std/io/trait.Read.html#method.take
    /// [`std::io::Write`]: https://doc.rust-lang.org/nightly/std/io/trait.Write.html
    #[inline]
    fn take(self, limit: u64) -> Take<Self>
        where Self: Sized
    {
        Take::new(self, limit)
    }

    /// Transforms this stream into a stream that automatically retries on interrupts
    ///
    /// The returned adapter will behave identically to the original stream, except that it retries
    /// all operations automatically if an error of kind
    /// [`ErrorKind`]`::Interrupted` occurs.
    ///
    /// Note
    /// ----
    /// Methods that are already expected to retry are forwarded directly to the underlying reader.
    ///
    /// [`ErrorKind`]: https://doc.rust-lang.org/nightly/std/io/enum.ErrorKind.html
    #[inline]
    fn retry(self) -> Retry<Self>
        where Self: Sized
    {
        Retry::new(self)
    }
}

impl<R: ?Sized> Stream for R {}