Skip to content

Commit 3efe254

Browse files
committed
Added unstable, no_std_net options
1 parent 6d622ab commit 3efe254

File tree

9 files changed

+105
-10
lines changed

9 files changed

+105
-10
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ jobs:
5151
matrix.rust == 'nightly'
5252
run: cargo test --test debugger_visualizer --features "url/serde,url/debugger_visualizer" -- --test-threads=1
5353
- name: Test `no_std` support
54-
run: cargo test --no-default-features --features=alloc
54+
run: cargo test --no-default-features --features=alloc,no_std_net
5555
- name: Build `aarch64-unknown-none` with `no_std`
56-
run: cargo +nightly build -Zbuild-std=core,alloc --target aarch64-unknown-none -v --release --no-default-features --features=alloc
56+
run: cargo +nightly build -Zbuild-std=core,alloc --target aarch64-unknown-none -v --release --no-default-features --features=alloc,unstable
5757

5858
WASM:
5959
runs-on: ubuntu-latest

idna/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ doctest = false
1717
default = ["std"]
1818
std = ["alloc", "unicode-bidi/std", "unicode-normalization/std"]
1919
alloc = []
20+
# Enable nightly features
21+
unstable = []
2022

2123
[[test]]
2224
name = "tests"

idna/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
//! > that minimizes the impact of this transition for client software,
3333
//! > allowing client software to access domains that are valid under either system.
3434
#![no_std]
35+
#[cfg_attr(feature = "unstable", feature("error_in_core"))]
3536

3637
// For forwards compatibility
3738
#[cfg(feature = "std")]

idna/src/uts46.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,9 @@ impl From<Errors> for Result<(), Errors> {
713713
#[cfg(feature = "std")]
714714
impl std::error::Error for Errors {}
715715

716+
#[cfg(feature = "nightly")]
717+
impl core::error::Error for Errors{}
718+
716719
impl fmt::Display for Errors {
717720
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
718721
fmt::Debug::fmt(self, f)

url/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,22 @@ idna = { version = "0.3.0", path = "../idna", default-features = false, features
3333
percent-encoding = { version = "2.2.0", path = "../percent_encoding", default-features = false, features = ["alloc"] }
3434
data-url = { version = "0.2.0", path = "../data-url", default-features = false, features = ["alloc"] }
3535
serde = {version = "1.0", optional = true, default-features = false, features = ["alloc", "derive"]}
36-
no-std-net = { version = "0.6.0", default-features = false }
36+
no-std-net = { version = "0.6.0", default-features = false, optional = true }
3737

3838
[features]
3939
default = ["std"]
40-
std = ["idna/std", "percent-encoding/std", "form_urlencoded/std", "no-std-net/std", "alloc"]
40+
std = ["idna/std", "percent-encoding/std", "form_urlencoded/std", "alloc"]
4141
alloc = []
4242
# UNSTABLE FEATURES (requires Rust nightly)
4343
# Enable to use the #[debugger_visualizer] attribute.
4444
debugger_visualizer = []
4545
# Expose internal offsets of the URL.
4646
expose_internals = []
4747
serde = ["dep:serde", "no-std-net/serde"]
48+
# For no_std: Allow the use of no_std_net instead of nightly
49+
no_std_net = ["no-std-net"]
50+
# For no_std: Use errors_in_core and net_in_core
51+
unstable = ["idna/unstable"]
4852

4953
[[bench]]
5054
name = "parse_url"

url/src/host.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9+
use crate::net::{Ipv4Addr, Ipv6Addr};
910
use alloc::{
1011
borrow::ToOwned,
1112
string::{String, ToString},
@@ -15,7 +16,6 @@ use core::{
1516
cmp,
1617
fmt::{self, Formatter},
1718
};
18-
use no_std_net::{Ipv4Addr, Ipv6Addr};
1919

2020
use percent_encoding::{percent_decode, utf8_percent_encode, CONTROLS};
2121
#[cfg(feature = "serde")]

url/src/lib.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,25 @@ url = { version = "2", features = ["serde"] }
121121
122122
*/
123123

124+
#![no_std]
124125
#![doc(html_root_url = "https://docs.rs/url/2.3.1")]
125126
#![cfg_attr(
126127
feature = "debugger_visualizer",
127128
feature(debugger_visualizer),
128129
debugger_visualizer(natvis_file = "../../debug_metadata/url.natvis")
129130
)]
130-
#![no_std]
131+
#![cfg_attr(
132+
all(
133+
not(feature = "std"),
134+
not(feature = "no_std_net"),
135+
feature = "unstable"
136+
),
137+
feature(ip_in_core)
138+
)]
139+
#![cfg_attr(
140+
all(not(feature = "std"), feature = "unstable"),
141+
feature(error_in_core)
142+
)]
131143

132144
pub use form_urlencoded;
133145

@@ -141,10 +153,16 @@ extern crate alloc;
141153
#[cfg(not(feature = "alloc"))]
142154
compile_error!("the `alloc` feature must be enabled");
143155

