From 72c85fa49cb64486a6490709640ee300abd470cd Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Thu, 27 May 2021 17:53:49 -0500 Subject: [PATCH] Replace DataPayload Deref with .get() throughout ICU4X See #667 --- components/datetime/src/datetime.rs | 5 +- components/datetime/src/format/datetime.rs | 2 +- .../datetime/src/format/zoned_datetime.rs | 2 +- components/datetime/src/skeleton.rs | 24 ++++---- components/datetime/src/time_zone.rs | 21 +++++-- components/datetime/src/zoned_datetime.rs | 1 + components/datetime/tests/datetime.rs | 4 +- components/decimal/src/lib.rs | 2 +- .../src/locale_canonicalizer.rs | 30 +++------- components/plurals/benches/parser.rs | 15 ++--- components/plurals/src/lib.rs | 2 +- components/uniset/src/props.rs | 2 +- experimental/provider_ppucd/src/support.rs | 2 +- provider/cldr/src/download/cldr_allinone.rs | 2 +- provider/cldr/src/transform/dates.rs | 26 ++++++--- provider/cldr/src/transform/likelysubtags.rs | 5 +- provider/cldr/src/transform/numbers/mod.rs | 4 +- provider/cldr/src/transform/plurals.rs | 13 +++-- provider/cldr/src/transform/time_zones/mod.rs | 15 +++-- provider/core/src/data_provider.rs | 55 +++++++------------ provider/core/src/export.rs | 3 +- provider/core/src/hello_world.rs | 2 +- provider/core/src/inv.rs | 2 +- provider/core/src/struct_provider.rs | 2 +- provider/core/tests/data_provider.rs | 2 +- provider/fs/tests/test_file_io.rs | 8 +-- provider/testdata/src/lib.rs | 2 +- 27 files changed, 125 insertions(+), 128 deletions(-) diff --git a/components/datetime/src/datetime.rs b/components/datetime/src/datetime.rs index d5d731c5eb2..43f30565c78 100644 --- a/components/datetime/src/datetime.rs +++ b/components/datetime/src/datetime.rs @@ -105,6 +105,7 @@ impl<'d> DateTimeFormat<'d> { .take_payload()?; let pattern = data + .get() .patterns .get_pattern_for_options(options)? .unwrap_or_default(); @@ -191,7 +192,7 @@ impl<'d> DateTimeFormat<'d> { { FormattedDateTime { pattern: &self.pattern, - symbols: &self.symbols, + symbols: self.symbols.get(), datetime: value, locale: &self.locale, } @@ -228,7 +229,7 @@ impl<'d> DateTimeFormat<'d> { w: &mut impl std::fmt::Write, value: &impl DateTimeInput, ) -> std::fmt::Result { - datetime::write_pattern(&self.pattern, &self.symbols, value, &self.locale, w) + datetime::write_pattern(&self.pattern, self.symbols.get(), value, &self.locale, w) .map_err(|_| std::fmt::Error) } diff --git a/components/datetime/src/format/datetime.rs b/components/datetime/src/format/datetime.rs index 473509ef525..5ea8df3bbd9 100644 --- a/components/datetime/src/format/datetime.rs +++ b/components/datetime/src/format/datetime.rs @@ -268,7 +268,7 @@ mod tests { let mut sink = String::new(); write_pattern( &pattern, - &data.symbols, + &data.get().symbols, &datetime, &"und".parse().unwrap(), &mut sink, diff --git a/components/datetime/src/format/zoned_datetime.rs b/components/datetime/src/format/zoned_datetime.rs index 0aea2ad4435..5f6a6c281ad 100644 --- a/components/datetime/src/format/zoned_datetime.rs +++ b/components/datetime/src/format/zoned_datetime.rs @@ -77,7 +77,7 @@ where W: fmt::Write + ?Sized, { let pattern = &zoned_datetime_format.datetime_format.pattern; - let symbols = &zoned_datetime_format.datetime_format.symbols; + let symbols = zoned_datetime_format.datetime_format.symbols.get(); match field.symbol { FieldSymbol::TimeZone(_time_zone) => time_zone::write_field( diff --git a/components/datetime/src/skeleton.rs b/components/datetime/src/skeleton.rs index 1811b6de092..34eae44b8e8 100644 --- a/components/datetime/src/skeleton.rs +++ b/components/datetime/src/skeleton.rs @@ -635,7 +635,7 @@ mod test { provider::{gregory::DatesV1, key::GREGORY_V1}, }; - fn get_data_provider() -> DataPayload<'static, DatesV1> { + fn get_data_payload() -> DataPayload<'static, DatesV1> { let provider = icu_testdata::get_provider(); let langid = langid!("en"); provider @@ -669,10 +669,10 @@ mod test { ..Default::default() }; let requested_fields = components.to_vec_fields(); - let data_provider = get_data_provider(); + let data_provider = get_data_payload(); match get_best_available_format_pattern( - &data_provider.patterns.datetime.skeletons, + &data_provider.get().patterns.datetime.skeletons, &requested_fields, ) { BestSkeleton::AllFieldsMatch(available_format_pattern) @@ -696,10 +696,10 @@ mod test { ..Default::default() }; let requested_fields = components.to_vec_fields(); - let data_provider = get_data_provider(); + let data_provider = get_data_payload(); match get_best_available_format_pattern( - &data_provider.patterns.datetime.skeletons, + &data_provider.get().patterns.datetime.skeletons, &requested_fields, ) { BestSkeleton::MissingOrExtraFields(available_format_pattern) => { @@ -721,11 +721,11 @@ mod test { ..Default::default() }; let requested_fields = components.to_vec_fields(); - let data_provider = get_data_provider(); + let data_provider = get_data_payload(); match create_best_pattern_for_fields( - &data_provider.patterns.datetime.skeletons, - &data_provider.patterns.datetime.length_patterns, + &data_provider.get().patterns.datetime.skeletons, + &data_provider.get().patterns.datetime.length_patterns, &requested_fields, ) { BestSkeleton::AllFieldsMatch(available_format_pattern) => { @@ -744,11 +744,11 @@ mod test { fn test_skeleton_empty_bag() { let components: components::Bag = Default::default(); let requested_fields = components.to_vec_fields(); - let data_provider = get_data_provider(); + let data_provider = get_data_payload(); assert_eq!( get_best_available_format_pattern( - &data_provider.patterns.datetime.skeletons, + &data_provider.get().patterns.datetime.skeletons, &requested_fields ), BestSkeleton::NoMatch, @@ -765,11 +765,11 @@ mod test { ..Default::default() }; let requested_fields = components.to_vec_fields(); - let data_provider = get_data_provider(); + let data_provider = get_data_payload(); assert_eq!( get_best_available_format_pattern( - &data_provider.patterns.datetime.skeletons, + &data_provider.get().patterns.datetime.skeletons, &requested_fields ), BestSkeleton::NoMatch, diff --git a/components/datetime/src/time_zone.rs b/components/datetime/src/time_zone.rs index cb1dec1a2f1..7143c1578af 100644 --- a/components/datetime/src/time_zone.rs +++ b/components/datetime/src/time_zone.rs @@ -399,8 +399,14 @@ impl<'d> TimeZoneFormat<'d> { &self .exemplar_cities .as_ref() + .map(|p| p.get()) .and_then(|cities| time_zone.time_zone_id().and_then(|id| cities.get(id))) - .map(|location| self.zone_formats.region_format.replace("{0}", location)) + .map(|location| { + self.zone_formats + .get() + .region_format + .replace("{0}", location) + }) .ok_or(fmt::Error)?, ) .map_err(DateTimeFormatError::from) @@ -417,6 +423,7 @@ impl<'d> TimeZoneFormat<'d> { sink.write_str( self.mz_generic_short .as_ref() + .map(|p| p.get()) .and_then(|metazones| time_zone.metazone_id().and_then(|mz| metazones.get(mz))) .ok_or(fmt::Error)?, ) @@ -434,6 +441,7 @@ impl<'d> TimeZoneFormat<'d> { sink.write_str( self.mz_generic_long .as_ref() + .map(|p| p.get()) .and_then(|metazones| time_zone.metazone_id().and_then(|mz| metazones.get(mz))) .ok_or(fmt::Error)?, ) @@ -451,6 +459,7 @@ impl<'d> TimeZoneFormat<'d> { sink.write_str( self.mz_specific_short .as_ref() + .map(|p| p.get()) .and_then(|metazones| time_zone.metazone_id().and_then(|mz| metazones.get(mz))) .and_then(|specific_names| { time_zone @@ -473,6 +482,7 @@ impl<'d> TimeZoneFormat<'d> { sink.write_str( self.mz_specific_long .as_ref() + .map(|p| p.get()) .and_then(|metazones| time_zone.metazone_id().and_then(|mz| metazones.get(mz))) .and_then(|specific_names| { time_zone @@ -497,20 +507,21 @@ impl<'d> TimeZoneFormat<'d> { ) -> Result<(), DateTimeFormatError> { let gmt_offset = time_zone.gmt_offset(); if gmt_offset.is_zero() { - sink.write_str(&self.zone_formats.gmt_zero_format.clone()) + sink.write_str(&self.zone_formats.get().gmt_zero_format.clone()) .map_err(DateTimeFormatError::from) } else { // TODO(blocked on #277) Use formatter utility instead of replacing "{0}". sink.write_str( &self .zone_formats + .get() .gmt_format .replace( "{0}", if gmt_offset.is_positive() { - &self.zone_formats.hour_format.0 + &self.zone_formats.get().hour_format.0 } else { - &self.zone_formats.hour_format.1 + &self.zone_formats.get().hour_format.1 }, ) // support all combos of "(HH|H):mm" by replacing longest patterns first. @@ -531,6 +542,7 @@ impl<'d> TimeZoneFormat<'d> { sink.write_str( self.exemplar_cities .as_ref() + .map(|p| p.get()) .and_then(|cities| time_zone.time_zone_id().and_then(|id| cities.get(id))) .ok_or(fmt::Error)?, ) @@ -551,6 +563,7 @@ impl<'d> TimeZoneFormat<'d> { sink.write_str( self.exemplar_cities .as_ref() + .map(|p| p.get()) .and_then(|cities| cities.get("Etc/Unknown")) .unwrap_or(&Cow::Borrowed("Unknown")), ) diff --git a/components/datetime/src/zoned_datetime.rs b/components/datetime/src/zoned_datetime.rs index 3a0ac3b75e8..82faa468126 100644 --- a/components/datetime/src/zoned_datetime.rs +++ b/components/datetime/src/zoned_datetime.rs @@ -119,6 +119,7 @@ impl<'d> ZonedDateTimeFormat<'d> { .take_payload()?; let pattern = data + .get() .patterns .get_pattern_for_options(options)? .unwrap_or_default(); diff --git a/components/datetime/tests/datetime.rs b/components/datetime/tests/datetime.rs index 5dd2c47c127..7c446534f2c 100644 --- a/components/datetime/tests/datetime.rs +++ b/components/datetime/tests/datetime.rs @@ -132,7 +132,7 @@ fn test_dayperiod_patterns() { }); let provider = StructProvider { key: GREGORY_V1, - data: data.as_ref(), + data: data.get(), }; let dtf = DateTimeFormat::try_new(langid.clone(), &provider, &format_options) @@ -195,7 +195,7 @@ fn test_time_zone_patterns() { }); let date_provider = StructProvider { key: GREGORY_V1, - data: data.as_ref(), + data: data.get(), }; let dtf = ZonedDateTimeFormat::try_new( diff --git a/components/decimal/src/lib.rs b/components/decimal/src/lib.rs index a0a9dd22763..7768d94eed5 100644 --- a/components/decimal/src/lib.rs +++ b/components/decimal/src/lib.rs @@ -112,7 +112,7 @@ impl<'d> FixedDecimalFormat<'d> { FormattedFixedDecimal { value, options: &self.options, - symbols: &self.symbols, + symbols: self.symbols.get(), } } } diff --git a/components/locale_canonicalizer/src/locale_canonicalizer.rs b/components/locale_canonicalizer/src/locale_canonicalizer.rs index 867b4957d86..867ba8c8276 100644 --- a/components/locale_canonicalizer/src/locale_canonicalizer.rs +++ b/components/locale_canonicalizer/src/locale_canonicalizer.rs @@ -91,6 +91,7 @@ impl LocaleCanonicalizer<'_> { /// ``` pub fn maximize>(&self, mut langid: T) -> CanonicalizationResult { let langid = langid.as_mut(); + let data = self.likely_subtags.get(); if !langid.language.is_empty() && langid.script.is_some() && langid.region.is_some() { return CanonicalizationResult::Unmodified; @@ -98,36 +99,21 @@ impl LocaleCanonicalizer<'_> { if let Some(language) = langid.language.into() { if let Some(region) = langid.region { - maximize_locale!( - langid, - self.likely_subtags.language_region, - language, - region.into() - ); + maximize_locale!(langid, data.language_region, language, region.into()); } if let Some(script) = langid.script { - maximize_locale!( - langid, - self.likely_subtags.language_script, - language, - script.into() - ); + maximize_locale!(langid, data.language_script, language, script.into()); } - maximize_locale!(langid, self.likely_subtags.language, language); + maximize_locale!(langid, data.language, language); } else if let Some(script) = langid.script { if let Some(region) = langid.region { - maximize_locale!( - langid, - self.likely_subtags.script_region, - script.into(), - region.into() - ); + maximize_locale!(langid, data.script_region, script.into(), region.into()); } - maximize_locale!(langid, self.likely_subtags.script, script.into()); + maximize_locale!(langid, data.script, script.into()); } else if let Some(region) = langid.region { - maximize_locale!(langid, self.likely_subtags.region, region.into()); + maximize_locale!(langid, data.region, region.into()); } - update_langid(&self.likely_subtags.und, langid) + update_langid(&data.und, langid) } /// This returns a new Locale that is the result of running the diff --git a/components/plurals/benches/parser.rs b/components/plurals/benches/parser.rs index af83f5c8421..539e2dec42c 100644 --- a/components/plurals/benches/parser.rs +++ b/components/plurals/benches/parser.rs @@ -12,14 +12,14 @@ use icu_provider::prelude::*; fn parser(c: &mut Criterion) { use icu_plurals::rules::parse_condition; - let plurals_data = helpers::get_plurals_data(); + let fixture_data = helpers::get_plurals_data(); let provider = icu_testdata::get_provider(); let mut rules = vec![]; - for langid in &plurals_data.langs { - let plurals_data: DataPayload = provider + for langid in &fixture_data.langs { + let data_payload: DataPayload = provider .load_payload(&DataRequest { resource_path: ResourcePath { key: icu_plurals::provider::key::CARDINAL_V1, @@ -32,14 +32,9 @@ fn parser(c: &mut Criterion) { .unwrap() .take_payload() .unwrap(); + let data = data_payload.get(); - let r = &[ - &plurals_data.zero, - &plurals_data.one, - &plurals_data.two, - &plurals_data.few, - &plurals_data.many, - ]; + let r = &[&data.zero, &data.one, &data.two, &data.few, &data.many]; for i in r { if let Some(x) = i { diff --git a/components/plurals/src/lib.rs b/components/plurals/src/lib.rs index 416ee70d6ae..d93a5153b11 100644 --- a/components/plurals/src/lib.rs +++ b/components/plurals/src/lib.rs @@ -276,7 +276,7 @@ impl PluralRules { type_: PluralRuleType, ) -> Result { let data = resolver::resolve_plural_data(langid.clone(), data_provider, type_)?; - Self::new_from_data(langid, &data) + Self::new_from_data(langid, data.get()) } /// Returns the [`Plural Category`] appropriate for the given number. diff --git a/components/uniset/src/props.rs b/components/uniset/src/props.rs index 4b248c6e4bc..109a08cb868 100644 --- a/components/uniset/src/props.rs +++ b/components/uniset/src/props.rs @@ -26,7 +26,7 @@ fn get_prop<'d, D: DataProvider<'d, UnicodeProperty<'d>> + ?Sized>(ppucd_provide let resp: DataResponse = ppucd_provider.load_payload(&data_req)?; let ppucd_property_payload: DataPayload = resp.take_payload()?; - let ppucd_property: UnicodeProperty = ppucd_property_payload.as_ref().clone(); + let ppucd_property: UnicodeProperty = ppucd_property_payload.get().clone(); ppucd_property.try_into() } diff --git a/experimental/provider_ppucd/src/support.rs b/experimental/provider_ppucd/src/support.rs index 4230f5c480c..c3226235450 100644 --- a/experimental/provider_ppucd/src/support.rs +++ b/experimental/provider_ppucd/src/support.rs @@ -135,5 +135,5 @@ fn test_ppucd_provider_parse() { 8287, 8288, 12288, 12289, ], }; - assert_eq!(exp_prop_uniset, ppucd_property_cow.as_ref().clone()); + assert_eq!(exp_prop_uniset, ppucd_property_cow.get().clone()); } diff --git a/provider/cldr/src/download/cldr_allinone.rs b/provider/cldr/src/download/cldr_allinone.rs index 625ff900a95..749d2e54a7c 100644 --- a/provider/cldr/src/download/cldr_allinone.rs +++ b/provider/cldr/src/download/cldr_allinone.rs @@ -53,7 +53,7 @@ use std::path::PathBuf; /// .unwrap() /// .take_payload() /// .unwrap(); -/// assert_eq!(data.few, Some(Cow::Borrowed("n % 10 = 3 and n % 100 != 13"))); +/// assert_eq!(data.get().few, Some(Cow::Borrowed("n % 10 = 3 and n % 100 != 13"))); /// } /// /// // Calling demo(downloader) will cause the data to actually get downloaded. diff --git a/provider/cldr/src/transform/dates.rs b/provider/cldr/src/transform/dates.rs index 38c885cce74..d5b03091a2b 100644 --- a/provider/cldr/src/transform/dates.rs +++ b/provider/cldr/src/transform/dates.rs @@ -517,14 +517,22 @@ fn test_basic() { .take_payload() .unwrap(); - assert_eq!("srpna", cs_dates.symbols.months.format.wide.0[7]); + assert_eq!("srpna", cs_dates.get().symbols.months.format.wide.0[7]); assert_eq!( "po", - cs_dates.symbols.weekdays.format.short.as_ref().unwrap().0[1] + cs_dates + .get() + .symbols + .weekdays + .format + .short + .as_ref() + .unwrap() + .0[1] ); - assert_eq!("d. M. y", cs_dates.patterns.date.medium); + assert_eq!("d. M. y", cs_dates.get().patterns.date.medium); } #[test] @@ -548,9 +556,9 @@ fn test_with_numbering_system() { .take_payload() .unwrap(); - assert_eq!("d MMM y", cs_dates.patterns.date.medium); + assert_eq!("d MMM y", cs_dates.get().patterns.date.medium); // TODO(#308): Support numbering system variations. We currently throw them away. - assert_eq!("d/M/yy", cs_dates.patterns.date.short); + assert_eq!("d/M/yy", cs_dates.get().patterns.date.short); } #[test] @@ -575,10 +583,11 @@ fn unalias_contexts() { .unwrap(); // Czech months are not unaliased because `wide` differs. - assert!(cs_dates.symbols.months.stand_alone.is_some()); + assert!(cs_dates.get().symbols.months.stand_alone.is_some()); // Czech months are not unaliased because `wide` differs. assert!(cs_dates + .get() .symbols .months .stand_alone @@ -587,6 +596,7 @@ fn unalias_contexts() { .abbreviated .is_none()); assert!(cs_dates + .get() .symbols .months .stand_alone @@ -595,6 +605,7 @@ fn unalias_contexts() { .short .is_none()); assert!(cs_dates + .get() .symbols .months .stand_alone @@ -603,6 +614,7 @@ fn unalias_contexts() { .narrow .is_none()); assert!(cs_dates + .get() .symbols .months .stand_alone @@ -612,5 +624,5 @@ fn unalias_contexts() { .is_some()); // Czech weekdays are unaliased because they completely overlap. - assert!(cs_dates.symbols.weekdays.stand_alone.is_none()); + assert!(cs_dates.get().symbols.weekdays.stand_alone.is_none()); } diff --git a/provider/cldr/src/transform/likelysubtags.rs b/provider/cldr/src/transform/likelysubtags.rs index f9743678e1b..1816589216a 100644 --- a/provider/cldr/src/transform/likelysubtags.rs +++ b/provider/cldr/src/transform/likelysubtags.rs @@ -196,11 +196,12 @@ fn test_basic() { let langid = langid!("cu-Glag"); let entry = result + .get() .script .binary_search_by_key(&(langid.script.map(|s| s.into())), |(script, _)| { Some(*script) }) .unwrap(); - assert_eq!(result.script[entry].1.language, "cu"); - assert_eq!(result.script[entry].1.region.unwrap(), "BG"); + assert_eq!(result.get().script[entry].1.language, "cu"); + assert_eq!(result.get().script[entry].1.region.unwrap(), "BG"); } diff --git a/provider/cldr/src/transform/numbers/mod.rs b/provider/cldr/src/transform/numbers/mod.rs index cc336e61761..6124dad79ca 100644 --- a/provider/cldr/src/transform/numbers/mod.rs +++ b/provider/cldr/src/transform/numbers/mod.rs @@ -216,6 +216,6 @@ fn test_basic() { .take_payload() .unwrap(); - assert_eq!(ar_decimal.decimal_separator, "٫"); - assert_eq!(ar_decimal.digits[0], '٠'); + assert_eq!(ar_decimal.get().decimal_separator, "٫"); + assert_eq!(ar_decimal.get().digits[0], '٠'); } diff --git a/provider/cldr/src/transform/plurals.rs b/provider/cldr/src/transform/plurals.rs index 221e7a237a5..af4ee1c9086 100644 --- a/provider/cldr/src/transform/plurals.rs +++ b/provider/cldr/src/transform/plurals.rs @@ -207,15 +207,18 @@ fn test_basic() { .take_payload() .unwrap(); - assert_eq!(None, cs_rules.zero); + assert_eq!(None, cs_rules.get().zero); assert_eq!( Some("i = 1 and v = 0"), - cs_rules.one.as_ref().map(|v| v.borrow()) + cs_rules.get().one.as_ref().map(|v| v.borrow()) ); - assert_eq!(None, cs_rules.two); + assert_eq!(None, cs_rules.get().two); assert_eq!( Some("i = 2..4 and v = 0"), - cs_rules.few.as_ref().map(|v| v.borrow()) + cs_rules.get().few.as_ref().map(|v| v.borrow()) + ); + assert_eq!( + Some("v != 0"), + cs_rules.get().many.as_ref().map(|v| v.borrow()) ); - assert_eq!(Some("v != 0"), cs_rules.many.as_ref().map(|v| v.borrow())); } diff --git a/provider/cldr/src/transform/time_zones/mod.rs b/provider/cldr/src/transform/time_zones/mod.rs index 04b341e95b5..49ef92270dd 100644 --- a/provider/cldr/src/transform/time_zones/mod.rs +++ b/provider/cldr/src/transform/time_zones/mod.rs @@ -175,7 +175,7 @@ mod tests { .unwrap() .take_payload() .unwrap(); - assert_eq!("GMT", time_zone_formats.gmt_zero_format); + assert_eq!("GMT", time_zone_formats.get().gmt_zero_format); let exemplar_cities: DataPayload = provider .load_payload(&DataRequest { @@ -190,7 +190,7 @@ mod tests { .unwrap() .take_payload() .unwrap(); - assert_eq!("Pohnpei", exemplar_cities["Pacific/Ponape"]); + assert_eq!("Pohnpei", exemplar_cities.get()["Pacific/Ponape"]); let generic_names_long: DataPayload = provider .load_payload(&DataRequest { @@ -207,7 +207,7 @@ mod tests { .unwrap(); assert_eq!( "Australian Central Western Time", - generic_names_long["Australia_CentralWestern"] + generic_names_long.get()["Australia_CentralWestern"] ); let specific_names_long: DataPayload = provider @@ -225,7 +225,7 @@ mod tests { .unwrap(); assert_eq!( "Australian Central Western Standard Time", - specific_names_long["Australia_CentralWestern"]["standard"] + specific_names_long.get()["Australia_CentralWestern"]["standard"] ); let generic_names_short: DataPayload = provider @@ -241,7 +241,7 @@ mod tests { .unwrap() .take_payload() .unwrap(); - assert_eq!("PT", generic_names_short["America_Pacific"]); + assert_eq!("PT", generic_names_short.get()["America_Pacific"]); let specific_names_short: DataPayload = provider .load_payload(&DataRequest { @@ -256,6 +256,9 @@ mod tests { .unwrap() .take_payload() .unwrap(); - assert_eq!("PDT", specific_names_short["America_Pacific"]["daylight"]); + assert_eq!( + "PDT", + specific_names_short.get()["America_Pacific"]["daylight"] + ); } } diff --git a/provider/core/src/data_provider.rs b/provider/core/src/data_provider.rs index ce4dcb5f520..f44427f2472 100644 --- a/provider/core/src/data_provider.rs +++ b/provider/core/src/data_provider.rs @@ -7,7 +7,6 @@ use crate::resource::ResourceKey; use crate::resource::ResourcePath; use core::ops::Deref; use icu_locid::LanguageIdentifier; -use std::borrow::Borrow; use std::borrow::Cow; use std::convert::TryFrom; use std::fmt; @@ -96,7 +95,7 @@ pub struct DataResponseMetadata { /// /// let payload = DataPayload::from_borrowed("Demo"); /// -/// assert_eq!("Demo", &*payload); +/// assert_eq!("Demo", payload.get()); /// ``` #[derive(Debug, Clone, PartialEq)] pub struct DataPayload<'d, T> @@ -144,7 +143,7 @@ where /// /// payload.with_mut(|s| s.push_str(" World")); /// - /// assert_eq!("Hello World", &*payload); + /// assert_eq!("Hello World", payload.get()); /// ``` /// /// To transfer data from the context into the data struct, use the `move` keyword: @@ -158,8 +157,8 @@ where /// let new_value = "Bar".to_string(); /// payload.with_mut(move |v| v.push(new_value)); /// - /// assert_eq!("Foo", payload[0]); - /// assert_eq!("Bar", payload[1]); + /// assert_eq!("Foo", payload.get()[0]); + /// assert_eq!("Bar", payload.get()[1]); /// ``` #[inline] pub fn with_mut(&mut self, f: F) @@ -189,39 +188,23 @@ where pub fn into_cow(self) -> Cow<'d, T> { self.cow } -} - -impl<'d, T> Borrow for DataPayload<'d, T> -where - T: ToOwned + ?Sized, - ::Owned: Debug, -{ - #[inline] - fn borrow(&self) -> &T { - self.cow.borrow() - } -} - -impl<'d, T> AsRef for DataPayload<'d, T> -where - T: ToOwned + ?Sized, - ::Owned: Debug, -{ - #[inline] - fn as_ref(&self) -> &T { - self.cow.as_ref() - } -} - -impl<'d, T> Deref for DataPayload<'d, T> -where - T: ToOwned + ?Sized, - ::Owned: Debug, -{ - type Target = T; + /// Borrows the underlying data. + /// + /// This function should be used like `Deref` would normally be used. For more information on + /// why DataPayload cannot implement `Deref`, see the `yoke` crate. + /// + /// # Examples + /// + /// ``` + /// use icu_provider::prelude::*; + /// + /// let payload = DataPayload::from_borrowed("Demo"); + /// + /// assert_eq!("Demo", payload.get()); + /// ``` #[inline] - fn deref(&self) -> &Self::Target { + pub fn get(&self) -> &T { self.cow.deref() } } diff --git a/provider/core/src/export.rs b/provider/core/src/export.rs index 778033dc3c0..3252ee3d2b6 100644 --- a/provider/core/src/export.rs +++ b/provider/core/src/export.rs @@ -38,7 +38,6 @@ where where 's: 'd, { - use std::borrow::Borrow; for resc_options in provider.supported_options_for_key(resc_key)? { if !self.include_resource_options(&resc_options) { continue; @@ -50,7 +49,7 @@ where }, }; let payload = provider.load_payload(&req)?.take_payload()?; - self.put_payload(&req, payload.borrow())?; + self.put_payload(&req, payload.get())?; } Ok(()) } diff --git a/provider/core/src/hello_world.rs b/provider/core/src/hello_world.rs index 03b163b05e1..91bee7a36b1 100644 --- a/provider/core/src/hello_world.rs +++ b/provider/core/src/hello_world.rs @@ -63,7 +63,7 @@ impl Default for HelloWorldV1<'_> { /// .take_payload() /// .unwrap(); /// -/// assert_eq!("Hallo Welt", german_hello_world.message); +/// assert_eq!("Hallo Welt", german_hello_world.get().message); /// ``` #[derive(Debug, PartialEq, Default)] pub struct HelloWorldProvider<'s> { diff --git a/provider/core/src/inv.rs b/provider/core/src/inv.rs index a31bc561e47..dcf09c7442c 100644 --- a/provider/core/src/inv.rs +++ b/provider/core/src/inv.rs @@ -31,7 +31,7 @@ use std::fmt::Debug; /// .take_payload() /// .unwrap(); /// -/// assert_eq!("(und) Hello World", result.message); +/// assert_eq!("(und) Hello World", result.get().message); /// ``` pub struct InvariantDataProvider; diff --git a/provider/core/src/struct_provider.rs b/provider/core/src/struct_provider.rs index 220cc6dfa62..4ff2a23647b 100644 --- a/provider/core/src/struct_provider.rs +++ b/provider/core/src/struct_provider.rs @@ -41,7 +41,7 @@ use std::fmt::Debug; /// .take_payload() /// .expect("Data should be present"); /// -/// assert_eq!(*payload, local_data); +/// assert_eq!(payload.get(), &local_data); /// assert!(matches!(payload.into_cow(), Cow::Borrowed(_))) /// ``` pub struct StructProvider<'d, T> { diff --git a/provider/core/tests/data_provider.rs b/provider/core/tests/data_provider.rs index 542da596307..86c6aa1f021 100644 --- a/provider/core/tests/data_provider.rs +++ b/provider/core/tests/data_provider.rs @@ -343,7 +343,7 @@ where .unwrap() .take_payload() .unwrap(); - if v1.message == v2.message { + if v1.get().message == v2.get().message { panic!() } } diff --git a/provider/fs/tests/test_file_io.rs b/provider/fs/tests/test_file_io.rs index 60d9250c29d..f02aa6ea966 100644 --- a/provider/fs/tests/test_file_io.rs +++ b/provider/fs/tests/test_file_io.rs @@ -57,7 +57,7 @@ fn test_json() { .expect("The data should be valid") .take_payload() .expect("The data should be present"); - assert_eq!(*plurals_data, EXPECTED_RU_DATA); + assert_eq!(plurals_data.get(), &EXPECTED_RU_DATA); } #[test] @@ -70,7 +70,7 @@ fn test_json_dyn_erased_serde() { .expect("The data should be valid") .take_payload() .expect("The data should be present"); - assert_eq!(*plurals_data, EXPECTED_RU_DATA); + assert_eq!(plurals_data.get(), &EXPECTED_RU_DATA); } #[test] @@ -162,7 +162,7 @@ fn test_bincode() { .expect("The data should be valid") .take_payload() .expect("The data should be present"); - assert_eq!(*plurals_data, EXPECTED_SR_DATA); + assert_eq!(plurals_data.get(), &EXPECTED_SR_DATA); } #[test] @@ -176,5 +176,5 @@ fn test_bincode_dyn_erased_serde() { .expect("The data should be valid") .take_payload() .expect("The data should be present"); - assert_eq!(*plurals_data, EXPECTED_SR_DATA); + assert_eq!(plurals_data.get(), &EXPECTED_SR_DATA); } diff --git a/provider/testdata/src/lib.rs b/provider/testdata/src/lib.rs index cf26f5b83f7..211891c3c20 100644 --- a/provider/testdata/src/lib.rs +++ b/provider/testdata/src/lib.rs @@ -58,7 +58,7 @@ //! .unwrap() //! .take_payload() //! .unwrap(); -//! assert_eq!(data.few, Some(Cow::Borrowed("v = 0 and i % 10 = 2..4 and i % 100 != 12..14"))); +//! assert_eq!(data.get().few, Some(Cow::Borrowed("v = 0 and i % 10 = 2..4 and i % 100 != 12..14"))); //! ``` //! //! [`ICU4X`]: ../icu/index.html