From c945ff9e789b198a99144806c0f50fd6382cdd95 Mon Sep 17 00:00:00 2001 From: Zachary Pierce Date: Mon, 7 May 2018 07:37:05 -0700 Subject: [PATCH] Add no_std + alloc support for ucd-util --- ci/script.sh | 2 ++ ucd-util/Cargo.toml | 7 +++++++ ucd-util/src/hangul.rs | 3 +++ ucd-util/src/ideograph.rs | 3 +++ ucd-util/src/lib.rs | 11 +++++++++++ ucd-util/src/name.rs | 6 ++++++ 6 files changed, 32 insertions(+) diff --git a/ci/script.sh b/ci/script.sh index 39810f6..bdbf7d8 100755 --- a/ci/script.sh +++ b/ci/script.sh @@ -18,4 +18,6 @@ cargo doc --all --verbose cargo test --all --verbose if [ "$TRAVIS_RUST_VERSION" = "nightly" ]; then cargo bench --all --verbose --no-run + # Test no_std + alloc mode for ucd-util + cargo test --lib --manifest-path ucd-util/Cargo.toml --no-default-features --features "alloc" fi diff --git a/ucd-util/Cargo.toml b/ucd-util/Cargo.toml index ee41ece..93c0a7c 100644 --- a/ucd-util/Cargo.toml +++ b/ucd-util/Cargo.toml @@ -11,3 +11,10 @@ repository = "https://github.com/BurntSushi/rucd" readme = "README.md" keywords = ["unicode", "database", "character", "property"] license = "MIT/Apache-2.0" + +[features] +default = ["std"] +# Disable this on-by-default feature and add "alloc" to allow use in no_std builds +std = [] +# Required for use in no_std builds, presently nightly-only +alloc = [] \ No newline at end of file diff --git a/ucd-util/src/hangul.rs b/ucd-util/src/hangul.rs index dd3bbc8..ca63361 100644 --- a/ucd-util/src/hangul.rs +++ b/ucd-util/src/hangul.rs @@ -1,3 +1,6 @@ +#[cfg(all(feature = "alloc", not(feature = "std")))] +use alloc::string::{String, ToString}; + use unicode_tables::jamo_short_name::JAMO_SHORT_NAME; // This implementation should correspond to the algorithms described in diff --git a/ucd-util/src/ideograph.rs b/ucd-util/src/ideograph.rs index 0502c6e..a774ea3 100644 --- a/ucd-util/src/ideograph.rs +++ b/ucd-util/src/ideograph.rs @@ -1,3 +1,6 @@ +#[cfg(all(feature = "alloc", not(feature = "std")))] +use alloc::string::String; + /// A set of ranges that corresponds to the set of all ideograph codepoints. /// /// These ranges are defined in Unicode 4.8 Table 4-13. diff --git a/ucd-util/src/lib.rs b/ucd-util/src/lib.rs index 4dc5606..5161947 100644 --- a/ucd-util/src/lib.rs +++ b/ucd-util/src/lib.rs @@ -9,8 +9,19 @@ canonicalization functions, you'll need to supply your own table, which can be generated using `ucd-generate`. */ +#![cfg_attr(not(feature = "std"), no_std)] +#![cfg_attr(all(feature = "alloc", not(feature = "std")), feature(alloc))] + #![deny(missing_docs)] +#[cfg(all(test, not(feature = "std")))] +#[macro_use] +extern crate std; + +#[cfg(all(feature = "alloc", not(feature = "std")))] +#[macro_use] +extern crate alloc; + mod hangul; mod ideograph; mod name; diff --git a/ucd-util/src/name.rs b/ucd-util/src/name.rs index 20c2f4b..936b759 100644 --- a/ucd-util/src/name.rs +++ b/ucd-util/src/name.rs @@ -1,3 +1,6 @@ +#[cfg(all(feature = "alloc", not(feature = "std")))] +use alloc::string::String; + /// Normalize the given character name in place according to UAX44-LM2. /// /// See: http://unicode.org/reports/tr44/#UAX44-LM2 @@ -137,6 +140,9 @@ fn symbolic_name_normalize_bytes(slice: &mut [u8]) -> &mut [u8] { #[cfg(test)] mod tests { + #[cfg(all(feature = "alloc", not(feature = "std")))] + use alloc::string::{String, ToString}; + use super::{ character_name_normalize, character_name_normalize_bytes, symbolic_name_normalize, symbolic_name_normalize_bytes,