Skip to content

Commit 9567960

Browse files
committed
Initial demonstration of ResourceKey::type_id
See unicode-org#570
1 parent 54d3b85 commit 9567960

File tree

6 files changed

+26
-11
lines changed

6 files changed

+26
-11
lines changed

provider/core/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ std = ["icu_locid/std"]
3636
provider_serde = ["serde", "erased-serde"]
3737
macros = ["icu_provider_macros"]
3838

39+
# The "expressive_keys" feature enables type checking of resource keys.
40+
expressive_keys = []
41+
3942
[dependencies]
4043
icu_locid = { version = "0.3", path = "../../components/locid" }
4144
tinystr = { version = "0.4.10", features = ["alloc"], default-features = false }

provider/core/src/data_provider/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::yoke;
1919
// JSON string. It also exercises most of the data provider code paths.
2020

2121
/// Key for HelloAlt, used for testing mismatched types
22-
const HELLO_ALT_KEY: ResourceKey = crate::resource_key!(Core, "helloalt", 1);
22+
const HELLO_ALT_KEY: ResourceKey = crate::resource_key!(Core, HelloAlt, "helloalt", 1);
2323

2424
/// A data struct serialization-compatible with HelloWorldV1 used for testing mismatched types
2525
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Yokeable, ZeroCopyFrom)]

provider/core/src/hello_world.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use icu_locid::LanguageIdentifier;
1818
use litemap::LiteMap;
1919

2020
pub mod key {
21-
use crate::resource::ResourceKey;
22-
pub const HELLO_WORLD_V1: ResourceKey = resource_key!(Core, "helloworld", 1);
21+
use super::*;
22+
pub const HELLO_WORLD_V1: ResourceKey = resource_key!(Core, HelloWorldV1, "helloworld", 1);
2323
}
2424

2525
/// A struct containing "Hello World" in the requested language.

provider/core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
//! [`impl_dyn_provider!`]: impl_dyn_provider
104104
105105
#![cfg_attr(not(any(test, feature = "std")), no_std)]
106+
#![feature(const_type_id)]
106107

107108
extern crate alloc;
108109

provider/core/src/resource.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ pub struct ResourceKey {
7979
pub category: ResourceCategory,
8080
pub sub_category: TinyStr16,
8181
pub version: u16,
82+
#[cfg(feature = "expressive_keys")]
83+
pub type_id: core::any::TypeId,
8284
}
8385

8486
/// Shortcut to construct a const resource identifier.
@@ -104,21 +106,29 @@ pub struct ResourceKey {
104106
/// ```
105107
#[macro_export]
106108
macro_rules! resource_key {
107-
($category:ident, $sub_category:literal, $version:tt) => {
108-
$crate::resource_key!($crate::ResourceCategory::$category, $sub_category, $version)
109+
($category:ident, $struct_type:ty, $sub_category:literal, $version:tt) => {
110+
$crate::resource_key!(
111+
$crate::ResourceCategory::$category,
112+
$struct_type,
113+
$sub_category,
114+
$version
115+
)
109116
};
110-
(x, $pu:literal, $sub_category:literal, $version:tt) => {
117+
(x, $pu:literal, $struct_type:ty, $sub_category:literal, $version:tt) => {
111118
$crate::resource_key!(
112119
$crate::ResourceCategory::PrivateUse($crate::internal::tinystr4!($pu)),
120+
$struct_type,
113121
$sub_category,
114122
$version
115123
)
116124
};
117-
($category:expr, $sub_category:literal, $version:tt) => {
125+
($category:path, $struct_type:ty, $sub_category:literal, $version:tt) => {
118126
$crate::ResourceKey {
119127
category: $category,
120128
sub_category: $crate::internal::tinystr16!($sub_category),
121129
version: $version,
130+
#[cfg(feature = "expressive_keys")]
131+
type_id: core::any::TypeId::of::<$struct_type>(),
122132
}
123133
};
124134
}
@@ -410,23 +420,24 @@ mod tests {
410420
fn get_key_test_cases() -> [KeyTestCase; 4] {
411421
[
412422
KeyTestCase {
413-
resc_key: resource_key!(Core, "cardinal", 1),
423+
resc_key: resource_key!(Core, (), "cardinal", 1),
414424
expected: "core/cardinal@1",
415425
},
416426
KeyTestCase {
417427
resc_key: ResourceKey {
418428
category: ResourceCategory::PrivateUse(tinystr4!("priv")),
419429
sub_category: tinystr::tinystr16!("cardinal"),
420430
version: 1,
431+
type_id: core::any::TypeId::of::<()>(),
421432
},
422433
expected: "x-priv/cardinal@1",
423434
},
424435
KeyTestCase {
425-
resc_key: resource_key!(Core, "maxlengthsubcatg", 1),
436+
resc_key: resource_key!(Core, (), "maxlengthsubcatg", 1),
426437
expected: "core/maxlengthsubcatg@1",
427438
},
428439
KeyTestCase {
429-
resc_key: resource_key!(Core, "cardinal", 65535),
440+
resc_key: resource_key!(Core, (),"cardinal", 65535),
430441
expected: "core/cardinal@65535",
431442
},
432443
]

provider/core/tests/sizes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ use static_assertions::const_assert_eq;
88
const_assert_eq!(8, core::mem::size_of::<ResourceCategory>());
99
const_assert_eq!(16, core::mem::size_of::<tinystr::TinyStr16>());
1010
const_assert_eq!(4, core::mem::size_of::<u32>());
11-
const_assert_eq!(32, core::mem::size_of::<ResourceKey>());
11+
const_assert_eq!(40, core::mem::size_of::<ResourceKey>());

0 commit comments

Comments
 (0)