Skip to content

Commit 653d75e

Browse files
authored
Merge pull request #58 from madsmtm/no_std
Add `no_std` support
2 parents 1fd2a2d + 87e3693 commit 653d75e

File tree

8 files changed

+57
-21
lines changed

8 files changed

+57
-21
lines changed

Cargo.toml

+10-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ documentation = "https://docs.rs/unicode-bidi/"
99
keywords = ["rtl", "unicode", "text", "layout", "bidi"]
1010
readme="README.md"
1111
edition = "2018"
12+
categories = [
13+
"no-std",
14+
"encoding",
15+
"text-processing",
16+
]
1217

1318
# No data is shipped; benches, examples and tests also depend on data.
1419
exclude = [
@@ -29,14 +34,16 @@ name = "unicode_bidi"
2934
[dependencies]
3035
flame = { version = "0.2", optional = true }
3136
flamer = { version = "0.4", optional = true }
32-
matches = "0.1"
33-
serde = { version = ">=0.8, <2.0", optional = true, features = ["derive"] }
37+
serde = { version = ">=0.8, <2.0", default-features = false, optional = true, features = ["derive"] }
3438

3539
[dev-dependencies]
3640
serde_test = ">=0.8, <2.0"
3741

3842
[features]
39-
default = []
43+
# Note: We don't actually use the `std` feature for anything other than making
44+
# doctests work. But it may come in handy in the future.
45+
default = ["std"]
46+
std = []
4047
unstable = [] # travis-cargo needs it
4148
bench_it = []
4249
flame_it = ["flame", "flamer"]

src/char_data/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ mod tables;
1313

1414
pub use self::tables::{BidiClass, UNICODE_VERSION};
1515

16-
use std::cmp::Ordering::{Equal, Less, Greater};
17-
use std::char;
16+
use core::cmp::Ordering::{Equal, Less, Greater};
17+
use core::char;
1818

1919
use self::tables::bidi_class_table;
2020
use crate::BidiClass::*;

src/deprecated.rs

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
//! This module holds deprecated assets only.
1111
12+
use alloc::vec::Vec;
13+
1214
use super::*;
1315

1416
/// Find the level runs within a line and return them in visual order.

src/explicit.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//!
1212
//! <http://www.unicode.org/reports/tr9/#Explicit_Levels_and_Directions>
1313
14-
use matches::matches;
14+
use alloc::vec::Vec;
1515

1616
use super::char_data::{BidiClass::{self, *}, is_rtl};
1717
use super::level::Level;
@@ -46,7 +46,10 @@ pub fn compute(
4646
let last_level = stack.last().level;
4747

4848
// X5a-X5c: Isolate initiators get the level of the last entry on the stack.
49-
let is_isolate = matches!(original_classes[i], RLI | LRI | FSI);
49+
let is_isolate = match original_classes[i] {
50+
RLI | LRI | FSI => true,
51+
_ => false,
52+
};
5053
if is_isolate {
5154
levels[i] = last_level;
5255
match stack.last().status {

src/implicit.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
//! 3.3.4 - 3.3.6. Resolve implicit levels and types.
1111
12-
use std::cmp::max;
13-
use matches::matches;
12+
use core::cmp::max;
13+
use alloc::vec::Vec;
1414

1515
use super::char_data::BidiClass::{self, *};
1616
use super::prepare::{IsolatingRunSequence, LevelRun, not_removed_by_x9, removed_by_x9};
@@ -223,5 +223,8 @@ pub fn resolve_levels(original_classes: &[BidiClass], levels: &mut [Level]) -> L
223223
/// <http://www.unicode.org/reports/tr9/#NI>
224224
#[allow(non_snake_case)]
225225
fn is_NI(class: BidiClass) -> bool {
226-
matches!(class, B | S | WS | ON | FSI | LRI | RLI | PDI)
226+
match class {
227+
B | S | WS | ON | FSI | LRI | RLI | PDI => true,
228+
_ => false,
229+
}
227230
}

src/level.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
//!
1414
//! <http://www.unicode.org/reports/tr9/#BD2>
1515
16-
use std::convert::{From, Into};
16+
use alloc::vec::Vec;
17+
use core::convert::{From, Into};
18+
use alloc::string::{String, ToString};
1719

1820
use super::char_data::BidiClass;
1921

src/lib.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,24 @@
5353
//! ]);
5454
//! ```
5555
//!
56+
//! # Features
57+
//!
58+
//! - `std`: Enabled by default, but can be disabled to make `unicode_bidi`
59+
//! `#![no_std]` + `alloc` compatible.
60+
//! - `serde`: Adds [`serde::Serialize`] and [`serde::Deserialize`]
61+
//! implementations to relevant types.
62+
//!
5663
//! [tr9]: <http://www.unicode.org/reports/tr9/>
5764
5865
#![forbid(unsafe_code)]
5966

67+
#![no_std]
68+
// We need to link to std to make doc tests work on older Rust versions
69+
#![cfg(feature = "std")]
70+
extern crate std;
71+
#[macro_use]
72+
extern crate alloc;
73+
6074
pub mod deprecated;
6175
pub mod format_chars;
6276
pub mod level;
@@ -70,10 +84,12 @@ pub use crate::char_data::{BidiClass, bidi_class, UNICODE_VERSION};
7084
pub use crate::level::{Level, LTR_LEVEL, RTL_LEVEL};
7185
pub use crate::prepare::LevelRun;
7286

73-
use std::borrow::Cow;
74-
use std::cmp::{max, min};
75-
use std::iter::repeat;
76-
use std::ops::Range;
87+
use alloc::borrow::Cow;
88+
use alloc::vec::Vec;
89+
use alloc::string::String;
90+
use core::cmp::{max, min};
91+
use core::iter::repeat;
92+
use core::ops::Range;
7793

7894
use crate::BidiClass::*;
7995
use crate::format_chars as chars;

src/prepare.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
//!
1212
//! <http://www.unicode.org/reports/tr9/#Preparations_for_Implicit_Processing>
1313
14-
use std::cmp::max;
15-
use std::ops::Range;
16-
use matches::matches;
14+
use core::cmp::max;
15+
use core::ops::Range;
16+
use alloc::vec::Vec;
1717

1818
use super::BidiClass::{self, *};
1919
use super::level::Level;
@@ -73,7 +73,7 @@ pub fn isolating_run_sequences(
7373

7474
sequence.push(run);
7575

76-
if matches!(end_class, RLI | LRI | FSI) {
76+
if let RLI | LRI | FSI = end_class {
7777
// Resume this sequence after the isolate.
7878
stack.push(sequence);
7979
} else {
@@ -113,7 +113,7 @@ pub fn isolating_run_sequences(
113113
};
114114

115115
// Get the level of the next non-removed char after the runs.
116-
let succ_level = if matches!(original_classes[end_of_seq - 1], RLI | LRI | FSI) {
116+
let succ_level = if let RLI | LRI | FSI = original_classes[end_of_seq - 1] {
117117
para_level
118118
} else {
119119
match original_classes[end_of_seq..].iter().position(
@@ -163,7 +163,10 @@ fn level_runs(levels: &[Level], original_classes: &[BidiClass]) -> Vec<LevelRun>
163163
///
164164
/// <http://www.unicode.org/reports/tr9/#X9>
165165
pub fn removed_by_x9(class: BidiClass) -> bool {
166-
matches!(class, RLE | LRE | RLO | LRO | PDF | BN)
166+
match class {
167+
RLE | LRE | RLO | LRO | PDF | BN => true,
168+
_ => false,
169+
}
167170
}
168171

169172
// For use as a predicate for `position` / `rposition`

0 commit comments

Comments
 (0)