Avoid double-boxing bodies where possible (#569)

* Avoid double-boxing bodies where possible

* Extract `try_downcast` to `util` module
This commit is contained in:
Kai Jewson
2021-11-27 10:20:31 +01:00
committed by GitHub
parent 96b353b556
commit decdd4c948
3 changed files with 22 additions and 17 deletions
+1 -15
View File
@@ -8,7 +8,7 @@ use crate::{
MatchedPath, OriginalUri,
},
routing::strip_prefix::StripPrefix,
util::{ByteStr, PercentDecodedByteStr},
util::{try_downcast, ByteStr, PercentDecodedByteStr},
BoxError,
};
use bytes::Bytes;
@@ -657,20 +657,6 @@ impl<B, E> Fallback<B, E> {
}
}
fn try_downcast<T, K>(k: K) -> Result<T, K>
where
T: 'static,
K: Send + 'static,
{
use std::any::Any;
let k = Box::new(k) as Box<dyn Any + Send + 'static>;
match k.downcast() {
Ok(t) => Ok(*t),
Err(other) => Err(*other.downcast().unwrap()),
}
}
enum Endpoint<B> {
MethodRouter(MethodRouter<B>),
Route(Route<B>),