From 1d77ba2bc447b83307327bd78d1ea236a1625989 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Thu, 9 Apr 2020 19:20:10 +0300 Subject: [PATCH] Add no_std + alloc support --- .travis.yml | 2 ++ Cargo.toml | 5 +++++ src/__test_api.rs | 5 +++++ src/decompose.rs | 6 +++--- src/lib.rs | 11 ++++++++++- src/no_std_prelude.rs | 6 ++++++ src/normalize.rs | 4 ++-- src/recompose.rs | 2 +- src/stream_safe.rs | 8 ++++++-- src/test.rs | 5 ++++- 10 files changed, 44 insertions(+), 10 deletions(-) create mode 100755 src/no_std_prelude.rs diff --git a/.travis.yml b/.travis.yml index a736bf7..f1132c9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,9 +7,11 @@ sudo: false script: - cargo build --verbose - cargo test --verbose + - cargo test --verbose --no-default-features - cargo package - cd target/package/unicode-normalization-* - cargo test --verbose + - cargo test --verbose --no-default-features notifications: email: on_success: never diff --git a/Cargo.toml b/Cargo.toml index 423eeef..6cf8fa7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,3 +25,8 @@ exclude = [ "target/*", "Cargo.lock", "scripts/tmp", "*.txt", "tests/*" ] [dependencies.tinyvec] version = "0.3.3" features = ["alloc"] + + +[features] +default = ["std"] +std = [] diff --git a/src/__test_api.rs b/src/__test_api.rs index 934fa72..f1a3f92 100644 --- a/src/__test_api.rs +++ b/src/__test_api.rs @@ -4,10 +4,15 @@ // // If you're caught using this outside this crates tests/, you get to clean up the mess. +#[cfg(not(feature = "std"))] +use crate::no_std_prelude::*; + use crate::stream_safe::StreamSafe; + pub fn stream_safe(s: &str) -> String { StreamSafe::new(s.chars()).collect() } + pub mod quick_check { pub use crate::quick_check::*; } diff --git a/src/decompose.rs b/src/decompose.rs index 0ba15d0..f228023 100644 --- a/src/decompose.rs +++ b/src/decompose.rs @@ -7,9 +7,9 @@ // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::fmt::{self, Write}; -use std::iter::Fuse; -use std::ops::Range; +use core::fmt::{self, Write}; +use core::iter::Fuse; +use core::ops::Range; use tinyvec::TinyVec; #[derive(Clone)] diff --git a/src/lib.rs b/src/lib.rs index dc58c50..6749adc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,6 +42,13 @@ html_logo_url = "https://unicode-rs.github.io/unicode-rs_sm.png", html_favicon_url = "https://unicode-rs.github.io/unicode-rs_sm.png" )] +#![cfg_attr(not(feature = "std"), no_std)] + +#[cfg(not(feature = "std"))] +extern crate alloc; + +#[cfg(feature = "std")] +extern crate core; extern crate tinyvec; @@ -54,7 +61,9 @@ pub use crate::quick_check::{ pub use crate::recompose::Recompositions; pub use crate::stream_safe::StreamSafe; pub use crate::tables::UNICODE_VERSION; -use std::str::Chars; +use core::str::Chars; + +mod no_std_prelude; mod decompose; mod lookups; diff --git a/src/no_std_prelude.rs b/src/no_std_prelude.rs new file mode 100755 index 0000000..838d122 --- /dev/null +++ b/src/no_std_prelude.rs @@ -0,0 +1,6 @@ +#[cfg(not(feature = "std"))] +pub use alloc::{ + str::Chars, + string::{String, ToString}, + vec::Vec, +}; diff --git a/src/normalize.rs b/src/normalize.rs index 3d54360..1097c42 100644 --- a/src/normalize.rs +++ b/src/normalize.rs @@ -12,8 +12,8 @@ use crate::lookups::{ canonical_fully_decomposed, compatibility_fully_decomposed, composition_table, }; -use std::char; -use std::ops::FnMut; + +use core::{char, ops::FnMut}; /// Compute canonical Unicode decomposition for character. /// See [Unicode Standard Annex #15](http://www.unicode.org/reports/tr15/) diff --git a/src/recompose.rs b/src/recompose.rs index 29022e2..2a1960a 100644 --- a/src/recompose.rs +++ b/src/recompose.rs @@ -9,7 +9,7 @@ // except according to those terms. use crate::decompose::Decompositions; -use std::fmt::{self, Write}; +use core::fmt::{self, Write}; use tinyvec::TinyVec; #[derive(Clone)] diff --git a/src/stream_safe.rs b/src/stream_safe.rs index 80123f0..1ba7d76 100644 --- a/src/stream_safe.rs +++ b/src/stream_safe.rs @@ -107,7 +107,11 @@ mod tests { use super::{classify_nonstarters, StreamSafe}; use crate::lookups::canonical_combining_class; use crate::normalize::decompose_compatible; - use std::char; + + #[cfg(not(feature = "std"))] + use crate::no_std_prelude::*; + + use core::char; fn stream_safe(s: &str) -> String { StreamSafe::new(s.chars()).collect() @@ -131,7 +135,7 @@ mod tests { None => continue, }; let c = classify_nonstarters(ch); - let mut s = vec![]; + let mut s = Vec::new(); decompose_compatible(ch, |c| s.push(c)); assert_eq!(s.len(), c.decomposition_len); diff --git a/src/test.rs b/src/test.rs index 1a0f13a..2e87a87 100644 --- a/src/test.rs +++ b/src/test.rs @@ -10,7 +10,10 @@ use super::char::is_combining_mark; use super::UnicodeNormalization; -use std::char; +use core::char; + +#[cfg(not(feature = "std"))] +use crate::no_std_prelude::*; #[test] fn test_nfd() {