Skip to content

Commit

Permalink
Implement no_std support (#34)
Browse files Browse the repository at this point in the history
This implements no_std support for unicase. no_std support has been implemented for every Rust version starting with 1.36 which added stable support for the alloc crate. All of unicase's features are available on no_std.
  • Loading branch information
CryZe authored and seanmonstar committed Nov 12, 2019
1 parent 141699c commit fbab380
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ matrix:
- rust: 1.3.0
# verify `const fn`
- rust: 1.31.0
# verify `no_std`
- rust: stable
script:
- rustup target add thumbv6m-none-eabi
- cargo build --target thumbv6m-none-eabi

cache:
apt: true
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ repository = "https://github.com/seanmonstar/unicase"
documentation = "https://docs.rs/unicase"
license = "MIT/Apache-2.0"
readme = "README.md"
keywords = ["lowercase", "case", "case-insensitive", "case-folding"]
categories = ["internationalization", "text-processing"]
keywords = ["lowercase", "case", "case-insensitive", "case-folding", "no_std"]
categories = ["internationalization", "text-processing", "no-std"]

exclude = [
"scripts/*"
Expand Down
4 changes: 4 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ fn main() {
if rustc::is_min_version("1.31.0").map(|(is_min, _)| is_min).unwrap_or(true) {
println!("cargo:rustc-cfg=__unicase__const_fns");
}

if rustc::is_min_version("1.36.0").map(|(is_min, _)| is_min).unwrap_or(true) {
println!("cargo:rustc-cfg=__unicase__core_and_alloc");
}
}
2 changes: 1 addition & 1 deletion scripts/mapgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def remove_useless_comparison(case_line):
rs.write('// %s\n' % datetime.date.today())
rs.write('\n')
rs.write('use super::fold::Fold;\n\n')
rs.write('use std::char;\n');
rs.write('use core::char;\n');
rs.write("pub fn lookup(orig: char) -> Fold {\n")
rs.write(' // The code below is is intended to reduce the binary size from that of a simple 1:1 lookup table.\n')
rs.write(' // It exploits two facts:\n')
Expand Down
14 changes: 8 additions & 6 deletions src/ascii.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use alloc::string::String;
#[cfg(__unicase__iter_cmp)]
use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::ops::{Deref, DerefMut};
use core::str::FromStr;
#[cfg(not(__unicase__core_and_alloc))]
#[allow(deprecated, unused)]
use std::ascii::AsciiExt;
#[cfg(__unicase__iter_cmp)]
use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};
use std::str::FromStr;

use super::{Ascii, Encoding, UniCase};

Expand Down
31 changes: 25 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
#![cfg_attr(test, deny(warnings))]
#![doc(html_root_url = "https://docs.rs/unicase/2.5.1")]
#![cfg_attr(feature = "nightly", feature(test))]
#![cfg_attr(
all(
__unicase__core_and_alloc,
not(test),
),
no_std,
)]

//! # UniCase
//!
Expand Down Expand Up @@ -45,13 +52,23 @@
#[cfg(feature = "nightly")]
extern crate test;

use std::borrow::Cow;
#[cfg(all(__unicase__core_and_alloc, not(test)))]
extern crate alloc;
#[cfg(all(__unicase__core_and_alloc, not(test)))]
use alloc::string::String;

#[cfg(not(all(__unicase__core_and_alloc, not(test))))]
extern crate std as alloc;
#[cfg(not(all(__unicase__core_and_alloc, not(test))))]
extern crate std as core;

use alloc::borrow::Cow;
#[cfg(__unicase__iter_cmp)]
use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};
use std::str::FromStr;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::ops::{Deref, DerefMut};
use core::str::FromStr;

use self::unicode::Unicode;

Expand Down Expand Up @@ -118,8 +135,10 @@ impl<S: AsRef<str>> UniCase<S> {
///
/// Note: This scans the text to determine if it is all ASCII or not.
pub fn new(s: S) -> UniCase<S> {
#[cfg(not(__unicase__core_and_alloc))]
#[allow(deprecated, unused)]
use std::ascii::AsciiExt;

if s.as_ref().is_ascii() {
UniCase(Encoding::Ascii(Ascii(s)))
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/unicode/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use super::fold::Fold;

use std::char;
use core::char;
pub fn lookup(orig: char) -> Fold {
// The code below is is intended to reduce the binary size from that of a simple 1:1 lookup table.
// It exploits two facts:
Expand Down
4 changes: 2 additions & 2 deletions src/unicode/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(__unicase__iter_cmp)]
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use core::cmp::Ordering;
use core::hash::{Hash, Hasher};

use self::map::lookup;
mod map;
Expand Down

0 comments on commit fbab380

Please sign in to comment.