From 283f020069c0bb3b93768b683266149b1e63aacd Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 22 Jul 2021 09:12:02 -0700 Subject: [PATCH 01/11] no_std in pluralrules --- components/plurals/Cargo.toml | 1 + components/plurals/src/lib.rs | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/components/plurals/Cargo.toml b/components/plurals/Cargo.toml index 55cdddf97af..83eb8ee4295 100644 --- a/components/plurals/Cargo.toml +++ b/components/plurals/Cargo.toml @@ -51,6 +51,7 @@ path = "src/lib.rs" bench = false # This option is required for Benchmark CI [features] +std = ["icu_locid/std", "icu_provider/std"] default = ["provider_serde"] bench = [] provider_serde = ["serde"] diff --git a/components/plurals/src/lib.rs b/components/plurals/src/lib.rs index 43f15aa9557..2e36cd51958 100644 --- a/components/plurals/src/lib.rs +++ b/components/plurals/src/lib.rs @@ -64,6 +64,11 @@ //! [`Plural Category`]: PluralCategory //! [`Language Plural Rules`]: https://unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules //! [`CLDR`]: http://cldr.unicode.org/ + +#![cfg_attr(not(any(test, feature = "std")), no_std)] + +extern crate alloc; + mod data; mod error; mod operands; From 82cf2f04d3e03977c2c8864d26f2abe54f1f62c9 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 22 Jul 2021 09:13:27 -0700 Subject: [PATCH 02/11] autoreplace --- components/plurals/src/data.rs | 4 ++-- components/plurals/src/error.rs | 2 +- components/plurals/src/lib.rs | 2 +- components/plurals/src/operands.rs | 16 ++++++++-------- components/plurals/src/provider.rs | 2 +- components/plurals/src/rules/ast.rs | 4 +++- components/plurals/src/rules/lexer.rs | 2 +- components/plurals/src/rules/parser.rs | 8 ++++++-- components/plurals/src/rules/serializer.rs | 4 ++-- 9 files changed, 25 insertions(+), 19 deletions(-) diff --git a/components/plurals/src/data.rs b/components/plurals/src/data.rs index 58723539a57..4132cee2d79 100644 --- a/components/plurals/src/data.rs +++ b/components/plurals/src/data.rs @@ -7,8 +7,8 @@ use crate::provider::PluralRuleStringsV1; use crate::rules; use crate::rules::ast; use crate::{PluralCategory, PluralRulesError}; -use std::borrow::Cow; -use std::convert::TryInto; +use alloc::borrow::Cow; +use core::convert::TryInto; /// A raw function pointer to a `PluralRulesFn` // pub type PluralRulesFn = fn(&PluralOperands) -> PluralCategory; diff --git a/components/plurals/src/error.rs b/components/plurals/src/error.rs index 5ace4bc1ee0..94b4d216e2f 100644 --- a/components/plurals/src/error.rs +++ b/components/plurals/src/error.rs @@ -17,7 +17,7 @@ pub enum PluralRulesError { DataProvider(DataError), } -impl std::error::Error for PluralRulesError {} +impl core::error::Error for PluralRulesError {} impl From for PluralRulesError { fn from(e: ParserError) -> Self { diff --git a/components/plurals/src/lib.rs b/components/plurals/src/lib.rs index 2e36cd51958..a7173efb1db 100644 --- a/components/plurals/src/lib.rs +++ b/components/plurals/src/lib.rs @@ -80,7 +80,7 @@ use icu_locid::LanguageIdentifier; use icu_provider::prelude::*; pub use operands::PluralOperands; use provider::{resolver, PluralRuleStringsV1, PluralRuleStringsV1Marker}; -use std::convert::TryInto; +use core::convert::TryInto; /// A type of a plural rule which can be associated with the [`PluralRules`] struct. /// diff --git a/components/plurals/src/operands.rs b/components/plurals/src/operands.rs index b9dc92dcc90..56d802ef563 100644 --- a/components/plurals/src/operands.rs +++ b/components/plurals/src/operands.rs @@ -4,11 +4,11 @@ use displaydoc::Display; use fixed_decimal::FixedDecimal; -use std::convert::TryFrom; -use std::io::Error as IOError; -use std::isize; -use std::num::ParseIntError; -use std::str::FromStr; +use core::convert::TryFrom; +use core::io::Error as IOError; +use core::isize; +use core::num::ParseIntError; +use core::str::FromStr; /// A full plural operands representation of a number. See [CLDR Plural Rules](http://unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules) for complete operands description. /// Plural operands in compliance with [CLDR Plural Rules](http://unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules). @@ -96,7 +96,7 @@ pub enum OperandsError { Invalid, } -impl std::error::Error for OperandsError {} +impl core::error::Error for OperandsError {} impl From for OperandsError { fn from(_: ParseIntError) -> Self { @@ -230,8 +230,8 @@ impl From<&FixedDecimal> for PluralOperands { /// digits each from the integer and fraction parts. fn from(dec: &FixedDecimal) -> Self { let mag_range = dec.magnitude_range(); - let mag_high = std::cmp::min(17, *mag_range.end()); - let mag_low = std::cmp::max(-18, *mag_range.start()); + let mag_high = core::cmp::min(17, *mag_range.end()); + let mag_low = core::cmp::max(-18, *mag_range.start()); let mut i: u64 = 0; for magnitude in (0..=mag_high).rev() { diff --git a/components/plurals/src/provider.rs b/components/plurals/src/provider.rs index 03b5e639f01..7fb144afb2f 100644 --- a/components/plurals/src/provider.rs +++ b/components/plurals/src/provider.rs @@ -7,7 +7,7 @@ //! Read more about data providers: [`icu_provider`] use icu_provider::yoke::{self, *}; -use std::borrow::Cow; +use alloc::borrow::Cow; pub mod key { use icu_provider::{resource_key, ResourceKey}; diff --git a/components/plurals/src/rules/ast.rs b/components/plurals/src/rules/ast.rs index 09fb6b9d0d1..0512a04efb1 100644 --- a/components/plurals/src/rules/ast.rs +++ b/components/plurals/src/rules/ast.rs @@ -38,7 +38,9 @@ //! [`PluralCategory`]: crate::PluralCategory //! [`parse`]: super::parse() //! [`test_condition`]: super::test_condition() -use std::ops::RangeInclusive; +use alloc::boxed::Box; +use alloc::string::String; +use core::ops::RangeInclusive; /// A complete AST representation of a plural rule. /// Comprises a vector of [`AndConditions`] and optionally a set of [`Samples`]. diff --git a/components/plurals/src/rules/lexer.rs b/components/plurals/src/rules/lexer.rs index 4277cf5b11a..c51b8980b0a 100644 --- a/components/plurals/src/rules/lexer.rs +++ b/components/plurals/src/rules/lexer.rs @@ -34,7 +34,7 @@ pub enum LexerError { UnknownToken(u8), } -impl std::error::Error for LexerError {} +impl core::error::Error for LexerError {} /// Unicode Plural Rule lexer is an iterator /// over tokens produced from an input string. diff --git a/components/plurals/src/rules/parser.rs b/components/plurals/src/rules/parser.rs index 9c29b95432a..3d70d05ae92 100644 --- a/components/plurals/src/rules/parser.rs +++ b/components/plurals/src/rules/parser.rs @@ -2,10 +2,14 @@ // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). +use alloc::string::String; +use alloc::string::ToString; +use alloc::vec::Vec; +use alloc::vec; use super::ast; use super::lexer::{Lexer, Token}; use displaydoc::Display; -use std::iter::Peekable; +use core::iter::Peekable; #[derive(Display, Debug, PartialEq, Eq)] pub enum ParserError { @@ -23,7 +27,7 @@ pub enum ParserError { ExpectedSampleType, } -impl std::error::Error for ParserError {} +impl core::error::Error for ParserError {} /// Unicode Plural Rule parser converts an /// input string into a Rule [`AST`]. diff --git a/components/plurals/src/rules/serializer.rs b/components/plurals/src/rules/serializer.rs index fab3f7c6271..9d6334f7cf4 100644 --- a/components/plurals/src/rules/serializer.rs +++ b/components/plurals/src/rules/serializer.rs @@ -3,8 +3,8 @@ // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). use crate::rules::ast; -use std::fmt; -use std::ops::RangeInclusive; +use core::fmt; +use core::ops::RangeInclusive; /// Unicode Plural Rule serializer converts an [`AST`] to a [`String`]. /// From 4f4fb68d31ea4bb940c122895bf97fb9d5248e06 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 22 Jul 2021 09:14:16 -0700 Subject: [PATCH 03/11] fix errors --- components/plurals/src/error.rs | 3 ++- components/plurals/src/operands.rs | 3 ++- components/plurals/src/rules/lexer.rs | 3 ++- components/plurals/src/rules/parser.rs | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/components/plurals/src/error.rs b/components/plurals/src/error.rs index 94b4d216e2f..0e03cf10762 100644 --- a/components/plurals/src/error.rs +++ b/components/plurals/src/error.rs @@ -17,7 +17,8 @@ pub enum PluralRulesError { DataProvider(DataError), } -impl core::error::Error for PluralRulesError {} +#[cfg(feature = "std")] +impl std::error::Error for PluralRulesError {} impl From for PluralRulesError { fn from(e: ParserError) -> Self { diff --git a/components/plurals/src/operands.rs b/components/plurals/src/operands.rs index 56d802ef563..f1840b5f25b 100644 --- a/components/plurals/src/operands.rs +++ b/components/plurals/src/operands.rs @@ -96,7 +96,8 @@ pub enum OperandsError { Invalid, } -impl core::error::Error for OperandsError {} +#[cfg(feature = "std")] +impl std::error::Error for OperandsError {} impl From for OperandsError { fn from(_: ParseIntError) -> Self { diff --git a/components/plurals/src/rules/lexer.rs b/components/plurals/src/rules/lexer.rs index c51b8980b0a..4ab4f668bb7 100644 --- a/components/plurals/src/rules/lexer.rs +++ b/components/plurals/src/rules/lexer.rs @@ -34,7 +34,8 @@ pub enum LexerError { UnknownToken(u8), } -impl core::error::Error for LexerError {} +#[cfg(feature = "std")] +impl std::error::Error for LexerError {} /// Unicode Plural Rule lexer is an iterator /// over tokens produced from an input string. diff --git a/components/plurals/src/rules/parser.rs b/components/plurals/src/rules/parser.rs index 3d70d05ae92..e31017ef8fd 100644 --- a/components/plurals/src/rules/parser.rs +++ b/components/plurals/src/rules/parser.rs @@ -27,7 +27,8 @@ pub enum ParserError { ExpectedSampleType, } -impl core::error::Error for ParserError {} +#[cfg(feature = "std")] +impl std::error::Error for ParserError {} /// Unicode Plural Rule parser converts an /// input string into a Rule [`AST`]. From e9c6f599d8761c396e33969d05bd9f51effc2711 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 22 Jul 2021 09:17:16 -0700 Subject: [PATCH 04/11] powi --- components/plurals/src/operands.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/components/plurals/src/operands.rs b/components/plurals/src/operands.rs index f1840b5f25b..5dcf177f236 100644 --- a/components/plurals/src/operands.rs +++ b/components/plurals/src/operands.rs @@ -5,7 +5,6 @@ use displaydoc::Display; use fixed_decimal::FixedDecimal; use core::convert::TryFrom; -use core::io::Error as IOError; use core::isize; use core::num::ParseIntError; use core::str::FromStr; @@ -80,6 +79,9 @@ impl PluralOperands { /// Returns the number represented by this [`PluralOperands`] as floating point. /// The precision of the number returned is up to the representation accuracy /// of a double. + /// + /// This method requires the `"std"` feature be enabled + #[cfg(feature = "std")] pub fn n(&self) -> f64 { let fraction = self.t as f64 / 10_f64.powi(self.v as i32); self.i as f64 + fraction @@ -105,8 +107,9 @@ impl From for OperandsError { } } -impl From for OperandsError { - fn from(_: IOError) -> Self { +#[cfg(feature = "std")] +impl From for OperandsError { + fn from(_: std::io::Error) -> Self { Self::Invalid } } From ae19ab4c6ed14bab25b6ddc0f475b76b18ac5870 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 22 Jul 2021 09:19:17 -0700 Subject: [PATCH 05/11] Fix icu_plurals use in ffi/ecma402 --- Cargo.lock | 1 + ffi/ecma402/Cargo.toml | 1 + 2 files changed, 2 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 1b2c788336e..0e767dcb0d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -899,6 +899,7 @@ dependencies = [ "ecma402_traits", "icu", "icu_locid", + "icu_plurals", "icu_provider", "icu_testdata", "serde", diff --git a/ffi/ecma402/Cargo.toml b/ffi/ecma402/Cargo.toml index 056d20fa7dd..a49bebfd2a3 100644 --- a/ffi/ecma402/Cargo.toml +++ b/ffi/ecma402/Cargo.toml @@ -29,6 +29,7 @@ all-features = true ecma402_traits = { version = "0.2.0" } icu = { path = "../../components/icu", default-features = false } icu_provider = { version = "0.2", path = "../../provider/core" } +icu_plurals = { version = "0.2", path = "../../components/plurals", features = ["std"] } [dev-dependencies] From 9b7b7b7c68e38d2b93206b11a7dd9aa09c97eec6 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 22 Jul 2021 09:20:15 -0700 Subject: [PATCH 06/11] fmt --- components/plurals/src/lib.rs | 2 +- components/plurals/src/operands.rs | 4 ++-- components/plurals/src/provider.rs | 2 +- components/plurals/src/rules/parser.rs | 8 ++++---- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/components/plurals/src/lib.rs b/components/plurals/src/lib.rs index a7173efb1db..6e314ae6202 100644 --- a/components/plurals/src/lib.rs +++ b/components/plurals/src/lib.rs @@ -75,12 +75,12 @@ mod operands; pub mod provider; pub mod rules; +use core::convert::TryInto; pub use error::PluralRulesError; use icu_locid::LanguageIdentifier; use icu_provider::prelude::*; pub use operands::PluralOperands; use provider::{resolver, PluralRuleStringsV1, PluralRuleStringsV1Marker}; -use core::convert::TryInto; /// A type of a plural rule which can be associated with the [`PluralRules`] struct. /// diff --git a/components/plurals/src/operands.rs b/components/plurals/src/operands.rs index 5dcf177f236..7c7bed15033 100644 --- a/components/plurals/src/operands.rs +++ b/components/plurals/src/operands.rs @@ -2,12 +2,12 @@ // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). -use displaydoc::Display; -use fixed_decimal::FixedDecimal; use core::convert::TryFrom; use core::isize; use core::num::ParseIntError; use core::str::FromStr; +use displaydoc::Display; +use fixed_decimal::FixedDecimal; /// A full plural operands representation of a number. See [CLDR Plural Rules](http://unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules) for complete operands description. /// Plural operands in compliance with [CLDR Plural Rules](http://unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules). diff --git a/components/plurals/src/provider.rs b/components/plurals/src/provider.rs index 7fb144afb2f..247874ae3e8 100644 --- a/components/plurals/src/provider.rs +++ b/components/plurals/src/provider.rs @@ -6,8 +6,8 @@ //! //! Read more about data providers: [`icu_provider`] -use icu_provider::yoke::{self, *}; use alloc::borrow::Cow; +use icu_provider::yoke::{self, *}; pub mod key { use icu_provider::{resource_key, ResourceKey}; diff --git a/components/plurals/src/rules/parser.rs b/components/plurals/src/rules/parser.rs index e31017ef8fd..aebe89fa8f5 100644 --- a/components/plurals/src/rules/parser.rs +++ b/components/plurals/src/rules/parser.rs @@ -2,14 +2,14 @@ // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). +use super::ast; +use super::lexer::{Lexer, Token}; use alloc::string::String; use alloc::string::ToString; -use alloc::vec::Vec; use alloc::vec; -use super::ast; -use super::lexer::{Lexer, Token}; -use displaydoc::Display; +use alloc::vec::Vec; use core::iter::Peekable; +use displaydoc::Display; #[derive(Display, Debug, PartialEq, Eq)] pub enum ParserError { From 598766e0d2f661cad13e57a4f5772d82af6b8ebc Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 22 Jul 2021 09:29:20 -0700 Subject: [PATCH 07/11] add no_std to fixeddecimal --- components/decimal/Cargo.toml | 1 + components/decimal/src/lib.rs | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/components/decimal/Cargo.toml b/components/decimal/Cargo.toml index e438e96ee41..6ba8dd4e10f 100644 --- a/components/decimal/Cargo.toml +++ b/components/decimal/Cargo.toml @@ -47,6 +47,7 @@ rand_distr = "0.4" getrandom = { version = "0.2", features = ["js"] } [features] +std = ["icu_locid/std", "icu_provider/std", "fixed_decimal/std"] default = ["provider_serde"] bench = [] provider_serde = ["serde"] diff --git a/components/decimal/src/lib.rs b/components/decimal/src/lib.rs index 6d759eafd05..75a46b77a5f 100644 --- a/components/decimal/src/lib.rs +++ b/components/decimal/src/lib.rs @@ -56,6 +56,10 @@ //! //! [`FixedDecimalFormat`]: FixedDecimalFormat +#![cfg_attr(not(any(test, feature = "std")), no_std)] + +extern crate alloc; + pub mod error; pub mod format; mod grouper; From ae0d23dcc49e71b49a4ed7368dd0a777aa110e9d Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 22 Jul 2021 09:30:33 -0700 Subject: [PATCH 08/11] autoreplace --- components/decimal/src/error.rs | 2 +- components/decimal/src/format.rs | 4 ++-- components/decimal/src/provider.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/components/decimal/src/error.rs b/components/decimal/src/error.rs index c1eaf8066d1..17f5d055c64 100644 --- a/components/decimal/src/error.rs +++ b/components/decimal/src/error.rs @@ -12,7 +12,7 @@ pub enum Error { Data(icu_provider::DataError), } -impl std::error::Error for Error {} +impl core::error::Error for Error {} impl From for Error { fn from(e: icu_provider::DataError) -> Self { diff --git a/components/decimal/src/format.rs b/components/decimal/src/format.rs index 826da251ea3..132085699cc 100644 --- a/components/decimal/src/format.rs +++ b/components/decimal/src/format.rs @@ -32,9 +32,9 @@ impl<'l> FormattedFixedDecimal<'l> { } impl<'l> Writeable for FormattedFixedDecimal<'l> { - fn write_to(&self, sink: &mut W) -> std::result::Result<(), std::fmt::Error> + fn write_to(&self, sink: &mut W) -> core::result::Result<(), core::fmt::Error> where - W: std::fmt::Write + ?Sized, + W: core::fmt::Write + ?Sized, { let affixes = self.get_affixes(); if let Some(affixes) = affixes { diff --git a/components/decimal/src/provider.rs b/components/decimal/src/provider.rs index 8e94544d847..063952ed1d2 100644 --- a/components/decimal/src/provider.rs +++ b/components/decimal/src/provider.rs @@ -7,7 +7,7 @@ //! Read more about data providers: [`icu_provider`] use icu_provider::yoke::{self, *}; -use std::borrow::Cow; +use alloc::borrow::Cow; pub mod key { //! Resource keys for [`icu_decimal`](crate). From 22de6c1b5e625a06dbc09af86c093f3f65f011a6 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 22 Jul 2021 09:30:37 -0700 Subject: [PATCH 09/11] fmt --- components/decimal/src/provider.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/decimal/src/provider.rs b/components/decimal/src/provider.rs index 063952ed1d2..39ab018d129 100644 --- a/components/decimal/src/provider.rs +++ b/components/decimal/src/provider.rs @@ -6,8 +6,8 @@ //! //! Read more about data providers: [`icu_provider`] -use icu_provider::yoke::{self, *}; use alloc::borrow::Cow; +use icu_provider::yoke::{self, *}; pub mod key { //! Resource keys for [`icu_decimal`](crate). From dc3fd1e2ed356b13de48c2a78f18acad92e7710c Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 22 Jul 2021 09:30:58 -0700 Subject: [PATCH 10/11] fix error --- components/decimal/src/error.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/decimal/src/error.rs b/components/decimal/src/error.rs index 17f5d055c64..61aa4b4cea6 100644 --- a/components/decimal/src/error.rs +++ b/components/decimal/src/error.rs @@ -12,7 +12,8 @@ pub enum Error { Data(icu_provider::DataError), } -impl core::error::Error for Error {} +#[cfg(feature = "std")] +impl std::error::Error for Error {} impl From for Error { fn from(e: icu_provider::DataError) -> Self { From 8de60645b5c3346520193c663aa87bc2259bfe4b Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 22 Jul 2021 09:37:57 -0700 Subject: [PATCH 11/11] require std feature for pluralrules test --- components/plurals/Cargo.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/components/plurals/Cargo.toml b/components/plurals/Cargo.toml index 83eb8ee4295..f5a4f7c5937 100644 --- a/components/plurals/Cargo.toml +++ b/components/plurals/Cargo.toml @@ -74,6 +74,10 @@ required-features = ["provider_serde"] name = "plurals" required-features = ["provider_serde"] +[[test]] +name = "operands" +required-features = ["provider_serde", "std"] + [[example]] name = "unread_emails" required-features = ["provider_serde"]