@@ -73,6 +73,14 @@ assert!(data_url.fragment() == Some(""));
73
73
# run().unwrap();
74
74
```
75
75
76
+ ## Default Features
77
+
78
+ Versions `<= 2.5.2` of the crate have no default features. Versions `> 2.5.2` have the default feature 'std'.
79
+ If you are upgrading across this boundary and you have specified `default-features = false`, then
80
+ you will need to add the 'std' feature or the 'alloc' feature to your dependency.
81
+ The 'std' feature has the same behavior as the previous versions. The 'alloc' feature
82
+ provides no_std support.
83
+
76
84
## Serde
77
85
78
86
Enable the `serde` feature to include `Deserialize` and `Serialize` implementations for `url::Url`.
@@ -134,6 +142,7 @@ url = { version = "2", features = ["debugger_visualizer"] }
134
142
135
143
*/
136
144
145
+ #![ no_std]
137
146
#![ doc( html_root_url = "https://docs.rs/url/2.5.2" ) ]
138
147
#![ cfg_attr(
139
148
feature = "debugger_visualizer" ,
@@ -145,29 +154,48 @@ url = { version = "2", features = ["debugger_visualizer"] }
145
154
146
155
pub use form_urlencoded;
147
156
157
+ // For forwards compatibility
158
+ #[ cfg( feature = "std" ) ]
159
+ extern crate std;
160
+
161
+ #[ macro_use]
162
+ extern crate alloc;
163
+
148
164
#[ cfg( feature = "serde" ) ]
149
165
extern crate serde;
150
166
151
167
use crate :: host:: HostInternal ;
152
- use crate :: parser:: {
153
- to_u32, Context , Parser , SchemeType , PATH_SEGMENT , SPECIAL_PATH_SEGMENT , USERINFO ,
154
- } ;
155
- use percent_encoding:: { percent_decode, percent_encode, utf8_percent_encode} ;
156
- use std:: borrow:: Borrow ;
157
- use std:: cmp;
158
- use std:: fmt:: { self , Write } ;
159
- use std:: hash;
168
+
169
+ use crate :: net:: IpAddr ;
170
+ #[ cfg( feature = "std" ) ]
160
171
#[ cfg( any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ]
161
- use std:: io;
162
- use std:: mem;
163
- use std:: net:: IpAddr ;
172
+ use crate :: net:: { SocketAddr , ToSocketAddrs } ;
173
+ use crate :: parser:: { to_u32, Context , Parser , SchemeType , USERINFO } ;
174
+ use alloc:: borrow:: ToOwned ;
175
+ use alloc:: str;
176
+ use alloc:: string:: { String , ToString } ;
177
+ use core:: borrow:: Borrow ;
178
+ use core:: convert:: TryFrom ;
179
+ use core:: fmt:: Write ;
180
+ use core:: ops:: { Range , RangeFrom , RangeTo } ;
181
+ use core:: { cmp, fmt, hash, mem} ;
182
+ use percent_encoding:: utf8_percent_encode;
183
+ #[ cfg( feature = "std" ) ]
164
184
#[ cfg( any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ]
165
- use std:: net :: { SocketAddr , ToSocketAddrs } ;
166
- use std :: ops :: { Range , RangeFrom , RangeTo } ;
185
+ use std:: io ;
186
+ # [ cfg ( feature = "std" ) ]
167
187
use std:: path:: { Path , PathBuf } ;
168
- use std:: str;
169
188
170
- use std:: convert:: TryFrom ;
189
+ /// `std` version of `net`
190
+ #[ cfg( feature = "std" ) ]
191
+ pub ( crate ) mod net {
192
+ pub use std:: net:: * ;
193
+ }
194
+ /// `no_std` nightly version of `net`
195
+ #[ cfg( not( feature = "std" ) ) ]
196
+ pub ( crate ) mod net {
197
+ pub use core:: net:: * ;
198
+ }
171
199
172
200
pub use crate :: host:: Host ;
173
201
pub use crate :: origin:: { OpaqueOrigin , Origin } ;
@@ -1279,11 +1307,12 @@ impl Url {
1279
1307
/// })
1280
1308
/// }
1281
1309
/// ```
1310
+ #[ cfg( feature = "std" ) ]
1282
1311
#[ cfg( any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ]
1283
1312
pub fn socket_addrs (
1284
1313
& self ,
1285
1314
default_port_number : impl Fn ( ) -> Option < u16 > ,
1286
- ) -> io:: Result < Vec < SocketAddr > > {
1315
+ ) -> io:: Result < alloc :: vec :: Vec < SocketAddr > > {
1287
1316
// Note: trying to avoid the Vec allocation by returning `impl AsRef<[SocketAddr]>`
1288
1317
// causes borrowck issues because the return value borrows `default_port_number`:
1289
1318
//
@@ -1353,7 +1382,11 @@ impl Url {
1353
1382
///
1354
1383
/// ```
1355
1384
/// use url::Url;
1385
+ ///
1386
+ /// # #[cfg(feature = "std")]
1356
1387
/// # use std::error::Error;
1388
+ /// # #[cfg(not(feature = "std"))]
1389
+ /// # use core::error::Error;
1357
1390
///
1358
1391
/// # fn run() -> Result<(), Box<dyn Error>> {
1359
1392
/// let url = Url::parse("https://example.com/foo/bar")?;
@@ -1767,7 +1800,11 @@ impl Url {
1767
1800
///
1768
1801
/// ```
1769
1802
/// use url::Url;
1803
+ ///
1804
+ /// # #[cfg(feature = "std")]
1770
1805
/// # use std::error::Error;
1806
+ /// # #[cfg(not(feature = "std"))]
1807
+ /// # use core::error::Error;
1771
1808
///
1772
1809
/// # fn run() -> Result<(), Box<dyn Error>> {
1773
1810
/// let mut url = Url::parse("ssh://example.net:2048/")?;
@@ -1786,7 +1823,11 @@ impl Url {
1786
1823
///
1787
1824
/// ```rust
1788
1825
/// use url::Url;
1826
+ ///
1827
+ /// # #[cfg(feature = "std")]
1789
1828
/// # use std::error::Error;
1829
+ /// # #[cfg(not(feature = "std"))]
1830
+ /// # use core::error::Error;
1790
1831
///
1791
1832
/// # fn run() -> Result<(), Box<dyn Error>> {
1792
1833
/// let mut url = Url::parse("https://example.org/")?;
@@ -2469,9 +2510,14 @@ impl Url {
2469
2510
/// # run().unwrap();
2470
2511
/// # }
2471
2512
/// ```
2472
- #[ cfg( any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ]
2513
+ ///
2514
+ /// This method is only available if the `std` Cargo feature is enabled.
2515
+ #[ cfg( all(
2516
+ feature = "std" ,
2517
+ any( unix, windows, target_os = "redox" , target_os = "wasi" )
2518
+ ) ) ]
2473
2519
#[ allow( clippy:: result_unit_err) ]
2474
- pub fn from_file_path < P : AsRef < Path > > ( path : P ) -> Result < Url , ( ) > {
2520
+ pub fn from_file_path < P : AsRef < std :: path :: Path > > ( path : P ) -> Result < Url , ( ) > {
2475
2521
let mut serialization = "file://" . to_owned ( ) ;
2476
2522
let host_start = serialization. len ( ) as u32 ;
2477
2523
let ( host_end, host) = path_to_file_url_segments ( path. as_ref ( ) , & mut serialization) ?;
@@ -2506,9 +2552,14 @@ impl Url {
2506
2552
///
2507
2553
/// Note that `std::path` does not consider trailing slashes significant
2508
2554
/// and usually does not include them (e.g. in `Path::parent()`).
2509
- #[ cfg( any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ]
2555
+ ///
2556
+ /// This method is only available if the `std` Cargo feature is enabled.
2557
+ #[ cfg( all(
2558
+ feature = "std" ,
2559
+ any( unix, windows, target_os = "redox" , target_os = "wasi" )
2560
+ ) ) ]
2510
2561
#[ allow( clippy:: result_unit_err) ]
2511
- pub fn from_directory_path < P : AsRef < Path > > ( path : P ) -> Result < Url , ( ) > {
2562
+ pub fn from_directory_path < P : AsRef < std :: path :: Path > > ( path : P ) -> Result < Url , ( ) > {
2512
2563
let mut url = Url :: from_file_path ( path) ?;
2513
2564
if !url. serialization . ends_with ( '/' ) {
2514
2565
url. serialization . push ( '/' )
@@ -2622,8 +2673,13 @@ impl Url {
2622
2673
/// or if `Path::new_opt()` returns `None`.
2623
2674
/// (That is, if the percent-decoded path contains a NUL byte or,
2624
2675
/// for a Windows path, is not UTF-8.)
2676
+ ///
2677
+ /// This method is only available if the `std` Cargo feature is enabled.
2625
2678
#[ inline]
2626
- #[ cfg( any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ]
2679
+ #[ cfg( all(
2680
+ feature = "std" ,
2681
+ any( unix, windows, target_os = "redox" , target_os = "wasi" )
2682
+ ) ) ]
2627
2683
#[ allow( clippy:: result_unit_err) ]
2628
2684
pub fn to_file_path ( & self ) -> Result < PathBuf , ( ) > {
2629
2685
if let Some ( segments) = self . path_segments ( ) {
@@ -2827,11 +2883,13 @@ impl<'de> serde::Deserialize<'de> for Url {
2827
2883
}
2828
2884
}
2829
2885
2830
- #[ cfg( any( unix, target_os = "redox" , target_os = "wasi" ) ) ]
2886
+ #[ cfg( all ( feature = "std" , any( unix, target_os = "redox" , target_os = "wasi" ) ) ) ]
2831
2887
fn path_to_file_url_segments (
2832
2888
path : & Path ,
2833
2889
serialization : & mut String ,
2834
2890
) -> Result < ( u32 , HostInternal ) , ( ) > {
2891
+ use parser:: SPECIAL_PATH_SEGMENT ;
2892
+ use percent_encoding:: percent_encode;
2835
2893
#[ cfg( any( unix, target_os = "redox" ) ) ]
2836
2894
use std:: os:: unix:: prelude:: OsStrExt ;
2837
2895
#[ cfg( target_os = "wasi" ) ]
@@ -2857,7 +2915,7 @@ fn path_to_file_url_segments(
2857
2915
Ok ( ( host_end, HostInternal :: None ) )
2858
2916
}
2859
2917
2860
- #[ cfg( windows) ]
2918
+ #[ cfg( all ( feature = "std" , windows) ) ]
2861
2919
fn path_to_file_url_segments (
2862
2920
path : & Path ,
2863
2921
serialization : & mut String ,
@@ -2866,11 +2924,14 @@ fn path_to_file_url_segments(
2866
2924
}
2867
2925
2868
2926
// Build this unconditionally to alleviate https://github.com/servo/rust-url/issues/102
2927
+ #[ cfg( feature = "std" ) ]
2869
2928
#[ cfg_attr( not( windows) , allow( dead_code) ) ]
2870
2929
fn path_to_file_url_segments_windows (
2871
2930
path : & Path ,
2872
2931
serialization : & mut String ,
2873
2932
) -> Result < ( u32 , HostInternal ) , ( ) > {
2933
+ use crate :: parser:: PATH_SEGMENT ;
2934
+ use percent_encoding:: percent_encode;
2874
2935
use std:: path:: { Component , Prefix } ;
2875
2936
if !path. is_absolute ( ) {
2876
2937
return Err ( ( ) ) ;
@@ -2929,16 +2990,19 @@ fn path_to_file_url_segments_windows(
2929
2990
Ok ( ( host_end, host_internal) )
2930
2991
}
2931
2992
2932
- #[ cfg( any( unix, target_os = "redox" , target_os = "wasi" ) ) ]
2993
+ #[ cfg( all ( feature = "std" , any( unix, target_os = "redox" , target_os = "wasi" ) ) ) ]
2933
2994
fn file_url_segments_to_pathbuf (
2934
2995
host : Option < & str > ,
2935
2996
segments : str:: Split < ' _ , char > ,
2936
2997
) -> Result < PathBuf , ( ) > {
2998
+ use alloc:: vec:: Vec ;
2999
+ use percent_encoding:: percent_decode;
2937
3000
use std:: ffi:: OsStr ;
2938
3001
#[ cfg( any( unix, target_os = "redox" ) ) ]
2939
3002
use std:: os:: unix:: prelude:: OsStrExt ;
2940
3003
#[ cfg( target_os = "wasi" ) ]
2941
3004
use std:: os:: wasi:: prelude:: OsStrExt ;
3005
+ use std:: path:: PathBuf ;
2942
3006
2943
3007
if host. is_some ( ) {
2944
3008
return Err ( ( ) ) ;
@@ -2974,7 +3038,7 @@ fn file_url_segments_to_pathbuf(
2974
3038
Ok ( path)
2975
3039
}
2976
3040
2977
- #[ cfg( windows) ]
3041
+ #[ cfg( all ( feature = "std" , windows) ) ]
2978
3042
fn file_url_segments_to_pathbuf (
2979
3043
host : Option < & str > ,
2980
3044
segments : str:: Split < char > ,
@@ -2983,11 +3047,13 @@ fn file_url_segments_to_pathbuf(
2983
3047
}
2984
3048
2985
3049
// Build this unconditionally to alleviate https://github.com/servo/rust-url/issues/102
3050
+ #[ cfg( feature = "std" ) ]
2986
3051
#[ cfg_attr( not( windows) , allow( dead_code) ) ]
2987
3052
fn file_url_segments_to_pathbuf_windows (
2988
3053
host : Option < & str > ,
2989
3054
mut segments : str:: Split < ' _ , char > ,
2990
3055
) -> Result < PathBuf , ( ) > {
3056
+ use percent_encoding:: percent_decode;
2991
3057
let mut string = if let Some ( host) = host {
2992
3058
r"\\" . to_owned ( ) + host
2993
3059
} else {
0 commit comments