From c100a9212735928ed5ada2a38754a21247c86677 Mon Sep 17 00:00:00 2001 From: Dmitry Murzin Date: Thu, 20 Jun 2019 03:35:56 +0300 Subject: [PATCH] Implement 'FromParam' for 'NonZero*' types. --- core/lib/src/request/form/from_form_value.rs | 26 ++++++++++++++------ core/lib/src/request/param.rs | 17 ++++++++++--- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/core/lib/src/request/form/from_form_value.rs b/core/lib/src/request/form/from_form_value.rs index 7802eaf6..5c6e1d89 100644 --- a/core/lib/src/request/form/from_form_value.rs +++ b/core/lib/src/request/form/from_form_value.rs @@ -1,5 +1,9 @@ -use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6, SocketAddr}; use std::str::FromStr; +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6, SocketAddr}; +use std::num::{ + NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize, + NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize, +}; use crate::http::RawStr; @@ -77,9 +81,14 @@ use crate::http::RawStr; /// Rocket implements `FromFormValue` for many standard library types. Their /// behavior is documented here. /// -/// * **f32, f64, isize, i8, i16, i32, i64, i128, usize, u8, u16, u32, u64, -/// u128, IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6, -/// SocketAddr** +/// * +/// * Primitive types: **f32, f64, isize, i8, i16, i32, i64, i128, +/// usize, u8, u16, u32, u64, u128** +/// * `IpAddr` and `SocketAddr` types: **IpAddr, Ipv4Addr, Ipv6Addr, +/// SocketAddrV4, SocketAddrV6, SocketAddr** +/// * `NonZero*` types: **NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, +/// NonZeroI128, NonZeroIsize, NonZeroU8, NonZeroU16, NonZeroU32, +/// NonZeroU64, NonZeroU128, NonZeroUsize** /// /// A value is validated successfully if the `from_str` method for the given /// type returns successfully. Otherwise, the raw form value is returned as @@ -231,9 +240,12 @@ macro_rules! impl_with_fromstr { )+) } -impl_with_fromstr!(f32, f64, isize, i8, i16, i32, i64, i128, usize, u8, u16, - u32, u64, u128, IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6, - SocketAddr); +impl_with_fromstr!( + f32, f64, isize, i8, i16, i32, i64, i128, usize, u8, u16, u32, u64, u128, + NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize, + NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize, + IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6, SocketAddr +); impl<'v, T: FromFormValue<'v>> FromFormValue<'v> for Option { type Error = !; diff --git a/core/lib/src/request/param.rs b/core/lib/src/request/param.rs index b64e8dc9..9f773bc2 100644 --- a/core/lib/src/request/param.rs +++ b/core/lib/src/request/param.rs @@ -72,9 +72,14 @@ use crate::http::{RawStr, uri::{Segments, SegmentError}}; /// Rocket implements `FromParam` for several standard library types. Their /// behavior is documented here. /// -/// * **f32, f64, isize, i8, i16, i32, i64, i128, usize, u8, u16, u32, u64, -/// u128, bool, IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6, -/// SocketAddr** +/// * +/// * Primitive types: **f32, f64, isize, i8, i16, i32, i64, i128, +/// usize, u8, u16, u32, u64, u128, bool** +/// * `IpAddr` and `SocketAddr` types: **IpAddr, Ipv4Addr, Ipv6Addr, +/// SocketAddrV4, SocketAddrV6, SocketAddr** +/// * `NonZero*` types: **NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, +/// NonZeroI128, NonZeroIsize, NonZeroU8, NonZeroU16, NonZeroU32, +/// NonZeroU64, NonZeroU128, NonZeroUsize** /// /// A value is parsed successfully if the `from_str` method from the given /// type returns successfully. Otherwise, the raw path segment is returned @@ -239,10 +244,16 @@ macro_rules! impl_with_fromstr { )+) } +use std::num::{ + NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize, + NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize, +}; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6, SocketAddr}; impl_with_fromstr! { i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64, + NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize, + NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize, bool, IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6, SocketAddr }