156+
#[cfg(not(any(feature = "no_std_net", feature = "std", feature = "unstable")))]
157+
compile_error!(
158+
"Either the `no_std_net`, `std` or, on nightly, the `unstable` feature, must be enabled"
159+
);
160+
144161
#[cfg(feature = "serde")]
145162
extern crate serde;
146163

147164
use crate::host::HostInternal;
165+
use crate::net::IpAddr;
148166
use crate::parser::{to_u32, Context, Parser, SchemeType, USERINFO};
149167
use alloc::borrow::ToOwned;
150168
use alloc::string::{String, ToString};
@@ -156,9 +174,24 @@ use core::hash;
156174
use core::mem;
157175
use core::ops::{Range, RangeFrom, RangeTo};
158176
use core::str;
159-
use no_std_net::IpAddr;
160177
use percent_encoding::utf8_percent_encode;
161178

179+
/// `std` version of `net`
180+
#[cfg(feature = "std")]
181+
pub(crate) mod net {
182+
pub use std::net::*;
183+
}
184+
/// `no_std` non-nightly of `net`
185+
#[cfg(all(not(feature = "std"), feature = "no_std_net"))]
186+
pub(crate) mod net {
187+
pub use no_std_net::*;
188+
}
189+
/// `no_std` nightly version of `net`
190+
#[cfg(all(not(feature = "std"), not(feature = "no_std_net")))]
191+
pub(crate) mod net {
192+
pub use core::net::*;
193+
}
194+
162195
#[cfg(feature = "std")]
163196
use std::{
164197
io,

url/src/parser.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ macro_rules! simple_enum_error {
7575
#[cfg(feature = "std")]
7676
impl std::error::Error for ParseError {}
7777

78+
#[cfg(feature = "unstable")]
79+
impl core::error::Error for ParseError {}
80+
7881
simple_enum_error! {
7982
EmptyHost => "empty host",
8083
IdnaError => "invalid international domain name",

url/tests/unit.rs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,64 @@
77
// except according to those terms.
88

99
//! Unit tests
10+
#![no_std]
11+
#![cfg_attr(
12+
all(
13+
not(feature = "std"),
14+
not(feature = "no_std_net"),
15+
feature = "unstable"
16+
),
17+
feature(ip_in_core)
18+
)]
19+
#![cfg_attr(
20+
all(not(feature = "std"), feature = "unstable"),
21+
feature(error_in_core)
22+
)]
1023

24+
#[cfg(feature = "std")]
25+
extern crate std;
26+
27+
#[macro_use]
28+
extern crate alloc;
29+
30+
#[cfg(not(feature = "alloc"))]
31+
compile_error!("the `alloc` feature must be enabled");
32+
33+
#[cfg(not(any(feature = "no_std_net", feature = "std", feature = "unstable")))]
34+
compile_error!(
35+
"Either the `no_std_net`, `std` or, on nightly, the `unstable` feature, must be enabled"
36+
);
37+
38+
use alloc::borrow::Cow;
39+
use alloc::borrow::ToOwned;
40+
use alloc::string::{String, ToString};
41+
use alloc::vec::Vec;
1142
use core::cell::{Cell, RefCell};
12-
use no_std_net::{Ipv4Addr, Ipv6Addr};
13-
use std::borrow::Cow;
1443
#[cfg(feature = "std")]
1544
use std::path::{Path, PathBuf};
1645
use url::{form_urlencoded, Host, Origin, Url};
1746

47+
/// `std` version of `net`
48+
#[cfg(feature = "std")]
49+
pub(crate) mod net {
50+
pub use std::net::*;
51+
}
52+
/// `no_std` non-nightly of `net`
53+
#[cfg(all(not(feature = "std"), feature = "no_std_net"))]
54+
pub(crate) mod net {
55+
pub use no_std_net::*;
56+
}
57+
/// `no_std` nightly version of `net`
58+
#[cfg(all(not(feature = "std"), not(feature = "no_std_net")))]
59+
pub(crate) mod net {
60+
pub use core::net::*;
61+
}
62+
63+
use crate::net::{Ipv4Addr, Ipv6Addr};
64+
1865
#[test]
1966
fn size() {
20-
use std::mem::size_of;
67+
use core::mem::size_of;
2168
assert_eq!(size_of::<Url>(), size_of::<Option<Url>>());
2269
}
2370

@@ -262,6 +309,7 @@ fn issue_124() {
262309
}
263310

264311
#[test]
312+
#[cfg(feature = "std")]
265313
fn test_equality() {
266314
use std::collections::hash_map::DefaultHasher;
267315
use std::hash::{Hash, Hasher};
@@ -542,6 +590,7 @@ fn test_leading_dots() {
542590
}
543591

544592
#[test]
593+
#[cfg(feature = "std")]
545594
/// https://github.com/servo/rust-url/issues/302
546595
fn test_origin_hash() {
547596
use std::collections::hash_map::DefaultHasher;

0 commit comments

Comments
 (0)