Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Nov 12, 2024
1 parent 7f15e80 commit a9c2362
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 31 deletions.
2 changes: 1 addition & 1 deletion components/decimal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ criterion = { workspace = true }
default = ["compiled_data"]
std = ["fixed_decimal/std", "icu_locale_core/std", "icu_provider/std"]
serde = ["dep:serde", "icu_provider/serde", "zerovec/serde"]
datagen = ["serde", "dep:databake"]
datagen = ["serde", "dep:databake", "zerovec/databake"]
bench = ["serde"]
compiled_data = ["dep:icu_decimal_data"]

Expand Down
37 changes: 9 additions & 28 deletions components/decimal/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use alloc::borrow::Cow;
use icu_provider::prelude::*;
use zerovec::VarZeroCow;

#[cfg(feature = "compiled_data")]
#[derive(Debug)]
Expand Down Expand Up @@ -55,9 +56,9 @@ pub const MARKERS: &[DataMarkerInfo] = &[DecimalSymbolsV1Marker::INFO];
/// to be stable, their Rust representation might not be. Use with caution.
/// </div>
#[derive(Debug, PartialEq, Clone, yoke::Yokeable, Copy, zerofrom::ZeroFrom)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
#[cfg_attr(feature = "datagen", databake(path = icu_decimal::provider))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct GroupingSizesV1 {
/// The size of the first (lowest-magnitude) group.
///
Expand All @@ -82,13 +83,13 @@ pub struct GroupingSizesV1 {
/// to be stable, their Rust representation might not be. Use with caution.
/// </div>
#[derive(Debug, PartialEq, Clone, yoke::Yokeable, zerofrom::ZeroFrom)]
#[cfg_attr(feature = "datagen", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(feature = "datagen", derive(serde::Serialize))]
#[zerovec::make_varule(DecimalSymbolsV1StringsULE)]
#[zerovec::derive(Debug)]
#[zerovec::skip_derive(Ord)]
#[cfg_attr(feature = "serde", zerovec::derive(Serialize))]
#[cfg_attr(feature = "datagen", zerovec::derive(Deserialize))]
#[cfg_attr(feature = "serde", zerovec::derive(Deserialize))]
#[cfg_attr(feature = "datagen", zerovec::derive(Serialize))]
pub struct DecimalSymbolsV1Strings<'data> {
/// Prefix to apply when a negative sign is needed.
#[cfg_attr(feature = "serde", serde(borrow))]
Expand Down Expand Up @@ -145,19 +146,15 @@ impl databake::BakeSize for &DecimalSymbolsV1StringsULE {
/// </div>
#[icu_provider::data_struct(DecimalSymbolsV1Marker = "decimal/symbols@1")]
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
#[cfg_attr(feature = "datagen", databake(path = icu_decimal::provider))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct DecimalSymbolsV1<'data> {
/// String data for the symbols: +/- affixes and separators
#[cfg_attr(feature = "serde", serde(borrow))]
#[cfg_attr(
feature = "serde",
serde(deserialize_with = "deserialize_borrowed_cow")
)]
// We use a Cow here to reduce the stack size of DecimalSymbolsV1: instead of serializing multiple strs,
// We use a VarZeroCow here to reduce the stack size of DecimalSymbolsV1: instead of serializing multiple strs,
// this type will now serialize as a single u8 buffer with optimized indexing that packs all the data together
pub strings: Cow<'data, DecimalSymbolsV1StringsULE>,
pub strings: VarZeroCow<'data, DecimalSymbolsV1StringsULE>,

/// Settings used to determine where to place groups in the integer part of the number.
pub grouping_sizes: GroupingSizesV1,
Expand All @@ -167,21 +164,6 @@ pub struct DecimalSymbolsV1<'data> {
pub digits: [char; 10],
}

#[cfg(feature = "serde")]
fn deserialize_borrowed_cow<'de, 'data, D: serde::Deserializer<'de>>(
deserializer: D,
) -> Result<Cow<'data, DecimalSymbolsV1StringsULE>, D::Error>
where
'de: 'data,
{
use serde::Deserialize;
if deserializer.is_human_readable() {
Box::<DecimalSymbolsV1StringsULE>::deserialize(deserializer).map(Cow::Owned)
} else {
<&DecimalSymbolsV1StringsULE>::deserialize(deserializer).map(Cow::Borrowed)
}
}

impl<'data> DecimalSymbolsV1<'data> {
/// Return (prefix, suffix) for the minus sign
pub fn minus_sign_affixes(&self) -> (&str, &str) {
Expand Down Expand Up @@ -217,9 +199,8 @@ impl Default for DecimalSymbolsV1<'static> {
decimal_separator: ".".into(),
grouping_separator: ",".into(),
};
let strings = zerovec::ule::encode_varule_to_box(&strings);
Self {
strings: Cow::Owned(strings),
strings: VarZeroCow::from_encodeable(&strings),
grouping_sizes: GroupingSizesV1 {
primary: 3,
secondary: 3,
Expand Down
4 changes: 2 additions & 2 deletions provider/source/src/decimal/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use icu_provider::prelude::*;
use std::borrow::Cow;
use std::collections::HashSet;
use std::convert::TryFrom;
use zerovec::VarZeroCow;

impl DataProvider<DecimalSymbolsV1Marker> for SourceDataProvider {
fn load(&self, req: DataRequest) -> Result<DataResponse<DecimalSymbolsV1Marker>, DataError> {
Expand Down Expand Up @@ -86,9 +87,8 @@ impl TryFrom<NumbersWithNumsys<'_>> for DecimalSymbolsV1<'static> {
decimal_separator: Cow::Owned(symbols.decimal.clone()),
grouping_separator: Cow::Owned(symbols.group.clone()),
};
let strings = zerovec::ule::encode_varule_to_box(&strings);
Ok(Self {
strings: Cow::Owned(strings),
strings: VarZeroCow::from_encodeable(&strings),
grouping_sizes: GroupingSizesV1 {
primary: parsed_pattern.positive.primary_grouping,
secondary: parsed_pattern.positive.secondary_grouping,
Expand Down

0 comments on commit a9c2362

Please sign in to comment.