Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EitherWriteable and use it for DoublePlaceholder in currency format #4750

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ zerovec-derive = { version = "0.10.1", path = "utils/zerovec/derive", default-fe

# External deps from crates.io
criterion = { version = "0.5" }
either = { version = "1", default-features = false }

# Tools
icu_benchmark_macros = { path = "tools/benchmark/macros" }
Expand Down
2 changes: 1 addition & 1 deletion components/datetime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ all-features = true

[dependencies]
displaydoc = { version = "0.2.3", default-features = false }
either = { version = "1.6.1", default-features = false }
either = { workspace = true }
fixed_decimal = { workspace = true }
icu_calendar = { workspace = true }
icu_decimal = { workspace = true }
Expand Down
14 changes: 3 additions & 11 deletions components/experimental/src/dimension/currency/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,9 @@ impl<'l> Writeable for FormattedCurrency<'l> {
}
.ok_or(core::fmt::Error)?;

// TODO: rewrite this so it does not allocate
sink.write_str(
pattern
.interpolate([
&self.value.write_to_string(), // placeholder 0 (currency value)
currency_sign_value, // placeholder 1 (currency sign value)
])
.write_to_string()
.into_owned()
.as_str(),
)?;
pattern
.interpolate((self.value, currency_sign_value))
.write_to(sink)?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion provider/datagen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ proc-macro2 = {version = "1", optional = true }

# Other external dependencies
displaydoc = { version = "0.2.3", default-features = false }
either = { workspace = true }
elsa = "1.10"
itertools = "0.10"
log = "0.4"
Expand All @@ -93,7 +94,6 @@ ureq = { version = "2", optional = true }
clap = { version = "4", optional = true, features = ["derive"] }
eyre = { version = "0.6", optional = true }
simple_logger = { version = "4.1.0", default-features = false, optional = true }
either = "1.9.0"

[dev-dependencies]
crlify = { path = "../../utils/crlify" }
Expand Down
3 changes: 2 additions & 1 deletion utils/pattern/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ independent = true
all-features = true

[dependencies]
either = { workspace = true }
displaydoc = { version = "0.2.3", default-features = false }
writeable = { workspace = true }
writeable = { workspace = true, features = ["either"] }
databake = { workspace = true, features = ["derive"], optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }
yoke = { workspace = true, features = ["derive"], optional = true }
Expand Down
21 changes: 13 additions & 8 deletions utils/pattern/src/double.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! Code for the [`DoublePlaceholder`] pattern backend.

use core::{cmp::Ordering, str::FromStr};
use either::Either;
use writeable::Writeable;

use crate::common::*;
Expand Down Expand Up @@ -62,16 +63,17 @@ impl FromStr for DoublePlaceholderKey {
}
}

impl<W> PlaceholderValueProvider<DoublePlaceholderKey> for (W, W)
impl<W0, W1> PlaceholderValueProvider<DoublePlaceholderKey> for (W0, W1)
where
W: Writeable,
W0: Writeable,
W1: Writeable,
{
type W<'a> = &'a W where W: 'a;
type W<'a> = Either<&'a W0, &'a W1> where W0: 'a, W1: 'a;
#[inline]
fn value_for(&self, key: DoublePlaceholderKey) -> Self::W<'_> {
match key {
DoublePlaceholderKey::Place0 => &self.0,
DoublePlaceholderKey::Place1 => &self.1,
DoublePlaceholderKey::Place0 => Either::Left(&self.0),
DoublePlaceholderKey::Place1 => Either::Right(&self.1),
}
}
}
Expand All @@ -84,7 +86,10 @@ where
#[inline]
fn value_for(&self, key: DoublePlaceholderKey) -> Self::W<'_> {
let [item0, item1] = self;
(item0, item1).value_for(key)
match key {
DoublePlaceholderKey::Place0 => item0,
DoublePlaceholderKey::Place1 => item1,
}
}
}

Expand Down Expand Up @@ -230,8 +235,8 @@ impl DoublePlaceholderInfo {
/// assert_eq!(
/// Pattern::<DoublePlaceholder, _>::try_from_str("{1}{0}")
/// .unwrap()
/// .interpolate_to_string((1, 2)),
/// "21",
/// .interpolate_to_string((1, "A")),
/// "A1",
/// );
/// ```
///
Expand Down
4 changes: 4 additions & 0 deletions utils/writeable/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ independent = true
[package.metadata.docs.rs]
all-features = true

[dependencies]
either = { workspace = true, optional = true }

[dev-dependencies]
icu_benchmark_macros = { path = "../../tools/benchmark/macros" }
rand = { version = "0.8", features = ["small_rng"] }
Expand All @@ -29,6 +32,7 @@ criterion = { workspace = true }

[features]
bench = []
either = ["dep:either"]

[package.metadata.cargo-all-features]
# Bench feature gets tested separately and is only relevant for CI
Expand Down
48 changes: 48 additions & 0 deletions utils/writeable/src/either.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::*;
use ::either::Either;

/// A [`Writeable`] impl that delegates to one type or another type.
impl<W0, W1> Writeable for Either<W0, W1>
where
W0: Writeable,
W1: Writeable,
{
fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
match self {
Either::Left(w) => w.write_to(sink),
Either::Right(w) => w.write_to(sink),
}
}

fn write_to_parts<S: PartsWrite + ?Sized>(&self, sink: &mut S) -> fmt::Result {
match self {
Either::Left(w) => w.write_to_parts(sink),
Either::Right(w) => w.write_to_parts(sink),
}
}

fn writeable_length_hint(&self) -> LengthHint {
match self {
Either::Left(w) => w.writeable_length_hint(),
Either::Right(w) => w.writeable_length_hint(),
}
}

fn write_to_string(&self) -> Cow<str> {
match self {
Either::Left(w) => w.write_to_string(),
Either::Right(w) => w.write_to_string(),
}
}

fn write_cmp_bytes(&self, other: &[u8]) -> core::cmp::Ordering {
match self {
Either::Left(w) => w.write_cmp_bytes(other),
Either::Right(w) => w.write_cmp_bytes(other),
}
}
}
2 changes: 2 additions & 0 deletions utils/writeable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
extern crate alloc;

mod cmp;
#[cfg(feature = "either")]
mod either;
mod impls;
mod ops;

Expand Down
Loading