Struct icu_provider::DataKey
source · pub struct DataKey { /* private fields */ }
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");
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
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 PartialOrd for DataKey
impl PartialOrd for DataKey
source§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.impl Copy for DataKey
impl Eq for DataKey
Auto Trait Implementations§
impl Freeze for DataKey
impl RefUnwindSafe for DataKey
impl Send for DataKey
impl Sync for DataKey
impl Unpin for DataKey
impl UnwindSafe for DataKey
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)