Skip to content

Move icu_plurals and icu_decimal to no_std #888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/decimal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions components/decimal/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub enum Error {
Data(icu_provider::DataError),
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl From<icu_provider::DataError> for Error {
Expand Down
4 changes: 2 additions & 2 deletions components/decimal/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ impl<'l> FormattedFixedDecimal<'l> {
}

impl<'l> Writeable for FormattedFixedDecimal<'l> {
fn write_to<W>(&self, sink: &mut W) -> std::result::Result<(), std::fmt::Error>
fn write_to<W>(&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 {
Expand Down
4 changes: 4 additions & 0 deletions components/decimal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion components/decimal/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//!
//! Read more about data providers: [`icu_provider`]

use alloc::borrow::Cow;
use icu_provider::yoke::{self, *};
use std::borrow::Cow;

pub mod key {
//! Resource keys for [`icu_decimal`](crate).
Expand Down
1 change: 1 addition & 0 deletions components/plurals/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
4 changes: 2 additions & 2 deletions components/plurals/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions components/plurals/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub enum PluralRulesError {
DataProvider(DataError),
}

#[cfg(feature = "std")]
impl std::error::Error for PluralRulesError {}

impl From<ParserError> for PluralRulesError {
Expand Down
7 changes: 6 additions & 1 deletion components/plurals/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,23 @@
//! [`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;
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 std::convert::TryInto;

/// A type of a plural rule which can be associated with the [`PluralRules`] struct.
///
Expand Down
22 changes: 13 additions & 9 deletions components/plurals/src/operands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +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 core::convert::TryFrom;
use core::isize;
use core::num::ParseIntError;
use core::str::FromStr;
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;

/// 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).
Expand Down Expand Up @@ -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")]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth highlighting: some of the math functions aren't in std

pub fn n(&self) -> f64 {
let fraction = self.t as f64 / 10_f64.powi(self.v as i32);
self.i as f64 + fraction
Expand All @@ -96,6 +98,7 @@ pub enum OperandsError {
Invalid,
}

#[cfg(feature = "std")]
impl std::error::Error for OperandsError {}

impl From<ParseIntError> for OperandsError {
Expand All @@ -104,8 +107,9 @@ impl From<ParseIntError> for OperandsError {
}
}

impl From<IOError> for OperandsError {
fn from(_: IOError) -> Self {
#[cfg(feature = "std")]
impl From<std::io::Error> for OperandsError {
fn from(_: std::io::Error) -> Self {
Self::Invalid
}
}
Expand Down Expand Up @@ -230,8 +234,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() {
Expand Down
2 changes: 1 addition & 1 deletion components/plurals/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//!
//! Read more about data providers: [`icu_provider`]

use alloc::borrow::Cow;
use icu_provider::yoke::{self, *};
use std::borrow::Cow;

pub mod key {
use icu_provider::{resource_key, ResourceKey};
Expand Down
4 changes: 3 additions & 1 deletion components/plurals/src/rules/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down
1 change: 1 addition & 0 deletions components/plurals/src/rules/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub enum LexerError {
UnknownToken(u8),
}

#[cfg(feature = "std")]
impl std::error::Error for LexerError {}

/// Unicode Plural Rule lexer is an iterator
Expand Down
7 changes: 6 additions & 1 deletion components/plurals/src/rules/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

use super::ast;
use super::lexer::{Lexer, Token};
use alloc::string::String;
use alloc::string::ToString;
use alloc::vec;
use alloc::vec::Vec;
use core::iter::Peekable;
use displaydoc::Display;
use std::iter::Peekable;

#[derive(Display, Debug, PartialEq, Eq)]
pub enum ParserError {
Expand All @@ -23,6 +27,7 @@ pub enum ParserError {
ExpectedSampleType,
}

#[cfg(feature = "std")]
impl std::error::Error for ParserError {}

/// Unicode Plural Rule parser converts an
Expand Down
4 changes: 2 additions & 2 deletions components/plurals/src/rules/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
///
Expand Down
1 change: 1 addition & 0 deletions ffi/ecma402/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to add this because ecma402_traits requires error types to implement Error

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



[dev-dependencies]
Expand Down