Skip to content

Commit

Permalink
Adding T: Bake bound on ZeroVec's Bake, and required impls(#3451)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian authored May 26, 2023
1 parent 460b572 commit 51da740
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 10 deletions.
2 changes: 1 addition & 1 deletion components/datetime/src/pattern/item/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

#[derive(Debug, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "datagen", derive(serde::Serialize))]
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake), databake(path = icu_datetime::pattern))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[allow(clippy::exhaustive_enums)] // this type is stable
pub enum GenericPatternItem {
Expand Down
2 changes: 1 addition & 1 deletion components/locid_transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ default = []
std = []
bench = ["serde"]
serde = ["dep:serde", "icu_locid/serde", "tinystr/serde", "zerovec/serde", "icu_provider/serde"]
datagen = ["serde", "dep:databake", "zerovec/databake", "icu_locid/databake"]
datagen = ["serde", "dep:databake", "zerovec/databake", "icu_locid/databake", "tinystr/databake"]

[[bench]]
name = "locale_canonicalizer"
Expand Down
2 changes: 1 addition & 1 deletion components/properties/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ icu_testdata = { path = "../../provider/testdata", default-features = false, fea
[features]
std = ["icu_collections/std", "icu_provider/std"]
serde = ["dep:serde", "tinystr/serde", "zerovec/serde", "icu_collections/serde", "icu_provider/serde"]
datagen = ["serde", "dep:databake", "zerovec/databake", "icu_collections/databake"]
datagen = ["serde", "dep:databake", "zerovec/databake", "icu_collections/databake", "tinystr/databake"]
bidi = [ "dep:unicode-bidi" ]
4 changes: 2 additions & 2 deletions components/timezone/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use zerovec::{ZeroMap2d, ZeroSlice, ZeroVec};
/// </div>
#[repr(transparent)]
#[derive(Debug, Clone, Copy, Eq, Ord, PartialEq, PartialOrd, yoke::Yokeable, ULE, Hash)]
#[cfg_attr(feature = "datagen", derive(serde::Serialize))]
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake), databake(path = icu_timezone::provider))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct TimeZoneBcp47Id(pub TinyAsciiStr<8>);

Expand Down Expand Up @@ -83,7 +83,7 @@ impl<'a> zerovec::maps::ZeroMapKV<'a> for TimeZoneBcp47Id {
/// </div>
#[repr(transparent)]
#[derive(Debug, Clone, Copy, Eq, Ord, PartialEq, PartialOrd, yoke::Yokeable, ULE, Hash)]
#[cfg_attr(feature = "datagen", derive(serde::Serialize))]
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake), databake(path = icu_timezone::provider))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct MetazoneId(pub TinyAsciiStr<4>);

