Skip to content

Commit

Permalink
Add as_bytes method to Body (#708)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luro02 authored and seanmonstar committed Nov 11, 2019
1 parent 1ce6731 commit 3b23593
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/async_impl/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ struct WrapStream<S>(S);
struct WrapHyper(hyper::Body);

impl Body {
/// Returns a reference to the internal data of the `Body`.
///
/// `None` is returned, if the underlying data is a stream.
pub fn as_bytes(&self) -> Option<&[u8]> {
match &self.inner {
Inner::Reusable(bytes) => Some(bytes.as_ref()),
Inner::Streaming { .. } => None,
}
}

/// Wrap a futures `Stream` in a box inside `Body`.
///
/// # Example
Expand Down Expand Up @@ -339,3 +349,15 @@ impl HttpBody for WrapHyper {
HttpBody::size_hint(&self.0)
}
}

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

#[test]
fn test_as_bytes() {
let test_data = b"Test body";
let body = Body::from(&test_data[..]);
assert_eq!(body.as_bytes(), Some(&test_data[..]));
}
}

0 comments on commit 3b23593

Please sign in to comment.