Bladeren bron

Make idna no_std compatible

Add default feature flag "std" that enables a `std::error::Error` impl for `Errors`.
Mads Marquart 3 jaren geleden
bovenliggende
commit
a24c4d6264
4 gewijzigde bestanden met toevoegingen van 31 en 9 verwijderingen
  1. 8 2
      idna/Cargo.toml
  2. 12 0
      idna/src/lib.rs
  3. 4 3
      idna/src/punycode.rs
  4. 7 4
      idna/src/uts46.rs

+ 8 - 2
idna/Cargo.toml

@@ -3,6 +3,7 @@ name = "idna"
 version = "0.3.0"
 version = "0.3.0"
 authors = ["The rust-url developers"]
 authors = ["The rust-url developers"]
 description = "IDNA (Internationalizing Domain Names in Applications) and Punycode."
 description = "IDNA (Internationalizing Domain Names in Applications) and Punycode."
+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"
 autotests = false
 autotests = false
@@ -12,6 +13,11 @@ rust-version = "1.51"
 [lib]
 [lib]
 doctest = false
 doctest = false
 
 
+[features]
+default = ["std"]
+std = ["alloc", "unicode-bidi/std", "unicode-normalization/std"]
+alloc = []
+
 [[test]]
 [[test]]
 name = "tests"
 name = "tests"
 harness = false
 harness = false
@@ -26,8 +32,8 @@ tester = "0.9"
 serde_json = "1.0"
 serde_json = "1.0"
 
 
 [dependencies]
 [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]]
 [[bench]]
 name = "all"
 name = "all"

+ 12 - 0
idna/src/lib.rs

@@ -31,11 +31,23 @@
 //! > This document specifies a mechanism
 //! > This document specifies a mechanism
 //! > that minimizes the impact of this transition for client software,
 //! > that minimizes the impact of this transition for client software,
 //! > allowing client software to access domains that are valid under either system.
 //! > 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)]
 #[cfg(test)]
 #[macro_use]
 #[macro_use]
 extern crate assert_matches;
 extern crate assert_matches;
 
 
+use alloc::string::String;
+
 pub mod punycode;
 pub mod punycode;
 mod uts46;
 mod uts46;
 
 

+ 4 - 3
idna/src/punycode.rs

@@ -13,8 +13,9 @@
 //! `encode_str` and `decode_to_string` provide convenience wrappers
 //! `encode_str` and `decode_to_string` provide convenience wrappers
 //! that convert from and to Rust’s UTF-8 based `str` and `String` types.
 //! 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
 // Bootstring parameters for Punycode
 static BASE: u32 = 36;
 static BASE: u32 = 36;
@@ -168,7 +169,7 @@ impl Decoder {
 }
 }
 
 
 pub(crate) struct Decode<'a> {
 pub(crate) struct Decode<'a> {
-    base: std::str::Chars<'a>,
+    base: core::str::Chars<'a>,
     pub(crate) insertions: &'a [(usize, char)],
     pub(crate) insertions: &'a [(usize, char)],
     inserted: usize,
     inserted: usize,
     position: usize,
     position: usize,

+ 7 - 4
idna/src/uts46.rs

@@ -11,7 +11,9 @@
 
 
 use self::Mapping::*;
 use self::Mapping::*;
 use crate::punycode;
 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_bidi::{bidi_class, BidiClass};
 use unicode_normalization::char::is_combining_mark;
 use unicode_normalization::char::is_combining_mark;
 use unicode_normalization::{is_nfc, UnicodeNormalization};
 use unicode_normalization::{is_nfc, UnicodeNormalization};
@@ -70,10 +72,10 @@ fn find_char(codepoint: char) -> &'static Mapping {
 }
 }
 
 
 struct Mapper<'a> {
 struct Mapper<'a> {
-    chars: std::str::Chars<'a>,
+    chars: core::str::Chars<'a>,
     config: Config,
     config: Config,
     errors: &'a mut Errors,
     errors: &'a mut Errors,
-    slice: Option<std::str::Chars<'static>>,
+    slice: Option<core::str::Chars<'static>>,
 }
 }
 
 
 impl<'a> Iterator for Mapper<'a> {
 impl<'a> Iterator for Mapper<'a> {
@@ -708,7 +710,8 @@ impl From<Errors> for Result<(), Errors> {
     }
     }
 }
 }
 
 
-impl StdError for Errors {}
+#[cfg(feature = "std")]
+impl std::error::Error for Errors {}
 
 
 impl fmt::Display for Errors {
 impl fmt::Display for Errors {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {