@@ -139,6 +139,21 @@ url = { version = "2", features = ["debugger_visualizer"] }
139139 feature = "debugger_visualizer" ,
140140 debugger_visualizer( natvis_file = "../../debug_metadata/url.natvis" )
141141) ]
142+ #![ cfg_attr( not( feature = "std" ) , no_std) ]
143+
144+ // Use std_core_compat for dependencies that
145+ // are in std in the Minimum Supported Rust Version
146+ // and in core in the latest stable release.
147+ #[ cfg( feature = "std" ) ]
148+ extern crate std as std_core_compat;
149+
150+ #[ cfg( not( feature = "std" ) ) ]
151+ extern crate core as std_core_compat;
152+
153+ extern crate alloc;
154+
155+ #[ cfg( not( feature = "alloc" ) ) ]
156+ compile_error ! ( "the `alloc` feature must currently be enabled" ) ;
142157
143158pub use form_urlencoded;
144159
@@ -150,21 +165,21 @@ use crate::parser::{
150165 to_u32, Context , Parser , SchemeType , PATH_SEGMENT , SPECIAL_PATH_SEGMENT , USERINFO ,
151166} ;
152167use percent_encoding:: { percent_decode, percent_encode, utf8_percent_encode} ;
153- use std :: borrow:: Borrow ;
154- use std :: cmp;
155- use std :: fmt:: { self , Write } ;
156- use std :: hash;
157- # [ cfg ( any ( unix , windows , target_os = "redox" , target_os = "wasi" ) ) ]
158- use std :: io ;
159- use std :: mem ;
160- use std :: net :: IpAddr ;
161- # [ cfg ( any ( unix , windows , target_os = "redox" , target_os = "wasi" ) ) ]
162- use std :: net :: { SocketAddr , ToSocketAddrs } ;
163- use std :: ops :: { Range , RangeFrom , RangeTo } ;
164- use std :: path :: { Path , PathBuf } ;
165- use std :: str ;
166-
167- use std :: convert:: TryFrom ;
168+ use core :: borrow:: Borrow ;
169+ use core :: cmp;
170+ use core :: fmt:: { self , Write } ;
171+ use core :: hash;
172+ use core :: mem ;
173+ use std_core_compat :: net :: IpAddr ;
174+ use core :: ops :: { Range , RangeFrom , RangeTo } ;
175+ use core :: str ;
176+
177+ use alloc :: borrow :: ToOwned ;
178+ use alloc :: format ;
179+ use alloc :: string :: String ;
180+ use alloc :: string :: ToString ;
181+
182+ use core :: convert:: TryFrom ;
168183
169184pub use crate :: host:: Host ;
170185pub use crate :: origin:: { OpaqueOrigin , Origin } ;
@@ -1276,11 +1291,13 @@ impl Url {
12761291 /// })
12771292 /// }
12781293 /// ```
1279- #[ cfg( any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ]
1294+ #[ cfg( all ( feature = "std" , any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ) ]
12801295 pub fn socket_addrs (
12811296 & self ,
12821297 default_port_number : impl Fn ( ) -> Option < u16 > ,
1283- ) -> io:: Result < Vec < SocketAddr > > {
1298+ ) -> std:: io:: Result < Vec < std:: net:: SocketAddr > > {
1299+ use std:: net:: ToSocketAddrs ;
1300+ use std:: io;
12841301 // Note: trying to avoid the Vec allocation by returning `impl AsRef<[SocketAddr]>`
12851302 // causes borrowck issues because the return value borrows `default_port_number`:
12861303 //
@@ -2466,9 +2483,9 @@ impl Url {
24662483 /// # run().unwrap();
24672484 /// # }
24682485 /// ```
2469- #[ cfg( any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ]
2486+ #[ cfg( all ( feature = "std" , any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ) ]
24702487 #[ allow( clippy:: result_unit_err) ]
2471- pub fn from_file_path < P : AsRef < Path > > ( path : P ) -> Result < Url , ( ) > {
2488+ pub fn from_file_path < P : AsRef < std :: path :: Path > > ( path : P ) -> Result < Url , ( ) > {
24722489 let mut serialization = "file://" . to_owned ( ) ;
24732490 let host_start = serialization. len ( ) as u32 ;
24742491 let ( host_end, host) = path_to_file_url_segments ( path. as_ref ( ) , & mut serialization) ?;
@@ -2503,9 +2520,9 @@ impl Url {
25032520 ///
25042521 /// Note that `std::path` does not consider trailing slashes significant
25052522 /// and usually does not include them (e.g. in `Path::parent()`).
2506- #[ cfg( any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ]
2523+ #[ cfg( all ( feature = "std" , any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ) ]
25072524 #[ allow( clippy:: result_unit_err) ]
2508- pub fn from_directory_path < P : AsRef < Path > > ( path : P ) -> Result < Url , ( ) > {
2525+ pub fn from_directory_path < P : AsRef < std :: path :: Path > > ( path : P ) -> Result < Url , ( ) > {
25092526 let mut url = Url :: from_file_path ( path) ?;
25102527 if !url. serialization . ends_with ( '/' ) {
25112528 url. serialization . push ( '/' )
@@ -2620,9 +2637,9 @@ impl Url {
26202637 /// (That is, if the percent-decoded path contains a NUL byte or,
26212638 /// for a Windows path, is not UTF-8.)
26222639 #[ inline]
2623- #[ cfg( any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ]
2640+ #[ cfg( all ( feature = "std" , any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ) ]
26242641 #[ allow( clippy:: result_unit_err) ]
2625- pub fn to_file_path ( & self ) -> Result < PathBuf , ( ) > {
2642+ pub fn to_file_path ( & self ) -> Result < std :: path :: PathBuf , ( ) > {
26262643 if let Some ( segments) = self . path_segments ( ) {
26272644 let host = match self . host ( ) {
26282645 None | Some ( Host :: Domain ( "localhost" ) ) => None ,
@@ -2824,9 +2841,9 @@ impl<'de> serde::Deserialize<'de> for Url {
28242841 }
28252842}
28262843
2827- #[ cfg( any( unix, target_os = "redox" , target_os = "wasi" ) ) ]
2844+ #[ cfg( all ( feature = "std" , any( unix, target_os = "redox" , target_os = "wasi" ) ) ) ]
28282845fn path_to_file_url_segments (
2829- path : & Path ,
2846+ path : & std :: path :: Path ,
28302847 serialization : & mut String ,
28312848) -> Result < ( u32 , HostInternal ) , ( ) > {
28322849 #[ cfg( any( unix, target_os = "redox" ) ) ]
@@ -2864,8 +2881,9 @@ fn path_to_file_url_segments(
28642881
28652882// Build this unconditionally to alleviate https://github.com/servo/rust-url/issues/102
28662883#[ cfg_attr( not( windows) , allow( dead_code) ) ]
2884+ #[ cfg( feature = "std" ) ]
28672885fn path_to_file_url_segments_windows (
2868- path : & Path ,
2886+ path : & std :: path :: Path ,
28692887 serialization : & mut String ,
28702888) -> Result < ( u32 , HostInternal ) , ( ) > {
28712889 use std:: path:: { Component , Prefix } ;
@@ -2926,12 +2944,13 @@ fn path_to_file_url_segments_windows(
29262944 Ok ( ( host_end, host_internal) )
29272945}
29282946
2929- #[ cfg( any( unix, target_os = "redox" , target_os = "wasi" ) ) ]
2947+ #[ cfg( all ( feature = "std" , any( unix, target_os = "redox" , target_os = "wasi" ) ) ) ]
29302948fn file_url_segments_to_pathbuf (
29312949 host : Option < & str > ,
29322950 segments : str:: Split < ' _ , char > ,
2933- ) -> Result < PathBuf , ( ) > {
2951+ ) -> Result < std :: path :: PathBuf , ( ) > {
29342952 use std:: ffi:: OsStr ;
2953+ use std:: path:: PathBuf ;
29352954 #[ cfg( any( unix, target_os = "redox" ) ) ]
29362955 use std:: os:: unix:: prelude:: OsStrExt ;
29372956 #[ cfg( target_os = "wasi" ) ]
@@ -2981,10 +3000,11 @@ fn file_url_segments_to_pathbuf(
29813000
29823001// Build this unconditionally to alleviate https://github.com/servo/rust-url/issues/102
29833002#[ cfg_attr( not( windows) , allow( dead_code) ) ]
3003+ #[ cfg( feature = "std" ) ]
29843004fn file_url_segments_to_pathbuf_windows (
29853005 host : Option < & str > ,
29863006 mut segments : str:: Split < ' _ , char > ,
2987- ) -> Result < PathBuf , ( ) > {
3007+ ) -> Result < std :: path :: PathBuf , ( ) > {
29883008 let mut string = if let Some ( host) = host {
29893009 r"\\" . to_owned ( ) + host
29903010 } else {
@@ -3024,7 +3044,7 @@ fn file_url_segments_to_pathbuf_windows(
30243044 Err ( ..) => return Err ( ( ) ) ,
30253045 }
30263046 }
3027- let path = PathBuf :: from ( string) ;
3047+ let path = std :: path :: PathBuf :: from ( string) ;
30283048 debug_assert ! (
30293049 path. is_absolute( ) ,
30303050 "to_file_path() failed to produce an absolute Path"
0 commit comments