Bläddra i källkod

Make form_urlencoded no_std compatible

Mads Marquart 3 år sedan
förälder
incheckning
57ea92efca
2 ändrade filer med 20 tillägg och 3 borttagningar
  1. 7 1
      form_urlencoded/Cargo.toml
  2. 13 2
      form_urlencoded/src/lib.rs

+ 7 - 1
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" }

+ 13 - 2
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.