Expand Down
2 changes: 1 addition & 1 deletion components/timezone/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl FromStr for GmtOffset {
/// A time zone variant: currently either daylight time or standard time.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, ULE)]
#[repr(transparent)]
#[cfg_attr(feature = "datagen", derive(serde::Serialize))]
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake), databake(path = icu_timezone))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[allow(clippy::exhaustive_structs)] // newtype
pub struct ZoneVariant(pub TinyAsciiStr<2>);
Expand Down
2 changes: 1 addition & 1 deletion experimental/displaynames/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ icu_testdata = { path = "../../provider/testdata", default-features = false, fea
[features]
std = ["icu_collections/std", "icu_locid/std", "icu_provider/std"]
serde = ["dep:serde", "zerovec/serde", "icu_collections/serde", "tinystr/serde", "icu_provider/serde"]
datagen = ["serde", "std", "dep:databake", "zerovec/databake", "icu_collections/databake"]
datagen = ["serde", "std", "dep:databake", "zerovec/databake", "icu_collections/databake", "tinystr/databake"]
2 changes: 1 addition & 1 deletion provider/adapters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ writeable = { path = "../../utils/writeable" }
[features]
std = ["icu_locid/std", "icu_provider/std"]
serde = ["dep:serde", "zerovec/serde", "icu_locid/serde", "icu_provider/serde"]
datagen = ["std", "serde", "dep:databake", "icu_provider/datagen", "icu_locid/databake", "zerovec/databake"]
datagen = ["std", "serde", "dep:databake", "icu_provider/datagen", "icu_locid/databake", "zerovec/databake", "tinystr/databake"]
27 changes: 27 additions & 0 deletions utils/tinystr/src/databake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::TinyAsciiStr;
use crate::UnvalidatedTinyAsciiStr;
use databake::*;

impl<const N: usize> Bake for TinyAsciiStr<N> {
Expand All @@ -15,7 +16,33 @@ impl<const N: usize> Bake for TinyAsciiStr<N> {
}
}

impl<const N: usize> databake::Bake for UnvalidatedTinyAsciiStr<N> {
fn bake(&self, env: &databake::CrateEnv) -> databake::TokenStream {
match self.try_into_tinystr() {
Ok(tiny) => {
let tiny = tiny.bake(env);
databake::quote! {
#tiny.to_unvalidated()
}
}
Err(_) => {
let bytes = self.0.bake(env);
env.insert("tinystr");
databake::quote! {
tinystr::UnvalidatedTinyAsciiStr::from_bytes_unchecked(*#bytes)
}
}
}
}
}

#[test]
fn test() {
test_bake!(TinyAsciiStr<10>, const: crate::tinystr!(10usize, "foo"), tinystr);
}

#[test]
fn test_unvalidated() {
test_bake!(UnvalidatedTinyAsciiStr<10>, const: crate::tinystr!(10usize, "foo").to_unvalidated(), tinystr);
test_bake!(UnvalidatedTinyAsciiStr<3>, const: crate::UnvalidatedTinyAsciiStr::from_bytes_unchecked(*b"AB\xCD"), tinystr);
}
7 changes: 6 additions & 1 deletion utils/tinystr/src/unvalidated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ impl<const N: usize> UnvalidatedTinyAsciiStr<N> {
pub fn try_into_tinystr(&self) -> Result<TinyAsciiStr<N>, TinyStrError> {
TinyAsciiStr::try_from_raw(self.0)
}

#[doc(hidden)]
pub const fn from_bytes_unchecked(bytes: [u8; N]) -> Self {
Self(bytes)
}
}

impl<const N: usize> TinyAsciiStr<N> {
#[inline]
// Converts into a [`UnvalidatedTinyAsciiStr`]
pub fn to_unvalidated(self) -> UnvalidatedTinyAsciiStr<N> {
pub const fn to_unvalidated(self) -> UnvalidatedTinyAsciiStr<N> {
UnvalidatedTinyAsciiStr(*self.all_bytes())
}
}
Expand Down
36 changes: 36 additions & 0 deletions utils/zerovec/src/ule/unvalidated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ impl UnvalidatedChar {
Self([u0, u1, u2])
}

#[inline]
#[doc(hidden)]
pub const fn from_u24(c: u32) -> Self {
let [u0, u1, u2, _u3] = c.to_le_bytes();
Self([u0, u1, u2])
}

/// Attempt to convert a [`UnvalidatedChar`] to a `char`.
///
/// # Examples
Expand Down Expand Up @@ -412,6 +419,28 @@ impl<'de> serde::Deserialize<'de> for UnvalidatedChar {
}
}

#[cfg(feature = "databake")]
impl databake::Bake for UnvalidatedChar {
fn bake(&self, env: &databake::CrateEnv) -> databake::TokenStream {
match self.try_to_char() {
Ok(ch) => {
env.insert("zerovec");
let ch = ch.bake(env);
databake::quote! {
zerovec::ule::UnvalidatedChar::from_char(#ch)
}
}
Err(_) => {
env.insert("zerovec");
let u24 = u32::from_le_bytes([self.0[0], self.0[1], self.0[2], 0]);
databake::quote! {
zerovec::ule::UnvalidatedChar::from_u24(#u24)
}
}
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -479,4 +508,11 @@ mod test {
ule_bytes
);
}

#[test]
fn test_char_bake() {
databake::test_bake!(UnvalidatedChar, const: crate::ule::UnvalidatedChar::from_char('b'), zerovec);
// surrogate code point
databake::test_bake!(UnvalidatedChar, const: crate::ule::UnvalidatedChar::from_u24(55296u32), zerovec);
}
}
2 changes: 1 addition & 1 deletion utils/zerovec/src/zerovec/databake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use databake::*;

impl<T> Bake for ZeroVec<'_, T>
where
T: AsULE + ?Sized,
T: AsULE + ?Sized + Bake,
{
fn bake(&self, env: &CrateEnv) -> TokenStream {
env.insert("zerovec");
Expand Down

0 comments on commit 51da740

Please sign in to comment.