1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use core::num::*;
use core::str::FromStr;
use crate::parse_error::ParseError;
pub trait Parse<'a> {
fn from_str(s: &'a str) -> Result<Self, ParseError>
where
Self: Sized;
}
impl<'a> Parse<'a> for &'a str {
fn from_str(s: &'a str) -> Result<Self, ParseError> {
Ok(s)
}
}
macro_rules! impl_parse {
( $( $Ty: ty )+) => {
$(
impl<'a> Parse<'a> for $Ty {
fn from_str(s: &'a str) -> Result<Self, ParseError> {
<Self as FromStr>::from_str(&s).map_err(|e| e.into())
}
}
)+
};
}
#[cfg(feature = "alloc")]
macro_rules! impl_parse_infallible {
( $( $Ty: ty )+) => {
$(
impl<'a> Parse<'a> for $Ty {
fn from_str(s: &'a str) -> Result<Self, ParseError> {
Ok(<Self as FromStr>::from_str(&s).unwrap())
}
}
)+
};
}
impl_parse!(isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128);
impl_parse!(bool char f32 f64);
impl_parse!(NonZeroU8 NonZeroU16 NonZeroU32 NonZeroU64 NonZeroU128 NonZeroUsize);
impl_parse!(NonZeroI8 NonZeroI16 NonZeroI32 NonZeroI64 NonZeroI128 NonZeroIsize);
#[cfg(feature = "alloc")]
mod impl_alloc {
extern crate alloc;
use alloc::string::String;
use super::{FromStr, Parse, ParseError};
impl_parse_infallible!(String);
}
#[cfg(feature = "std")]
mod impl_std {
use std::ffi::OsString;
use std::net::*;
use std::path::PathBuf;
use super::{FromStr, Parse, ParseError};
impl_parse_infallible!(OsString PathBuf);
impl_parse!(IpAddr SocketAddr Ipv4Addr Ipv6Addr SocketAddrV4 SocketAddrV6);
}
pub trait ExtParseStr: __private::Sealed {
fn lending_parse<'a, F: Parse<'a>>(&'a self) -> Result<F, ParseError>;
}
impl ExtParseStr for str {
fn lending_parse<'a, F: Parse<'a>>(&'a self) -> Result<F, ParseError> {
Parse::from_str(self)
}
}
#[doc(hidden)]
mod __private {
pub trait Sealed {}
impl Sealed for str {}
}