@@ -131,27 +131,36 @@ url = { version = "2", default-features = false }
131131*/
132132
133133#![ doc( html_root_url = "https://docs.rs/url/2.2.2" ) ]
134+ #![ no_std]
135+ #[ macro_use]
136+ extern crate alloc;
137+ extern crate std;
134138
135139pub use form_urlencoded;
136140
137141#[ cfg( feature = "serde" ) ]
138142extern crate serde;
139143
140144use crate :: host:: HostInternal ;
141- use crate :: parser:: { to_u32, Context , Parser , SchemeType , PATH_SEGMENT , USERINFO } ;
142- use percent_encoding:: { percent_decode, percent_encode, utf8_percent_encode} ;
143- use std:: borrow:: Borrow ;
144- use std:: cmp;
145- use std:: fmt:: { self , Write } ;
146- use std:: hash;
147- use std:: io;
148- use std:: mem;
149- use std:: net:: { IpAddr , SocketAddr , ToSocketAddrs } ;
150- use std:: ops:: { Range , RangeFrom , RangeTo } ;
151- use std:: path:: { Path , PathBuf } ;
152- use std:: str;
153-
154- use std:: convert:: TryFrom ;
145+ use crate :: parser:: { to_u32, Context , Parser , SchemeType , USERINFO } ;
146+ use alloc:: borrow:: ToOwned ;
147+ use alloc:: string:: { String , ToString } ;
148+ use core:: borrow:: Borrow ;
149+ use core:: cmp;
150+ use core:: convert:: TryFrom ;
151+ use core:: fmt:: { self , Write } ;
152+ use core:: hash;
153+ use core:: mem;
154+ use core:: ops:: { Range , RangeFrom , RangeTo } ;
155+ use core:: str;
156+ use percent_encoding:: utf8_percent_encode;
157+ use std:: net:: IpAddr ;
158+ #[ cfg( feature = "std" ) ]
159+ use std:: {
160+ io,
161+ net:: { SocketAddr , ToSocketAddrs } ,
162+ path:: { Path , PathBuf } ,
163+ } ;
155164
156165pub use crate :: host:: Host ;
157166pub use crate :: origin:: { OpaqueOrigin , Origin } ;
@@ -1144,10 +1153,11 @@ impl Url {
11441153 /// })
11451154 /// }
11461155 /// ```
1156+ #[ cfg( feature = "std" ) ]
11471157 pub fn socket_addrs (
11481158 & self ,
11491159 default_port_number : impl Fn ( ) -> Option < u16 > ,
1150- ) -> io:: Result < Vec < SocketAddr > > {
1160+ ) -> io:: Result < alloc :: vec :: Vec < SocketAddr > > {
11511161 // Note: trying to avoid the Vec allocation by returning `impl AsRef<[SocketAddr]>`
11521162 // causes borrowck issues because the return value borrows `default_port_number`:
11531163 //
@@ -1156,6 +1166,7 @@ impl Url {
11561166 // > This RFC proposes that *all* type parameters are considered in scope
11571167 // > for `impl Trait` in return position
11581168
1169+ // TODO: Return custom error type to support no_std
11591170 fn io_result < T > ( opt : Option < T > , message : & str ) -> io:: Result < T > {
11601171 opt. ok_or_else ( || io:: Error :: new ( io:: ErrorKind :: InvalidData , message) )
11611172 }
@@ -2314,7 +2325,9 @@ impl Url {
23142325 /// # run().unwrap();
23152326 /// # }
23162327 /// ```
2317- #[ cfg( any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ]
2328+ ///
2329+ /// This method is only available if the `std` Cargo feature is enabled.
2330+ #[ cfg( all( feature = "std" , any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ) ]
23182331 #[ allow( clippy:: result_unit_err) ]
23192332 pub fn from_file_path < P : AsRef < Path > > ( path : P ) -> Result < Url , ( ) > {
23202333 let mut serialization = "file://" . to_owned ( ) ;
@@ -2351,7 +2364,9 @@ impl Url {
23512364 ///
23522365 /// Note that `std::path` does not consider trailing slashes significant
23532366 /// and usually does not include them (e.g. in `Path::parent()`).
2354- #[ cfg( any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ]
2367+ ///
2368+ /// This method is only available if the `std` Cargo feature is enabled.
2369+ #[ cfg( all( feature = "std" , any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ) ]
23552370 #[ allow( clippy:: result_unit_err) ]
23562371 pub fn from_directory_path < P : AsRef < Path > > ( path : P ) -> Result < Url , ( ) > {
23572372 let mut url = Url :: from_file_path ( path) ?;
@@ -2467,8 +2482,10 @@ impl Url {
24672482 /// or if `Path::new_opt()` returns `None`.
24682483 /// (That is, if the percent-decoded path contains a NUL byte or,
24692484 /// for a Windows path, is not UTF-8.)
2485+ ///
2486+ /// This method is only available if the `std` Cargo feature is enabled.
24702487 #[ inline]
2471- #[ cfg( any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ]
2488+ #[ cfg( all ( feature = "std" , any( unix, windows, target_os = "redox" , target_os = "wasi" ) ) ) ]
24722489 #[ allow( clippy:: result_unit_err) ]
24732490 pub fn to_file_path ( & self ) -> Result < PathBuf , ( ) > {
24742491 if let Some ( segments) = self . path_segments ( ) {
@@ -2672,11 +2689,13 @@ impl<'de> serde::Deserialize<'de> for Url {
26722689 }
26732690}
26742691
2675- #[ cfg( any( unix, target_os = "redox" , target_os = "wasi" ) ) ]
2692+ #[ cfg( all ( feature = "std" , any( unix, target_os = "redox" , target_os = "wasi" ) ) ) ]
26762693fn path_to_file_url_segments (
26772694 path : & Path ,
26782695 serialization : & mut String ,
26792696) -> Result < ( u32 , HostInternal ) , ( ) > {
2697+ use crate :: parser:: PATH_SEGMENT ;
2698+ use percent_encoding:: percent_encode;
26802699 #[ cfg( any( unix, target_os = "redox" ) ) ]
26812700 use std:: os:: unix:: prelude:: OsStrExt ;
26822701 #[ cfg( target_os = "wasi" ) ]
@@ -2702,20 +2721,23 @@ fn path_to_file_url_segments(
27022721 Ok ( ( host_end, HostInternal :: None ) )
27032722}
27042723
2705- #[ cfg( windows) ]
2724+ #[ cfg( all ( feature = "std" , windows) ) ]
27062725fn path_to_file_url_segments (
27072726 path : & Path ,
27082727 serialization : & mut String ,
27092728) -> Result < ( u32 , HostInternal ) , ( ) > {
27102729 path_to_file_url_segments_windows ( path, serialization)
27112730}
27122731
2732+ #[ cfg( feature = "std" ) ]
27132733// Build this unconditionally to alleviate https://github.com/servo/rust-url/issues/102
27142734#[ cfg_attr( not( windows) , allow( dead_code) ) ]
27152735fn path_to_file_url_segments_windows (
27162736 path : & Path ,
27172737 serialization : & mut String ,
27182738) -> Result < ( u32 , HostInternal ) , ( ) > {
2739+ use crate :: parser:: PATH_SEGMENT ;
2740+ use percent_encoding:: percent_encode;
27192741 use std:: path:: { Component , Prefix } ;
27202742 if !path. is_absolute ( ) {
27212743 return Err ( ( ) ) ;
@@ -2770,11 +2792,13 @@ fn path_to_file_url_segments_windows(
27702792 Ok ( ( host_end, host_internal) )
27712793}
27722794
2773- #[ cfg( any( unix, target_os = "redox" , target_os = "wasi" ) ) ]
2795+ #[ cfg( all ( feature = "std" , any( unix, target_os = "redox" , target_os = "wasi" ) ) ) ]
27742796fn file_url_segments_to_pathbuf (
27752797 host : Option < & str > ,
27762798 segments : str:: Split < ' _ , char > ,
27772799) -> Result < PathBuf , ( ) > {
2800+ use alloc:: vec:: Vec ;
2801+ use percent_encoding:: percent_decode;
27782802 use std:: ffi:: OsStr ;
27792803 #[ cfg( any( unix, target_os = "redox" ) ) ]
27802804 use std:: os:: unix:: prelude:: OsStrExt ;
@@ -2810,20 +2834,22 @@ fn file_url_segments_to_pathbuf(
28102834 Ok ( path)
28112835}
28122836
2813- #[ cfg( windows) ]
2837+ #[ cfg( all ( feature = "std" , windows) ) ]
28142838fn file_url_segments_to_pathbuf (
28152839 host : Option < & str > ,
28162840 segments : str:: Split < char > ,
28172841) -> Result < PathBuf , ( ) > {
28182842 file_url_segments_to_pathbuf_windows ( host, segments)
28192843}
28202844
2845+ #[ cfg( feature = "std" ) ]
28212846// Build this unconditionally to alleviate https://github.com/servo/rust-url/issues/102
28222847#[ cfg_attr( not( windows) , allow( dead_code) ) ]
28232848fn file_url_segments_to_pathbuf_windows (
28242849 host : Option < & str > ,
28252850 mut segments : str:: Split < ' _ , char > ,
28262851) -> Result < PathBuf , ( ) > {
2852+ use percent_encoding:: percent_decode;
28272853 let mut string = if let Some ( host) = host {
28282854 r"\\" . to_owned ( ) + host
28292855 } else {
0 commit comments