Skip to content

Commit b133109

Browse files
authored
Merge pull request #291 from http-rs/stabilize-upgrade
Stabilize upgrade
2 parents 975ad8f + 933596a commit b133109

File tree

4 files changed

+22
-94
lines changed

4 files changed

+22
-94
lines changed

src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,7 @@ mod status_code;
140140
mod version;
141141

142142
pub mod trace;
143-
cfg_unstable! {
144-
pub mod upgrade;
145-
}
143+
pub mod upgrade;
146144

147145
pub use body::Body;
148146
pub use error::{Error, Result};

src/response.rs

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,9 @@ use crate::headers::{
1414
};
1515
use crate::mime::Mime;
1616
use crate::trailers::{self, Trailers};
17+
use crate::upgrade;
1718
use crate::{Body, Extensions, StatusCode, Version};
1819

19-
cfg_unstable! {
20-
use crate::upgrade;
21-
}
22-
23-
#[cfg(not(feature = "unstable"))]
2420
pin_project_lite::pin_project! {
2521
/// An HTTP response.
2622
///
@@ -44,38 +40,6 @@ pin_project_lite::pin_project! {
4440
has_trailers: bool,
4541
trailers_sender: Option<async_channel::Sender<Trailers>>,
4642
trailers_receiver: Option<async_channel::Receiver<Trailers>>,
47-
#[pin]
48-
body: Body,
49-
ext: Extensions,
50-
local_addr: Option<String>,
51-
peer_addr: Option<String>,
52-
}
53-
}
54-
55-
#[cfg(feature = "unstable")]
56-
pin_project_lite::pin_project! {
57-
/// An HTTP response.
58-
///
59-
/// # Examples
60-
///
61-
/// ```
62-
/// # fn main() -> Result<(), http_types::Error> {
63-
/// #
64-
/// use http_types::{Response, StatusCode};
65-
///
66-
/// let mut res = Response::new(StatusCode::Ok);
67-
/// res.set_body("Hello, Nori!");
68-
/// #
69-
/// # Ok(()) }
70-
/// ```
71-
#[derive(Debug)]
72-
pub struct Response {
73-
status: StatusCode,
74-
headers: Headers,
75-
version: Option<Version>,
76-
trailers_sender: Option<async_channel::Sender<Trailers>>,
77-
trailers_receiver: Option<async_channel::Receiver<Trailers>>,
78-
has_trailers: bool,
7943
upgrade_sender: Option<async_channel::Sender<upgrade::Connection>>,
8044
upgrade_receiver: Option<async_channel::Receiver<upgrade::Connection>>,
8145
has_upgrade: bool,
@@ -89,32 +53,6 @@ pin_project_lite::pin_project! {
8953

9054
impl Response {
9155
/// Create a new response.
92-
#[cfg(not(feature = "unstable"))]
93-
pub fn new<S>(status: S) -> Self
94-
where
95-
S: TryInto<StatusCode>,
96-
S::Error: Debug,
97-
{
98-
let status = status
99-
.try_into()
100-
.expect("Could not convert into a valid `StatusCode`");
101-
let (trailers_sender, trailers_receiver) = async_channel::bounded(1);
102-
Self {
103-
status,
104-
headers: Headers::new(),
105-
version: None,
106-
body: Body::empty(),
107-
trailers_sender: Some(trailers_sender),
108-
trailers_receiver: Some(trailers_receiver),
109-
has_trailers: false,
110-
ext: Extensions::new(),
111-
peer_addr: None,
112-
local_addr: None,
113-
}
114-
}
115-
116-
/// Create a new response.
117-
#[cfg(feature = "unstable")]
11856
pub fn new<S>(status: S) -> Self
11957
where
12058
S: TryInto<StatusCode>,
@@ -558,7 +496,6 @@ impl Response {
558496
}
559497

560498
/// Sends an upgrade connection to the a receiver.
561-
#[cfg(feature = "unstable")]
562499
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
563500
pub fn send_upgrade(&mut self) -> upgrade::Sender {
564501
self.has_upgrade = true;
@@ -570,7 +507,6 @@ impl Response {
570507
}
571508

572509
/// Receive an upgraded connection from a sender.
573-
#[cfg(feature = "unstable")]
574510
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
575511
pub async fn recv_upgrade(&mut self) -> upgrade::Receiver {
576512
self.has_upgrade = true;
@@ -582,7 +518,6 @@ impl Response {
582518
}
583519

584520
/// Returns `true` if a protocol upgrade is in progress.
585-
#[cfg(feature = "unstable")]
586521
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
587522
pub fn has_upgrade(&self) -> bool {
588523
self.has_upgrade
@@ -646,11 +581,8 @@ impl Clone for Response {
646581
trailers_sender: self.trailers_sender.clone(),
647582
trailers_receiver: self.trailers_receiver.clone(),
648583
has_trailers: false,
649-
#[cfg(feature = "unstable")]
650584
upgrade_sender: self.upgrade_sender.clone(),
651-
#[cfg(feature = "unstable")]
652585
upgrade_receiver: self.upgrade_receiver.clone(),
653-
#[cfg(feature = "unstable")]
654586
has_upgrade: false,
655587
body: Body::empty(),
656588
ext: Extensions::new(),

src/upgrade/connection.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
use futures_lite::{io, prelude::*};
22

3+
use std::fmt::{self, Debug};
34
use std::pin::Pin;
45
use std::task::{Context, Poll};
56

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

12-
impl Connection {
13-
pub fn new<T: InnerConnection + 'static>(t: T) -> Self {
14-
RawConnection { inner: Box::new(t) }
12+
impl Debug for Connection {
13+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14+
let inner = "Box<dyn Asyncread + AsyncWrite + Send + Sync + Unpin>";
15+
f.debug_struct("Connection")
16+
.field(&"inner", &inner)
17+
.finish()
1518
}
1619
}
1720

18-
/// A boxed upgraded HTTP connection.
19-
pub type Connection = RawConnection<Box<dyn InnerConnection + 'static>>;
21+
impl Connection {
22+
/// Create a new instance of `Connection`.
23+
pub fn new<T>(t: T) -> Self
24+
where
25+
T: AsyncRead + AsyncWrite + Send + Sync + Unpin + 'static,
26+
{
27+
Self { inner: Box::new(t) }
28+
}
29+
}
2030

2131
/// Trait to signal the requirements for an underlying connection type.
2232
pub trait InnerConnection: AsyncRead + AsyncWrite + Send + Sync + Unpin {}
2333
impl<T: AsyncRead + AsyncWrite + Send + Sync + Unpin> InnerConnection for T {}
2434

25-
impl<Inner: AsyncRead + Unpin> AsyncRead for RawConnection<Inner> {
35+
impl AsyncRead for Connection {
2636
fn poll_read(
2737
mut self: Pin<&mut Self>,
2838
cx: &mut Context<'_>,
@@ -32,7 +42,7 @@ impl<Inner: AsyncRead + Unpin> AsyncRead for RawConnection<Inner> {
3242
}
3343
}
3444

35-
impl<Inner: AsyncWrite + Unpin> AsyncWrite for RawConnection<Inner> {
45+
impl AsyncWrite for Connection {
3646
fn poll_write(
3747
mut self: Pin<&mut Self>,
3848
cx: &mut Context<'_>,

src/utils/mod.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,6 @@ use crate::{Error, Status, StatusCode};
99
use std::cmp::Ordering;
1010
use std::str::FromStr;
1111

12-
/// Declares unstable items.
13-
#[doc(hidden)]
14-
macro_rules! cfg_unstable {
15-
($($item:item)*) => {
16-
$(
17-
#[cfg(feature = "unstable")]
18-
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
19-
$item
20-
)*
21-
}
22-
}
23-
2412
/// Parse a weight of the form `q=0.123`.
2513
pub(crate) fn parse_weight(s: &str) -> crate::Result<f32> {
2614
let mut parts = s.split('=');

0 commit comments

Comments
 (0)