[−][src]Trait rocket_http::uri::FromUriParam
Conversion trait for parameters used in uri!
invocations.
Overview
In addition to implementing UriDisplay
, to use a custom type in a uri!
expression, the FromUriParam
trait must be implemented. The UriDisplay
derive automatically generates identity implementations of FromUriParam
,
so in the majority of cases, as with UriDisplay
, this trait is never
implemented manually.
In the rare case that UriDisplay
is implemented manually, this trait, too,
must be implemented explicitly. In the majority of cases, implementation can
be automated. Rocket provides the impl_from_uri_param_identity
macro to
generate the identity implementations automatically. For a type T
, these
are:
impl<P: UriPart> FromUriParam<P, T> for T
impl<'x, P: UriPart> FromUriParam<P, &'x T> for T
impl<'x, P: UriPart> FromUriParam<P, &'x mut T> for T
See impl_from_uri_param_identity
for usage details.
Code Generation
This trait is invoked once per expression passed into a uri!
invocation.
In particular, for a route URI parameter of type T
and a user-supplied
expression of type S
, <T as FromUriParam<S>>::from_uri_param
is
invoked. The returned value is used in place of the user's value and
rendered using its UriDisplay
implementation.
This trait allows types that differ from the route URI parameter's types to
be used in their place at no cost. For instance, the following
implementation, provided by Rocket, allows an &str
to be used in a uri!
invocation for route URI parameters declared as String
:
impl<'a, P: UriPart> FromUriParam<P, &'a str> for String { type Target = &'a str; }
Because the FromUriParam::Target
type is the same as the input type, the
conversion is a no-op and free of cost, allowing an &str
to be used in
place of a String
without penalty. A similar no-op conversion exists for
&RawStr
:
impl<'a, 'b, P: UriPart> FromUriParam<P, &'a str> for &'b RawStr { type Target = &'a str; }
Provided Implementations
The following types have identity implementations:
String
,i8
,i16
,i32
,i64
,i128
,isize
,u8
,u16
,u32
,u64
,u128
,usize
,f32
,f64
,bool
,IpAddr
,Ipv4Addr
,Ipv6Addr
,&str
,&RawStr
,Cow<str>
The following conversions are implemented:
&str
toString
&str
toRawStr
String
to&str
String
toRawStr
The following types have identity implementations only in Path
:
&Path
,PathBuf
The following conversions are implemented only in Path
:
&str
to&Path
&str
toPathBuf
PathBuf
to&Path
See Foreign Impls for all provided implementations.
Implementing
This trait should only be implemented when you'd like to allow a type
different from the route's declared type to be used in its place in a uri!
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<P, T> for S
where P
is Path
for conversions valid in the path part of a
URI, Uri
for conversions valid in the query part of a URI, or P: UriPart
when a conversion is valid in either case.
This is typically only warranted for owned-value types with corresponding
reference types: String
and &str
, for instance. In this case, it's
desirable to allow an &str
to be used in place of a String
.
When implementing FromUriParam
, be aware that Rocket will use the
UriDisplay
implementation of FromUriParam::Target
, not of the
source type. Incorrect implementations can result in creating unsafe URIs.
Example
The following example implements FromUriParam<Query, (&str, &str)>
for a
User
type. The implementation allows an (&str, &str)
type to be used in
a uri!
invocation where a User
type is expected in the query part of the
URI.
use std::fmt; use rocket::http::RawStr; use rocket::http::uri::{Formatter, UriDisplay, FromUriParam, Query}; #[derive(FromForm)] struct User<'a> { name: &'a RawStr, nickname: String, } impl<'a> UriDisplay<Query> for User<'a> { fn fmt(&self, f: &mut Formatter<Query>) -> fmt::Result { f.write_named_value("name", &self.name)?; f.write_named_value("nickname", &self.nickname) } } impl<'a, 'b> FromUriParam<Query, (&'a str, &'b str)> for User<'a> { type Target = User<'a>; fn from_uri_param((name, nickname): (&'a str, &'b str)) -> User<'a> { User { name: name.into(), nickname: nickname.to_string() } } }
With these implementations, the following typechecks:
use rocket::http::RawStr; use rocket::request::Form; #[post("/<name>?<user..>")] fn some_route(name: &RawStr, user: Form<User>) { /* .. */ } let uri = uri!(some_route: name = "hey", user = ("Robert Mike", "Bob")); assert_eq!(uri.path(), "/hey"); assert_eq!(uri.query(), Some("name=Robert%20Mike&nickname=Bob"));
Associated Types
type Target: UriDisplay<P>
The resulting type of this conversion.
Required methods
fn from_uri_param(param: T) -> Self::Target
Converts a value of type T
into a value of type Self::Target
. The
resulting value of type Self::Target
will be rendered into a URI using
its UriDisplay
implementation.
Implementations on Foreign Types
impl<P: UriPart> FromUriParam<P, String> for String
[src]
impl<P: UriPart> FromUriParam<P, String> for String
impl<'x, P: UriPart> FromUriParam<P, &'x String> for String
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x String> for String
impl<'x, P: UriPart> FromUriParam<P, &'x mut String> for String
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x mut String> for String
impl<P: UriPart> FromUriParam<P, i8> for i8
[src]
impl<P: UriPart> FromUriParam<P, i8> for i8
impl<'x, P: UriPart> FromUriParam<P, &'x i8> for i8
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x i8> for i8
impl<'x, P: UriPart> FromUriParam<P, &'x mut i8> for i8
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x mut i8> for i8
impl<P: UriPart> FromUriParam<P, i16> for i16
[src]
impl<P: UriPart> FromUriParam<P, i16> for i16
impl<'x, P: UriPart> FromUriParam<P, &'x i16> for i16
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x i16> for i16
impl<'x, P: UriPart> FromUriParam<P, &'x mut i16> for i16
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x mut i16> for i16
impl<P: UriPart> FromUriParam<P, i32> for i32
[src]
impl<P: UriPart> FromUriParam<P, i32> for i32
impl<'x, P: UriPart> FromUriParam<P, &'x i32> for i32
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x i32> for i32
impl<'x, P: UriPart> FromUriParam<P, &'x mut i32> for i32
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x mut i32> for i32
impl<P: UriPart> FromUriParam<P, i64> for i64
[src]
impl<P: UriPart> FromUriParam<P, i64> for i64
impl<'x, P: UriPart> FromUriParam<P, &'x i64> for i64
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x i64> for i64
impl<'x, P: UriPart> FromUriParam<P, &'x mut i64> for i64
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x mut i64> for i64
impl<P: UriPart> FromUriParam<P, i128> for i128
[src]
impl<P: UriPart> FromUriParam<P, i128> for i128
impl<'x, P: UriPart> FromUriParam<P, &'x i128> for i128
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x i128> for i128
impl<'x, P: UriPart> FromUriParam<P, &'x mut i128> for i128
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x mut i128> for i128
impl<P: UriPart> FromUriParam<P, isize> for isize
[src]
impl<P: UriPart> FromUriParam<P, isize> for isize
impl<'x, P: UriPart> FromUriParam<P, &'x isize> for isize
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x isize> for isize
impl<'x, P: UriPart> FromUriParam<P, &'x mut isize> for isize
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x mut isize> for isize
impl<P: UriPart> FromUriParam<P, u8> for u8
[src]
impl<P: UriPart> FromUriParam<P, u8> for u8
impl<'x, P: UriPart> FromUriParam<P, &'x u8> for u8
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x u8> for u8
impl<'x, P: UriPart> FromUriParam<P, &'x mut u8> for u8
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x mut u8> for u8
impl<P: UriPart> FromUriParam<P, u16> for u16
[src]
impl<P: UriPart> FromUriParam<P, u16> for u16
impl<'x, P: UriPart> FromUriParam<P, &'x u16> for u16
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x u16> for u16
impl<'x, P: UriPart> FromUriParam<P, &'x mut u16> for u16
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x mut u16> for u16
impl<P: UriPart> FromUriParam<P, u32> for u32
[src]
impl<P: UriPart> FromUriParam<P, u32> for u32
impl<'x, P: UriPart> FromUriParam<P, &'x u32> for u32
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x u32> for u32
impl<'x, P: UriPart> FromUriParam<P, &'x mut u32> for u32
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x mut u32> for u32
impl<P: UriPart> FromUriParam<P, u64> for u64
[src]
impl<P: UriPart> FromUriParam<P, u64> for u64
impl<'x, P: UriPart> FromUriParam<P, &'x u64> for u64
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x u64> for u64
impl<'x, P: UriPart> FromUriParam<P, &'x mut u64> for u64
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x mut u64> for u64
impl<P: UriPart> FromUriParam<P, u128> for u128
[src]
impl<P: UriPart> FromUriParam<P, u128> for u128
impl<'x, P: UriPart> FromUriParam<P, &'x u128> for u128
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x u128> for u128
impl<'x, P: UriPart> FromUriParam<P, &'x mut u128> for u128
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x mut u128> for u128
impl<P: UriPart> FromUriParam<P, usize> for usize
[src]
impl<P: UriPart> FromUriParam<P, usize> for usize
impl<'x, P: UriPart> FromUriParam<P, &'x usize> for usize
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x usize> for usize
impl<'x, P: UriPart> FromUriParam<P, &'x mut usize> for usize
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x mut usize> for usize
impl<P: UriPart> FromUriParam<P, f32> for f32
[src]
impl<P: UriPart> FromUriParam<P, f32> for f32
impl<'x, P: UriPart> FromUriParam<P, &'x f32> for f32
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x f32> for f32
impl<'x, P: UriPart> FromUriParam<P, &'x mut f32> for f32
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x mut f32> for f32
impl<P: UriPart> FromUriParam<P, f64> for f64
[src]
impl<P: UriPart> FromUriParam<P, f64> for f64
impl<'x, P: UriPart> FromUriParam<P, &'x f64> for f64
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x f64> for f64
impl<'x, P: UriPart> FromUriParam<P, &'x mut f64> for f64
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x mut f64> for f64
impl<P: UriPart> FromUriParam<P, bool> for bool
[src]
impl<P: UriPart> FromUriParam<P, bool> for bool
impl<'x, P: UriPart> FromUriParam<P, &'x bool> for bool
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x bool> for bool
impl<'x, P: UriPart> FromUriParam<P, &'x mut bool> for bool
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x mut bool> for bool
impl<P: UriPart> FromUriParam<P, IpAddr> for IpAddr
[src]
impl<P: UriPart> FromUriParam<P, IpAddr> for IpAddr
impl<'x, P: UriPart> FromUriParam<P, &'x IpAddr> for IpAddr
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x IpAddr> for IpAddr
impl<'x, P: UriPart> FromUriParam<P, &'x mut IpAddr> for IpAddr
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x mut IpAddr> for IpAddr
impl<P: UriPart> FromUriParam<P, Ipv4Addr> for Ipv4Addr
[src]
impl<P: UriPart> FromUriParam<P, Ipv4Addr> for Ipv4Addr
impl<'x, P: UriPart> FromUriParam<P, &'x Ipv4Addr> for Ipv4Addr
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x Ipv4Addr> for Ipv4Addr
impl<'x, P: UriPart> FromUriParam<P, &'x mut Ipv4Addr> for Ipv4Addr
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x mut Ipv4Addr> for Ipv4Addr
impl<P: UriPart> FromUriParam<P, Ipv6Addr> for Ipv6Addr
[src]
impl<P: UriPart> FromUriParam<P, Ipv6Addr> for Ipv6Addr
impl<'x, P: UriPart> FromUriParam<P, &'x Ipv6Addr> for Ipv6Addr
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x Ipv6Addr> for Ipv6Addr
impl<'x, P: UriPart> FromUriParam<P, &'x mut Ipv6Addr> for Ipv6Addr
[src]
impl<'x, P: UriPart> FromUriParam<P, &'x mut Ipv6Addr> for Ipv6Addr
impl<'a, P: UriPart> FromUriParam<P, &'a str> for &'a str
[src]
impl<'a, P: UriPart> FromUriParam<P, &'a str> for &'a str
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x &'a str> for &'a str
[src]
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x &'a str> for &'a str
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x mut &'a str> for &'a str
[src]
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x mut &'a str> for &'a str
impl<'a, P: UriPart> FromUriParam<P, Cow<'a, str>> for Cow<'a, str>
[src]
impl<'a, P: UriPart> FromUriParam<P, Cow<'a, str>> for Cow<'a, str>
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x Cow<'a, str>> for Cow<'a, str>
[src]
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x Cow<'a, str>> for Cow<'a, str>
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x mut Cow<'a, str>> for Cow<'a, str>
[src]
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x mut Cow<'a, str>> for Cow<'a, str>
type Target = &'x mut Cow<'a, str>
fn from_uri_param(param: &'x mut Cow<'a, str>) -> &'x mut Cow<'a, str>
[src]
fn from_uri_param(param: &'x mut Cow<'a, str>) -> &'x mut Cow<'a, str>
impl<'a, P: UriPart> FromUriParam<P, &'a str> for String
[src]
impl<'a, P: UriPart> FromUriParam<P, &'a str> for String
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x &'a str> for String
[src]
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x &'a str> for String
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x mut &'a str> for String
[src]
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x mut &'a str> for String
impl<'a, P: UriPart> FromUriParam<P, String> for &'a str
[src]
impl<'a, P: UriPart> FromUriParam<P, String> for &'a str
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x String> for &'a str
[src]
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x String> for &'a str
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x mut String> for &'a str
[src]
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x mut String> for &'a str
impl<'a> FromUriParam<Path, &'a Path> for &'a Path
[src]
impl<'a> FromUriParam<Path, &'a Path> for &'a Path
impl<'x, 'a> FromUriParam<Path, &'x &'a Path> for &'a Path
[src]
impl<'x, 'a> FromUriParam<Path, &'x &'a Path> for &'a Path
impl<'x, 'a> FromUriParam<Path, &'x mut &'a Path> for &'a Path
[src]
impl<'x, 'a> FromUriParam<Path, &'x mut &'a Path> for &'a Path
impl FromUriParam<Path, PathBuf> for PathBuf
[src]
impl FromUriParam<Path, PathBuf> for PathBuf
impl<'x> FromUriParam<Path, &'x PathBuf> for PathBuf
[src]
impl<'x> FromUriParam<Path, &'x PathBuf> for PathBuf
impl<'x> FromUriParam<Path, &'x mut PathBuf> for PathBuf
[src]
impl<'x> FromUriParam<Path, &'x mut PathBuf> for PathBuf
impl<'a> FromUriParam<Path, &'a Path> for PathBuf
[src]
impl<'a> FromUriParam<Path, &'a Path> for PathBuf
impl<'x, 'a> FromUriParam<Path, &'x &'a Path> for PathBuf
[src]
impl<'x, 'a> FromUriParam<Path, &'x &'a Path> for PathBuf
impl<'x, 'a> FromUriParam<Path, &'x mut &'a Path> for PathBuf
[src]
impl<'x, 'a> FromUriParam<Path, &'x mut &'a Path> for PathBuf
impl<'a> FromUriParam<Path, PathBuf> for &'a Path
[src]
impl<'a> FromUriParam<Path, PathBuf> for &'a Path
impl<'x, 'a> FromUriParam<Path, &'x PathBuf> for &'a Path
[src]
impl<'x, 'a> FromUriParam<Path, &'x PathBuf> for &'a Path
impl<'x, 'a> FromUriParam<Path, &'x mut PathBuf> for &'a Path
[src]
impl<'x, 'a> FromUriParam<Path, &'x mut PathBuf> for &'a Path
impl<'a> FromUriParam<Path, &'a str> for PathBuf
[src]
impl<'a> FromUriParam<Path, &'a str> for PathBuf
A no cost conversion allowing an &str
to be used in place of a PathBuf
.
impl<'a, 'b> FromUriParam<Path, &'a &'b str> for PathBuf
[src]
impl<'a, 'b> FromUriParam<Path, &'a &'b str> for PathBuf
A no cost conversion allowing an &&str
to be used in place of a PathBuf
.
impl<P: UriPart, A, T: FromUriParam<P, A>> FromUriParam<P, A> for Option<T>
[src]
impl<P: UriPart, A, T: FromUriParam<P, A>> FromUriParam<P, A> for Option<T>
A no cost conversion allowing any T
to be used in place of an Option<T>
.
type Target = T::Target
fn from_uri_param(param: A) -> Self::Target
[src]
fn from_uri_param(param: A) -> Self::Target
impl<P: UriPart, A, E, T: FromUriParam<P, A>> FromUriParam<P, A> for Result<T, E>
[src]
impl<P: UriPart, A, E, T: FromUriParam<P, A>> FromUriParam<P, A> for Result<T, E>
A no cost conversion allowing T
to be used in place of an Result<T, E>
.
type Target = T::Target
fn from_uri_param(param: A) -> Self::Target
[src]
fn from_uri_param(param: A) -> Self::Target
Implementors
impl<'a, 'b, P: UriPart> FromUriParam<P, &'a str> for &'b RawStr
[src]
impl<'a, 'b, P: UriPart> FromUriParam<P, &'a str> for &'b RawStr
impl<'a, P: UriPart> FromUriParam<P, &'a RawStr> for &'a RawStr
[src]
impl<'a, P: UriPart> FromUriParam<P, &'a RawStr> for &'a RawStr
impl<'a, P: UriPart> FromUriParam<P, String> for &'a RawStr
[src]
impl<'a, P: UriPart> FromUriParam<P, String> for &'a RawStr
impl<'x, 'a, 'b, P: UriPart> FromUriParam<P, &'x &'a str> for &'b RawStr
[src]
impl<'x, 'a, 'b, P: UriPart> FromUriParam<P, &'x &'a str> for &'b RawStr
impl<'x, 'a, 'b, P: UriPart> FromUriParam<P, &'x mut &'a str> for &'b RawStr
[src]
impl<'x, 'a, 'b, P: UriPart> FromUriParam<P, &'x mut &'a str> for &'b RawStr
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x &'a RawStr> for &'a RawStr
[src]
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x &'a RawStr> for &'a RawStr
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x String> for &'a RawStr
[src]
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x String> for &'a RawStr
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x mut &'a RawStr> for &'a RawStr
[src]
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x mut &'a RawStr> for &'a RawStr
type Target = &'x mut &'a RawStr
fn from_uri_param(param: &'x mut &'a RawStr) -> &'x mut &'a RawStr
[src]
fn from_uri_param(param: &'x mut &'a RawStr) -> &'x mut &'a RawStr
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x mut String> for &'a RawStr
[src]
impl<'x, 'a, P: UriPart> FromUriParam<P, &'x mut String> for &'a RawStr