@@ -121,6 +121,10 @@ url = { version = "2", features = ["serde"] }
121
121
*/
122
122
123
123
#![ doc( html_root_url = "https://docs.rs/url/2.2.2" ) ]
124
+ #![ no_std]
125
+ #[ macro_use]
126
+ extern crate alloc;
127
+ extern crate std;
124
128
125
129
#[ macro_use]
126
130
extern crate matches;
@@ -130,20 +134,25 @@ pub use form_urlencoded;
130
134
extern crate serde;
131
135
132
136
use crate :: host:: HostInternal ;
133
- use crate :: parser:: { to_u32, Context , Parser , SchemeType , PATH_SEGMENT , USERINFO } ;
134
- use percent_encoding:: { percent_decode, percent_encode, utf8_percent_encode} ;
135
- use std:: borrow:: Borrow ;
136
- use std:: cmp;
137
- use std:: fmt:: { self , Write } ;
138
- use std:: hash;
139
- use std:: io;
140
- use std:: mem;
141
- use std:: net:: { IpAddr , SocketAddr , ToSocketAddrs } ;
142
- use std:: ops:: { Range , RangeFrom , RangeTo } ;
143
- use std:: path:: { Path , PathBuf } ;
144
- use std:: str;
145
-
146
- use std:: convert:: TryFrom ;
137
+ use crate :: parser:: { to_u32, Context , Parser , SchemeType , USERINFO } ;
138
+ use alloc:: borrow:: ToOwned ;
139
+ use alloc:: string:: { String , ToString } ;
140
+ use core:: borrow:: Borrow ;
141
+ use core:: cmp;
142
+ use core:: convert:: TryFrom ;
143
+ use core:: fmt:: { self , Write } ;
144
+ use core:: hash;
145
+ use core:: mem;
146
+ use core:: ops:: { Range , RangeFrom , RangeTo } ;
147
+ use core:: str;
148
+ use percent_encoding:: utf8_percent_encode;
149
+ use std:: net:: IpAddr ;
150
+ #[ cfg( feature = "std" ) ]
151
+ use std:: {
152
+ io,
153
+ net:: { SocketAddr , ToSocketAddrs } ,
154
+ path:: { Path , PathBuf } ,
155
+ } ;
147
156
148
157
pub use crate :: host:: Host ;
149
158
pub use crate :: origin:: { OpaqueOrigin , Origin } ;
@@ -1136,10 +1145,11 @@ impl Url {
1136
1145
/// })
1137
1146
/// }
1138
1147
/// ```
1148
+ #[ cfg( feature = "std" ) ]
1139
1149
pub fn socket_addrs (
1140
1150
& self ,
1141
1151
default_port_number : impl Fn ( ) -> Option < u16 > ,
1142
- ) -> io:: Result < Vec < SocketAddr > > {
1152
+ ) -> io:: Result < alloc :: vec :: Vec < SocketAddr > > {
1143
1153
// Note: trying to avoid the Vec allocation by returning `impl AsRef<[SocketAddr]>`
1144
1154
// causes borrowck issues because the return value borrows `default_port_number`:
1145
1155
//
@@ -1148,6 +1158,7 @@ impl Url {
1148
1158
// > This RFC proposes that *all* type parameters are considered in scope
1149
1159
// > for `impl Trait` in return position
1150
1160
1161
+ // TODO: Return custom error type to support no_std
1151
1162
fn io_result < T > ( opt : Option < T > , message : & str ) -> io:: Result < T > {
1152
1163
opt. ok_or_else ( || io:: Error :: new ( io:: ErrorKind :: InvalidData , message) )
1153
1164
}
@@ -2298,7 +2309,9 @@ impl Url {
2298
2309
/// # run().unwrap();
2299
2310
/// # }
2300
2311
/// ```
2301
- #[ cfg( any( unix, windows, target_os = "redox" ) ) ]
2312
+ ///
2313
+ /// This method is only available if the `std` Cargo feature is enabled.
2314
+ #[ cfg( all( feature = "std" , any( unix, windows, target_os = "redox" ) ) ) ]
2302
2315
#[ allow( clippy:: result_unit_err) ]
2303
2316
pub fn from_file_path < P : AsRef < Path > > ( path : P ) -> Result < Url , ( ) > {
2304
2317
let mut serialization = "file://" . to_owned ( ) ;
@@ -2335,7 +2348,9 @@ impl Url {
2335
2348
///
2336
2349
/// Note that `std::path` does not consider trailing slashes significant
2337
2350
/// and usually does not include them (e.g. in `Path::parent()`).
2338
- #[ cfg( any( unix, windows, target_os = "redox" ) ) ]
2351
+ ///
2352
+ /// This method is only available if the `std` Cargo feature is enabled.
2353
+ #[ cfg( all( feature = "std" , any( unix, windows, target_os = "redox" ) ) ) ]
2339
2354
#[ allow( clippy:: result_unit_err) ]
2340
2355
pub fn from_directory_path < P : AsRef < Path > > ( path : P ) -> Result < Url , ( ) > {
2341
2356
let mut url = Url :: from_file_path ( path) ?;
@@ -2451,8 +2466,10 @@ impl Url {
2451
2466
/// or if `Path::new_opt()` returns `None`.
2452
2467
/// (That is, if the percent-decoded path contains a NUL byte or,
2453
2468
/// for a Windows path, is not UTF-8.)
2469
+ ///
2470
+ /// This method is only available if the `std` Cargo feature is enabled.
2454
2471
#[ inline]
2455
- #[ cfg( any( unix, windows, target_os = "redox" ) ) ]
2472
+ #[ cfg( all ( feature = "std" , any( unix, windows, target_os = "redox" ) ) ) ]
2456
2473
#[ allow( clippy:: result_unit_err) ]
2457
2474
pub fn to_file_path ( & self ) -> Result < PathBuf , ( ) > {
2458
2475
if let Some ( segments) = self . path_segments ( ) {
@@ -2656,11 +2673,13 @@ impl<'de> serde::Deserialize<'de> for Url {
2656
2673
}
2657
2674
}
2658
2675
2659
- #[ cfg( any( unix, target_os = "redox" ) ) ]
2676
+ #[ cfg( all ( feature = "std" , any( unix, target_os = "redox" ) ) ) ]
2660
2677
fn path_to_file_url_segments (
2661
2678
path : & Path ,
2662
2679
serialization : & mut String ,
2663
2680
) -> Result < ( u32 , HostInternal ) , ( ) > {
2681
+ use crate :: parser:: PATH_SEGMENT ;
2682
+ use percent_encoding:: percent_encode;
2664
2683
use std:: os:: unix:: prelude:: OsStrExt ;
2665
2684
if !path. is_absolute ( ) {
2666
2685
return Err ( ( ) ) ;
@@ -2683,20 +2702,23 @@ fn path_to_file_url_segments(
2683
2702
Ok ( ( host_end, HostInternal :: None ) )
2684
2703
}
2685
2704
2686
- #[ cfg( windows) ]
2705
+ #[ cfg( all ( feature = "std" , windows) ) ]
2687
2706
fn path_to_file_url_segments (
2688
2707
path : & Path ,
2689
2708
serialization : & mut String ,
2690
2709
) -> Result < ( u32 , HostInternal ) , ( ) > {
2691
2710
path_to_file_url_segments_windows ( path, serialization)
2692
2711
}
2693
2712
2713
+ #[ cfg( feature = "std" ) ]
2694
2714
// Build this unconditionally to alleviate https://github.com/servo/rust-url/issues/102
2695
2715
#[ cfg_attr( not( windows) , allow( dead_code) ) ]
2696
2716
fn path_to_file_url_segments_windows (
2697
2717
path : & Path ,
2698
2718
serialization : & mut String ,
2699
2719
) -> Result < ( u32 , HostInternal ) , ( ) > {
2720
+ use crate :: parser:: PATH_SEGMENT ;
2721
+ use percent_encoding:: percent_encode;
2700
2722
use std:: path:: { Component , Prefix } ;
2701
2723
if !path. is_absolute ( ) {
2702
2724
return Err ( ( ) ) ;
@@ -2751,11 +2773,13 @@ fn path_to_file_url_segments_windows(
2751
2773
Ok ( ( host_end, host_internal) )
2752
2774
}
2753
2775
2754
- #[ cfg( any( unix, target_os = "redox" ) ) ]
2776
+ #[ cfg( all ( feature = "std" , any( unix, target_os = "redox" ) ) ) ]
2755
2777
fn file_url_segments_to_pathbuf (
2756
2778
host : Option < & str > ,
2757
2779
segments : str:: Split < ' _ , char > ,
2758
2780
) -> Result < PathBuf , ( ) > {
2781
+ use alloc:: vec:: Vec ;
2782
+ use percent_encoding:: percent_decode;
2759
2783
use std:: ffi:: OsStr ;
2760
2784
use std:: os:: unix:: prelude:: OsStrExt ;
2761
2785
@@ -2788,20 +2812,22 @@ fn file_url_segments_to_pathbuf(
2788
2812
Ok ( path)
2789
2813
}
2790
2814
2791
- #[ cfg( windows) ]
2815
+ #[ cfg( all ( feature = "std" , windows) ) ]
2792
2816
fn file_url_segments_to_pathbuf (
2793
2817
host : Option < & str > ,
2794
2818
segments : str:: Split < char > ,
2795
2819
) -> Result < PathBuf , ( ) > {
2796
2820
file_url_segments_to_pathbuf_windows ( host, segments)
2797
2821
}
2798
2822
2823
+ #[ cfg( feature = "std" ) ]
2799
2824
// Build this unconditionally to alleviate https://github.com/servo/rust-url/issues/102
2800
2825
#[ cfg_attr( not( windows) , allow( dead_code) ) ]
2801
2826
fn file_url_segments_to_pathbuf_windows (
2802
2827
host : Option < & str > ,
2803
2828
mut segments : str:: Split < ' _ , char > ,
2804
2829
) -> Result < PathBuf , ( ) > {
2830
+ use percent_encoding:: percent_decode;
2805
2831
let mut string = if let Some ( host) = host {
2806
2832
r"\\" . to_owned ( ) + host
2807
2833
} else {
0 commit comments