From 7a0c5dbb69fcb18bf02e888568fd882e2d7794a7 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 12 Aug 2021 18:24:09 +0200 Subject: [PATCH 1/2] Make no_std compatible Because of the matches crate, to actually be used on no_std targets, this requires either: - https://github.com/SimonSapin/rust-std-candidates/pull/23 - That we bump the MSRV to 1.42.0 - That we change usage of the matches! macro --- Cargo.toml | 10 ++++++++-- src/char_data/mod.rs | 4 ++-- src/deprecated.rs | 2 ++ src/explicit.rs | 1 + src/implicit.rs | 3 ++- src/level.rs | 4 +++- src/lib.rs | 24 ++++++++++++++++++++---- src/prepare.rs | 5 +++-- 8 files changed, 41 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 650aa3e..3e98dce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,11 @@ documentation = "https://docs.rs/unicode-bidi/" keywords = ["rtl", "unicode", "text", "layout", "bidi"] readme="README.md" edition = "2018" +categories = [ + "no-std", + "encoding", + "text-processing", +] # No data is shipped; benches, examples and tests also depend on data. exclude = [ @@ -30,13 +35,14 @@ name = "unicode_bidi" flame = { version = "0.2", optional = true } flamer = { version = "0.4", optional = true } matches = "0.1" -serde = { version = ">=0.8, <2.0", optional = true, features = ["derive"] } +serde = { version = ">=0.8, <2.0", default-features = false, optional = true, features = ["derive"] } [dev-dependencies] serde_test = ">=0.8, <2.0" [features] -default = [] +default = ["std"] +std = [] unstable = [] # travis-cargo needs it bench_it = [] flame_it = ["flame", "flamer"] diff --git a/src/char_data/mod.rs b/src/char_data/mod.rs index da85c84..c91bfad 100644 --- a/src/char_data/mod.rs +++ b/src/char_data/mod.rs @@ -13,8 +13,8 @@ mod tables; pub use self::tables::{BidiClass, UNICODE_VERSION}; -use std::cmp::Ordering::{Equal, Less, Greater}; -use std::char; +use core::cmp::Ordering::{Equal, Less, Greater}; +use core::char; use self::tables::bidi_class_table; use crate::BidiClass::*; diff --git a/src/deprecated.rs b/src/deprecated.rs index 8bd206b..491436a 100644 --- a/src/deprecated.rs +++ b/src/deprecated.rs @@ -9,6 +9,8 @@ //! This module holds deprecated assets only. +use alloc::vec::Vec; + use super::*; /// Find the level runs within a line and return them in visual order. diff --git a/src/explicit.rs b/src/explicit.rs index 95de505..a17912d 100644 --- a/src/explicit.rs +++ b/src/explicit.rs @@ -12,6 +12,7 @@ //! use matches::matches; +use alloc::vec::Vec; use super::char_data::{BidiClass::{self, *}, is_rtl}; use super::level::Level; diff --git a/src/implicit.rs b/src/implicit.rs index 7e43294..287d42d 100644 --- a/src/implicit.rs +++ b/src/implicit.rs @@ -9,8 +9,9 @@ //! 3.3.4 - 3.3.6. Resolve implicit levels and types. -use std::cmp::max; +use core::cmp::max; use matches::matches; +use alloc::vec::Vec; use super::char_data::BidiClass::{self, *}; use super::prepare::{IsolatingRunSequence, LevelRun, not_removed_by_x9, removed_by_x9}; diff --git a/src/level.rs b/src/level.rs index bc74205..c1adf1f 100644 --- a/src/level.rs +++ b/src/level.rs @@ -13,7 +13,9 @@ //! //! -use std::convert::{From, Into}; +use alloc::vec::Vec; +use core::convert::{From, Into}; +use alloc::string::{String, ToString}; use super::char_data::BidiClass; diff --git a/src/lib.rs b/src/lib.rs index ba3220b..09bbcfb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,10 +53,24 @@ //! ]); //! ``` //! +//! # Features +//! +//! - `std`: Enabled by default, but can be disabled to make `unicode_bidi` +//! `#![no_std]` + `alloc` compatible. +//! - `serde`: Adds [`serde::Serialize`] and [`serde::Deserialize`] +//! implementations to relevant types. +//! //! [tr9]: #![forbid(unsafe_code)] +#![no_std] +// We need to link to std to make doc tests work on older Rust versions +#![cfg(feature = "std")] +extern crate std; +#[macro_use] +extern crate alloc; + pub mod deprecated; pub mod format_chars; pub mod level; @@ -70,10 +84,12 @@ pub use crate::char_data::{BidiClass, bidi_class, UNICODE_VERSION}; pub use crate::level::{Level, LTR_LEVEL, RTL_LEVEL}; pub use crate::prepare::LevelRun; -use std::borrow::Cow; -use std::cmp::{max, min}; -use std::iter::repeat; -use std::ops::Range; +use alloc::borrow::Cow; +use alloc::vec::Vec; +use alloc::string::String; +use core::cmp::{max, min}; +use core::iter::repeat; +use core::ops::Range; use crate::BidiClass::*; use crate::format_chars as chars; diff --git a/src/prepare.rs b/src/prepare.rs index ccb8c0f..eaba2cb 100644 --- a/src/prepare.rs +++ b/src/prepare.rs @@ -11,9 +11,10 @@ //! //! -use std::cmp::max; -use std::ops::Range; +use core::cmp::max; +use core::ops::Range; use matches::matches; +use alloc::vec::Vec; use super::BidiClass::{self, *}; use super::level::Level; From 87e36936cb3aeccce8fe6fefa49ed447fd3b2f25 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 12 Aug 2021 18:38:57 +0200 Subject: [PATCH 2/2] Remove matches dependency Shouldn't change the MSRV guarantee (tested on Rust 1.36.0). --- Cargo.toml | 3 ++- src/explicit.rs | 6 ++++-- src/implicit.rs | 6 ++++-- src/prepare.rs | 10 ++++++---- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3e98dce..44cd868 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,13 +34,14 @@ name = "unicode_bidi" [dependencies] flame = { version = "0.2", optional = true } flamer = { version = "0.4", optional = true } -matches = "0.1" serde = { version = ">=0.8, <2.0", default-features = false, optional = true, features = ["derive"] } [dev-dependencies] serde_test = ">=0.8, <2.0" [features] +# Note: We don't actually use the `std` feature for anything other than making +# doctests work. But it may come in handy in the future. default = ["std"] std = [] unstable = [] # travis-cargo needs it diff --git a/src/explicit.rs b/src/explicit.rs index a17912d..ec39dee 100644 --- a/src/explicit.rs +++ b/src/explicit.rs @@ -11,7 +11,6 @@ //! //! -use matches::matches; use alloc::vec::Vec; use super::char_data::{BidiClass::{self, *}, is_rtl}; @@ -47,7 +46,10 @@ pub fn compute( let last_level = stack.last().level; // X5a-X5c: Isolate initiators get the level of the last entry on the stack. - let is_isolate = matches!(original_classes[i], RLI | LRI | FSI); + let is_isolate = match original_classes[i] { + RLI | LRI | FSI => true, + _ => false, + }; if is_isolate { levels[i] = last_level; match stack.last().status { diff --git a/src/implicit.rs b/src/implicit.rs index 287d42d..2bb3581 100644 --- a/src/implicit.rs +++ b/src/implicit.rs @@ -10,7 +10,6 @@ //! 3.3.4 - 3.3.6. Resolve implicit levels and types. use core::cmp::max; -use matches::matches; use alloc::vec::Vec; use super::char_data::BidiClass::{self, *}; @@ -224,5 +223,8 @@ pub fn resolve_levels(original_classes: &[BidiClass], levels: &mut [Level]) -> L /// #[allow(non_snake_case)] fn is_NI(class: BidiClass) -> bool { - matches!(class, B | S | WS | ON | FSI | LRI | RLI | PDI) + match class { + B | S | WS | ON | FSI | LRI | RLI | PDI => true, + _ => false, + } } diff --git a/src/prepare.rs b/src/prepare.rs index eaba2cb..752087b 100644 --- a/src/prepare.rs +++ b/src/prepare.rs @@ -13,7 +13,6 @@ use core::cmp::max; use core::ops::Range; -use matches::matches; use alloc::vec::Vec; use super::BidiClass::{self, *}; @@ -74,7 +73,7 @@ pub fn isolating_run_sequences( sequence.push(run); - if matches!(end_class, RLI | LRI | FSI) { + if let RLI | LRI | FSI = end_class { // Resume this sequence after the isolate. stack.push(sequence); } else { @@ -114,7 +113,7 @@ pub fn isolating_run_sequences( }; // Get the level of the next non-removed char after the runs. - let succ_level = if matches!(original_classes[end_of_seq - 1], RLI | LRI | FSI) { + let succ_level = if let RLI | LRI | FSI = original_classes[end_of_seq - 1] { para_level } else { match original_classes[end_of_seq..].iter().position( @@ -164,7 +163,10 @@ fn level_runs(levels: &[Level], original_classes: &[BidiClass]) -> Vec /// /// pub fn removed_by_x9(class: BidiClass) -> bool { - matches!(class, RLE | LRE | RLO | LRO | PDF | BN) + match class { + RLE | LRE | RLO | LRO | PDF | BN => true, + _ => false, + } } // For use as a predicate for `position` / `rposition`