io_arc/
lib.rs

1//! Proof of concept Arc with IO trait delegation
2//!
3//! # Examples
4//!
5//! ```no_run
6//! use std::net::TcpStream;
7//! use io_arc::IoArc;
8//! use std::io::{self, prelude::*};
9//!
10//! fn main() -> io::Result<()> {
11//!     let stream = TcpStream::connect("localhost:8080")?;
12//!     let stream = IoArc::new(stream);
13//!
14//!     let mut stream1 = stream.clone();
15//!     let mut _stream2 = stream.clone();
16//!
17//!     stream1.write(b"hello world")?; // Write is implemented for Arc<TcpStream> directly
18//!     Ok(())
19//! }
20//! ```
21
22#![forbid(unsafe_code, future_incompatible, rust_2018_idioms)]
23#![deny(missing_debug_implementations, nonstandard_style)]
24#![warn(missing_docs, missing_doc_code_examples, unreachable_pub)]
25
26use futures_io::{AsyncRead, AsyncWrite};
27use std::io::{self, prelude::*};
28use std::pin::Pin;
29use std::sync::Arc;
30use std::task::{Context, Poll};
31
32use std::borrow::Borrow;
33
34/// A variant of `Arc` that delegates IO traits if available on `&T`.
35#[derive(Debug)]
36pub struct IoArc<T>(Arc<T>);
37
38impl<T> IoArc<T> {
39    /// Create a new instance of IoArc.
40    pub fn new(data: T) -> Self {
41        Self(Arc::new(data))
42    }
43}
44
45impl<T> Read for IoArc<T>
46where
47    for<'a> &'a T: Read,
48{
49    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
50        (&mut &*self.0).read(buf)
51    }
52}
53
54impl<T> Write for IoArc<T>
55where
56    for<'a> &'a T: Write,
57{
58    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
59        (&mut &*self.0).write(buf)
60    }
61
62    fn flush(&mut self) -> io::Result<()> {
63        (&mut &*self.0).flush()
64    }
65}
66
67impl<T> AsyncRead for IoArc<T>
68where
69    for<'a> &'a T: AsyncRead,
70{
71    fn poll_read(
72        self: Pin<&mut Self>,
73        cx: &mut Context<'_>,
74        buf: &mut [u8],
75    ) -> Poll<io::Result<usize>> {
76        Pin::new(&mut &*self.0).poll_read(cx, buf)
77    }
78}
79
80impl<T> AsyncWrite for IoArc<T>
81where
82    for<'a> &'a T: AsyncWrite,
83{
84    fn poll_write(
85        self: Pin<&mut Self>,
86        cx: &mut Context<'_>,
87        buf: &[u8],
88    ) -> Poll<io::Result<usize>> {
89        Pin::new(&mut &*self.0).poll_write(cx, buf)
90    }
91
92    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
93        Pin::new(&mut &*self.0).poll_flush(cx)
94    }
95
96    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
97        Pin::new(&mut &*self.0).poll_close(cx)
98    }
99}
100
101impl<T: Default> Default for IoArc<T> {
102    fn default() -> Self {
103        Self::new(Default::default())
104    }
105}
106
107impl<T> From<T> for IoArc<T> {
108    fn from(t: T) -> Self {
109        Self::new(t)
110    }
111}
112
113impl<T> Borrow<T> for IoArc<T> {
114    fn borrow(&self) -> &T {
115        self.0.borrow()
116    }
117}
118
119impl<T> AsRef<T> for IoArc<T> {
120    fn as_ref(&self) -> &T {
121        self.0.as_ref()
122    }
123}
124
125impl<T> Unpin for IoArc<T> {}
126
127impl<T> Clone for IoArc<T> {
128    fn clone(&self) -> Self {
129        Self(self.0.clone())
130    }
131}