Просмотр исходного кода

Make data-url no_std compatible

Mads Marquart 3 лет назад
Родитель
Сommit
070b8a3d9f
4 измененных файлов с 24 добавлено и 2 удалено
  1. 6 0
      data-url/Cargo.toml
  2. 2 0
      data-url/src/forgiving_base64.rs
  3. 13 0
      data-url/src/lib.rs
  4. 3 2
      data-url/src/mime.rs

+ 6 - 0
data-url/Cargo.toml

@@ -3,12 +3,18 @@ name = "data-url"
 version = "0.2.0"
 version = "0.2.0"
 authors = ["Simon Sapin <simon.sapin@exyr.org>"]
 authors = ["Simon Sapin <simon.sapin@exyr.org>"]
 description = "Processing of data: URL according to WHATWG’s Fetch Standard"
 description = "Processing of data: URL according to WHATWG’s Fetch Standard"
+categories = ["no_std"]
 repository = "https://github.com/servo/rust-url"
 repository = "https://github.com/servo/rust-url"
 license = "MIT OR Apache-2.0"
 license = "MIT OR Apache-2.0"
 edition = "2018"
 edition = "2018"
 autotests = false
 autotests = false
 rust-version = "1.51"
 rust-version = "1.51"
 
 
+[features]
+default = ["std"]
+std = ["alloc"]
+alloc = []
+
 [dev-dependencies]
 [dev-dependencies]
 tester = "0.9"
 tester = "0.9"
 serde = {version = "1.0", features = ["derive"]}
 serde = {version = "1.0", features = ["derive"]}

+ 2 - 0
data-url/src/forgiving_base64.rs

@@ -1,5 +1,7 @@
 //! <https://infra.spec.whatwg.org/#forgiving-base64-decode>
 //! <https://infra.spec.whatwg.org/#forgiving-base64-decode>
 
 
+use alloc::vec::Vec;
+
 #[derive(Debug)]
 #[derive(Debug)]
 pub struct InvalidBase64(InvalidBase64Details);
 pub struct InvalidBase64(InvalidBase64Details);
 
 

+ 13 - 0
data-url/src/lib.rs

@@ -14,6 +14,19 @@
 //! assert_eq!(body, b"Hello World!");
 //! assert_eq!(body, b"Hello World!");
 //! assert!(fragment.is_none());
 //! 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 {
 macro_rules! require {
     ($condition: expr) => {
     ($condition: expr) => {

+ 3 - 2
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;
 
 
 /// <https://mimesniff.spec.whatwg.org/#mime-type-representation>
 /// <https://mimesniff.spec.whatwg.org/#mime-type-representation>
 #[derive(Debug, PartialEq, Eq)]
 #[derive(Debug, PartialEq, Eq)]