From 964fb73ebf1a9ccb6995fad6c2f69e46b2764ac5 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Tue, 21 Feb 2023 05:13:20 +0100 Subject: [PATCH 1/6] Add `std` feature to `percent_encoding` For future compatibility --- percent_encoding/Cargo.toml | 4 +++- percent_encoding/src/lib.rs | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/percent_encoding/Cargo.toml b/percent_encoding/Cargo.toml index cbd8220c3..5dbc69bd9 100644 --- a/percent_encoding/Cargo.toml +++ b/percent_encoding/Cargo.toml @@ -3,11 +3,13 @@ name = "percent-encoding" version = "2.2.0" authors = ["The rust-url developers"] description = "Percent encoding and decoding" +categories = ["no_std"] repository = "https://github.com/servo/rust-url/" license = "MIT OR Apache-2.0" edition = "2018" rust-version = "1.51" [features] -default = ["alloc"] +default = ["std"] +std = ["alloc"] alloc = [] diff --git a/percent_encoding/src/lib.rs b/percent_encoding/src/lib.rs index cf7462018..e02a43949 100644 --- a/percent_encoding/src/lib.rs +++ b/percent_encoding/src/lib.rs @@ -36,8 +36,12 @@ //! //! assert_eq!(utf8_percent_encode("foo ", FRAGMENT).to_string(), "foo%20%3Cbar%3E"); //! ``` - #![no_std] + +// For forwards compatibility +#[cfg(feature = "std")] +extern crate std as _; + #[cfg(feature = "alloc")] extern crate alloc; From 57ea92efcad65cb81b3d8a5a159ea9ce64f90596 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Tue, 21 Feb 2023 06:41:27 +0100 Subject: [PATCH 2/6] Make form_urlencoded no_std compatible --- form_urlencoded/Cargo.toml | 8 +++++++- form_urlencoded/src/lib.rs | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/form_urlencoded/Cargo.toml b/form_urlencoded/Cargo.toml index bdbb5cec3..39727b5b2 100644 --- a/form_urlencoded/Cargo.toml +++ b/form_urlencoded/Cargo.toml @@ -3,6 +3,7 @@ name = "form_urlencoded" version = "1.1.0" authors = ["The rust-url developers"] description = "Parser and serializer for the application/x-www-form-urlencoded syntax, as used by HTML forms." +categories = ["no_std"] repository = "https://github.com/servo/rust-url" license = "MIT OR Apache-2.0" edition = "2018" @@ -11,5 +12,10 @@ rust-version = "1.51" [lib] test = false +[features] +default = ["std"] +std = ["alloc", "percent-encoding/std"] +alloc = ["percent-encoding/alloc"] + [dependencies] -percent-encoding = { version = "2.2.0", path = "../percent_encoding" } +percent-encoding = { version = "2.2.0", default-features = false, path = "../percent_encoding" } diff --git a/form_urlencoded/src/lib.rs b/form_urlencoded/src/lib.rs index 60ad2aafd..30221ad22 100644 --- a/form_urlencoded/src/lib.rs +++ b/form_urlencoded/src/lib.rs @@ -12,10 +12,21 @@ //! //! Converts between a string (such as an URL’s query string) //! and a sequence of (name, value) pairs. +#![no_std] +// For forwards compatibility +#[cfg(feature = "std")] +extern crate std as _; + +extern crate alloc; + +#[cfg(not(feature = "alloc"))] +compile_error!("the `alloc` feature must currently be enabled"); + +use alloc::borrow::{Borrow, Cow, ToOwned}; +use alloc::string::String; +use core::str; use percent_encoding::{percent_decode, percent_encode_byte}; -use std::borrow::{Borrow, Cow}; -use std::str; /// Convert a byte string in the `application/x-www-form-urlencoded` syntax /// into a iterator of (name, value) pairs. From 070b8a3d9f518d7c9bfee813f2d0961330baad37 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Tue, 21 Feb 2023 05:14:42 +0100 Subject: [PATCH 3/6] Make data-url no_std compatible --- data-url/Cargo.toml | 6 ++++++ data-url/src/forgiving_base64.rs | 2 ++ data-url/src/lib.rs | 13 +++++++++++++ data-url/src/mime.rs | 5 +++-- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/data-url/Cargo.toml b/data-url/Cargo.toml index 117d8f5e5..514fb620c 100644 --- a/data-url/Cargo.toml +++ b/data-url/Cargo.toml @@ -3,12 +3,18 @@ name = "data-url" version = "0.2.0" authors = ["Simon Sapin "] description = "Processing of data: URL according to WHATWG’s Fetch Standard" +categories = ["no_std"] repository = "https://github.com/servo/rust-url" license = "MIT OR Apache-2.0" edition = "2018" autotests = false rust-version = "1.51" +[features] +default = ["std"] +std = ["alloc"] +alloc = [] + [dev-dependencies] tester = "0.9" serde = {version = "1.0", features = ["derive"]} diff --git a/data-url/src/forgiving_base64.rs b/data-url/src/forgiving_base64.rs index 4f8550ed2..390d88bc4 100644 --- a/data-url/src/forgiving_base64.rs +++ b/data-url/src/forgiving_base64.rs @@ -1,5 +1,7 @@ //! +use alloc::vec::Vec; + #[derive(Debug)] pub struct InvalidBase64(InvalidBase64Details); diff --git a/data-url/src/lib.rs b/data-url/src/lib.rs index 4a4f376f4..34aedd186 100644 --- a/data-url/src/lib.rs +++ b/data-url/src/lib.rs @@ -14,6 +14,19 @@ //! assert_eq!(body, b"Hello World!"); //! assert!(fragment.is_none()); //! ``` +#![no_std] + +// For forwards compatibility +#[cfg(feature = "std")] +extern crate std as _; + +#[macro_use] +extern crate alloc; + +#[cfg(not(feature = "alloc"))] +compile_error!("the `alloc` feature must currently be enabled"); + +use alloc::{string::String, vec::Vec}; macro_rules! require { ($condition: expr) => { diff --git a/data-url/src/mime.rs b/data-url/src/mime.rs index b3e95bcba..b4173d8f1 100644 --- a/data-url/src/mime.rs +++ b/data-url/src/mime.rs @@ -1,5 +1,6 @@ -use std::fmt::{self, Write}; -use std::str::FromStr; +use alloc::{borrow::ToOwned, string::String, vec::Vec}; +use core::fmt::{self, Write}; +use core::str::FromStr; /// #[derive(Debug, PartialEq, Eq)] From a24c4d62645c677a8fedd232446623e275e5a338 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 1 Mar 2023 15:57:14 +0100 Subject: [PATCH 4/6] Make idna no_std compatible Add default feature flag "std" that enables a `std::error::Error` impl for `Errors`. --- idna/Cargo.toml | 10 ++++++++-- idna/src/lib.rs | 12 ++++++++++++ idna/src/punycode.rs | 7 ++++--- idna/src/uts46.rs | 11 +++++++---- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/idna/Cargo.toml b/idna/Cargo.toml index 942f1222a..7fd9a53f1 100644 --- a/idna/Cargo.toml +++ b/idna/Cargo.toml @@ -3,6 +3,7 @@ name = "idna" version = "0.3.0" authors = ["The rust-url developers"] description = "IDNA (Internationalizing Domain Names in Applications) and Punycode." +categories = ["no_std"] repository = "https://github.com/servo/rust-url/" license = "MIT OR Apache-2.0" autotests = false @@ -12,6 +13,11 @@ rust-version = "1.51" [lib] doctest = false +[features] +default = ["std"] +std = ["alloc", "unicode-bidi/std", "unicode-normalization/std"] +alloc = [] + [[test]] name = "tests" harness = false @@ -26,8 +32,8 @@ tester = "0.9" serde_json = "1.0" [dependencies] -unicode-bidi = "0.3" -unicode-normalization = "0.1.17" +unicode-bidi = { version = "0.3.10", default-features = false, features = ["hardcoded-data"] } +unicode-normalization = { version = "0.1.22", default-features = false } [[bench]] name = "all" diff --git a/idna/src/lib.rs b/idna/src/lib.rs index 37d638741..909fb1dd1 100644 --- a/idna/src/lib.rs +++ b/idna/src/lib.rs @@ -31,11 +31,23 @@ //! > This document specifies a mechanism //! > that minimizes the impact of this transition for client software, //! > allowing client software to access domains that are valid under either system. +#![no_std] + +// For forwards compatibility +#[cfg(feature = "std")] +extern crate std; + +extern crate alloc; + +#[cfg(not(feature = "alloc"))] +compile_error!("the `alloc` feature must currently be enabled"); #[cfg(test)] #[macro_use] extern crate assert_matches; +use alloc::string::String; + pub mod punycode; mod uts46; diff --git a/idna/src/punycode.rs b/idna/src/punycode.rs index 21955f359..faef379ca 100644 --- a/idna/src/punycode.rs +++ b/idna/src/punycode.rs @@ -13,8 +13,9 @@ //! `encode_str` and `decode_to_string` provide convenience wrappers //! that convert from and to Rust’s UTF-8 based `str` and `String` types. -use std::char; -use std::u32; +use alloc::{string::String, vec::Vec}; +use core::char; +use core::u32; // Bootstring parameters for Punycode static BASE: u32 = 36; @@ -168,7 +169,7 @@ impl Decoder { } pub(crate) struct Decode<'a> { - base: std::str::Chars<'a>, + base: core::str::Chars<'a>, pub(crate) insertions: &'a [(usize, char)], inserted: usize, position: usize, diff --git a/idna/src/uts46.rs b/idna/src/uts46.rs index dc832fc49..fb39c1512 100644 --- a/idna/src/uts46.rs +++ b/idna/src/uts46.rs @@ -11,7 +11,9 @@ use self::Mapping::*; use crate::punycode; -use std::{error::Error as StdError, fmt}; + +use alloc::string::String; +use core::fmt; use unicode_bidi::{bidi_class, BidiClass}; use unicode_normalization::char::is_combining_mark; use unicode_normalization::{is_nfc, UnicodeNormalization}; @@ -70,10 +72,10 @@ fn find_char(codepoint: char) -> &'static Mapping { } struct Mapper<'a> { - chars: std::str::Chars<'a>, + chars: core::str::Chars<'a>, config: Config, errors: &'a mut Errors, - slice: Option>, + slice: Option>, } impl<'a> Iterator for Mapper<'a> { @@ -708,7 +710,8 @@ impl From for Result<(), Errors> { } } -impl StdError for Errors {} +#[cfg(feature = "std")] +impl std::error::Error for Errors {} impl fmt::Display for Errors { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { From 934edd4b1ab85c1c618e7aff5c7e4046888558cf Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Tue, 5 Oct 2021 14:36:13 +0200 Subject: [PATCH 5/6] Test no_std support in CI --- .github/workflows/main.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a52a6db13..154c62e3f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -47,6 +47,8 @@ jobs: matrix.os == 'windows-latest' && matrix.rust == 'nightly' run: cargo test --test debugger_visualizer --features "url/serde,url/debugger_visualizer" -- --test-threads=1 + - name: Test `no_std` support + run: cargo test --no-default-features --features=alloc WASM: runs-on: ubuntu-latest From 4afc9829dcb9bbcbbfb49c8a30c39734fd906384 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sat, 1 Apr 2023 14:44:11 +0300 Subject: [PATCH 6/6] Fix error wording in `data-url` and `idna` when missing `alloc` feature --- data-url/src/lib.rs | 2 +- idna/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/data-url/src/lib.rs b/data-url/src/lib.rs index 34aedd186..99e71a97e 100644 --- a/data-url/src/lib.rs +++ b/data-url/src/lib.rs @@ -24,7 +24,7 @@ extern crate std as _; extern crate alloc; #[cfg(not(feature = "alloc"))] -compile_error!("the `alloc` feature must currently be enabled"); +compile_error!("the `alloc` feature must be enabled"); use alloc::{string::String, vec::Vec}; diff --git a/idna/src/lib.rs b/idna/src/lib.rs index 909fb1dd1..92914f9ba 100644 --- a/idna/src/lib.rs +++ b/idna/src/lib.rs @@ -40,7 +40,7 @@ extern crate std; extern crate alloc; #[cfg(not(feature = "alloc"))] -compile_error!("the `alloc` feature must currently be enabled"); +compile_error!("the `alloc` feature must be enabled"); #[cfg(test)] #[macro_use]