Skip to content

Commit 6385c81

Browse files
authored
Merge pull request servo#722 from madsmtm/partial_no_std
`no_std` support for `form_urlencoded`, `data-url` and `idna`
2 parents 1006cf5 + 4afc982 commit 6385c81

File tree

13 files changed

+85
-16
lines changed

13 files changed

+85
-16
lines changed

.github/workflows/main.yml

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ jobs:
4747
matrix.os == 'windows-latest' &&
4848
matrix.rust == 'nightly'
4949
run: cargo test --test debugger_visualizer --features "url/serde,url/debugger_visualizer" -- --test-threads=1
50+
- name: Test `no_std` support
51+
run: cargo test --no-default-features --features=alloc
5052

5153
WASM:
5254
runs-on: ubuntu-latest

data-url/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ name = "data-url"
33
version = "0.2.0"
44
authors = ["Simon Sapin <[email protected]>"]
55
description = "Processing of data: URL according to WHATWG’s Fetch Standard"
6+
categories = ["no_std"]
67
repository = "https://github.com/servo/rust-url"
78
license = "MIT OR Apache-2.0"
89
edition = "2018"
910
autotests = false
1011
rust-version = "1.51"
1112

13+
[features]
14+
default = ["std"]
15+
std = ["alloc"]
16+
alloc = []
17+
1218
[dev-dependencies]
1319
tester = "0.9"
1420
serde = {version = "1.0", features = ["derive"]}

data-url/src/forgiving_base64.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! <https://infra.spec.whatwg.org/#forgiving-base64-decode>
22
3+
use alloc::vec::Vec;
4+
35
#[derive(Debug)]
46
pub struct InvalidBase64(InvalidBase64Details);
57

data-url/src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@
1414
//! assert_eq!(body, b"Hello World!");
1515
//! assert!(fragment.is_none());
1616
//! ```
17+
#![no_std]
18+
19+
// For forwards compatibility
20+
#[cfg(feature = "std")]
21+
extern crate std as _;
22+
23+
#[macro_use]
24+
extern crate alloc;
25+
26+
#[cfg(not(feature = "alloc"))]
27+
compile_error!("the `alloc` feature must be enabled");
28+
29+
use alloc::{string::String, vec::Vec};
1730

