Implement 'UriDisplay' for 'str', 'RawStr' directly.

This commit is contained in:
Sergio Benitez 2018-04-11 10:46:15 -07:00
parent e9ec5b2d13
commit 226990584b
2 changed files with 16 additions and 10 deletions

View File

@ -32,13 +32,19 @@ use http::uri::UriDisplay;
/// ///
/// # Implementing /// # Implementing
/// ///
/// Because Rocket provides a blanket implementation for all types, this trait /// Rocket provides a blanket implementation for all types that implement
/// typically does not need to be implemented. This trait should only be /// [`UriDisplay`]. As such, this trait typically does not need to be implemented.
/// implemented when you'd like to allow a type different from the route's /// Instead, implement [`UriDisplay`].
/// declared type to be used in its place in a `uri!` invocation. This is ///
/// typically only warranted for owned-value types with corresponding reference /// This trait should only be implemented when you'd like to allow a type
/// types: `String` and `&str`, for instance. In this case, it's desireable to /// different from the route's declared type to be used in its place in a `uri!`
/// allow an `&str` to be used in place of a `String`. /// invocation. For instance, if the route has a type of `T` and you'd like to
/// use a type of `S` in a `uri!` invocation, you'd implement `FromUriParam<T>
/// for S`.
///
/// This is typically only warranted for owned-value types with
/// corresponding reference types: `String` and `&str`, for instance. In this
/// case, it's desireable to allow an `&str` to be used in place of a `String`.
/// ///
/// When implementing `FromUriParam`, be aware that Rocket will use the /// When implementing `FromUriParam`, be aware that Rocket will use the
/// [`UriDisplay`] implementation of `Target`, _not_ of the source type. /// [`UriDisplay`] implementation of `Target`, _not_ of the source type.

View File

@ -165,7 +165,7 @@ impl<'a> fmt::Display for &'a UriDisplay {
} }
/// Percent-encodes the raw string. /// Percent-encodes the raw string.
impl<'a> UriDisplay for &'a RawStr { impl UriDisplay for RawStr {
#[inline(always)] #[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", Uri::percent_encode((*self).as_str())) write!(f, "{}", Uri::percent_encode((*self).as_str()))
@ -173,7 +173,7 @@ impl<'a> UriDisplay for &'a RawStr {
} }
/// Percent-encodes the raw string. /// Percent-encodes the raw string.
impl<'a> UriDisplay for &'a str { impl UriDisplay for str {
#[inline(always)] #[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", Uri::percent_encode(self)) write!(f, "{}", Uri::percent_encode(self))
@ -207,7 +207,7 @@ impl UriDisplay for PathBuf {
} }
/// Percent-encodes each segment in the path. /// Percent-encodes each segment in the path.
impl<'a> UriDisplay for &'a Path { impl UriDisplay for Path {
#[inline(always)] #[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let string = self.to_string_lossy(); let string = self.to_string_lossy();