Struct icu_plurals::PluralOperands

source ·
pub struct PluralOperands { /* private fields */ }
Expand description

A full plural operands representation of a number. See CLDR Plural Rules for complete operands description.

Plural operands in compliance with CLDR Plural Rules.

See full operands description.

§Data Types

The following types can be converted to PluralOperands:

  • Integers, signed and unsigned
  • Strings representing an arbitrary-precision decimal
  • [FixedDecimal]

This crate does not support selection from a floating-point number, because floats are not capable of carrying trailing zeros, which are required for proper plural rule selection. For example, in English, “1 star” has a different plural form than “1.0 stars”, but this distinction cannot be represented using a float. Clients should use [FixedDecimal] instead.

§Examples

From int

use icu::plurals::rules::RawPluralOperands;
use icu::plurals::PluralOperands;

assert_eq!(
    PluralOperands::from(RawPluralOperands {
        i: 2,
        v: 0,
        w: 0,
        f: 0,
        t: 0,
        c: 0,
    }),
    PluralOperands::from(2_usize)
);

From &str

use icu::plurals::rules::RawPluralOperands;
use icu::plurals::PluralOperands;

assert_eq!(
    Ok(PluralOperands::from(RawPluralOperands {
        i: 123,
        v: 2,
        w: 2,
        f: 45,
        t: 45,
        c: 0,
    })),
    "123.45".parse()
);

From [FixedDecimal]

use fixed_decimal::FixedDecimal;
use icu::plurals::rules::RawPluralOperands;
use icu::plurals::PluralOperands;

assert_eq!(
    PluralOperands::from(RawPluralOperands {
        i: 123,
        v: 2,
        w: 2,
        f: 45,
        t: 45,
        c: 0,
    }),
    (&FixedDecimal::from(12345).multiplied_pow10(-2)).into()
);

Implementations§

source§

impl PluralOperands

source

pub fn is_exactly_zero(self) -> bool

Whether these PluralOperands are exactly the number 0, which might be a special case.

source

pub fn is_exactly_one(self) -> bool

Whether these PluralOperands are exactly the number 1, which might be a special case.

Trait Implementations§

source§

impl Clone for PluralOperands

source§

fn clone(&self) -> PluralOperands

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for PluralOperands

source§

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

Formats the value using the given formatter. Read more
source§

impl Default for PluralOperands

source§

fn default() -> PluralOperands

Returns the “default value” for a type. Read more
source§

impl From<&CompactDecimal> for PluralOperands

source§

fn from(compact: &CompactDecimal) -> Self

Converts a [fixed_decimal::CompactDecimal] to PluralOperands. Retains at most 18 digits each from the integer and fraction parts.

§Examples
use fixed_decimal::CompactDecimal;
use fixed_decimal::FixedDecimal;
use icu::locale::locale;
use icu::plurals::rules::RawPluralOperands;
use icu::plurals::PluralCategory;
use icu::plurals::PluralOperands;
use icu::plurals::PluralRules;

let fixed_decimal = "1000000.20".parse::<FixedDecimal>().unwrap();
let compact_decimal = "1.00000020c6".parse::<CompactDecimal>().unwrap();

assert_eq!(
    PluralOperands::from(RawPluralOperands {
        i: 1000000,
        v: 2,
        w: 1,
        f: 20,
        t: 2,
        c: 0,
    }),
    PluralOperands::from(&fixed_decimal)
);

assert_eq!(
    PluralOperands::from(RawPluralOperands {
        i: 1000000,
        v: 2,
        w: 1,
        f: 20,
        t: 2,
        c: 6,
    }),
    PluralOperands::from(&compact_decimal)
);

let rules = PluralRules::try_new_cardinal(locale!("fr").into()).unwrap();
assert_eq!(rules.category_for(&fixed_decimal), PluralCategory::Other);
assert_eq!(rules.category_for(&compact_decimal), PluralCategory::Many);
source§

impl From<&FixedDecimal> for PluralOperands

source§

fn from(dec: &FixedDecimal) -> Self

Converts a [fixed_decimal::FixedDecimal] to PluralOperands. Retains at most 18 digits each from the integer and fraction parts.

source§

impl From<PluralOperands> for RawPluralOperands

source§

fn from(po: PluralOperands) -> RawPluralOperands

Converts to this type from the input type.
source§

impl From<RawPluralOperands> for PluralOperands

source§

fn from(rpo: RawPluralOperands) -> PluralOperands

Converts to this type from the input type.
source§

impl From<i128> for PluralOperands

source§

fn from(input: i128) -> Self

Converts to this type from the input type.
source§

impl From<i16> for PluralOperands

source§

fn from(input: i16) -> Self

Converts to this type from the input type.
source§

impl From<i32> for PluralOperands

source§

fn from(input: i32) -> Self

Converts to this type from the input type.
source§

impl From<i64> for PluralOperands

source§

fn from(input: i64) -> Self

Converts to this type from the input type.
source§

impl From<i8> for PluralOperands

source§

fn from(input: i8) -> Self

Converts to this type from the input type.
source§

impl From<isize> for PluralOperands

source§

fn from(input: isize) -> Self

Converts to this type from the input type.
source§

impl From<u128> for PluralOperands

source§

fn from(input: u128) -> Self

Converts to this type from the input type.
source§

impl From<u16> for PluralOperands

source§

fn from(input: u16) -> Self

Converts to this type from the input type.
source§

impl From<u32> for PluralOperands

source§

fn from(input: u32) -> Self

Converts to this type from the input type.
source§

impl From<u64> for PluralOperands

source§

fn from(input: u64) -> Self

Converts to this type from the input type.
source§

impl From<u8> for PluralOperands

source§

fn from(input: u8) -> Self

Converts to this type from the input type.
source§

impl From<usize> for PluralOperands

source§

fn from(input: usize) -> Self

Converts to this type from the input type.
source§

impl FromStr for PluralOperands

source§

type Err = OperandsError

The associated error which can be returned from parsing.
source§

fn from_str(input: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl PartialEq for PluralOperands

source§

fn eq(&self, other: &PluralOperands) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for PluralOperands

source§

impl StructuralPartialEq for PluralOperands

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.
§

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

§

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