1831
macro_rules! require {
1932
($condition: expr) => {

data-url/src/mime.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use std::fmt::{self, Write};
2-
use std::str::FromStr;
1+
use alloc::{borrow::ToOwned, string::String, vec::Vec};
2+
use core::fmt::{self, Write};
3+
use core::str::FromStr;
34

45
/// <https://mimesniff.spec.whatwg.org/#mime-type-representation>
56
#[derive(Debug, PartialEq, Eq)]

form_urlencoded/Cargo.toml

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name = "form_urlencoded"
33
version = "1.1.0"
44
authors = ["The rust-url developers"]
55
description = "Parser and serializer for the application/x-www-form-urlencoded syntax, as used by HTML forms."
6+
categories = ["no_std"]
67
repository = "https://github.com/servo/rust-url"
78
license = "MIT OR Apache-2.0"
89
edition = "2018"
@@ -11,5 +12,10 @@ rust-version = "1.51"
1112
[lib]
1213
test = false
1314

15+
[features]
16+
default = ["std"]
17+
std = ["alloc", "percent-encoding/std"]
18+
alloc = ["percent-encoding/alloc"]
19+
1420
[dependencies]
15-
percent-encoding = { version = "2.2.0", path = "../percent_encoding" }
21+
percent-encoding = { version = "2.2.0", default-features = false, path = "../percent_encoding" }

form_urlencoded/src/lib.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,21 @@
1212
//!
1313
//! Converts between a string (such as an URL’s query string)
1414
//! and a sequence of (name, value) pairs.
15+
#![no_std]
1516

17+
// For forwards compatibility
18+
#[cfg(feature = "std")]
19+
extern crate std as _;
20+
21+
extern crate alloc;
22+
23+
#[cfg(not(feature = "alloc"))]
24+
compile_error!("the `alloc` feature must currently be enabled");
25+
26+
use alloc::borrow::{Borrow, Cow, ToOwned};
27+
use alloc::string::String;
28+
use core::str;
1629
use percent_encoding::{percent_decode, percent_encode_byte};
17-
use std::borrow::{Borrow, Cow};
18-
use std::str;
1930

2031
/// Convert a byte string in the `application/x-www-form-urlencoded` syntax
2132
/// into a iterator of (name, value) pairs.

idna/Cargo.toml

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name = "idna"
33
version = "0.3.0"
44
authors = ["The rust-url developers"]
55
description = "IDNA (Internationalizing Domain Names in Applications) and Punycode."
6+
categories = ["no_std"]
67
repository = "https://github.com/servo/rust-url/"
78
license = "MIT OR Apache-2.0"
89
autotests = false
@@ -12,6 +13,11 @@ rust-version = "1.51"
1213
[lib]
1314
doctest = false
1415

16+
[features]
17+
default = ["std"]
18+
std = ["alloc", "unicode-bidi/std", "unicode-normalization/std"]
19+
alloc = []
20+
1521
[[test]]
1622
name = "tests"
1723
harness = false
@@ -26,8 +32,8 @@ tester = "0.9"
2632
serde_json = "1.0"
2733

2834
[dependencies]
29-
unicode-bidi = "0.3"
30-
unicode-normalization = "0.1.17"
35+
unicode-bidi = { version = "0.3.10", default-features = false, features = ["hardcoded-data"] }
36+
unicode-normalization = { version = "0.1.22", default-features = false }
3137

3238
[[bench]]
3339
name = "all"

idna/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,23 @@
3131
//! > This document specifies a mechanism
3232
//! > that minimizes the impact of this transition for client software,
3333
//! > allowing client software to access domains that are valid under either system.
34+
#![no_std]
35+
36+
// For forwards compatibility
37+
#[cfg(feature = "std")]
38+
extern crate std;
39+
40+
extern crate alloc;
41+
42+
#[cfg(not(feature = "alloc"))]
43+
compile_error!("the `alloc` feature must be enabled");
3444

3545
#[cfg(test)]
3646
#[macro_use]
3747
extern crate assert_matches;
3848

49+
use alloc::string::String;
50+
3951
pub mod punycode;
4052
mod uts46;
4153

idna/src/punycode.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
//! `encode_str` and `decode_to_string` provide convenience wrappers
1414
//! that convert from and to Rust’s UTF-8 based `str` and `String` types.
1515
16-
use std::char;
17-
use std::u32;
16+
use alloc::{string::String, vec::Vec};
17+
use core::char;
18+
use core::u32;
1819

1920
// Bootstring parameters for Punycode
2021
static BASE: u32 = 36;
@@ -168,7 +169,7 @@ impl Decoder {
168169
}
169170

170171
pub(crate) struct Decode<'a> {
171-
base: std::str::Chars<'a>,
172+
base: core::str::Chars<'a>,
172173
pub(crate) insertions: &'a [(usize, char)],
173174
inserted: usize,
174175
position: usize,

idna/src/uts46.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
1212
use self::Mapping::*;
1313
use crate::punycode;
14-
use std::{error::Error as StdError, fmt};
14+
15+
use alloc::string::String;
16+
use core::fmt;
1517
use unicode_bidi::{bidi_class, BidiClass};
1618
use unicode_normalization::char::is_combining_mark;
1719
use unicode_normalization::{is_nfc, UnicodeNormalization};
@@ -70,10 +72,10 @@ fn find_char(codepoint: char) -> &'static Mapping {
7072
}
7173

7274
struct Mapper<'a> {
73-
chars: std::str::Chars<'a>,
75+
chars: core::str::Chars<'a>,
7476
config: Config,
7577
errors: &'a mut Errors,
76-
slice: Option<std::str::Chars<'static>>,
78+
slice: Option<core::str::Chars<'static>>,
7779
}
7880

7981
impl<'a> Iterator for Mapper<'a> {
@@ -708,7 +710,8 @@ impl From<Errors> for Result<(), Errors> {
708710
}
709711
}
710712

711-
impl StdError for Errors {}
713+
#[cfg(feature = "std")]
714+
impl std::error::Error for Errors {}
712715

713716
impl fmt::Display for Errors {
714717
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

percent_encoding/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ name = "percent-encoding"
33
version = "2.2.0"
44
authors = ["The rust-url developers"]
55
description = "Percent encoding and decoding"
6+
categories = ["no_std"]
67
repository = "https://github.com/servo/rust-url/"
78
license = "MIT OR Apache-2.0"
89
edition = "2018"
910
rust-version = "1.51"
1011

1112
[features]
12-
default = ["alloc"]
13+
default = ["std"]
14+
std = ["alloc"]
1315
alloc = []

percent_encoding/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@
3636
//!
3737
//! assert_eq!(utf8_percent_encode("foo <bar>", FRAGMENT).to_string(), "foo%20%3Cbar%3E");
3838
//! ```
39-
4039
#![no_std]
40+
41+
// For forwards compatibility
42+
#[cfg(feature = "std")]
43+
extern crate std as _;
44+
4145
#[cfg(feature = "alloc")]
4246
extern crate alloc;
4347

0 commit comments

Comments
 (0)