Skip to content

Commit a503f5e

Browse files
authored
Add no-alloc test, make litemap no-alloc (#6077)
Discovered that litemap had an `alloc` feature that partially worked, made it fully work.
1 parent 547c45c commit a503f5e

File tree

10 files changed

+188
-14
lines changed

10 files changed

+188
-14
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ members = [
9595
"tools/make/diplomat-gen",
9696
"tools/make/download-repo-sources",
9797
"tools/md-tests",
98+
"tools/noalloctest",
9899
]
99100
# Note: Workspaces in subdirectories, such as tutorials/crates, are
100101
# implicitly excluded from the main workspace.

Makefile.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ env = { ICU4X_DATA_DIR = "../stubdata" }
144144
category = "CI"
145145
dependencies = [
146146
"check-nostd",
147+
"check-noalloc",
147148
"check-freertos-wearos",
148149
]
149150

tools/make/ffi.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ args = ["check", "--package", "icu_freertos",
7474
"--no-default-features", "--features=wearos",
7575
"-Zbuild-std=core,alloc", "-Zbuild-std-features=panic_immediate_abort"]
7676

77+
[tasks.check-noalloc]
78+
description = "Build crates that are expected to work in no-alloc mode"
79+
category = "ICU4X FFI"
80+
dependencies = ["install-unknown-linux-nightly"]
81+
toolchain = "${PINNED_CI_NIGHTLY}"
82+
command = "cargo"
83+
args = ["rustc", "-p", "noalloctest",
84+
"--target", "x86_64-unknown-linux-gnu",
85+
"--",
86+
"-Clink-arg=-nostartfiles", "-Cpanic=abort",
87+
"--cfg", "icu4x_nostdtest"]
7788

7889
# Diplomat gen
7990

tools/noalloctest/Cargo.toml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This file is part of ICU4X. For terms of use, please see the file
2+
# called LICENSE at the top level of the ICU4X source tree
3+
# (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4+
5+
[package]
6+
name = "noalloctest"
7+
version.workspace = true
8+
rust-version.workspace = true
9+
authors.workspace = true
10+
edition.workspace = true
11+
repository.workspace = true
12+
homepage.workspace = true
13+
license.workspace = true
14+
categories.workspace = true
15+
include.workspace = true
16+
publish = false
17+
18+
# Testing: Build this crate with
19+
# cargo +nightly rustc -Zbuild-std=core,panic_abort -- -C link-arg=-nostartfiles -Cpanic=abort --cfg icu4x_noalloctest
20+
21+
[dependencies]
22+
# Dependencies that should be no-alloc should go here
23+
tinystr = {workspace = true, default-features = false}
24+
zerofrom = {workspace = true, default-features = false}
25+
yoke = {workspace = true, default-features = false}
26+
zerotrie = {workspace = true, default-features = false}
27+
potential_utf = {workspace = true, default-features = false}
28+
litemap = {workspace = true, default-features = false}
29+
30+
[lints.rust]
31+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(icu4x_noalloctest)'] }

tools/noalloctest/LICENSE

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
UNICODE LICENSE V3
2+
3+
COPYRIGHT AND PERMISSION NOTICE
4+
5+
Copyright © 2020-2024 Unicode, Inc.
6+
7+
NOTICE TO USER: Carefully read the following legal agreement. BY
8+
DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING DATA FILES, AND/OR
9+
SOFTWARE, YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE
10+
TERMS AND CONDITIONS OF THIS AGREEMENT. IF YOU DO NOT AGREE, DO NOT
11+
DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE THE DATA FILES OR SOFTWARE.
12+
13+
Permission is hereby granted, free of charge, to any person obtaining a
14+
copy of data files and any associated documentation (the "Data Files") or
15+
software and any associated documentation (the "Software") to deal in the
16+
Data Files or Software without restriction, including without limitation
17+
the rights to use, copy, modify, merge, publish, distribute, and/or sell
18+
copies of the Data Files or Software, and to permit persons to whom the
19+
Data Files or Software are furnished to do so, provided that either (a)
20+
this copyright and permission notice appear with all copies of the Data
21+
Files or Software, or (b) this copyright and permission notice appear in
22+
associated Documentation.
23+
24+
THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
25+
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
27+
THIRD PARTY RIGHTS.
28+
29+
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE
30+
BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES,
31+
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
32+
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
33+
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE DATA
34+
FILES OR SOFTWARE.
35+
36+
Except as contained in this notice, the name of a copyright holder shall
37+
not be used in advertising or otherwise to promote the sale, use or other
38+
dealings in these Data Files or Software without prior written
39+
authorization of the copyright holder.
40+
41+
SPDX-License-Identifier: Unicode-3.0
42+
43+
44+
45+
Portions of ICU4X may have been adapted from ICU4C and/or ICU4J.
46+
ICU 1.8.1 to ICU 57.1 © 1995-2016 International Business Machines Corporation and others.

tools/noalloctest/README.md

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/noalloctest/src/main.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// This file is part of ICU4X. For terms of use, please see the file
2+
// called LICENSE at the top level of the ICU4X source tree
3+
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4+
5+
// We have icu4x_noalloctest so that this crate does not trip up our all-crates CI
6+
#![cfg_attr(icu4x_noalloctest, no_std)]
7+
#![cfg_attr(icu4x_noalloctest, no_main)]
8+
9+
#[cfg(icu4x_noalloctest)]
10+
mod real {
11+
#[panic_handler]
12+
fn panic(_info: &core::panic::PanicInfo) -> ! {
13+
// don't care
14+
loop {}
15+
}
16+
17+
#[no_mangle]
18+
pub extern "C" fn _start() -> ! {
19+
// don't care
20+
loop {}
21+
}
22+
}
23+
24+
#[cfg(not(icu4x_noalloctest))]
25+
fn main() {}

utils/litemap/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#[cfg(doc)]
5151
extern crate std;
5252

53+
#[cfg(feature = "alloc")]
5354
extern crate alloc;
5455

5556
#[cfg(feature = "databake")]

utils/litemap/src/map.rs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,43 @@
33
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
44

55
use crate::store::*;
6-
use alloc::borrow::Borrow;
6+
#[cfg(feature = "alloc")]
77
use alloc::boxed::Box;
8+
#[cfg(feature = "alloc")]
89
use alloc::vec::Vec;
10+
use core::borrow::Borrow;
911
use core::cmp::Ordering;
1012
use core::iter::FromIterator;
1113
use core::marker::PhantomData;
1214
use core::mem;
1315
use core::ops::{Index, IndexMut, Range};
1416

15-
/// A simple "flat" map based on a sorted vector
16-
///
17-
/// See the [module level documentation][super] for why one should use this.
18-
///
19-
/// The API is roughly similar to that of [`std::collections::BTreeMap`].
20-
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
21-
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
22-
pub struct LiteMap<K: ?Sized, V: ?Sized, S = alloc::vec::Vec<(K, V)>> {
23-
pub(crate) values: S,
24-
pub(crate) _key_type: PhantomData<K>,
25-
pub(crate) _value_type: PhantomData<V>,
26-
}
17+
macro_rules! litemap_impl(
18+
($cfg:meta, $store:ident $(=$defaultty:ty)?) => {
19+
/// A simple "flat" map based on a sorted vector
20+
///
21+
/// See the [module level documentation][super] for why one should use this.
22+
///
23+
/// The API is roughly similar to that of [`std::collections::BTreeMap`].
24+
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
25+
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
26+
#[cfg($cfg)]
27+
pub struct LiteMap<K: ?Sized, V: ?Sized, $store $(= $defaultty)?> {
28+
pub(crate) values: $store,
29+
pub(crate) _key_type: PhantomData<K>,
30+
pub(crate) _value_type: PhantomData<V>,
31+
}
32+
};
33+
34+
);
35+
// You can't `cfg()` a default generic parameter, and we don't want to write this type twice
36+
// and keep them in sync so we use a small macro
37+
litemap_impl!(feature = "alloc", S = alloc::vec::Vec<(K, V)>);
38+
litemap_impl!(not(feature = "alloc"), S);
2739

40+
#[cfg(feature = "alloc")]
2841
impl<K, V> LiteMap<K, V> {
29-
/// Construct a new [`LiteMap`] backed by Vec
42+
/// Construct a new [`LiteMap`] backed by Vec
3043
pub const fn new_vec() -> Self {
3144
Self {
3245
values: alloc::vec::Vec::new(),
@@ -49,6 +62,7 @@ impl<K, V, S> LiteMap<K, V, S> {
4962
}
5063
}
5164

65+
#[cfg(feature = "alloc")]
5266
impl<K, V> LiteMap<K, V, Vec<(K, V)>> {
5367
/// Convert a [`LiteMap`] into a sorted `Vec<(K, V)>`.
5468
#[inline]
@@ -144,6 +158,7 @@ where
144158
///
145159
/// assert_eq!(boxed_map.get("one"), Some(&Box::from("uno")));
146160
/// ```
161+
#[cfg(feature = "alloc")]
147162
pub fn to_boxed_keys_values<KB: ?Sized, VB: ?Sized, SB>(&self) -> LiteMap<Box<KB>, Box<VB>, SB>
148163
where
149164
SB: StoreMut<Box<KB>, Box<VB>>,
@@ -182,6 +197,7 @@ where
182197
///
183198
/// assert_eq!(boxed_map.get("one"), Some(&11));
184199
/// ```
200+
#[cfg(feature = "alloc")]
185201
pub fn to_boxed_keys<KB: ?Sized, SB>(&self) -> LiteMap<Box<KB>, V, SB>
186202
where
187203
V: Clone,
@@ -219,6 +235,7 @@ where
219235
///
220236
/// assert_eq!(boxed_map.get(&11), Some(&Box::from("uno")));
221237
/// ```
238+
#[cfg(feature = "alloc")]
222239
pub fn to_boxed_values<VB: ?Sized, SB>(&self) -> LiteMap<K, Box<VB>, SB>
223240
where
224241
K: Clone,

0 commit comments

Comments
 (0)