1 use pin_project_lite::pin_project;
2 use std::{ops::Deref, sync::Arc};
3 
4 #[derive(Clone, Debug, PartialEq, Eq, Hash)]
5 pub(crate) struct PercentDecodedStr(Arc<str>);
6 
7 impl PercentDecodedStr {
new<S>(s: S) -> Option<Self> where S: AsRef<str>,8     pub(crate) fn new<S>(s: S) -> Option<Self>
9     where
10         S: AsRef<str>,
11     {
12         percent_encoding::percent_decode(s.as_ref().as_bytes())
13             .decode_utf8()
14             .ok()
15             .map(|decoded| Self(decoded.as_ref().into()))
16     }
17 
as_str(&self) -> &str18     pub(crate) fn as_str(&self) -> &str {
19         &self.0
20     }
21 
into_inner(self) -> Arc<str>22     pub(crate) fn into_inner(self) -> Arc<str> {
23         self.0
24     }
25 }
26 
27 impl Deref for PercentDecodedStr {
28     type Target = str;
29 
30     #[inline]
deref(&self) -> &Self::Target31     fn deref(&self) -> &Self::Target {
32         self.as_str()
33     }
34 }
35 
36 pin_project! {
37     #[project = EitherProj]
38     pub(crate) enum Either<A, B> {
39         A { #[pin] inner: A },
40         B { #[pin] inner: B },
41     }
42 }
43 
try_downcast<T, K>(k: K) -> Result<T, K> where T: 'static, K: Send + 'static,44 pub(crate) fn try_downcast<T, K>(k: K) -> Result<T, K>
45 where
46     T: 'static,
47     K: Send + 'static,
48 {
49     let mut k = Some(k);
50     if let Some(k) = <dyn std::any::Any>::downcast_mut::<Option<T>>(&mut k) {
51         Ok(k.take().unwrap())
52     } else {
53         Err(k.unwrap())
54     }
55 }
56 
57 #[test]
test_try_downcast()58 fn test_try_downcast() {
59     assert_eq!(try_downcast::<i32, _>(5_u32), Err(5_u32));
60     assert_eq!(try_downcast::<i32, _>(5_i32), Ok(5_i32));
61 }
62