Skip to content

Commit 2a16237

Browse files
dignifiedquireyoshuawuyts
authored andcommitted
improve structure using generic
1 parent cf4f77e commit 2a16237

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/upgrade/connection.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
use async_std::io::{self, prelude::*};
22

3-
use std::fmt;
43
use std::pin::Pin;
54
use std::task::{Context, Poll};
65

76
/// An upgraded HTTP connection.
8-
pub struct Connection {
9-
inner: Pin<Box<dyn InnerConnection + 'static>>,
7+
#[derive(Debug, Clone)]
8+
pub struct RawConnection<Inner> {
9+
inner: Inner,
1010
}
1111

12-
pub(crate) trait InnerConnection: Read + Write + Send + Sync + Unpin {}
12+
/// A boxed upgraded HTTP connection.
13+
pub type Connection = RawConnection<Box<dyn InnerConnection + 'static>>;
14+
15+
/// Trait to signal the requirements for an underlying connection type.
16+
pub trait InnerConnection: Read + Write + Send + Sync + Unpin {}
1317
impl<T: Read + Write + Send + Sync + Unpin> InnerConnection for T {}
1418

15-
impl Read for Connection {
19+
impl<Inner: Read + Unpin> Read for RawConnection<Inner> {
1620
fn poll_read(
1721
mut self: Pin<&mut Self>,
1822
cx: &mut Context<'_>,
@@ -22,7 +26,7 @@ impl Read for Connection {
2226
}
2327
}
2428

25-
impl Write for Connection {
29+
impl<Inner: Write + Unpin> Write for RawConnection<Inner> {
2630
fn poll_write(
2731
mut self: Pin<&mut Self>,
2832
cx: &mut Context<'_>,
@@ -39,11 +43,3 @@ impl Write for Connection {
3943
Pin::new(&mut self.inner).poll_close(cx)
4044
}
4145
}
42-
43-
impl fmt::Debug for Connection {
44-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45-
f.debug_struct("Connection")
46-
.field("inner", &"Pin<Box<dyn Inner>>")
47-
.finish()
48-
}
49-
}

0 commit comments

Comments
 (0)