#[repr(transparent)]pub struct Variant(_);
Expand description
A variant subtag (examples: "macos"
, "posix"
, "1996"
etc.)
Variant
represents a Unicode base language code conformat to the
unicode_variant_id
field of the Language and Locale Identifier.
Examples
use icu::locid::subtags::Variant;
let variant: Variant = "macos".parse().expect("Failed to parse a variant subtag.");
Implementations
sourceimpl Variant
impl Variant
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; 8]) -> Result<Self, ParserError>
pub fn try_from_raw(v: [u8; 8]) -> Result<Self, ParserError>
Safely creates a Variant
from a reference to its raw format
as returned by Variant::into_raw()
.
Examples
use icu::locid::subtags::Variant;
assert!(matches!(Variant::try_from_raw(*b"fonipa\0\0"), Ok(_)));
assert!(matches!(Variant::try_from_raw(*b"1992\0\0\0\0"), Ok(_)));
assert!(matches!(Variant::try_from_raw(*b"foo\0\0\0\0\0"), Err(_)));
// Unlike the other constructors, this one is case-sensitive:
assert!(matches!(Variant::try_from_raw(*b"POSIX\0\0\0"), Err(_)));
sourcepub fn into_raw(self) -> [u8; 8]
pub fn into_raw(self) -> [u8; 8]
Deconstructs the Variant
into raw format to be consumed
by from_raw_unchecked()
.
Examples
use icu::locid::subtags::Variant;
let variant = Variant::from_bytes(b"posix").expect("Parsing failed.");
let raw = variant.into_raw();
let variant = unsafe { Variant::from_raw_unchecked(raw) };
assert_eq!(variant.as_str(), "posix");
sourcepub const unsafe fn from_raw_unchecked(v: [u8; 8]) -> Self
pub const unsafe fn from_raw_unchecked(v: [u8; 8]) -> Self
Constructor which takes a raw value returned by
into_raw()
.
Examples
use icu::locid::subtags::Variant;
let variant = Variant::from_bytes(b"posix").expect("Parsing failed.");
let raw = variant.into_raw();
let variant = unsafe { Variant::from_raw_unchecked(raw) };
assert_eq!(variant.as_str(), "posix");
Safety
This function accepts a [u8; 8] that is expected to be a valid TinyAsciiStr<8>
representing a Variant
subtag in canonical syntax.
sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
A helper function for displaying
a Variant
subtag as a &
str
.
Examples
use icu::locid::subtags::Variant;
let variant = Variant::from_bytes(b"macos").expect("Parsing failed.");
assert_eq!(variant.as_str(), "macos");
Notice
: For many use cases, such as comparison,
Variant
implements PartialEq
<&
str
>
which allows for direct comparisons.
sourcepub fn strict_cmp(&self, other: &[u8]) -> Ordering
pub fn strict_cmp(&self, other: &[u8]) -> Ordering
Compare this Variant
with BCP-47 bytes.
The return value is equivalent to what would happen if you first converted this
Variant
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()
.
sourcepub fn normalizing_eq(&self, other: &str) -> bool
pub fn normalizing_eq(&self, other: &str) -> bool
Compare this Variant
with a potentially unnormalized BCP-47 string.
The return value is equivalent to what would happen if you first parsed the
BCP-47 string to a Variant
and then performed a structucal comparison.
Trait Implementations
sourceimpl AsULE for Variant
impl AsULE for Variant
Impl enabling Variant
to be used in a ZeroVec
. Enabled with the "zerovec"
feature.
Example
use icu::locid::subtags::Variant;
use icu::locid::subtags_variant as variant;
use zerovec::ZeroVec;
let zv = ZeroVec::<Variant>::parse_byte_slice(b"fonipa\0\01992\0\0\0\0posix\0\0\0")
.expect("Valid variant subtags");
assert_eq!(zv.get(1), Some(variant!("1992")));
ZeroVec::<Variant>::parse_byte_slice(b"invalid").expect_err("Invalid byte slice");
sourceimpl Bake for Variant
impl Bake for Variant
sourcefn bake(&self, env: &CrateEnv) -> TokenStream
fn bake(&self, env: &CrateEnv) -> TokenStream
Returns a TokenStream
that would evalutate to self
. Read more
sourceimpl<'de> Deserialize<'de> for Variant
impl<'de> Deserialize<'de> for Variant
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<Variant> for TinyAsciiStr<8>
impl From<Variant> for TinyAsciiStr<8>
sourceimpl Ord for Variant
impl Ord for Variant
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
Restrict a value to a certain interval. Read more
sourceimpl PartialOrd<Variant> for Variant
impl PartialOrd<Variant> for Variant
sourcefn partial_cmp(&self, other: &Variant) -> Option<Ordering>
fn partial_cmp(&self, other: &Variant) -> 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 Variant
impl ULE for Variant
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 &mut [u8]impl Write for &mut [u8]impl Read for &[u8]
unsafe fn from_byte_slice_unchecked(bytes: &[u8]) -> &[Self]ⓘNotable traits for &mut [u8]impl Write for &mut [u8]impl Read for &[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 Variant
impl Writeable for Variant
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 Variant
impl<'a> ZeroMapKV<'a> for Variant
impl Copy for Variant
impl Eq for Variant
impl StructuralEq for Variant
impl StructuralPartialEq for Variant
Auto Trait Implementations
impl RefUnwindSafe for Variant
impl Send for Variant
impl Sync for Variant
impl Unpin for Variant
impl UnwindSafe for Variant
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