Struct DataLocale

struct DataLocale { ... }

A locale type optimized for use in fallbacking and the ICU4X data pipeline.

DataLocale contains less functionality than [Locale] but more than LanguageIdentifier for better size and performance while still meeting the needs of the ICU4X data pipeline.

You can create a DataLocale from a borrowed [Locale], which is more efficient than cloning the [Locale], but less efficient than converting an owned [Locale]:

use icu_locale_core::locale;
use icu_provider::DataLocale;

let locale1 = locale!("en-u-ca-buddhist");
let data_locale = DataLocale::from(&locale1);

DataLocale only supports -u-sd keywords, to reflect the current state of CLDR data lookup and fallback. This may change in the future.

use icu_locale_core::{locale, Locale};
use icu_provider::DataLocale;

let locale = "hi-IN-t-en-h0-hybrid-u-attr-ca-buddhist-sd-inas"
    .parse::<Locale>()
    .unwrap();

assert_eq!(
    DataLocale::from(locale),
    DataLocale::from(locale!("hi-IN-u-sd-inas"))
);

Fields

language: Language

Language subtag

script: Option<Script>

Script subtag

region: Option<Region>

Region subtag

variant: Option<Variant>

Variant subtag

subdivision: Option<Subtag>

Subivision (-u-sd-) subtag

Implementations

impl DataLocale

fn to_string(self: &Self) -> String

Converts the given value to a String.

Under the hood, this uses an efficient Writeable implementation. However, in order to avoid allocating a string, it is more efficient to use Writeable directly.

impl DataLocale

fn total_cmp(self: &Self, other: &Self) -> Ordering

Returns an ordering suitable for use in BTreeSet.

fn strict_cmp(self: &Self, other: &[u8]) -> Ordering

Compare this DataLocale with BCP-47 bytes.

The return value is equivalent to what would happen if you first converted this DataLocale to a BCP-47 string and then performed a byte comparison.

This function is case-sensitive and results in a total order, so it is appropriate for binary search. The only argument producing Ordering::Equal is self.to_string().

Examples

use core::cmp::Ordering;
use icu_provider::DataLocale;

let bcp47_strings: &[&str] = &[
    "ca",
    "ca-ES",
    "ca-ES-u-sd-esct",
    "ca-ES-valencia",
    "cat",
    "pl-Latn-PL",
    "und",
    "und-fonipa",
    "zh",
];

for ab in bcp47_strings.windows(2) {
    let a = ab[0];
    let b = ab[1];
    assert_eq!(a.cmp(b), Ordering::Less, "strings: {} < {}", a, b);
    let a_loc: DataLocale = a.parse().unwrap();
    assert_eq!(
        a_loc.strict_cmp(a.as_bytes()),
        Ordering::Equal,
        "strict_cmp: {} == {}",
        a_loc,
        a
    );
    assert_eq!(
        a_loc.strict_cmp(b.as_bytes()),
        Ordering::Less,
        "strict_cmp: {} < {}",
        a_loc,
        b
    );
    let b_loc: DataLocale = b.parse().unwrap();
    assert_eq!(
        b_loc.strict_cmp(b.as_bytes()),
        Ordering::Equal,
        "strict_cmp: {} == {}",
        b_loc,
        b
    );
    assert_eq!(
        b_loc.strict_cmp(a.as_bytes()),
        Ordering::Greater,
        "strict_cmp: {} > {}",
        b_loc,
        a
    );
}

Comparison against invalid strings:

use icu_provider::DataLocale;

let invalid_strings: &[&str] = &[
    // Less than "ca-ES"
    "CA",
    "ar-x-gbp-FOO",
    // Greater than "ca-AR"
    "ca_ES",
    "ca-ES-x-gbp-FOO",
];

let data_locale = "ca-ES".parse::<DataLocale>().unwrap();

for s in invalid_strings.iter() {
    let expected_ordering = "ca-AR".cmp(s);
    let actual_ordering = data_locale.strict_cmp(s.as_bytes());
    assert_eq!(expected_ordering, actual_ordering, "{}", s);
}
fn is_unknown(self: &Self) -> bool

Returns whether this DataLocale is und in the locale and extensions portion.

Examples

use icu_provider::DataLocale;

assert!("und".parse::<DataLocale>().unwrap().is_unknown());
assert!(!"de-u-sd-denw".parse::<DataLocale>().unwrap().is_unknown());
assert!(!"und-ES".parse::<DataLocale>().unwrap().is_unknown());
fn into_locale(self: Self) -> Locale

Converts this DataLocale into a Locale.

impl DataLocale

const fn default() -> Self

const version of Default::default

impl Clone for DataLocale

fn clone(self: &Self) -> DataLocale

impl Copy for DataLocale

impl Debug for DataLocale

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

impl Default for DataLocale

fn default() -> Self

impl Display for DataLocale

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

impl Eq for DataLocale

impl Freeze for DataLocale

impl From for DataLocale

fn from(locale: Locale) -> Self

impl From for DataLocale

fn from(langid: LanguageIdentifier) -> Self

impl From for DataLocale

fn from(locale: &Locale) -> Self

impl From for DataLocale

fn from(langid: &LanguageIdentifier) -> Self

impl Hash for DataLocale

fn hash<__H: $crate::hash::Hasher>(self: &Self, state: &mut __H)

impl PartialEq for DataLocale

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

impl RefUnwindSafe for DataLocale

impl Send for DataLocale

impl StructuralPartialEq for DataLocale

impl Sync for DataLocale

impl Unpin for DataLocale

impl UnsafeUnpin for DataLocale

impl UnwindSafe for DataLocale

impl Writeable for DataLocale

fn write_to<W: core::fmt::Write + ?Sized>(self: &Self, sink: &mut W) -> Result
fn writeable_length_hint(self: &Self) -> LengthHint

impl<T> Any for DataLocale

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for DataLocale

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for DataLocale

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

impl<T> CloneToUninit for DataLocale

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> ErasedDestructor for DataLocale

impl<T> From for DataLocale

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for DataLocale

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T> ToString for DataLocale

fn to_string(self: &Self) -> String

impl<T, U> Into for DataLocale

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for DataLocale

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

impl<T, U> TryInto for DataLocale

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