1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-26 11:31:09 +00:00

fix actix_http::Error conversion. (#2449)

This commit is contained in:
fakeshadow 2021-11-17 21:13:05 +08:00 committed by GitHub
parent 68a3acb9c2
commit 168a7284d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 45 deletions

View file

@ -139,56 +139,56 @@ impl<S: fmt::Debug> fmt::Debug for AnyBody<S> {
}
}
impl From<&'static str> for AnyBody {
fn from(string: &'static str) -> AnyBody {
AnyBody::Bytes(Bytes::from_static(string.as_ref()))
impl<B> From<&'static str> for AnyBody<B> {
fn from(string: &'static str) -> Self {
Self::Bytes(Bytes::from_static(string.as_ref()))
}
}
impl From<&'static [u8]> for AnyBody {
fn from(bytes: &'static [u8]) -> AnyBody {
AnyBody::Bytes(Bytes::from_static(bytes))
impl<B> From<&'static [u8]> for AnyBody<B> {
fn from(bytes: &'static [u8]) -> Self {
Self::Bytes(Bytes::from_static(bytes))
}
}
impl From<Vec<u8>> for AnyBody {
fn from(vec: Vec<u8>) -> AnyBody {
AnyBody::Bytes(Bytes::from(vec))
impl<B> From<Vec<u8>> for AnyBody<B> {
fn from(vec: Vec<u8>) -> Self {
Self::Bytes(Bytes::from(vec))
}
}
impl From<String> for AnyBody {
fn from(string: String) -> AnyBody {
string.into_bytes().into()
impl<B> From<String> for AnyBody<B> {
fn from(string: String) -> Self {
Self::Bytes(Bytes::from(string))
}
}
impl From<&'_ String> for AnyBody {
fn from(string: &String) -> AnyBody {
AnyBody::Bytes(Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(&string)))
impl<B> From<&'_ String> for AnyBody<B> {
fn from(string: &String) -> Self {
Self::Bytes(Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(&string)))
}
}
impl From<Cow<'_, str>> for AnyBody {
fn from(string: Cow<'_, str>) -> AnyBody {
impl<B> From<Cow<'_, str>> for AnyBody<B> {
fn from(string: Cow<'_, str>) -> Self {
match string {
Cow::Owned(s) => AnyBody::from(s),
Cow::Owned(s) => Self::from(s),
Cow::Borrowed(s) => {
AnyBody::Bytes(Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(s)))
Self::Bytes(Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(s)))
}
}
}
}
impl From<Bytes> for AnyBody {
impl<B> From<Bytes> for AnyBody<B> {
fn from(bytes: Bytes) -> Self {
AnyBody::Bytes(bytes)
Self::Bytes(bytes)
}
}
impl From<BytesMut> for AnyBody {
impl<B> From<BytesMut> for AnyBody<B> {
fn from(bytes: BytesMut) -> Self {
AnyBody::Bytes(bytes.freeze())
Self::Bytes(bytes.freeze())
}
}

View file

@ -86,11 +86,14 @@ mod tests {
}
}
/// AnyBody alias because rustc does not (can not?) infer the default type parameter.
type TestBody = AnyBody;
#[actix_rt::test]
async fn test_static_str() {
assert_eq!(AnyBody::from("").size(), BodySize::Sized(0));
assert_eq!(AnyBody::from("test").size(), BodySize::Sized(4));
assert_eq!(AnyBody::from("test").get_ref(), b"test");
assert_eq!(TestBody::from("").size(), BodySize::Sized(0));
assert_eq!(TestBody::from("test").size(), BodySize::Sized(4));
assert_eq!(TestBody::from("test").get_ref(), b"test");
assert_eq!("test".size(), BodySize::Sized(4));
assert_eq!(
@ -104,14 +107,14 @@ mod tests {
#[actix_rt::test]
async fn test_static_bytes() {
assert_eq!(AnyBody::from(b"test".as_ref()).size(), BodySize::Sized(4));
assert_eq!(AnyBody::from(b"test".as_ref()).get_ref(), b"test");
assert_eq!(TestBody::from(b"test".as_ref()).size(), BodySize::Sized(4));
assert_eq!(TestBody::from(b"test".as_ref()).get_ref(), b"test");
assert_eq!(
AnyBody::copy_from_slice(b"test".as_ref()).size(),
TestBody::copy_from_slice(b"test".as_ref()).size(),
BodySize::Sized(4)
);
assert_eq!(
AnyBody::copy_from_slice(b"test".as_ref()).get_ref(),
TestBody::copy_from_slice(b"test".as_ref()).get_ref(),
b"test"
);
let sb = Bytes::from(&b"test"[..]);
@ -126,8 +129,8 @@ mod tests {
#[actix_rt::test]
async fn test_vec() {
assert_eq!(AnyBody::from(Vec::from("test")).size(), BodySize::Sized(4));
assert_eq!(AnyBody::from(Vec::from("test")).get_ref(), b"test");
assert_eq!(TestBody::from(Vec::from("test")).size(), BodySize::Sized(4));
assert_eq!(TestBody::from(Vec::from("test")).get_ref(), b"test");
let test_vec = Vec::from("test");
pin!(test_vec);
@ -144,8 +147,8 @@ mod tests {
#[actix_rt::test]
async fn test_bytes() {
let b = Bytes::from("test");
assert_eq!(AnyBody::from(b.clone()).size(), BodySize::Sized(4));
assert_eq!(AnyBody::from(b.clone()).get_ref(), b"test");
assert_eq!(TestBody::from(b.clone()).size(), BodySize::Sized(4));
assert_eq!(TestBody::from(b.clone()).get_ref(), b"test");
pin!(b);
assert_eq!(b.size(), BodySize::Sized(4));
@ -158,8 +161,8 @@ mod tests {
#[actix_rt::test]
async fn test_bytes_mut() {
let b = BytesMut::from("test");
assert_eq!(AnyBody::from(b.clone()).size(), BodySize::Sized(4));
assert_eq!(AnyBody::from(b.clone()).get_ref(), b"test");
assert_eq!(TestBody::from(b.clone()).size(), BodySize::Sized(4));
assert_eq!(TestBody::from(b.clone()).get_ref(), b"test");
pin!(b);
assert_eq!(b.size(), BodySize::Sized(4));
@ -172,10 +175,10 @@ mod tests {
#[actix_rt::test]
async fn test_string() {
let b = "test".to_owned();
assert_eq!(AnyBody::from(b.clone()).size(), BodySize::Sized(4));
assert_eq!(AnyBody::from(b.clone()).get_ref(), b"test");
assert_eq!(AnyBody::from(&b).size(), BodySize::Sized(4));
assert_eq!(AnyBody::from(&b).get_ref(), b"test");
assert_eq!(TestBody::from(b.clone()).size(), BodySize::Sized(4));
assert_eq!(TestBody::from(b.clone()).get_ref(), b"test");
assert_eq!(TestBody::from(&b).size(), BodySize::Sized(4));
assert_eq!(TestBody::from(&b).get_ref(), b"test");
pin!(b);
assert_eq!(b.size(), BodySize::Sized(4));
@ -216,22 +219,22 @@ mod tests {
#[actix_rt::test]
async fn test_body_debug() {
assert!(format!("{:?}", AnyBody::<BoxBody>::None).contains("Body::None"));
assert!(format!("{:?}", AnyBody::from(Bytes::from_static(b"1"))).contains('1'));
assert!(format!("{:?}", TestBody::None).contains("Body::None"));
assert!(format!("{:?}", TestBody::from(Bytes::from_static(b"1"))).contains('1'));
}
#[actix_rt::test]
async fn test_serde_json() {
use serde_json::{json, Value};
assert_eq!(
AnyBody::from(
TestBody::from(
serde_json::to_vec(&Value::String("test".to_owned())).unwrap()
)
.size(),
BodySize::Sized(6)
);
assert_eq!(
AnyBody::from(
TestBody::from(
serde_json::to_vec(&json!({"test-key":"test-value"})).unwrap()
)
.size(),

View file

@ -66,7 +66,7 @@ impl Error {
}
}
impl From<Error> for Response<AnyBody> {
impl<B> From<Error> for Response<AnyBody<B>> {
fn from(err: Error) -> Self {
let status_code = match err.inner.kind {
Kind::Parse => StatusCode::BAD_REQUEST,