Skip to content
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: 1 addition & 1 deletion provider/baked/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ impl DataExporter for BakedExporter {
// Safety invariant upheld: see above
const #data_ident: icu_provider::baked::zerotrie::DataForVarULEs<#marker_bake> = {
const TRIE: icu_provider::baked::zerotrie::ZeroTrieSimpleAscii<&'static [u8]> = icu_provider::baked:: #baked_trie;
const VALUES: &'static zerovec::VarZeroSlice<<<#marker_bake as icu_provider::baked::zerotrie::DynamicDataMarker>::DataStruct as icu_provider::ule::MaybeAsVarULE>::EncodedStruct> = #vzv_tokens;
const VALUES: &'static zerovec::VarZeroSlice<<<#marker_bake as icu_provider::baked::zerotrie::DynamicDataMarker>::DataStruct as icu_provider::ule::MaybeAsVarULE>::EncodedStruct, zerovec::vecs::Index32> = #vzv_tokens;
unsafe {
icu_provider::baked::zerotrie::DataForVarULEs::from_trie_and_values_unchecked(TRIE, VALUES)
}
Expand Down
2 changes: 1 addition & 1 deletion provider/baked/tests/data/hello_world_v1.rs.data

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

6 changes: 3 additions & 3 deletions provider/core/src/baked/zerotrie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
ule::MaybeAsVarULE,
};
pub use zerotrie::ZeroTrieSimpleAscii;
use zerovec::VarZeroSlice;
use zerovec::{vecs::Index32, VarZeroSlice};

fn get_index(
trie: ZeroTrieSimpleAscii<&'static [u8]>,
Expand Down Expand Up @@ -162,7 +162,7 @@ where
{
// Unsafe invariant: actual values contained MUST be valid indices into `values`
trie: ZeroTrieSimpleAscii<&'static [u8]>,
values: &'static VarZeroSlice<<M::DataStruct as MaybeAsVarULE>::EncodedStruct>,
values: &'static VarZeroSlice<<M::DataStruct as MaybeAsVarULE>::EncodedStruct, Index32>,
}

impl<M: DataMarker> super::private::Sealed for DataForVarULEs<M>
Expand All @@ -183,7 +183,7 @@ where
/// The actual values contained in the trie must be valid indices into `values`
pub const unsafe fn from_trie_and_values_unchecked(
trie: ZeroTrieSimpleAscii<&'static [u8]>,
values: &'static VarZeroSlice<<M::DataStruct as MaybeAsVarULE>::EncodedStruct>,
values: &'static VarZeroSlice<<M::DataStruct as MaybeAsVarULE>::EncodedStruct, Index32>,
) -> Self {
Self { trie, values }
}
Expand Down
8 changes: 5 additions & 3 deletions provider/core/src/export/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{dynutil::UpcastDataPayload, ule::MaybeAsVarULE};
use alloc::sync::Arc;
use databake::{Bake, BakeSize, CrateEnv, TokenStream};
use yoke::*;
use zerovec::vecs::Index32;
use zerovec::VarZeroVec;

#[cfg(doc)]
Expand Down Expand Up @@ -60,9 +61,9 @@ where
rest: &[&DataPayload<ExportMarker>],
ctx: &CrateEnv,
) -> Option<TokenStream> {
let first_varule = self.get().maybe_encode_as_varule()?;
let first_varule = self.get().maybe_as_encodeable()?;
let recovered_vec: Vec<
&<<M::DataStruct as Yokeable<'_>>::Output as MaybeAsVarULE>::EncodedStruct,
<<M::DataStruct as Yokeable<'_>>::Output as MaybeEncodeAsVarULE>::EncodeableStruct<'_>,
> = core::iter::once(first_varule)
.chain(rest.iter().map(|v| {
#[expect(clippy::expect_used)] // exporter code
Expand All @@ -72,12 +73,13 @@ where
.downcast_ref::<Self>()
.expect("payloads expected to be same type")
.get()
.maybe_encode_as_varule()
.maybe_as_encodeable()
.expect("MaybeEncodeAsVarULE impl should be symmetric")
}))
.collect();
let vzv: VarZeroVec<
<<M::DataStruct as Yokeable<'_>>::Output as MaybeAsVarULE>::EncodedStruct,
Index32,
> = VarZeroVec::from(&recovered_vec);
let vzs = vzv.as_slice();
Some(vzs.bake(ctx))
Expand Down
45 changes: 35 additions & 10 deletions provider/core/src/varule_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

#[cfg(feature = "export")]
use zerovec::ule::EncodeAsVarULE;
use zerovec::ule::VarULE;

#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -30,9 +32,13 @@ pub trait MaybeAsVarULE {
/// ✨ *Enabled with the `export` Cargo feature.*
#[cfg(feature = "export")]
pub trait MaybeEncodeAsVarULE: MaybeAsVarULE {
/// Returns the [`MaybeAsVarULE::EncodedStruct`] that represents this data struct,
/// or `None` if the data struct does not support this representation.
fn maybe_encode_as_varule(&self) -> Option<&Self::EncodedStruct>;
/// The type returned by [`Self::maybe_as_encodeable`].
type EncodeableStruct<'a>: EncodeAsVarULE<Self::EncodedStruct>
where
Self: 'a;
/// Returns something encodeable to the [`MaybeAsVarULE::EncodedStruct`] that represents
/// this data struct, or `None` if the data struct does not support this representation.
fn maybe_as_encodeable<'a>(&'a self) -> Option<Self::EncodeableStruct<'a>>;
}

