Struct icu::plurals::PluralRulesWithRanges

source ·
pub struct PluralRulesWithRanges<R> { /* private fields */ }
Expand description

A PluralRules that also has the ability to retrieve an appropriate Plural Category for a range.

Enabled with the experimental Cargo feature.

🚧 This code is experimental; it may change at any time, in breaking or non-breaking ways, including in SemVer minor releases. Use with caution. #4140

§Examples

use icu::locale::locale;
use icu::plurals::{PluralCategory, PluralOperands};
use icu::plurals::{PluralRuleType, PluralRulesWithRanges};

let ranges = PluralRulesWithRanges::try_new(
    locale!("ar").into(),
    PluralRuleType::Cardinal,
)
.expect("locale should be present");

let operands = PluralOperands::from(1_usize);
let operands2: PluralOperands =
    "2.0".parse().expect("parsing to operands should succeed");

assert_eq!(
    ranges.category_for_range(operands, operands2),
    PluralCategory::Other
);

Implementations§

source§

impl PluralRulesWithRanges<PluralRules>

source

pub fn try_new( prefs: PluralRulesPreferences, rule_type: PluralRuleType, ) -> Result<PluralRulesWithRanges<PluralRules>, DataError>

Constructs a new PluralRulesWithRanges for a given locale using compiled data.

§Examples
use icu::locale::locale;
use icu::plurals::{PluralRuleType, PluralRulesWithRanges};

let _ = PluralRulesWithRanges::try_new(
    locale!("en").into(),
    PluralRuleType::Cardinal,
).expect("locale should be present");

Enabled with the compiled_data Cargo feature.

📚 Help choosing a constructor

source

pub fn try_new_with_any_provider( provider: &(impl AnyProvider + ?Sized), prefs: PluralRulesPreferences, rule_type: PluralRuleType, ) -> Result<PluralRulesWithRanges<PluralRules>, DataError>

A version of [Self :: try_new] that uses custom data provided by an AnyProvider.

📚 Help choosing a constructor

source

pub fn try_new_with_buffer_provider( provider: &(impl BufferProvider + ?Sized), prefs: PluralRulesPreferences, rule_type: PluralRuleType, ) -> Result<PluralRulesWithRanges<PluralRules>, DataError>

A version of [Self :: try_new] that uses custom data provided by a BufferProvider.

Enabled with the serde feature.

📚 Help choosing a constructor

source

pub fn try_new_unstable( provider: &(impl DataProvider<PluralRangesV1Marker> + DataProvider<CardinalV1Marker> + DataProvider<OrdinalV1Marker> + ?Sized), prefs: PluralRulesPreferences, rule_type: PluralRuleType, ) -> Result<PluralRulesWithRanges<PluralRules>, DataError>

A version of Self::try_new that uses custom data provided by a DataProvider.

📚 Help choosing a constructor

⚠️ The bounds on provider may change over time, including in SemVer minor releases.
source

pub fn try_new_cardinal( prefs: PluralRulesPreferences, ) -> Result<PluralRulesWithRanges<PluralRules>, DataError>

Constructs a new PluralRulesWithRanges for a given locale for cardinal numbers using compiled data.

See PluralRules::try_new_cardinal for more information.

§Examples
use icu::locale::locale;
use icu::plurals::{PluralCategory, PluralRulesWithRanges};

let rules = PluralRulesWithRanges::try_new_cardinal(locale!("ru").into())
    .expect("locale should be present");

assert_eq!(rules.category_for_range(0_usize, 2_usize), PluralCategory::Few);

Enabled with the compiled_data Cargo feature.

📚 Help choosing a constructor

source

pub fn try_new_cardinal_with_any_provider( provider: &(impl AnyProvider + ?Sized), prefs: PluralRulesPreferences, ) -> Result<PluralRulesWithRanges<PluralRules>, DataError>

A version of [Self :: try_new_cardinal] that uses custom data provided by an AnyProvider.

📚 Help choosing a constructor

source

pub fn try_new_cardinal_with_buffer_provider( provider: &(impl BufferProvider + ?Sized), prefs: PluralRulesPreferences, ) -> Result<PluralRulesWithRanges<PluralRules>, DataError>

A version of [Self :: try_new_cardinal] that uses custom data provided by a BufferProvider.

Enabled with the serde feature.

📚 Help choosing a constructor

source

pub fn try_new_cardinal_unstable( provider: &(impl DataProvider<CardinalV1Marker> + DataProvider<PluralRangesV1Marker> + ?Sized), prefs: PluralRulesPreferences, ) -> Result<PluralRulesWithRanges<PluralRules>, DataError>

A version of Self::try_new_cardinal that uses custom data provided by a DataProvider.

📚 Help choosing a constructor

⚠️ The bounds on provider may change over time, including in SemVer minor releases.
source

pub fn try_new_ordinal( prefs: PluralRulesPreferences, ) -> Result<PluralRulesWithRanges<PluralRules>, DataError>

Constructs a new PluralRulesWithRanges for a given locale for ordinal numbers using compiled data.

See PluralRules::try_new_ordinal for more information.

§Examples
use icu::locale::locale;
use icu::plurals::{PluralCategory, PluralRulesWithRanges};

let rules = PluralRulesWithRanges::try_new_ordinal(
    locale!("ru").into(),
)
.expect("locale should be present");

assert_eq!(rules.category_for_range(0_usize, 2_usize), PluralCategory::Other);

Enabled with the compiled_data Cargo feature.

📚 Help choosing a constructor

source

pub fn try_new_ordinal_with_any_provider( provider: &(impl AnyProvider + ?Sized), prefs: PluralRulesPreferences, ) -> Result<PluralRulesWithRanges<PluralRules>, DataError>

