Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make recv_trailers return a concrete type #103

Merged
merged 1 commit into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::headers::{
self, HeaderName, HeaderValue, Headers, Names, ToHeaderValues, Values, CONTENT_TYPE,
};
use crate::mime::Mime;
use crate::trailers::{Trailers, TrailersSender};
use crate::trailers::{self, Trailers};
use crate::Cookie;
use crate::{Body, Method, TypeMap, Url, Version};

Expand All @@ -31,8 +31,8 @@ pin_project_lite::pin_project! {
url: Url,
headers: Headers,
version: Option<Version>,
sender: Option<sync::Sender<crate::Result<Trailers>>>,
receiver: sync::Receiver<crate::Result<Trailers>>,
sender: Option<sync::Sender<Trailers>>,
receiver: Option<sync::Receiver<Trailers>>,
#[pin]
body: Body,
local: TypeMap,
Expand All @@ -50,7 +50,7 @@ impl Request {
version: None,
body: Body::empty(),
sender: Some(sender),
receiver,
receiver: Some(receiver),
local: TypeMap::new(),
}
}
Expand Down Expand Up @@ -431,17 +431,21 @@ impl Request {
}

/// Sends trailers to the a receiver.
pub fn send_trailers(&mut self) -> TrailersSender {
pub fn send_trailers(&mut self) -> trailers::Sender {
let sender = self
.sender
.take()
.expect("Trailers sender can only be constructed once");
TrailersSender::new(sender)
trailers::Sender::new(sender)
}

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

/// An iterator visiting all header pairs in arbitrary order.
Expand Down
20 changes: 12 additions & 8 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::headers::{
self, HeaderName, HeaderValue, Headers, Names, ToHeaderValues, Values, CONTENT_TYPE,
};
use crate::mime::Mime;
use crate::trailers::{Trailers, TrailersSender};
use crate::trailers::{self, Trailers};
use crate::{Body, Cookie, StatusCode, TypeMap, Version};

pin_project_lite::pin_project! {
Expand All @@ -33,8 +33,8 @@ pin_project_lite::pin_project! {
status: StatusCode,
headers: Headers,
version: Option<Version>,
sender: Option<sync::Sender<crate::Result<Trailers>>>,
receiver: sync::Receiver<crate::Result<Trailers>>,
sender: Option<sync::Sender<Trailers>>,
receiver: Option<sync::Receiver<Trailers>>,
#[pin]
body: Body,
local: TypeMap,
Expand All @@ -51,7 +51,7 @@ impl Response {
version: None,
body: Body::empty(),
sender: Some(sender),
receiver,
receiver: Some(receiver),
local: TypeMap::new(),
}
}
Expand Down Expand Up @@ -396,17 +396,21 @@ impl Response {
}

/// Sends trailers to the a receiver.
pub fn send_trailers(&mut self) -> TrailersSender {
pub fn send_trailers(&mut self) -> trailers::Sender {
let sender = self
.sender
.take()
.expect("Trailers sender can only be constructed once");
TrailersSender::new(sender)
trailers::Sender::new(sender)
}

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

/// An iterator visiting all header pairs in arbitrary order.
Expand Down
49 changes: 40 additions & 9 deletions src/trailers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@
//! trailers.insert("Content-Type", "text/plain")?;
//!
//! task::spawn(async move {
//! let _trailers = req.recv_trailers().await;
//! let trailers = req.recv_trailers().await;
//! # drop(trailers)
//! });
//!
//! sender.send(Ok(trailers)).await;
//! sender.send(trailers).await;
//! #
//! # Ok(()) })}
//! ```
Expand All @@ -49,10 +50,14 @@
use crate::headers::{
HeaderName, HeaderValue, Headers, Iter, IterMut, Names, ToHeaderValues, Values,
};
use async_std::sync::Sender;
use async_std::prelude::*;
use async_std::sync;

use std::convert::TryInto;
use std::future::Future;
use std::ops::{Deref, DerefMut};
use std::pin::Pin;
use std::task::{Context, Poll};

/// A collection of trailing HTTP headers.
#[derive(Debug)]
Expand Down Expand Up @@ -182,21 +187,47 @@ impl DerefMut for Trailers {
/// called once, and cannot be cloned. That's because only a single instance of
/// `Trailers` should be created.
#[derive(Debug)]
pub struct TrailersSender {
sender: Sender<crate::Result<Trailers>>,
pub struct Sender {
sender: sync::Sender<Trailers>,
}

impl TrailersSender {
/// Create a new instance of `TrailersSender`.
impl Sender {
/// Create a new instance of `Sender`.
#[doc(hidden)]
pub fn new(sender: Sender<crate::Result<Trailers>>) -> Self {
pub fn new(sender: sync::Sender<Trailers>) -> Self {
Self { sender }
}

/// Send a `Trailer`.
///
/// The channel will be consumed after having sent trailers.
pub async fn send(self, trailers: crate::Result<Trailers>) {
pub async fn send(self, trailers: Trailers) {
self.sender.send(trailers).await
}
}

/// The receiving half of a channel to send trailers.
///
/// Unlike `async_std::sync::channel` the `send` method on this type can only be
/// called once, and cannot be cloned. That's because only a single instance of
/// `Trailers` should be created.
#[must_use = "Futures do nothing unless polled or .awaited"]
#[derive(Debug)]
pub struct Receiver {
receiver: sync::Receiver<Trailers>,
}

impl Receiver {
/// Create a new instance of `Receiver`.
pub(crate) fn new(receiver: sync::Receiver<Trailers>) -> Self {
Self { receiver }
}
}

impl Future for Receiver {
type Output = Option<Trailers>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.receiver).poll_next(cx)
}
}