/// Implements required traits on data structs, such as [`MaybeEncodeAsVarULE`].
Expand All @@ -44,7 +50,8 @@ macro_rules! data_struct {
}
$($(#[$attr])*)?
impl<$generic: $bound> $crate::ule::MaybeEncodeAsVarULE for $ty {
fn maybe_encode_as_varule(&self) -> Option<&Self::EncodedStruct> {
type EncodeableStruct<'b> = &'b [()] where Self: 'b;
fn maybe_as_encodeable<'b>(&'b self) -> Option<Self::EncodeableStruct<'b>> {
None
}
}
Expand All @@ -55,7 +62,8 @@ macro_rules! data_struct {
}
$($(#[$attr])*)?
impl $crate::ule::MaybeEncodeAsVarULE for $ty {
fn maybe_encode_as_varule(&self) -> Option<&Self::EncodedStruct> {
type EncodeableStruct<'b> = &'b [()] where Self: 'b;
fn maybe_as_encodeable<'b>(&'b self) -> Option<Self::EncodeableStruct<'b>> {
None
}
}
Expand All @@ -71,7 +79,8 @@ macro_rules! data_struct {
}
$(#[$attr])*
impl<'data> $crate::ule::MaybeEncodeAsVarULE for $ty {
fn maybe_encode_as_varule(&self) -> Option<&Self::EncodedStruct> {
type EncodeableStruct<'b> = &'b $varule where Self: 'b;
fn maybe_as_encodeable<'b>(&'b self) -> Option<Self::EncodeableStruct<'b>> {
// Workaround for <https://rust-lang.github.io/rfcs/3216-closure-lifetime-binder.html>
fn bind_lifetimes<F>(f: F) -> F where F: for<'data> Fn(&'data $ty) -> &'data $varule { f }
Some(bind_lifetimes($encode_as_varule)(self))
Expand Down Expand Up @@ -102,7 +111,11 @@ where
K0: ?Sized,
V: ?Sized,
{
fn maybe_encode_as_varule(&self) -> Option<&Self::EncodedStruct> {
type EncodeableStruct<'b>
= &'b [()]
where
Self: 'b;
fn maybe_as_encodeable<'b>(&'b self) -> Option<Self::EncodeableStruct<'b>> {
None
}
}
Expand Down Expand Up @@ -131,7 +144,11 @@ where
K1: ?Sized,
V: ?Sized,
{
fn maybe_encode_as_varule(&self) -> Option<&Self::EncodedStruct> {
type EncodeableStruct<'b>
= &'b [()]
where
Self: 'b;
fn maybe_as_encodeable<'b>(&'b self) -> Option<Self::EncodeableStruct<'b>> {
None
}
}
Expand All @@ -142,7 +159,11 @@ impl<T, const N: usize> MaybeAsVarULE for [T; N] {

#[cfg(feature = "export")]
impl<T, const N: usize> MaybeEncodeAsVarULE for [T; N] {
fn maybe_encode_as_varule(&self) -> Option<&Self::EncodedStruct> {
type EncodeableStruct<'a>
= &'a [()]
where
Self: 'a;
fn maybe_as_encodeable<'a>(&'a self) -> Option<Self::EncodeableStruct<'a>> {
None
}
}
Expand All @@ -153,7 +174,11 @@ impl MaybeAsVarULE for u16 {

#[cfg(feature = "export")]
impl MaybeEncodeAsVarULE for u16 {
fn maybe_encode_as_varule(&self) -> Option<&Self::EncodedStruct> {
type EncodeableStruct<'a>
= &'a [()]
where
Self: 'a;
fn maybe_as_encodeable<'a>(&'a self) -> Option<Self::EncodeableStruct<'a>> {
None
}
}
Loading