#[repr(transparent)]pub struct Language(_);
Expand description
A language subtag (examples: "en"
, "csb"
, "zh"
, "und"
, etc.)
Language
represents a Unicode base language code conformat to the
unicode_language_id
field of the Language and Locale Identifier.
Examples
use icu::locid::subtags::Language;
let language: Language = "en".parse()
.expect("Failed to parse a language subtag.");
If the Language
has no value assigned, it serializes to a string "und"
, which
can be then parsed back to an empty Language
field.
Examples
use icu::locid::subtags::Language;
assert_eq!(Language::default().as_str(), "und");
Notice
: ICU4X uses a narrow form of language subtag of 2-3 characters.
The specification allows language subtag to optionally also be 5-8 characters
but that form has not been used and ICU4X does not support it right now.
Implementations
sourceimpl Language
impl Language
sourcepub const fn from_bytes(v: &[u8]) -> Result<Self, ParserError>
pub const fn from_bytes(v: &[u8]) -> Result<Self, ParserError>
sourcepub const fn from_bytes_manual_slice(
v: &[u8],
start: usize,
end: usize
) -> Result<Self, ParserError>
pub const fn from_bytes_manual_slice(
v: &[u8],
start: usize,
end: usize
) -> Result<Self, ParserError>
Equivalent to from_bytes(bytes[start..end])
,
but callable in a const
context (which range indexing is not).
sourcepub fn try_from_raw(v: [u8; 3]) -> Result<Self, ParserError>
pub fn try_from_raw(v: [u8; 3]) -> Result<Self, ParserError>
Safely creates a Language
from a reference to its raw format
as returned by Language::into_raw()
.
Examples
use icu::locid::subtags::Language;
assert!(matches!(Language::try_from_raw(*b"de\0"), Ok(_)));
assert!(matches!(Language::try_from_raw(*b"ars"), Ok(_)));
assert!(matches!(Language::try_from_raw(*b"419"), Err(_)));
// Unlike the other constructors, this one is case-sensitive:
assert!(matches!(Language::try_from_raw(*b"EN\0"), Err(_)));
sourcepub fn into_raw(self) -> [u8; 3]
pub fn into_raw(self) -> [u8; 3]
Deconstructs the Language
into raw format to be consumed
by from_raw_unchecked()
.
Examples
use icu::locid::subtags::Language;
let lang = Language::from_bytes(b"en")
.expect("Parsing failed.");
let raw = lang.into_raw();
let lang = unsafe { Language::from_raw_unchecked(raw) };
assert_eq!(lang, "en");
sourcepub const unsafe fn from_raw_unchecked(v: [u8; 3]) -> Self
pub const unsafe fn from_raw_unchecked(v: [u8; 3]) -> Self
Constructor which takes a raw value returned by
into_raw()
.
Examples
use icu::locid::subtags::Language;
let lang = Language::from_bytes(b"en")
.expect("Parsing failed.");
let raw = lang.into_raw();
let lang = unsafe { Language::from_raw_unchecked(raw) };
assert_eq!(lang, "en");
Safety
This function accepts a [[u8; 3]
] that is expected to be a valid TinyAsciiStr<3>
representing a Language
subtag in canonical syntax.
sourcepub const UND: Self = {
const R: crate::subtags::Language =
match crate::subtags::Language::from_bytes("und".as_bytes()) {
Ok(r) => r,
_ => {
::std::rt::begin_panic("Invalid language code: und")
}
};
R
}
pub const UND: Self = { const R: crate::subtags::Language = match crate::subtags::Language::from_bytes("und".as_bytes()) { Ok(r) => r, _ => { ::std::rt::begin_panic("Invalid language code: und") } }; R }
sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
A helper function for displaying
a Language
subtag as a &
str
.
Examples
use icu::locid::subtags::Language;
let lang = Language::from_bytes(b"en")
.expect("Parsing failed.");
assert_eq!(lang.as_str(), "en");
Notice
: For many use cases, such as comparison,
Language
implements PartialEq
<&
str
>
which allows for direct comparisons.
Trait Implementations
sourceimpl AsULE for Language
impl AsULE for Language
Impl enabling Language
to be used in a ZeroVec
. Enabled with the "zerovec"
feature.
Example
use icu::locid::subtags::Language;
use icu::locid::language;
use zerovec::ZeroVec;
let zv = ZeroVec::<Language>::parse_byte_slice(b"de\0fr\0arsar\0")
.expect("Valid language subtags");
assert_eq!(zv.get(1), Some(language!("fr")));
ZeroVec::<Language>::parse_byte_slice(b"invalid")
.expect_err("Invalid byte slice");
sourceimpl<'de> Deserialize<'de> for Language
impl<'de> Deserialize<'de> for Language
sourcefn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl From<Language> for LanguageIdentifier
impl From<Language> for LanguageIdentifier
Examples
use icu::locid::LanguageIdentifier;
use icu::locid::language;
let language = language!("en");
let li = LanguageIdentifier::from(language);
assert_eq!(li.language, "en");
assert_eq!(li, "en");
sourceimpl From<Language> for Locale
impl From<Language> for Locale
Examples
use icu::locid::Locale;
use icu::locid::language;
let language = language!("en");
let loc = Locale::from(language);
assert_eq!(loc.id.language, "en");
assert_eq!(loc, "en");
sourceimpl From<Language> for TinyAsciiStr<3>
impl From<Language> for TinyAsciiStr<3>
sourceimpl Ord for Language
impl Ord for Language
sourceimpl PartialOrd<Language> for Language
impl PartialOrd<Language> for Language
sourcefn partial_cmp(&self, other: &Language) -> Option<Ordering>
fn partial_cmp(&self, other: &Language) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl ULE for Language
impl ULE for Language
sourcefn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError>
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError>
Validates a byte slice, &[u8]
. Read more
sourcefn parse_byte_slice(bytes: &[u8]) -> Result<&[Self], ZeroVecError>
fn parse_byte_slice(bytes: &[u8]) -> Result<&[Self], ZeroVecError>
Parses a byte slice, &[u8]
, and return it as &[Self]
with the same lifetime. Read more
sourceunsafe fn from_byte_slice_unchecked(bytes: &[u8]) -> &[Self]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
unsafe fn from_byte_slice_unchecked(bytes: &[u8]) -> &[Self]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
Takes a byte slice, &[u8]
, and return it as &[Self]
with the same lifetime, assuming
that this byte slice has previously been run through Self::parse_byte_slice()
with
success. Read more
sourceimpl Writeable for Language
impl Writeable for Language
sourcefn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
Writes bytes to the given sink. Errors from the sink are bubbled up.
The default implementation delegates to write_to_parts
, and discards any
Part
annotations. Read more
sourcefn write_len(&self) -> LengthHint
fn write_len(&self) -> LengthHint
Returns a hint for the number of bytes that will be written to the sink. Read more
sourcefn write_to_parts<S>(&self, sink: &mut S) -> Result<(), Error> where
S: PartsWrite + ?Sized,
fn write_to_parts<S>(&self, sink: &mut S) -> Result<(), Error> where
S: PartsWrite + ?Sized,
Write bytes and Part
annotations to the given sink. Errors from the
sink are bubbled up. The default implementation delegates to write_to
,
and doesn’t produce any Part
annotations. Read more
sourcefn write_to_string(&self) -> Cow<'_, str>
fn write_to_string(&self) -> Cow<'_, str>
Creates a new String
with the data from this Writeable
. Like ToString
,
but smaller and faster. Read more
sourceimpl<'a> ZeroMapKV<'a> for Language
impl<'a> ZeroMapKV<'a> for Language
type Container = ZeroVec<'a, Language>
type Container = ZeroVec<'a, Language>
The container that can be used with this type: ZeroVec
or VarZeroVec
.
impl Copy for Language
impl Eq for Language
impl StructuralEq for Language
impl StructuralPartialEq for Language
Auto Trait Implementations
impl RefUnwindSafe for Language
impl Send for Language
impl Sync for Language
impl Unpin for Language
impl UnwindSafe for Language
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more