From 9e4e46fd12d565a951f40a194b2ce23cda2e69a0 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Mon, 11 Sep 2017 02:07:06 -0700 Subject: [PATCH] Implement 'FromParam' for 'Cow'. --- lib/src/request/param.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/src/request/param.rs b/lib/src/request/param.rs index 7a002506..7f631660 100644 --- a/lib/src/request/param.rs +++ b/lib/src/request/param.rs @@ -1,7 +1,7 @@ use std::str::FromStr; -use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6, SocketAddr}; use std::path::PathBuf; use std::fmt::Debug; +use std::borrow::Cow; use http::uri::{URI, Segments, SegmentError}; use http::RawStr; @@ -214,22 +214,34 @@ impl<'a> FromParam<'a> for String { } } +impl<'a> FromParam<'a> for Cow<'a, str> { + type Error = &'a RawStr; + + #[inline(always)] + fn from_param(param: &'a RawStr) -> Result, Self::Error> { + param.percent_decode().map_err(|_| param) + } +} + macro_rules! impl_with_fromstr { - ($($T:ident),+) => ($( + ($($T:ty),+) => ($( impl<'a> FromParam<'a> for $T { type Error = &'a RawStr; #[inline(always)] fn from_param(param: &'a RawStr) -> Result { - $T::from_str(param.as_str()).map_err(|_| param) + <$T as FromStr>::from_str(param.as_str()).map_err(|_| param) } } )+) } -impl_with_fromstr!(f32, f64, isize, i8, i16, i32, i64, usize, u8, u16, u32, u64, - bool, IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6, - SocketAddr); +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6, SocketAddr}; + +impl_with_fromstr! { + i8, i16, i32, i64, isize, u8, u16, u32, u64, usize, f32, f64, bool, + IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6, SocketAddr +} impl<'a, T: FromParam<'a>> FromParam<'a> for Result { type Error = !;