remove useless anybody from impls

This commit is contained in:
Rob Ede 2021-12-28 02:32:37 +00:00
parent 36193b0a50
commit b824acd726
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
3 changed files with 9 additions and 95 deletions

View File

@ -1,17 +1,13 @@
use std::{ use std::{
borrow::Cow,
fmt, mem, fmt, mem,
pin::Pin, pin::Pin,
task::{Context, Poll}, task::{Context, Poll},
}; };
use bytes::{Bytes, BytesMut}; use bytes::Bytes;
use futures_core::Stream;
use pin_project_lite::pin_project; use pin_project_lite::pin_project;
use actix_http::body::{BodySize, BodyStream, BoxBody, MessageBody, SizedStream}; use actix_http::body::{BodySize, BoxBody, MessageBody};
use crate::BoxError;
pin_project! { pin_project! {
/// Represents various types of HTTP message body. /// Represents various types of HTTP message body.
@ -160,91 +156,6 @@ impl<S: fmt::Debug> fmt::Debug for AnyBody<S> {
} }
} }
impl<B> From<&'static str> for AnyBody<B> {
fn from(string: &'static str) -> Self {
Self::Bytes {
body: Bytes::from_static(string.as_ref()),
}
}
}
impl<B> From<&'static [u8]> for AnyBody<B> {
fn from(bytes: &'static [u8]) -> Self {
Self::Bytes {
body: Bytes::from_static(bytes),
}
}
}
impl<B> From<Vec<u8>> for AnyBody<B> {
fn from(vec: Vec<u8>) -> Self {
Self::Bytes {
body: Bytes::from(vec),
}
}
}
impl<B> From<String> for AnyBody<B> {
fn from(string: String) -> Self {
Self::Bytes {
body: Bytes::from(string),
}
}
}
impl<B> From<&'_ String> for AnyBody<B> {
fn from(string: &String) -> Self {
Self::Bytes {
body: Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(&string)),
}
}
}
impl<B> From<Cow<'_, str>> for AnyBody<B> {
fn from(string: Cow<'_, str>) -> Self {
match string {
Cow::Owned(s) => Self::from(s),
Cow::Borrowed(s) => Self::Bytes {
body: Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(s)),
},
}
}
}
impl<B> From<Bytes> for AnyBody<B> {
fn from(bytes: Bytes) -> Self {
Self::Bytes { body: bytes }
}
}
impl<B> From<BytesMut> for AnyBody<B> {
fn from(bytes: BytesMut) -> Self {
Self::Bytes {
body: bytes.freeze(),
}
}
}
impl<S, E> From<SizedStream<S>> for AnyBody
where
S: Stream<Item = Result<Bytes, E>> + 'static,
E: Into<BoxError> + 'static,
{
fn from(stream: SizedStream<S>) -> Self {
AnyBody::new_boxed(stream)
}
}
impl<S, E> From<BodyStream<S>> for AnyBody
where
S: Stream<Item = Result<Bytes, E>> + 'static,
E: Into<BoxError> + 'static,
{
fn from(stream: BodyStream<S>) -> Self {
AnyBody::new_boxed(stream)
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::marker::PhantomPinned; use std::marker::PhantomPinned;

View File

@ -190,7 +190,9 @@ where
let body_new = if is_redirect { let body_new = if is_redirect {
// try to reuse body // try to reuse body
match body { match body {
Some(ref bytes) => AnyBody::from(bytes.clone()), Some(ref bytes) => AnyBody::Bytes {
body: bytes.clone(),
},
// TODO: should this be AnyBody::Empty or AnyBody::None. // TODO: should this be AnyBody::Empty or AnyBody::None.
_ => AnyBody::empty(), _ => AnyBody::empty(),
} }

View File

@ -7,8 +7,8 @@ use std::{
}; };
use actix_http::{ use actix_http::{
error::PayloadError, header, header::HeaderMap, BoxedPayloadStream, Extensions, error::PayloadError, header::HeaderMap, BoxedPayloadStream, Extensions, HttpMessage,
HttpMessage, Payload, ResponseHead, StatusCode, Version, Payload, ResponseHead, StatusCode, Version,
}; };
use actix_rt::time::{sleep, Sleep}; use actix_rt::time::{sleep, Sleep};
use bytes::Bytes; use bytes::Bytes;
@ -119,12 +119,13 @@ impl<S> ClientResponse<S> {
if self.extensions().get::<Cookies>().is_none() { if self.extensions().get::<Cookies>().is_none() {
let mut cookies = Vec::new(); let mut cookies = Vec::new();
for hdr in self.headers().get_all(&header::SET_COOKIE) { for hdr in self.headers().get_all(&actix_http::header::SET_COOKIE) {
let s = std::str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?; let s = std::str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?;
cookies.push(Cookie::parse_encoded(s)?.into_owned()); cookies.push(Cookie::parse_encoded(s)?.into_owned());
} }
self.extensions_mut().insert(Cookies(cookies)); self.extensions_mut().insert(Cookies(cookies));
} }
Ok(Ref::map(self.extensions(), |ext| { Ok(Ref::map(self.extensions(), |ext| {
&ext.get::<Cookies>().unwrap().0 &ext.get::<Cookies>().unwrap().0
})) }))