Skip to content

Commit 8446ca5

Browse files
committed
Make idna no_std compatible
Add default feature flag "std" that enables a `std::error::Error` impl for `Errors`.
1 parent 194122c commit 8446ca5

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

idna/Cargo.toml

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ rust-version = "1.45"
1212
[lib]
1313
doctest = false
1414

15+
[features]
16+
default = ["std"]
17+
std = []
18+
1519
[[test]]
1620
name = "tests"
1721
harness = false
@@ -26,8 +30,8 @@ tester = "0.9"
2630
serde_json = "1.0"
2731

2832
[dependencies]
29-
unicode-bidi = "0.3"
30-
unicode-normalization = "0.1.17"
33+
unicode-bidi = { version = "0.3.7", default-features = false }
34+
unicode-normalization = { version = "0.1.17", default-features = false }
3135

3236
[[bench]]
3337
name = "all"

idna/src/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,18 @@
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+
extern crate alloc;
37+
38+
#[cfg(feature = "std")]
39+
extern crate std;
3440

35-
#[cfg(test)]
3641
#[macro_use]
3742
extern crate assert_matches;
3843

44+
use alloc::string::String;
45+
3946
pub mod punycode;
4047
mod uts46;
4148

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> {
@@ -697,7 +699,8 @@ impl From<Errors> for Result<(), Errors> {
697699
}
698700
}
699701

700-
impl StdError for Errors {}
702+
#[cfg(feature = "std")]
703+
impl std::error::Error for Errors {}
701704

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

0 commit comments

Comments
 (0)