Skip to content

Commit

Permalink
Making IterableDataProvider #[no_std] (#5176)
Browse files Browse the repository at this point in the history
By using `BTreeSet` instead of `HashSet`. This removes the `std` feature
from `icu_provider_adapters`.

Also removed `DataLocale::is_empty`, which is the same as
`DataLocale::is_und`, but "und" is a better description of that state.
  • Loading branch information
robertbastian authored Jul 8, 2024
1 parent 81884c3 commit c8a8801
Show file tree
Hide file tree
Showing 623 changed files with 1,326 additions and 1,395 deletions.
4 changes: 1 addition & 3 deletions components/experimental/src/transliterate/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use icu_locale_core::Locale;
use icu_normalizer::provider::*;
use icu_properties::{provider::*, sets};
use icu_provider::prelude::*;
#[cfg(feature = "datagen")]
use std::collections::HashSet;

mod parse;
mod pass1;
Expand Down Expand Up @@ -465,7 +463,7 @@ where
+ DataProvider<XidStartV1Marker>,
NP: ?Sized,
{
fn iter_ids(&self) -> Result<HashSet<DataIdentifierCow>, DataError> {
fn iter_ids(&self) -> Result<std::collections::BTreeSet<DataIdentifierCow>, DataError> {
let exclusive_data = self.collection.data.borrow();
Ok(exclusive_data
.0
Expand Down
1 change: 0 additions & 1 deletion provider/adapters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ icu_locale = { path = "../../components/locale" }
writeable = { path = "../../utils/writeable" }

[features]
std = ["icu_locale/std", "icu_provider/std"]
serde = ["dep:serde", "zerovec/serde", "icu_locale/serde", "icu_provider/serde"]
7 changes: 3 additions & 4 deletions provider/adapters/src/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

//! Helpers for switching between multiple providers.
use alloc::collections::BTreeSet;
use icu_provider::prelude::*;

/// A provider that is one of two types determined at runtime.
Expand Down Expand Up @@ -90,7 +91,6 @@ impl<M: DataMarker, P0: DryDataProvider<M>, P1: DryDataProvider<M>> DryDataProvi
}
}

#[cfg(feature = "std")]
impl<
M: DynamicDataMarker,
P0: IterableDynamicDataProvider<M>,
Expand All @@ -101,7 +101,7 @@ impl<
fn iter_ids_for_marker(
&self,
marker: DataMarkerInfo,
) -> Result<std::collections::HashSet<DataIdentifierCow>, DataError> {
) -> Result<BTreeSet<DataIdentifierCow>, DataError> {
use EitherProvider::*;
match self {
A(p) => p.iter_ids_for_marker(marker),
Expand All @@ -110,12 +110,11 @@ impl<
}
}

#[cfg(feature = "std")]
impl<M: DataMarker, P0: IterableDataProvider<M>, P1: IterableDataProvider<M>>
IterableDataProvider<M> for EitherProvider<P0, P1>
{
#[inline]
fn iter_ids(&self) -> Result<std::collections::HashSet<DataIdentifierCow>, DataError> {
fn iter_ids(&self) -> Result<BTreeSet<DataIdentifierCow>, DataError> {
use EitherProvider::*;
match self {
A(p) => p.iter_ids(),
Expand Down
7 changes: 3 additions & 4 deletions provider/adapters/src/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//!
//! Use [`EmptyDataProvider`] as a stand-in for a provider that always fails.
use alloc::collections::BTreeSet;
use icu_provider::prelude::*;

/// A data provider that always returns an error.
Expand Down Expand Up @@ -86,25 +87,23 @@ where
}
}

#[cfg(feature = "std")]
impl<M> IterableDataProvider<M> for EmptyDataProvider
where
M: DataMarker,
{
fn iter_ids(&self) -> Result<std::collections::HashSet<DataIdentifierCow>, DataError> {
fn iter_ids(&self) -> Result<BTreeSet<DataIdentifierCow>, DataError> {
Ok(Default::default())
}
}

#[cfg(feature = "std")]
impl<M> IterableDynamicDataProvider<M> for EmptyDataProvider
where
M: DynamicDataMarker,
{
fn iter_ids_for_marker(
&self,
_: DataMarkerInfo,
) -> Result<std::collections::HashSet<DataIdentifierCow>, DataError> {
) -> Result<BTreeSet<DataIdentifierCow>, DataError> {
Ok(Default::default())
}
}
7 changes: 3 additions & 4 deletions provider/adapters/src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
mod impls;

use alloc::collections::BTreeSet;
use icu_provider::prelude::*;

/// A data provider that selectively filters out data requests.
Expand Down Expand Up @@ -130,7 +131,6 @@ where
}
}

#[cfg(feature = "std")]
impl<M, D, F> IterableDynamicDataProvider<M> for FilterDataProvider<D, F>
where
M: DynamicDataMarker,
Expand All @@ -140,7 +140,7 @@ where
fn iter_ids_for_marker(
&self,
marker: DataMarkerInfo,
) -> Result<std::collections::HashSet<DataIdentifierCow>, DataError> {
) -> Result<BTreeSet<DataIdentifierCow>, DataError> {
self.inner.iter_ids_for_marker(marker).map(|set| {
// Use filter_map instead of filter to avoid cloning the locale
set.into_iter()
Expand All @@ -150,14 +150,13 @@ where
}
}

#[cfg(feature = "std")]
impl<M, D, F> IterableDataProvider<M> for FilterDataProvider<D, F>
where
M: DataMarker,
F: Fn(DataIdentifierBorrowed) -> bool,
D: IterableDataProvider<M>,
{
fn iter_ids(&self) -> Result<std::collections::HashSet<DataIdentifierCow>, DataError> {
fn iter_ids(&self) -> Result<BTreeSet<DataIdentifierCow>, DataError> {
self.inner.iter_ids().map(|vec| {
// Use filter_map instead of filter to avoid cloning the locale
vec.into_iter()
Expand Down
8 changes: 3 additions & 5 deletions provider/adapters/src/fork/by_error.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 ).

use super::ForkByErrorPredicate;
use alloc::vec::Vec;
use alloc::{collections::BTreeSet, vec::Vec};
use icu_provider::prelude::*;

/// A provider that returns data from one of two child providers based on a predicate function.
Expand Down Expand Up @@ -138,7 +138,6 @@ where
}
}

#[cfg(feature = "std")]
impl<M, P0, P1, F> IterableDynamicDataProvider<M> for ForkByErrorProvider<P0, P1, F>
where
M: DynamicDataMarker,
Expand All @@ -149,7 +148,7 @@ where
fn iter_ids_for_marker(
&self,
marker: DataMarkerInfo,
) -> Result<std::collections::HashSet<DataIdentifierCow>, DataError> {
) -> Result<BTreeSet<DataIdentifierCow>, DataError> {
let result = self.0.iter_ids_for_marker(marker);
match result {
Ok(ok) => return Ok(ok),
Expand Down Expand Up @@ -313,7 +312,6 @@ where
}
}

#[cfg(feature = "std")]
impl<M, P, F> IterableDynamicDataProvider<M> for MultiForkByErrorProvider<P, F>
where
M: DynamicDataMarker,
Expand All @@ -323,7 +321,7 @@ where
fn iter_ids_for_marker(
&self,
marker: DataMarkerInfo,
) -> Result<std::collections::HashSet<DataIdentifierCow>, DataError> {
) -> Result<BTreeSet<DataIdentifierCow>, DataError> {
let mut last_error = F::UNIT_ERROR.with_marker(marker);
for provider in self.providers.iter() {
let result = provider.iter_ids_for_marker(marker);
Expand Down
2 changes: 1 addition & 1 deletion provider/adapters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! - Use the [`fallback`] module to automatically resolve arbitrary locales for data loading.
// https://github.com/unicode-org/icu4x/blob/main/documents/process/boilerplate.md#library-annotations
#![cfg_attr(not(any(test, feature = "std")), no_std)]
#![cfg_attr(not(test), no_std)]
#![cfg_attr(
not(test),
deny(
Expand Down
10 changes: 5 additions & 5 deletions provider/baked/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ impl DataExporter for BakedExporter {
&self,
req: icu_provider::DataRequest,
) -> Result<icu_provider::DataResponse<#marker_bake>, icu_provider::DataError> {
if req.id.locale.is_empty() {
if req.id.locale.is_und() {
Ok(icu_provider::DataResponse {
payload: icu_provider::DataPayload::from_static_ref(Self::#singleton_ident),
metadata: Default::default(),
Expand All @@ -461,8 +461,8 @@ impl DataExporter for BakedExporter {
}, quote! {
#maybe_msrv
impl icu_provider::IterableDataProvider<#marker_bake> for $provider {
fn iter_ids(&self) -> Result<std::collections::HashSet<icu_provider::DataIdentifierCow<'static>>, icu_provider::DataError> {
Ok(HashSet::from_iter([Default::default()]))
fn iter_ids(&self) -> Result<std::collections::BtreeSet<icu_provider::DataIdentifierCow<'static>>, icu_provider::DataError> {
Ok([Default::default()].into_iter().collect())
}
}
})
Expand Down Expand Up @@ -498,7 +498,7 @@ impl DataExporter for BakedExporter {
quote! {
#maybe_msrv
impl icu_provider::IterableDataProvider<#marker_bake> for $provider {
fn iter_ids(&self) -> Result<std::collections::HashSet<icu_provider::DataIdentifierCow<'static>>, icu_provider::DataError> {
fn iter_ids(&self) -> Result<std::collections::BTreeSet<icu_provider::DataIdentifierCow<'static>>, icu_provider::DataError> {
Ok(Default::default())
}
}
Expand Down Expand Up @@ -603,7 +603,7 @@ impl DataExporter for BakedExporter {
quote! {
#maybe_msrv
impl icu_provider::IterableDataProvider<#marker_bake> for $provider {
fn iter_ids(&self) -> Result<std::collections::HashSet<icu_provider::DataIdentifierCow<'static>>, icu_provider::DataError> {
fn iter_ids(&self) -> Result<std::collections::BTreeSet<icu_provider::DataIdentifierCow<'static>>, icu_provider::DataError> {
Ok(icu_provider_baked::DataStore::iter(&Self::#data_ident).collect())
}
}
Expand Down
2 changes: 1 addition & 1 deletion provider/baked/tests/data/hello_world_v1_marker.rs.data
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ macro_rules! __impl_hello_world_v1_marker {
__impl_hello_world_v1_marker!($provider);
#[clippy::msrv = "1.70"]
impl icu_provider::IterableDataProvider<icu_provider::hello_world::HelloWorldV1Marker> for $provider {
fn iter_ids(&self) -> Result<std::collections::HashSet<icu_provider::DataIdentifierCow<'static>>, icu_provider::DataError> {
fn iter_ids(&self) -> Result<std::collections::BTreeSet<icu_provider::DataIdentifierCow<'static>>, icu_provider::DataError> {
Ok(icu_provider_baked::DataStore::iter(&Self::DATA_HELLO_WORLD_V1_MARKER).collect())
}
}
Expand Down
6 changes: 1 addition & 5 deletions provider/blob/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ postcard = { workspace = true, features = ["alloc"] }
serde = { workspace = true, features = ["alloc"] }
writeable = {workspace = true }
zerovec = { workspace = true, features = ["serde", "yoke"] }
zerotrie = { workspace = true, features = ["serde", "zerovec"] }
zerotrie = { workspace = true, features = ["serde", "zerovec", "alloc"] }

log = { workspace = true, optional = true }

Expand All @@ -47,11 +47,7 @@ std = ["icu_provider/std"]
export = [
"icu_provider/export",
"log",
"postcard/alloc",
"std",
"zerovec/serde",
"zerotrie/alloc",
"zerotrie/litemap",
]

[lib]
Expand Down
3 changes: 2 additions & 1 deletion provider/blob/benches/auxkey_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use icu_provider::prelude::*;
use icu_provider_adapters::fallback::LocaleFallbackProvider;
use icu_provider_blob::export::BlobExporter;
use icu_provider_blob::BlobDataProvider;
use std::collections::BTreeSet;

#[icu_provider::data_struct(
marker(MarkerA, "a@1"),
Expand All @@ -35,7 +36,7 @@ macro_rules! implement {
}
}
impl IterableDataProvider<$marker> for Baked {
fn iter_ids(&self) -> Result<std::collections::HashSet<DataIdentifierCow>, DataError> {
fn iter_ids(&self) -> Result<BTreeSet<DataIdentifierCow>, DataError> {
const LOCALES: &[LanguageIdentifier] = &[
langid!("af"),
langid!("am"),
Expand Down
10 changes: 10 additions & 0 deletions provider/blob/src/blob_data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use crate::blob_schema::BlobSchema;
use alloc::boxed::Box;
use alloc::collections::BTreeSet;
use icu_provider::buf::BufferFormat;
use icu_provider::prelude::*;
use icu_provider::Cart;
Expand Down Expand Up @@ -148,6 +149,15 @@ impl DynamicDryDataProvider<BufferMarker> for BlobDataProvider {
}
}

impl IterableDynamicDataProvider<BufferMarker> for BlobDataProvider {
fn iter_ids_for_marker(
&self,
marker: DataMarkerInfo,
) -> Result<BTreeSet<DataIdentifierCow>, DataError> {
self.data.get().iter_ids(marker)
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
35 changes: 18 additions & 17 deletions provider/blob/src/blob_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use core::fmt::Write;

use alloc::boxed::Box;
use alloc::collections::BTreeSet;
use core::fmt::Write;
use icu_provider::{marker::DataMarkerPathHash, prelude::*};
use serde::Deserialize;
use writeable::Writeable;
Expand Down Expand Up @@ -47,15 +47,14 @@ impl<'data> BlobSchema<'data> {
}
}

#[cfg(feature = "export")]
pub fn list_requests(
pub fn iter_ids(
&self,
marker: DataMarkerInfo,
) -> Result<std::collections::HashSet<DataIdentifierCow>, DataError> {
) -> Result<BTreeSet<DataIdentifierCow>, DataError> {
match self {
BlobSchema::V001(s) => s.list_requests(marker),
BlobSchema::V002(s) => s.list_requests(marker),
BlobSchema::V002Bigger(s) => s.list_requests(marker),
BlobSchema::V001(s) => s.iter_ids(marker),
BlobSchema::V002(s) => s.iter_ids(marker),
BlobSchema::V002Bigger(s) => s.iter_ids(marker),
}
}

Expand Down Expand Up @@ -100,7 +99,7 @@ impl<'data> BlobSchemaV1<'data> {
.get0(&marker.path.hashed())
.ok_or(DataErrorKind::MarkerNotFound)
.and_then(|cursor| {
if marker.is_singleton && !req.id.locale.is_empty() {
if marker.is_singleton && !req.id.locale.is_und() {
return Err(DataErrorKind::InvalidRequest);
}
cursor
Expand Down Expand Up @@ -131,18 +130,19 @@ impl<'data> BlobSchemaV1<'data> {
.ok_or_else(|| DataError::custom("Invalid blob bytes").with_req(marker, req))
}

#[cfg(feature = "export")]
pub fn list_requests(
pub fn iter_ids(
&self,
marker: DataMarkerInfo,
) -> Result<std::collections::HashSet<DataIdentifierCow>, DataError> {
) -> Result<BTreeSet<DataIdentifierCow>, DataError> {
Ok(self
.markers
.get0(&marker.path.hashed())
.ok_or_else(|| DataErrorKind::MarkerNotFound.with_marker(marker))?
.iter1_copied()
.filter_map(|(s, _)| std::str::from_utf8(&s.0).ok())
.filter_map(|(s, _)| core::str::from_utf8(&s.0).ok())
.filter_map(|s| {
#[allow(unused_imports)]
use alloc::borrow::ToOwned;
if let Some((locale, attrs)) = s.split_once(REQUEST_SEPARATOR) {
Some(DataIdentifierCow::from_owned(
DataMarkerAttributes::try_from_str(attrs).ok()?.to_owned(),
Expand Down Expand Up @@ -224,7 +224,7 @@ impl<'data, LocaleVecFormat: VarZeroVecFormat> BlobSchemaV2<'data, LocaleVecForm
.binary_search(&marker.path.hashed())
.ok()
.ok_or_else(|| DataErrorKind::MarkerNotFound.with_req(marker, req))?;
if marker.is_singleton && !req.id.locale.is_empty() {
if marker.is_singleton && !req.id.locale.is_und() {
return Err(DataErrorKind::InvalidRequest.with_req(marker, req));
}
let zerotrie = self
Expand All @@ -250,11 +250,10 @@ impl<'data, LocaleVecFormat: VarZeroVecFormat> BlobSchemaV2<'data, LocaleVecForm
Ok(buffer)
}

#[cfg(feature = "export")]
pub fn list_requests(
pub fn iter_ids(
&self,
marker: DataMarkerInfo,
) -> Result<std::collections::HashSet<DataIdentifierCow>, DataError> {
) -> Result<BTreeSet<DataIdentifierCow>, DataError> {
let marker_index = self
.markers
.binary_search(&marker.path.hashed())
Expand All @@ -267,6 +266,8 @@ impl<'data, LocaleVecFormat: VarZeroVecFormat> BlobSchemaV2<'data, LocaleVecForm
Ok(ZeroTrieSimpleAscii::from_store(zerotrie)
.iter()
.filter_map(|(s, _)| {
#[allow(unused_imports)]
use alloc::borrow::ToOwned;
if let Some((locale, attrs)) = s.split_once(REQUEST_SEPARATOR) {
Some(DataIdentifierCow::from_owned(
DataMarkerAttributes::try_from_str(attrs).ok()?.to_owned(),
Expand Down
Loading

0 comments on commit c8a8801

Please sign in to comment.