Skip to content

Commit

Permalink
Merge pull request #176 from http-rs/patch-trailers
Browse files Browse the repository at this point in the history
Fix trailers methods
  • Loading branch information
yoshuawuyts committed Jun 9, 2020
2 parents 2d69613 + 45c7860 commit 0836aa5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pin_project_lite::pin_project! {
ext: Extensions,
trailers_sender: Option<sync::Sender<Trailers>>,
trailers_receiver: Option<sync::Receiver<Trailers>>,
has_trailers: bool,
}
}

Expand All @@ -63,6 +64,7 @@ impl Request {
local_addr: None,
trailers_receiver: Some(trailers_receiver),
trailers_sender: Some(trailers_sender),
has_trailers: false,
}
}

Expand Down Expand Up @@ -542,6 +544,7 @@ impl Request {

/// Sends trailers to the a receiver.
pub fn send_trailers(&mut self) -> trailers::Sender {
self.has_trailers = true;
let sender = self
.trailers_sender
.take()
Expand All @@ -550,14 +553,19 @@ impl Request {
}

/// Receive trailers from a sender.
pub async fn recv_trailers(&mut self) -> trailers::Receiver {
pub fn recv_trailers(&mut self) -> trailers::Receiver {
let receiver = self
.trailers_receiver
.take()
.expect("Trailers receiver can only be constructed once");
trailers::Receiver::new(receiver)
}

/// Returns `true` if sending trailers is in progress.
pub fn has_trailers(&self) -> bool {
self.has_trailers
}

/// An iterator visiting all header pairs in arbitrary order.
pub fn iter(&self) -> headers::Iter<'_> {
self.headers.iter()
Expand Down Expand Up @@ -873,6 +881,7 @@ impl Clone for Request {
ext: Extensions::new(),
peer_addr: self.peer_addr.clone(),
local_addr: self.local_addr.clone(),
has_trailers: false,
}
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pin_project_lite::pin_project! {
status: StatusCode,
headers: Headers,
version: Option<Version>,
has_trailers: bool,
trailers_sender: Option<sync::Sender<Trailers>>,
trailers_receiver: Option<sync::Receiver<Trailers>>,
#[pin]
Expand Down Expand Up @@ -75,6 +76,7 @@ pin_project_lite::pin_project! {
version: Option<Version>,
trailers_sender: Option<sync::Sender<Trailers>>,
trailers_receiver: Option<sync::Receiver<Trailers>>,
has_trailers: bool,
upgrade_sender: Option<sync::Sender<upgrade::Connection>>,
upgrade_receiver: Option<sync::Receiver<upgrade::Connection>>,
has_upgrade: bool,
Expand Down Expand Up @@ -105,6 +107,7 @@ impl Response {
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,
Expand All @@ -130,6 +133,7 @@ impl Response {
body: Body::empty(),
trailers_sender: Some(trailers_sender),
trailers_receiver: Some(trailers_receiver),
has_trailers: false,
upgrade_sender: Some(upgrade_sender),
upgrade_receiver: Some(upgrade_receiver),
has_upgrade: false,
Expand Down Expand Up @@ -536,6 +540,7 @@ impl Response {

/// Sends trailers to the a receiver.
pub fn send_trailers(&mut self) -> trailers::Sender {
self.has_trailers = true;
let sender = self
.trailers_sender
.take()
Expand All @@ -544,14 +549,19 @@ impl Response {
}

/// Receive trailers from a sender.
pub async fn recv_trailers(&mut self) -> trailers::Receiver {
pub fn recv_trailers(&mut self) -> trailers::Receiver {
let receiver = self
.trailers_receiver
.take()
.expect("Trailers receiver can only be constructed once");
trailers::Receiver::new(receiver)
}

/// Returns `true` if sending trailers is in progress.
pub fn has_trailers(&self) -> bool {
self.has_trailers
}

/// Sends an upgrade connection to the a receiver.
#[cfg(feature = "unstable")]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
Expand Down Expand Up @@ -640,6 +650,7 @@ impl Clone for Response {
version: self.version.clone(),
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")]
Expand Down

0 comments on commit 0836aa5

Please sign in to comment.