diff --git a/src/lib.rs b/src/lib.rs index a8cdbfca..c332fb54 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -140,9 +140,7 @@ mod status_code; mod version; pub mod trace; -cfg_unstable! { - pub mod upgrade; -} +pub mod upgrade; pub use body::Body; pub use error::{Error, Result}; diff --git a/src/response.rs b/src/response.rs index c9e13306..766dacba 100644 --- a/src/response.rs +++ b/src/response.rs @@ -14,13 +14,9 @@ use crate::headers::{ }; use crate::mime::Mime; use crate::trailers::{self, Trailers}; +use crate::upgrade; use crate::{Body, Extensions, StatusCode, Version}; -cfg_unstable! { - use crate::upgrade; -} - -#[cfg(not(feature = "unstable"))] pin_project_lite::pin_project! { /// An HTTP response. /// @@ -44,38 +40,6 @@ pin_project_lite::pin_project! { has_trailers: bool, trailers_sender: Option>, trailers_receiver: Option>, - #[pin] - body: Body, - ext: Extensions, - local_addr: Option, - peer_addr: Option, - } -} - -#[cfg(feature = "unstable")] -pin_project_lite::pin_project! { - /// An HTTP response. - /// - /// # Examples - /// - /// ``` - /// # fn main() -> Result<(), http_types::Error> { - /// # - /// use http_types::{Response, StatusCode}; - /// - /// let mut res = Response::new(StatusCode::Ok); - /// res.set_body("Hello, Nori!"); - /// # - /// # Ok(()) } - /// ``` - #[derive(Debug)] - pub struct Response { - status: StatusCode, - headers: Headers, - version: Option, - trailers_sender: Option>, - trailers_receiver: Option>, - has_trailers: bool, upgrade_sender: Option>, upgrade_receiver: Option>, has_upgrade: bool, @@ -89,32 +53,6 @@ pin_project_lite::pin_project! { impl Response { /// Create a new response. - #[cfg(not(feature = "unstable"))] - pub fn new(status: S) -> Self - where - S: TryInto, - S::Error: Debug, - { - let status = status - .try_into() - .expect("Could not convert into a valid `StatusCode`"); - let (trailers_sender, trailers_receiver) = async_channel::bounded(1); - Self { - status, - headers: Headers::new(), - version: None, - body: Body::empty(), - trailers_sender: Some(trailers_sender), - trailers_receiver: Some(trailers_receiver), - has_trailers: false, - ext: Extensions::new(), - peer_addr: None, - local_addr: None, - } - } - - /// Create a new response. - #[cfg(feature = "unstable")] pub fn new(status: S) -> Self where S: TryInto, @@ -558,7 +496,6 @@ impl Response { } /// Sends an upgrade connection to the a receiver. - #[cfg(feature = "unstable")] #[cfg_attr(feature = "docs", doc(cfg(unstable)))] pub fn send_upgrade(&mut self) -> upgrade::Sender { self.has_upgrade = true; @@ -570,7 +507,6 @@ impl Response { } /// Receive an upgraded connection from a sender. - #[cfg(feature = "unstable")] #[cfg_attr(feature = "docs", doc(cfg(unstable)))] pub async fn recv_upgrade(&mut self) -> upgrade::Receiver { self.has_upgrade = true; @@ -582,7 +518,6 @@ impl Response { } /// Returns `true` if a protocol upgrade is in progress. - #[cfg(feature = "unstable")] #[cfg_attr(feature = "docs", doc(cfg(unstable)))] pub fn has_upgrade(&self) -> bool { self.has_upgrade @@ -646,11 +581,8 @@ impl Clone for Response { trailers_sender: self.trailers_sender.clone(), trailers_receiver: self.trailers_receiver.clone(), has_trailers: false, - #[cfg(feature = "unstable")] upgrade_sender: self.upgrade_sender.clone(), - #[cfg(feature = "unstable")] upgrade_receiver: self.upgrade_receiver.clone(), - #[cfg(feature = "unstable")] has_upgrade: false, body: Body::empty(), ext: Extensions::new(), diff --git a/src/upgrade/connection.rs b/src/upgrade/connection.rs index f48d5792..a6c3c64e 100644 --- a/src/upgrade/connection.rs +++ b/src/upgrade/connection.rs @@ -1,28 +1,38 @@ use futures_lite::{io, prelude::*}; +use std::fmt::{self, Debug}; use std::pin::Pin; use std::task::{Context, Poll}; /// An upgraded HTTP connection. -#[derive(Debug, Clone)] -pub struct RawConnection { - inner: Inner, +pub struct Connection { + inner: Box, } -impl Connection { - pub fn new(t: T) -> Self { - RawConnection { inner: Box::new(t) } +impl Debug for Connection { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let inner = "Box"; + f.debug_struct("Connection") + .field(&"inner", &inner) + .finish() } } -/// A boxed upgraded HTTP connection. -pub type Connection = RawConnection>; +impl Connection { + /// Create a new instance of `Connection`. + pub fn new(t: T) -> Self + where + T: AsyncRead + AsyncWrite + Send + Sync + Unpin + 'static, + { + Self { inner: Box::new(t) } + } +} /// Trait to signal the requirements for an underlying connection type. pub trait InnerConnection: AsyncRead + AsyncWrite + Send + Sync + Unpin {} impl InnerConnection for T {} -impl AsyncRead for RawConnection { +impl AsyncRead for Connection { fn poll_read( mut self: Pin<&mut Self>, cx: &mut Context<'_>, @@ -32,7 +42,7 @@ impl AsyncRead for RawConnection { } } -impl AsyncWrite for RawConnection { +impl AsyncWrite for Connection { fn poll_write( mut self: Pin<&mut Self>, cx: &mut Context<'_>, diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 394f689b..d2b01a71 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -9,18 +9,6 @@ use crate::{Error, Status, StatusCode}; use std::cmp::Ordering; use std::str::FromStr; -/// Declares unstable items. -#[doc(hidden)] -macro_rules! cfg_unstable { - ($($item:item)*) => { - $( - #[cfg(feature = "unstable")] - #[cfg_attr(feature = "docs", doc(cfg(unstable)))] - $item - )* - } -} - /// Parse a weight of the form `q=0.123`. pub(crate) fn parse_weight(s: &str) -> crate::Result { let mut parts = s.split('=');