Skip to content

Commit b8a0b36

Browse files
Add icu_provider::marker::ErasedMarker (#5590)
Using an erased marker for storage is a common pattern across multiple components. Also removed some unnecessary `cast()`s in datetime.
1 parent cc3d7f5 commit b8a0b36

File tree

18 files changed

+53
-95
lines changed

18 files changed

+53
-95
lines changed

components/datetime/src/format/neo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ where
158158
match provider.load_bound(req) {
159159
Ok(response) => {
160160
self.inner = OptionalNames::SingleLength {
161-
payload: response.payload.cast(),
161+
payload: response.payload,
162162
variables: arg_variables,
163163
};
164164
Ok(Ok(()))

components/datetime/src/raw/neo.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ impl DatePatternSelectionData {
202202
),
203203
..Default::default()
204204
})?
205-
.payload
206-
.cast();
205+
.payload;
207206
Ok(Self::SkeletonDate {
208207
skeleton: NeoDateSkeleton {
209208
length: length.get::<Self>(),
@@ -324,8 +323,7 @@ impl OverlapPatternSelectionData {
324323
id: DataIdentifierBorrowed::for_marker_attributes_and_locale(marker_attrs, locale),
325324
..Default::default()
326325
})?
327-
.payload
328-
.cast();
326+
.payload;
329327
Ok(Self::SkeletonDateTime {
330328
date_skeleton,
331329
time_skeleton,
@@ -407,7 +405,7 @@ impl TimePatternSelectionData {
407405
),
408406
..Default::default()
409407
}) {
410-
Ok(response) => Some(response.payload.cast()),
408+
Ok(response) => Some(response.payload),
411409
Err(DataError {
412410
kind: DataErrorKind::IdentifierNotFound,
413411
..
@@ -417,16 +415,17 @@ impl TimePatternSelectionData {
417415
}
418416
let payload = match maybe_payload {
419417
Some(payload) => payload,
420-
None => provider
421-
.load_bound(DataRequest {
422-
id: DataIdentifierBorrowed::for_marker_attributes_and_locale(
423-
components.id_str(),
424-
locale,
425-
),
426-
..Default::default()
427-
})?
428-
.payload
429-
.cast(),
418+
None => {
419+
provider
420+
.load_bound(DataRequest {
421+
id: DataIdentifierBorrowed::for_marker_attributes_and_locale(
422+
components.id_str(),
423+
locale,
424+
),
425+
..Default::default()
426+
})?
427+
.payload
428+
}
430429
};
431430
Ok(Self::SkeletonTime {
432431
skeleton: NeoTimeSkeleton {

components/experimental/src/compactdecimal/formatter.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
// called LICENSE at the top level of the ICU4X source tree
33
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
44

5-
use super::options::CompactDecimalFormatterOptions;
65
use crate::compactdecimal::{
76
format::FormattedCompactDecimal,
7+
options::CompactDecimalFormatterOptions,
88
provider::{
9-
Count, ErasedCompactDecimalFormatDataV1Marker, LongCompactDecimalFormatDataV1Marker,
10-
PatternULE, ShortCompactDecimalFormatDataV1Marker,
9+
CompactDecimalPatternDataV1, Count, LongCompactDecimalFormatDataV1Marker, PatternULE,
10+
ShortCompactDecimalFormatDataV1Marker,
1111
},
1212
ExponentError,
1313
};
@@ -16,8 +16,8 @@ use core::convert::TryFrom;
1616
use fixed_decimal::{CompactDecimal, FixedDecimal};
1717
use icu_decimal::FixedDecimalFormatter;
1818
use icu_plurals::PluralRules;
19-
use icu_provider::prelude::*;
2019
use icu_provider::DataError;
20+
use icu_provider::{marker::ErasedMarker, prelude::*};
2121
use zerovec::maps::ZeroMap2dCursor;
2222

2323
/// A formatter that renders locale-sensitive compact numbers.
@@ -59,7 +59,7 @@ use zerovec::maps::ZeroMap2dCursor;
5959
pub struct CompactDecimalFormatter {
6060
pub(crate) plural_rules: PluralRules,
6161
pub(crate) fixed_decimal_formatter: FixedDecimalFormatter,
62-
pub(crate) compact_data: DataPayload<ErasedCompactDecimalFormatDataV1Marker>,
62+
pub(crate) compact_data: DataPayload<ErasedMarker<CompactDecimalPatternDataV1<'static>>>,
6363
}
6464

6565
impl CompactDecimalFormatter {

components/experimental/src/compactdecimal/provider.rs

-5
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,3 @@ pub struct Pattern<'data> {
141141
/// " M" for the pattern "000 M"
142142
pub literal_text: Cow<'data, str>,
143143
}
144-
pub(crate) struct ErasedCompactDecimalFormatDataV1Marker;
145-
146-
impl DynamicDataMarker for ErasedCompactDecimalFormatDataV1Marker {
147-
type DataStruct = CompactDecimalPatternDataV1<'static>;
148-
}

components/experimental/src/relativetime/provider.rs

-6
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,3 @@ pub struct RelativeTimePatternDataV1<'data> {
7272
#[cfg_attr(feature = "serde", serde(borrow))]
7373
pub future: PluralElementsPackedCow<'data, SinglePlaceholderPattern>,
7474
}
75-
76-
pub(crate) struct ErasedRelativeTimeFormatV1Marker;
77-
78-
impl DynamicDataMarker for ErasedRelativeTimeFormatV1Marker {
79-
type DataStruct = RelativeTimePatternDataV1<'static>;
80-
}

components/experimental/src/relativetime/relativetime.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use icu_decimal::{
77
options::FixedDecimalFormatterOptions, provider::DecimalSymbolsV1Marker, FixedDecimalFormatter,
88
};
99
use icu_plurals::{provider::CardinalV1Marker, PluralRules};
10+
use icu_provider::marker::ErasedMarker;
1011
use icu_provider::prelude::*;
1112

1213
use crate::relativetime::format::FormattedRelativeTime;
@@ -104,7 +105,7 @@ use crate::relativetime::provider::*;
104105
/// ```
105106
pub struct RelativeTimeFormatter {
106107
pub(crate) plural_rules: PluralRules,
107-
pub(crate) rt: DataPayload<ErasedRelativeTimeFormatV1Marker>,
108+
pub(crate) rt: DataPayload<ErasedMarker<RelativeTimePatternDataV1<'static>>>,
108109
pub(crate) options: RelativeTimeFormatterOptions,
109110
pub(crate) fixed_decimal_format: FixedDecimalFormatter,
110111
}

components/list/src/list_formatter.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use crate::provider::*;
66
use crate::ListLength;
77
use core::fmt::{self, Write};
8+
use icu_provider::marker::ErasedMarker;
89
use icu_provider::prelude::*;
910
use writeable::*;
1011

@@ -15,7 +16,7 @@ extern crate writeable;
1516
/// [crate-level documentation](crate) for more details.
1617
#[derive(Debug)]
1718
pub struct ListFormatter {
18-
data: DataPayload<ErasedListV2Marker>,
19+
data: DataPayload<ErasedMarker<ListFormatterPatternsV2<'static>>>,
1920
}
2021

2122
macro_rules! constructor {

components/list/src/provider/mod.rs

-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
1818
use alloc::borrow::Cow;
1919
use icu_provider::prelude::*;
20-
use icu_provider::DynamicDataMarker;
2120

2221
mod serde_dfa;
2322
pub use serde_dfa::SerdeDFA;
@@ -102,12 +101,6 @@ impl ListFormatterPatternsV2<'_> {
102101
pub const WIDE_STR: &'static str = Self::WIDE.as_str();
103102
}
104103

105-
pub(crate) struct ErasedListV2Marker;
106-
107-
impl DynamicDataMarker for ErasedListV2Marker {
108-
type DataStruct = ListFormatterPatternsV2<'static>;
109-
}
110-
111104
/// A pattern that can behave conditionally on the next element.
112105
///
113106
/// <div class="stab unstable">

components/locale/src/exemplar_chars.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,12 @@
3333
use crate::provider::*;
3434
use core::ops::Deref;
3535
use icu_collections::codepointinvliststringlist::CodePointInversionListAndStringList;
36-
use icu_provider::prelude::*;
36+
use icu_provider::{marker::ErasedMarker, prelude::*};
3737

3838
/// A wrapper around `UnicodeSet` data (characters and strings)
3939
#[derive(Debug)]
4040
pub struct ExemplarCharacters {
41-
data: DataPayload<ErasedUnicodeSetMarker>,
42-
}
43-
44-
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
45-
pub(crate) struct ErasedUnicodeSetMarker;
46-
impl DynamicDataMarker for ErasedUnicodeSetMarker {
47-
type DataStruct = ExemplarCharactersV1<'static>;
41+
data: DataPayload<ErasedMarker<ExemplarCharactersV1<'static>>>,
4842
}
4943

5044
impl ExemplarCharacters {

components/plurals/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ pub mod provider;
8181
pub mod rules;
8282

8383
use core::cmp::{Ord, PartialOrd};
84+
use icu_provider::marker::ErasedMarker;
8485
use icu_provider::prelude::*;
8586
pub use operands::PluralOperands;
8687
use provider::CardinalV1Marker;
87-
use provider::ErasedPluralRulesV1Marker;
8888
use provider::OrdinalV1Marker;
89+
use provider::PluralRulesV1;
8990
use rules::runtime::test_rule;
9091

9192
#[cfg(feature = "experimental")]
@@ -280,7 +281,7 @@ impl PluralCategory {
280281
/// [`Plural Type`]: PluralRuleType
281282
/// [`Plural Category`]: PluralCategory
282283
#[derive(Debug)]
283-
pub struct PluralRules(DataPayload<ErasedPluralRulesV1Marker>);
284+
pub struct PluralRules(DataPayload<ErasedMarker<PluralRulesV1<'static>>>);
284285

285286
impl AsRef<PluralRules> for PluralRules {
286287
fn as_ref(&self) -> &PluralRules {

components/plurals/src/provider.rs

-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use alloc::vec::Vec;
2323
use core::fmt;
2424
use core::marker::PhantomData;
2525
use icu_provider::prelude::*;
26-
use icu_provider::DynamicDataMarker;
2726
use yoke::Yokeable;
2827
use zerofrom::ZeroFrom;
2928
use zerovec::ule::vartuple::VarTuple;
@@ -107,12 +106,6 @@ pub struct PluralRulesV1<'data> {
107106
pub many: Option<Rule<'data>>,
108107
}
109108

110-
pub(crate) struct ErasedPluralRulesV1Marker;
111-
112-
impl DynamicDataMarker for ErasedPluralRulesV1Marker {
113-
type DataStruct = PluralRulesV1<'static>;
114-
}
115-
116109
#[cfg(any(feature = "datagen", feature = "experimental"))]
117110
pub use ranges::*;
118111

components/properties/src/code_point_map.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::provider::*;
1616
use crate::{code_point_set::CodePointSetData, props::GeneralCategoryGroup};
1717
use core::ops::RangeInclusive;
1818
use icu_collections::codepointtrie::{CodePointMapRange, CodePointTrie, TrieValue};
19+
use icu_provider::marker::ErasedMarker;
1920
use icu_provider::prelude::*;
2021
use zerovec::ule::UleError;
2122

@@ -25,7 +26,7 @@ use zerovec::ule::UleError;
2526
/// [`CodePointMapDataBorrowed`].
2627
#[derive(Debug, Clone)]
2728
pub struct CodePointMapData<T: TrieValue> {
28-
data: DataPayload<ErasedPropertyCodePointMapV1Marker<T>>,
29+
data: DataPayload<ErasedMarker<PropertyCodePointMapV1<'static, T>>>,
2930
}
3031

3132
impl<T: TrieValue> CodePointMapData<T> {
@@ -96,10 +97,8 @@ impl<T: TrieValue> CodePointMapData<T> {
9697
P: TrieValue,
9798
{
9899
self.data
99-
.try_map_project::<ErasedPropertyCodePointMapV1Marker<P>, _, _>(move |data, _| {
100-
data.try_into_converted()
101-
})
102-
.map(CodePointMapData::from_data)
100+
.try_map_project(|data, _| data.try_into_converted())
101+
.map(CodePointMapData::from_data::<ErasedMarker<PropertyCodePointMapV1<'static, P>>>)
103102
}
104103

105104
/// Construct a new one from loaded data
@@ -115,9 +114,9 @@ impl<T: TrieValue> CodePointMapData<T> {
115114
/// Construct a new one an owned [`CodePointTrie`]
116115
pub fn from_code_point_trie(trie: CodePointTrie<'static, T>) -> Self {
117116
let set = PropertyCodePointMapV1::from_code_point_trie(trie);
118-
CodePointMapData::from_data(
119-
DataPayload::<ErasedPropertyCodePointMapV1Marker<T>>::from_owned(set),
120-
)
117+
CodePointMapData::from_data(DataPayload::<
118+
ErasedMarker<PropertyCodePointMapV1<'static, T>>,
119+
>::from_owned(set))
121120
}
122121

123122
/// Convert this type to a [`CodePointTrie`] as a borrowed value.

components/properties/src/code_point_set.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::provider::*;
1818
use crate::runtime::UnicodeProperty;
1919
use core::ops::RangeInclusive;
2020
use icu_collections::codepointinvlist::CodePointInversionList;
21+
use icu_provider::marker::ErasedMarker;
2122
use icu_provider::prelude::*;
2223

2324
/// A set of Unicode code points. Access its data via the borrowed version,
@@ -37,7 +38,7 @@ use icu_provider::prelude::*;
3738
/// ```
3839
#[derive(Debug)]
3940
pub struct CodePointSetData {
40-
data: DataPayload<ErasedPropertyCodePointSetV1Marker>,
41+
data: DataPayload<ErasedMarker<PropertyCodePointSetV1<'static>>>,
4142
}
4243

4344
impl CodePointSetData {
@@ -85,7 +86,7 @@ impl CodePointSetData {
8586
pub fn from_code_point_inversion_list(set: CodePointInversionList<'static>) -> Self {
8687
let set = PropertyCodePointSetV1::from_code_point_inversion_list(set);
8788
CodePointSetData::from_data(
88-
DataPayload::<ErasedPropertyCodePointSetV1Marker>::from_owned(set),
89+
DataPayload::<ErasedMarker<PropertyCodePointSetV1<'static>>>::from_owned(set),
8990
)
9091
}
9192

components/properties/src/names.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@ use crate::props::*;
66
use crate::provider::names::*;
77
use core::marker::PhantomData;
88
use icu_collections::codepointtrie::TrieValue;
9+
use icu_provider::marker::ErasedMarker;
910
use icu_provider::prelude::*;
1011
use yoke::Yokeable;
1112
use zerovec::ule::VarULE;
1213

13-
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
14-
pub struct ErasedMarker<DataStruct: for<'a> Yokeable<'a>>(PhantomData<DataStruct>);
15-
impl<DataStruct: for<'a> Yokeable<'a>> DynamicDataMarker for ErasedMarker<DataStruct> {
16-
type DataStruct = DataStruct;
17-
}
18-
1914
/// A struct capable of looking up a property value from a string name.
2015
/// Access its data by calling [`Self::as_borrowed()`] and using the methods on
2116
/// [`PropertyParserBorrowed`].

components/properties/src/provider.rs

-12
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,6 @@ pub enum PropertyCodePointSetV1<'data> {
405405
// https://docs.rs/serde/latest/serde/trait.Serializer.html#tymethod.serialize_unit_variant
406406
}
407407

408-
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
409-
pub(crate) struct ErasedPropertyCodePointSetV1Marker;
410-
impl DynamicDataMarker for ErasedPropertyCodePointSetV1Marker {
411-
type DataStruct = PropertyCodePointSetV1<'static>;
412-
}
413-
414408
// See CodePointSetData for documentation of these functions
415409
impl<'data> PropertyCodePointSetV1<'data> {
416410
#[inline]
@@ -489,12 +483,6 @@ pub enum PropertyCodePointMapV1<'data, T: TrieValue> {
489483
// https://docs.rs/serde/latest/serde/trait.Serializer.html#tymethod.serialize_unit_variant
490484
}
491485

492-
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
493-
pub(crate) struct ErasedPropertyCodePointMapV1Marker<T>(core::marker::PhantomData<T>);
494-
impl<T: TrieValue> DynamicDataMarker for ErasedPropertyCodePointMapV1Marker<T> {
495-
type DataStruct = PropertyCodePointMapV1<'static, T>;
496-
}
497-
498486
macro_rules! data_struct_generic {
499487
($(marker($marker:ident, $ty:ident, $path:literal),)+) => {
500488
$(

components/properties/src/unicode_set.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,14 @@
1717
use crate::provider::*;
1818
use crate::*;
1919
use icu_collections::codepointinvliststringlist::CodePointInversionListAndStringList;
20+
use icu_provider::marker::ErasedMarker;
2021
use icu_provider::prelude::*;
2122
use runtime::UnicodeProperty;
2223

2324
/// A wrapper around `UnicodeSet` data (characters and strings)
2425
#[derive(Debug)]
2526
pub struct UnicodeSetData {
26-
data: DataPayload<ErasedUnicodeSetlikeMarker>,
27-
}
28-
29-
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
30-
pub(crate) struct ErasedUnicodeSetlikeMarker;
31-
impl DynamicDataMarker for ErasedUnicodeSetlikeMarker {
32-
type DataStruct = PropertyUnicodeSetV1<'static>;
27+
data: DataPayload<ErasedMarker<PropertyUnicodeSetV1<'static>>>,
3328
}
3429

3530
impl UnicodeSetData {
@@ -84,7 +79,9 @@ impl UnicodeSetData {
8479
set: CodePointInversionListAndStringList<'static>,
8580
) -> Self {
8681
let set = PropertyUnicodeSetV1::from_code_point_inversion_list_string_list(set);
87-
UnicodeSetData::from_data(DataPayload::<ErasedUnicodeSetlikeMarker>::from_owned(set))
82+
UnicodeSetData::from_data(
83+
DataPayload::<ErasedMarker<PropertyUnicodeSetV1<'static>>>::from_owned(set),
84+
)
8885
}
8986

9087
/// Convert this type to a [`CodePointInversionListAndStringList`] as a borrowed value.

provider/core/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub mod marker {
150150
151151
pub use super::marker_full::{
152152
data_marker_path, impl_data_provider_never_marker, DataMarkerPath, DataMarkerPathHash,
153-
NeverMarker,
153+
ErasedMarker, NeverMarker,
154154
};
155155
}
156156

0 commit comments

Comments
 (0)