@@ -131,27 +131,36 @@ url = { version = "2", default-features = false }
131
131
*/
132
132
133
133
#![ 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;
134
138
135
139
pub use form_urlencoded;
136
140
137
141
#[ cfg( feature = "serde" ) ]
138
142
extern crate serde;
139
143
140
144
use 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
+ } ;
155
164
156
165
pub use crate :: host:: Host ;
157
166
pub use crate :: origin:: { OpaqueOrigin , Origin } ;
@@ -1144,10 +1153,11 @@ impl Url {
1144
1153
/// })
1145
1154
/// }
1146
1155
/// ```
1156
+ #[ cfg( feature = "std" ) ]
1147
1157
pub fn socket_addrs (
1148
1158
& self ,
1149
1159
default_port_number : impl Fn ( ) -> Option < u16 > ,
1150
- ) -> io:: Result < Vec < SocketAddr > > {
1160
+ ) -> io:: Result < alloc :: vec :: Vec < SocketAddr > > {
1151
1161
// Note: trying to avoid the Vec allocation by returning `impl AsRef<[SocketAddr]>`
1152
1162
// causes borrowck issues because the return value borrows `default_port_number`:
1153
1163
//
@@ -1156,6 +1166,7 @@ impl Url {
1156
1166
// > This RFC proposes that *all* type parameters are considered in scope
1157
1167
// > for `impl Trait` in return position
1158
1168
1169
+ // TODO: Return custom error type to support no_std
1159
1170
fn io_result < T > ( opt : Option < T > , message : & str ) -> io:: Result < T > {
1160
1171
opt. ok_or_else ( || io:: Error :: new ( io:: ErrorKind :: InvalidData , message) )
1161
1172
}
@@ -2314,7 +2325,9 @@ impl Url {
2314
2325
/// # run().unwrap();
2315
2326
/// # }
2316
2327
/// ```
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" ) ) ) ]
2318
2331
#[ allow( clippy:: result_unit_err) ]
2319
2332
pub fn from_file_path < P : AsRef < Path > > ( path : P ) -> Result < Url , ( ) > {
2320
2333
let mut serialization = "file://" . to_owned ( ) ;
@@ -2351,7 +2364,9 @@ impl Url {
2351
2364
///
2352
2365
/// Note that `std::path` does not consider trailing slashes significant
2353
2366
/// 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" ) ) ) ]
2355
2370
#[ allow( clippy:: result_unit_err) ]
2356
2371
pub fn from_directory_path < P : AsRef < Path > > ( path : P ) -> Result < Url , ( ) > {
2357
2372
let mut url = Url :: from_file_path ( path) ?;
@@ -2467,8 +2482,10 @@ impl Url {
2467
2482
/// or if `Path::new_opt()` returns `None`.
2468
2483
/// (That is, if the percent-decoded path contains a NUL byte or,
2469
2484
/// for a Windows path, is not UTF-8.)
2485
+ ///
2486
+ /// This method is only available if the `std` Cargo feature is enabled.
2470
2487
#[ 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" ) ) ) ]
2472
2489
#[ allow( clippy:: result_unit_err) ]
2473
2490
pub fn to_file_path ( & self ) -> Result < PathBuf , ( ) > {
2474
2491
if let Some ( segments) = self . path_segments ( ) {
@@ -2672,11 +2689,13 @@ impl<'de> serde::Deserialize<'de> for Url {
2672
2689
}
2673
2690
}
2674
2691
2675
- #[ cfg( any( unix, target_os = "redox" , target_os = "wasi" ) ) ]
2692
+ #[ cfg( all ( feature = "std" , any( unix, target_os = "redox" , target_os = "wasi" ) ) ) ]
2676
2693
fn path_to_file_url_segments (
2677
2694
path : & Path ,
2678
2695
serialization : & mut String ,
2679
2696
) -> Result < ( u32 , HostInternal ) , ( ) > {
2697
+ use crate :: parser:: PATH_SEGMENT ;
2698
+ use percent_encoding:: percent_encode;
2680
2699
#[ cfg( any( unix, target_os = "redox" ) ) ]
2681
2700
use std:: os:: unix:: prelude:: OsStrExt ;
2682
2701
#[ cfg( target_os = "wasi" ) ]
@@ -2702,20 +2721,23 @@ fn path_to_file_url_segments(
2702
2721
Ok ( ( host_end, HostInternal :: None ) )
2703
2722
}
2704
2723
2705
- #[ cfg( windows) ]
2724
+ #[ cfg( all ( feature = "std" , windows) ) ]
2706
2725
fn path_to_file_url_segments (
2707
2726
path : & Path ,
2708
2727
serialization : & mut String ,
2709
2728
) -> Result < ( u32 , HostInternal ) , ( ) > {
2710
2729
path_to_file_url_segments_windows ( path, serialization)
2711
2730
}
2712
2731
2732
+ #[ cfg( feature = "std" ) ]
2713
2733
// Build this unconditionally to alleviate https://github.com/servo/rust-url/issues/102
2714
2734
#[ cfg_attr( not( windows) , allow( dead_code) ) ]
2715
2735
fn path_to_file_url_segments_windows (
2716
2736
path : & Path ,
2717
2737
serialization : & mut String ,
2718
2738
) -> Result < ( u32 , HostInternal ) , ( ) > {
2739
+ use crate :: parser:: PATH_SEGMENT ;
2740
+ use percent_encoding:: percent_encode;
2719
2741
use std:: path:: { Component , Prefix } ;
2720
2742
if !path. is_absolute ( ) {
2721
2743
return Err ( ( ) ) ;
@@ -2770,11 +2792,13 @@ fn path_to_file_url_segments_windows(
2770
2792
Ok ( ( host_end, host_internal) )
2771
2793
}
2772
2794
2773
- #[ cfg( any( unix, target_os = "redox" , target_os = "wasi" ) ) ]
2795
+ #[ cfg( all ( feature = "std" , any( unix, target_os = "redox" , target_os = "wasi" ) ) ) ]
2774
2796
fn file_url_segments_to_pathbuf (
2775
2797
host : Option < & str > ,
2776
2798
segments : str:: Split < ' _ , char > ,
2777
2799
) -> Result < PathBuf , ( ) > {
2800
+ use alloc:: vec:: Vec ;
2801
+ use percent_encoding:: percent_decode;
2778
2802
use std:: ffi:: OsStr ;
2779
2803
#[ cfg( any( unix, target_os = "redox" ) ) ]
2780
2804
use std:: os:: unix:: prelude:: OsStrExt ;
@@ -2810,20 +2834,22 @@ fn file_url_segments_to_pathbuf(
2810
2834
Ok ( path)
2811
2835
}
2812
2836
2813
- #[ cfg( windows) ]
2837
+ #[ cfg( all ( feature = "std" , windows) ) ]
2814
2838
fn file_url_segments_to_pathbuf (
2815
2839
host : Option < & str > ,
2816
2840
segments : str:: Split < char > ,
2817
2841
) -> Result < PathBuf , ( ) > {
2818
2842
file_url_segments_to_pathbuf_windows ( host, segments)
2819
2843
}
2820
2844
2845
+ #[ cfg( feature = "std" ) ]
2821
2846
// Build this unconditionally to alleviate https://github.com/servo/rust-url/issues/102
2822
2847
#[ cfg_attr( not( windows) , allow( dead_code) ) ]
2823
2848
fn file_url_segments_to_pathbuf_windows (
2824
2849
host : Option < & str > ,
2825
2850
mut segments : str:: Split < ' _ , char > ,
2826
2851
) -> Result < PathBuf , ( ) > {
2852
+ use percent_encoding:: percent_decode;
2827
2853
let mut string = if let Some ( host) = host {
2828
2854
r"\\" . to_owned ( ) + host
2829
2855
} else {
0 commit comments