A version of [Self :: try_new_ordinal] that uses custom data provided by an AnyProvider.

📚 Help choosing a constructor

source

pub fn try_new_ordinal_with_buffer_provider( provider: &(impl BufferProvider + ?Sized), prefs: PluralRulesPreferences, ) -> Result<PluralRulesWithRanges<PluralRules>, DataError>

A version of [Self :: try_new_ordinal] that uses custom data provided by a BufferProvider.

Enabled with the serde feature.

📚 Help choosing a constructor

source

pub fn try_new_ordinal_unstable( provider: &(impl DataProvider<OrdinalV1Marker> + DataProvider<PluralRangesV1Marker> + ?Sized), prefs: PluralRulesPreferences, ) -> Result<PluralRulesWithRanges<PluralRules>, DataError>

A version of Self::try_new_ordinal that uses custom data provided by a DataProvider.

📚 Help choosing a constructor

⚠️ The bounds on provider may change over time, including in SemVer minor releases.
source§

impl<R> PluralRulesWithRanges<R>
where R: AsRef<PluralRules>,

source

pub fn try_new_with_rules( prefs: PluralRulesPreferences, rules: R, ) -> Result<PluralRulesWithRanges<R>, DataError>

Constructs a new PluralRulesWithRanges for a given locale from an existing PluralRules (either owned or as a reference) and compiled data.

§⚠️ Warning

The provided locale MUST be the same as the locale provided to the constructor of rules. Otherwise, Self::category_for_range will return incorrect results.

§Examples
use icu::locale::locale;
use icu::plurals::{PluralRuleType, PluralRulesWithRanges, PluralRules};

let rules = PluralRules::try_new(locale!("en").into(), PluralRuleType::Cardinal)
    .expect("locale should be present");

let _ =
    PluralRulesWithRanges::try_new_with_rules(locale!("en").into(), rules)
        .expect("locale should be present");

Enabled with the compiled_data Cargo feature.

📚 Help choosing a constructor

source

pub fn try_new_with_rules_with_any_provider( provider: &(impl AnyProvider + ?Sized), prefs: PluralRulesPreferences, rules: R, ) -> Result<PluralRulesWithRanges<R>, DataError>

A version of [Self :: try_new_with_rules] that uses custom data provided by an AnyProvider.

📚 Help choosing a constructor

source

pub fn try_new_with_rules_with_buffer_provider( provider: &(impl BufferProvider + ?Sized), prefs: PluralRulesPreferences, rules: R, ) -> Result<PluralRulesWithRanges<R>, DataError>

A version of [Self :: try_new_with_rules] that uses custom data provided by a BufferProvider.

Enabled with the serde feature.

📚 Help choosing a constructor

source

pub fn try_new_with_rules_unstable( provider: &(impl DataProvider<PluralRangesV1Marker> + ?Sized), prefs: PluralRulesPreferences, rules: R, ) -> Result<PluralRulesWithRanges<R>, DataError>

A version of Self::try_new_with_rules that uses custom data provided by a DataProvider.

📚 Help choosing a constructor

⚠️ The bounds on provider may change over time, including in SemVer minor releases.
source

pub fn rules(&self) -> &PluralRules

Gets a reference to the inner PluralRules.

§Examples
use icu::locale::locale;
use icu::plurals::{PluralCategory, PluralRulesWithRanges};

let ranges = PluralRulesWithRanges::try_new_cardinal(locale!("en").into())
    .expect("locale should be present");

let rules = ranges.rules();

assert_eq!(rules.category_for(1u8), PluralCategory::One);
source

pub fn category_for_range<S, E>(&self, start: S, end: E) -> PluralCategory

Returns the Plural Category appropriate for a range.

Note that the returned category is correct only if the range fulfills the following requirements:

  • The start value is strictly less than the end value.
  • Both values are positive.
§Examples
use icu::locale::locale;
use icu::plurals::{
    PluralCategory, PluralOperands, PluralRuleType, PluralRulesWithRanges,
};

let ranges = PluralRulesWithRanges::try_new(
    locale!("ro").into(),
    PluralRuleType::Cardinal,
)
.expect("locale should be present");
let operands: PluralOperands =
    "0.5".parse().expect("parsing to operands should succeed");
let operands2 = PluralOperands::from(1_usize);

assert_eq!(
    ranges.category_for_range(operands, operands2),
    PluralCategory::Few
);
source

pub fn resolve_range( &self, start: PluralCategory, end: PluralCategory, ) -> PluralCategory

Returns the Plural Category appropriate for a range from the categories of its endpoints.

Note that the returned category is correct only if the original numeric range fulfills the following requirements:

  • The start value is strictly less than the end value.
  • Both values are positive.
§Examples
use icu::locale::locale;
use icu::plurals::{PluralCategory, PluralRuleType, PluralRulesWithRanges};

let ranges = PluralRulesWithRanges::try_new(
    locale!("sl").into(),
    PluralRuleType::Ordinal,
)
.expect("locale should be present");

assert_eq!(
    ranges.resolve_range(PluralCategory::Other, PluralCategory::One),
    PluralCategory::Few
);

Trait Implementations§

source§

impl<R> Debug for PluralRulesWithRanges<R>
where R: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<R> Freeze for PluralRulesWithRanges<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for PluralRulesWithRanges<R>
where R: RefUnwindSafe,

§

impl<R> Send for PluralRulesWithRanges<R>
where R: Send,

§

impl<R> Sync for PluralRulesWithRanges<R>
where R: Sync,

§

impl<R> Unpin for PluralRulesWithRanges<R>
where R: Unpin,

§

impl<R> UnwindSafe for PluralRulesWithRanges<R>
where R: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> ErasedDestructor for T
where T: 'static,

source§

impl<T> MaybeSendSync for T
where T: Send + Sync,