Skip to content

Commit ab525e6

Browse files
committed
make no_std a feature to allow builds in beta; bump version
1 parent 4c51fb1 commit ab525e6

File tree

6 files changed

+66
-19
lines changed

6 files changed

+66
-19
lines changed

.travis.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
language: rust
22
sudo: false
33
script:
4-
- cargo build --verbose
5-
- cargo test --verbose
4+
- cargo build --verbose --features no_std
5+
- cargo test --verbose --features no_std
6+
- cargo clean
7+
- cargo build --verbose --features default
8+
- cargo test --verbose --features default
69
- rustdoc --test README.md -L target/debug -L target/debug/deps
710
- cargo doc
811
after_success: |

Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "unicode-width"
4-
version = "0.0.1"
4+
version = "0.1.0"
55
authors = ["kwantam <kwantam@gmail.com>"]
66

77
homepage = "https://github.com/unicode-rs/unicode-width"
@@ -16,3 +16,7 @@ according to Unicode Standard Annex #11 rules.
1616
"""
1717

1818
exclude = [ "target/*", "Cargo.lock" ]
19+
20+
[features]
21+
default = []
22+
no_std = []

README.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Determine displayed width of `char` and `str` types according to
44
[Unicode Standard Annex #11](http://www.unicode.org/reports/tr11/)
55
rules.
66

7-
[![Build Status](https://travis-ci.org/unicode-rs/unicode-width.svg?branch=master)](https://travis-ci.org/unicode-rs/unicode-width)
7+
[![Build Status](https://travis-ci.org/unicode-rs/unicode-width.svg)](https://travis-ci.org/unicode-rs/unicode-width)
88

99
```rust
1010
extern crate unicode_width;
@@ -21,4 +21,17 @@ fn main() {
2121
}
2222
```
2323

24-
[Documentation](http://unicode-rs.github.io/unicode-width/unicode_width/)
24+
## features
25+
26+
unicode-width supports a `no_std` feature. This eliminates dependence
27+
on std, and instead uses equivalent functions from core.
28+
29+
## crates.io
30+
31+
You can use this package in your project by adding the following
32+
to your `Cargo.toml`:
33+
34+
```toml
35+
[dependencies]
36+
unicode-width = "0.1.0"
37+
```

scripts/unicode.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
#
3-
# Copyright 2011-2013 The Rust Project Developers. See the COPYRIGHT
3+
# Copyright 2011-2015 The Rust Project Developers. See the COPYRIGHT
44
# file at the top-level directory of this distribution and at
55
# http://rust-lang.org/COPYRIGHT.
66
#
@@ -197,14 +197,20 @@ def emit_table(f, name, t_data, t_type = "&'static [(char, char)]", is_pub=True,
197197
f.write("\n ];\n\n")
198198

199199
def emit_charwidth_module(f, width_table):
200-
f.write("pub mod charwidth {\n")
201-
f.write(" use core::option::Option;\n")
202-
f.write(" use core::option::Option::{Some, None};\n")
203-
f.write(" use core::slice::SliceExt;\n")
204-
f.write(" use core::result::Result::{Ok, Err};\n")
200+
f.write("pub mod charwidth {")
205201
f.write("""
202+
#[cfg(feature = "no_std")]
203+
use core::option::Option::{self, Some, None};
204+
#[cfg(feature = "no_std")]
205+
use core::slice::SliceExt;
206+
#[cfg(feature = "no_std")]
207+
use core::result::Result::{Ok, Err};
208+
206209
fn bsearch_range_value_table(c: char, is_cjk: bool, r: &'static [(char, char, u8, u8)]) -> u8 {
210+
#[cfg(feature = "no_std")]
207211
use core::cmp::Ordering::{Equal, Less, Greater};
212+
#[cfg(not(feature = "no_std"))]
213+
use std::cmp::Ordering::{Equal, Less, Greater};
208214
match r.binary_search_by(|&(lo, hi, _, _)| {
209215
if lo <= c && c <= hi { Equal }
210216
else if hi < c { Less }

src/lib.rs

+22-6
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,45 @@
2727
//! }
2828
//! ```
2929
//!
30+
//! # features
31+
//!
32+
//! unicode-width supports a `no_std` feature. This eliminates dependence
33+
//! on std, and instead uses equivalent functions from core.
34+
//!
3035
//! # crates.io
3136
//!
3237
//! You can use this package in your project by adding the following
3338
//! to your `Cargo.toml`:
3439
//!
3540
//! ```toml
3641
//! [dependencies]
37-
//! unicode-width = "0.0.1"
42+
//! unicode-width = "0.1.0"
3843
//! ```
3944
4045
#![deny(missing_docs, unsafe_code)]
41-
#![feature(no_std, core)]
42-
#![no_std]
4346

47+
#![cfg_attr(feature = "no_std", no_std)]
48+
#![cfg_attr(feature = "no_std", feature(no_std, core))]
49+
50+
#[cfg(feature = "no_std")]
51+
#[macro_use]
4452
extern crate core;
4553

46-
#[cfg(test)]
54+
#[cfg(all(test, feature = "no_std"))]
4755
#[macro_use]
4856
extern crate std;
4957

58+
#[cfg(feature = "no_std")]
5059
use core::prelude::*;
5160

5261
use tables::charwidth as cw;
5362
pub use tables::UNICODE_VERSION;
5463

64+
#[cfg(feature = "no_std")]
65+
use core::ops::Add;
66+
#[cfg(not(feature = "no_std"))]
67+
use std::ops::Add;
68+
5569
mod tables;
5670

5771
/// Methods for determining displayed width of Unicode characters.
@@ -106,11 +120,11 @@ pub trait UnicodeWidthStr {
106120

107121
impl UnicodeWidthStr for str {
108122
fn width(&self) -> usize {
109-
self.chars().map(|c| cw::width(c, false).unwrap_or(0)).sum()
123+
self.chars().map(|c| cw::width(c, false).unwrap_or(0)).fold(0, Add::add)
110124
}
111125

112126
fn width_cjk(&self) -> usize {
113-
self.chars().map(|c| cw::width(c, true).unwrap_or(0)).sum()
127+
self.chars().map(|c| cw::width(c, true).unwrap_or(0)).fold(0, Add::add)
114128
}
115129
}
116130

@@ -133,6 +147,7 @@ mod tests {
133147
#[test]
134148
fn test_char() {
135149
use super::UnicodeWidthChar;
150+
#[cfg(feature = "no_std")]
136151
use core::option::Option::{Some, None};
137152

138153
assert_eq!(UnicodeWidthChar::width('h'), Some(2));
@@ -148,6 +163,7 @@ mod tests {
148163
#[test]
149164
fn test_char2() {
150165
use super::UnicodeWidthChar;
166+
#[cfg(feature = "no_std")]
151167
use core::option::Option::{Some, None};
152168

153169
assert_eq!(UnicodeWidthChar::width('\x00'),Some(0));

src/tables.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
pub const UNICODE_VERSION: (u64, u64, u64) = (7, 0, 0);
1818

1919
pub mod charwidth {
20-
use core::option::Option;
21-
use core::option::Option::{Some, None};
20+
#[cfg(feature = "no_std")]
21+
use core::option::Option::{self, Some, None};
22+
#[cfg(feature = "no_std")]
2223
use core::slice::SliceExt;
24+
#[cfg(feature = "no_std")]
2325
use core::result::Result::{Ok, Err};
2426

2527
fn bsearch_range_value_table(c: char, is_cjk: bool, r: &'static [(char, char, u8, u8)]) -> u8 {
28+
#[cfg(feature = "no_std")]
2629
use core::cmp::Ordering::{Equal, Less, Greater};
30+
#[cfg(not(feature = "no_std"))]
31+
use std::cmp::Ordering::{Equal, Less, Greater};
2732
match r.binary_search_by(|&(lo, hi, _, _)| {
2833
if lo <= c && c <= hi { Equal }
2934
else if hi < c { Less }

0 commit comments

Comments
 (0)