Skip to content

Add Full #40

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

Merged
merged 3 commits into from
Apr 6, 2021
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ categories = ["web-programming"]
bytes = "1"
http = "0.2"
pin-project-lite = "0.2"

[dev-dependencies]
tokio = { version = "1", features = ["macros", "rt"] }
151 changes: 151 additions & 0 deletions src/full.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
use crate::{Body, SizeHint};
use bytes::{Buf, Bytes};
use http::HeaderMap;
use pin_project_lite::pin_project;
use std::borrow::Cow;
use std::convert::{Infallible, TryFrom};
use std::pin::Pin;
use std::task::{Context, Poll};

pin_project! {
/// A body that consists of a single chunk.
#[derive(Clone, Copy, Debug)]
pub struct Full<D> {
data: Option<D>,
}
}

impl<D> Full<D>
where
D: Buf,
{
/// Create a new `Full`.
pub fn new(data: D) -> Self {
let data = if data.has_remaining() {
Some(data)
} else {
None
};
Full { data }
}
}

impl<D> Body for Full<D>
where
D: Buf,
{
type Data = D;
type Error = Infallible;

fn poll_data(
mut self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Option<Result<D, Self::Error>>> {
Poll::Ready(self.data.take().map(Ok))
}

fn poll_trailers(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Result<Option<HeaderMap>, Self::Error>> {
Poll::Ready(Ok(None))
}

fn is_end_stream(&self) -> bool {
self.data.is_none()
}

fn size_hint(&self) -> SizeHint {
self.data
.as_ref()
.map(|data| SizeHint::with_exact(u64::try_from(data.remaining()).unwrap()))
.unwrap_or_else(|| SizeHint::with_exact(0))
}
}

impl<D> Default for Full<D>
where
D: Buf,
{
/// Create an empty `Full`.
fn default() -> Self {
Full { data: None }
}
}

impl<D> From<Bytes> for Full<D>
where
D: Buf + From<Bytes>,
{
fn from(bytes: Bytes) -> Self {
Full::new(D::from(bytes))
}
}

impl<D> From<Vec<u8>> for Full<D>
where
D: Buf + From<Vec<u8>>,
{
fn from(vec: Vec<u8>) -> Self {
Full::new(D::from(vec))
}
}

impl<D> From<&'static [u8]> for Full<D>
where
D: Buf + From<&'static [u8]>,
{
fn from(slice: &'static [u8]) -> Self {
Full::new(D::from(slice))
}
}

impl<D, B> From<Cow<'static, B>> for Full<D>
where
D: Buf + From<&'static B> + From<B::Owned>,
B: ToOwned + ?Sized,
{
fn from(cow: Cow<'static, B>) -> Self {
match cow {
Cow::Borrowed(b) => Full::new(D::from(b)),
Cow::Owned(o) => Full::new(D::from(o)),
}
}
}

impl<D> From<String> for Full<D>
where
D: Buf + From<String>,
{
fn from(s: String) -> Self {
Full::new(D::from(s))
}
}

impl<D> From<&'static str> for Full<D>
where
D: Buf + From<&'static str>,
{
fn from(slice: &'static str) -> Self {
Full::new(D::from(slice))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn full_returns_some() {
let mut full = Full::new(&b"hello"[..]);
assert_eq!(full.size_hint().exact(), Some(b"hello".len() as u64));
assert_eq!(full.data().await, Some(Ok(&b"hello"[..])));
assert!(full.data().await.is_none());
}

#[tokio::test]
async fn empty_full_returns_none() {
assert!(Full::<&[u8]>::default().data().await.is_none());
assert!(Full::new(&b""[..]).data().await.is_none());
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
//! [`Body`]: trait.Body.html

mod empty;
mod full;
mod next;
mod size_hint;

pub mod combinators;

pub use self::empty::Empty;
pub use self::full::Full;
pub use self::next::{Data, Trailers};
pub use self::size_hint::SizeHint;

Expand Down