Struct icu_provider::DataKey
source · pub struct DataKey {
path: DataKeyPath,
hash: DataKeyHash,
metadata: DataKeyMetadata,
}
Expand description
Used for loading data from an ICU4X data provider.
A resource key is tightly coupled with the code that uses it to load data at runtime.
Executables can be searched for DataKey
instances to produce optimized data files.
Therefore, users should not generally create DataKey instances; they should instead use
the ones exported by a component.
DataKey
s are created with the data_key!
macro:
const K: DataKey = icu_provider::data_key!("foo/bar@1");
The human-readable path string ends with @
followed by one or more digits (the version
number). Paths do not contain characters other than ASCII letters and digits, _
, /
.
Invalid paths are compile-time errors (as data_key!
uses const
).
const K: DataKey = icu_provider::data_key!("foo/../bar@1");
Fields§
§path: DataKeyPath
§hash: DataKeyHash
§metadata: DataKeyMetadata
Implementations§
source§impl DataKey
impl DataKey
sourcepub const fn path(self) -> DataKeyPath
pub const fn path(self) -> DataKeyPath
Gets a human-readable representation of a DataKey
.
The human-readable path string ends with @
followed by one or more digits (the version
number). Paths do not contain characters other than ASCII letters and digits, _
, /
.
Useful for reading and writing data to a file system.
sourcepub const fn hashed(self) -> DataKeyHash
pub const fn hashed(self) -> DataKeyHash
Gets a platform-independent hash of a DataKey
.
The hash is 4 bytes and allows for fast key comparison.
§Example
use icu_provider::DataKey;
use icu_provider::DataKeyHash;
const KEY: DataKey = icu_provider::data_key!("foo@1");
const KEY_HASH: DataKeyHash = KEY.hashed();
assert_eq!(KEY_HASH.to_bytes(), [0xe2, 0xb6, 0x17, 0x71]);
sourcepub const fn metadata(self) -> DataKeyMetadata
pub const fn metadata(self) -> DataKeyMetadata
Gets the metadata associated with this DataKey
.
sourcepub const fn fallback_config(self) -> LocaleFallbackConfig
pub const fn fallback_config(self) -> LocaleFallbackConfig
Returns the [LocaleFallbackConfig
] for this DataKey
.
sourcepub const fn from_path_and_metadata(
path: DataKeyPath,
metadata: DataKeyMetadata,
) -> Self
pub const fn from_path_and_metadata( path: DataKeyPath, metadata: DataKeyMetadata, ) -> Self
const fn validate_path_manual_slice( path: &'static str, start: usize, end: usize, ) -> Result<(), (&'static str, usize)>
sourcepub fn match_key(self, key: Self) -> Result<(), DataError>
pub fn match_key(self, key: Self) -> Result<(), DataError>
Returns Ok
if this data key matches the argument, or the appropriate error.
Convenience method for data providers that support a single DataKey
.
§Examples
use icu_provider::prelude::*;
const FOO_BAR: DataKey = icu_provider::data_key!("foo/bar@1");
const FOO_BAZ: DataKey = icu_provider::data_key!("foo/baz@1");
const BAR_BAZ: DataKey = icu_provider::data_key!("bar/baz@1");
assert!(matches!(FOO_BAR.match_key(FOO_BAR), Ok(())));
assert!(matches!(
FOO_BAR.match_key(FOO_BAZ),
Err(DataError {
kind: DataErrorKind::MissingDataKey,
..
})
));
assert!(matches!(
FOO_BAR.match_key(BAR_BAZ),
Err(DataError {
kind: DataErrorKind::MissingDataKey,
..
})
));
// The error context contains the argument:
assert_eq!(FOO_BAR.match_key(BAR_BAZ).unwrap_err().key, Some(BAR_BAZ));
Trait Implementations§
source§impl Display for DataKey
impl Display for DataKey
This trait is implemented for compatibility with fmt!
.
To create a string, Writeable::write_to_string
is usually more efficient.
source§impl Ord for DataKey
impl Ord for DataKey
source§impl PartialEq for DataKey
impl PartialEq for DataKey
source§impl PartialOrd for DataKey
impl PartialOrd for DataKey
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl Writeable for DataKey
impl Writeable for DataKey
source§fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
write_to_parts
, and discards any
Part
annotations.source§fn writeable_length_hint(&self) -> LengthHint
fn writeable_length_hint(&self) -> LengthHint
source§fn write_to_string(&self) -> Cow<'_, str>
fn write_to_string(&self) -> Cow<'_, str>
String
with the data from this Writeable
. Like ToString
,
but smaller and faster. Read moresource§fn 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,
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.