Skip to content

Commit cd4b7c5

Browse files
authored
Move the icu crate to no_std (#911)
* Move uniset to no_std * Autoreplace * Migrate datetime to no_std * Autoreplace * fix errors * fix import * cargo fix * Move icu core crate to no-std * Add task for testing all of icu4x builds on nostd * fix import * fix warnings
1 parent 4ea6012 commit cd4b7c5

35 files changed

+130
-66
lines changed

components/datetime/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ path = "src/lib.rs"
5454
bench = false # This option is required for Benchmark CI
5555

5656
[features]
57+
std = ["icu_provider/std", "icu_locid/std"]
5758
default = ["provider_serde"]
5859
bench = []
5960
provider_serde = ["serde", "litemap/serde"]

components/datetime/src/date.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
// called LICENSE at the top level of the ICU4X source tree
33
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
44

5+
use core::convert::TryFrom;
6+
use core::ops::{Add, Sub};
7+
use core::str::FromStr;
58
use displaydoc::Display;
69
use icu_locid::Locale;
7-
use std::convert::TryFrom;
8-
use std::ops::{Add, Sub};
9-
use std::str::FromStr;
1010
use tinystr::TinyStr8;
1111

1212
#[derive(Display, Debug)]
1313
pub enum DateTimeError {
1414
#[displaydoc("{0}")]
15-
Parse(std::num::ParseIntError),
15+
Parse(core::num::ParseIntError),
1616
#[displaydoc("{field} must be between 0-{max}")]
1717
Overflow { field: &'static str, max: usize },
1818
#[displaydoc("{field} must be between {min}-0")]
@@ -21,10 +21,11 @@ pub enum DateTimeError {
2121
InvalidTimeZoneOffset,
2222
}
2323

24+
#[cfg(feature = "std")]
2425
impl std::error::Error for DateTimeError {}
2526

26-
impl From<std::num::ParseIntError> for DateTimeError {
27-
fn from(e: std::num::ParseIntError) -> Self {
27+
impl From<core::num::ParseIntError> for DateTimeError {
28+
fn from(e: core::num::ParseIntError) -> Self {
2829
DateTimeError::Parse(e)
2930
}
3031
}
@@ -299,7 +300,7 @@ impl From<usize> for IsoWeekday {
299300
if ordinal == 0 {
300301
ordinal = 7;
301302
}
302-
unsafe { std::mem::transmute(ordinal) }
303+
unsafe { core::mem::transmute(ordinal) }
303304
}
304305
}
305306

components/datetime/src/datetime.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010
helpers::DateTimePatterns,
1111
},
1212
};
13+
use alloc::string::String;
1314
use icu_locid::Locale;
1415
use icu_provider::prelude::*;
1516

@@ -235,17 +236,17 @@ impl<'data> DateTimeFormat<'data> {
235236
/// ```
236237
pub fn format_to_write(
237238
&self,
238-
w: &mut impl std::fmt::Write,
239+
w: &mut impl core::fmt::Write,
239240
value: &impl DateTimeInput,
240-
) -> std::fmt::Result {
241+
) -> core::fmt::Result {
241242
datetime::write_pattern(
242243
&self.pattern,
243244
self.symbols.as_ref().map(|s| s.get()),
244245
value,
245246
&self.locale,
246247
w,
247248
)
248-
.map_err(|_| std::fmt::Error)
249+
.map_err(|_| core::fmt::Error)
249250
}
250251

251252
/// Takes a [`DateTimeInput`] implementer and returns it formatted as a string.

components/datetime/src/error.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub enum DateTimeFormatError {
1616
Pattern(pattern::Error),
1717
/// An error originating from the [`Write`](std::fmt::Write) trait.
1818
#[displaydoc("{0}")]
19-
Format(std::fmt::Error),
19+
Format(core::fmt::Error),
2020
/// An error originating inside of the [`DataProvider`](icu_provider::DataProvider).
2121
#[displaydoc("{0}")]
2222
DataProvider(DataError),
@@ -32,6 +32,7 @@ pub enum DateTimeFormatError {
3232
UnsupportedField(FieldSymbol),
3333
}
3434

35+
#[cfg(feature = "std")]
3536
impl std::error::Error for DateTimeFormatError {}
3637

3738
impl From<pattern::Error> for DateTimeFormatError {
@@ -46,8 +47,8 @@ impl From<DataError> for DateTimeFormatError {
4647
}
4748
}
4849

49-
impl From<std::fmt::Error> for DateTimeFormatError {
50-
fn from(e: std::fmt::Error) -> Self {
50+
impl From<core::fmt::Error> for DateTimeFormatError {
51+
fn from(e: core::fmt::Error) -> Self {
5152
DateTimeFormatError::Format(e)
5253
}
5354
}

components/datetime/src/fields/length.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
// called LICENSE at the top level of the ICU4X source tree
33
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
44

5-
use displaydoc::Display;
6-
use std::{
5+
use core::{
76
cmp::{Ord, PartialOrd},
87
convert::TryFrom,
98
};
9+
use displaydoc::Display;
1010

1111
#[derive(Display, Debug, PartialEq)]
1212
pub enum LengthError {
1313
#[displaydoc("Invalid length")]
1414
InvalidLength,
1515
}
1616

17+
#[cfg(feature = "std")]
1718
impl std::error::Error for LengthError {}
1819

1920
#[derive(Debug, Eq, PartialEq, Clone, Copy, Ord, PartialOrd)]

components/datetime/src/fields/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use displaydoc::Display;
99
pub use length::{FieldLength, LengthError};
1010
pub use symbols::*;
1111

12-
use std::{
12+
use core::{
1313
cmp::{Ord, PartialOrd},
1414
convert::{TryFrom, TryInto},
1515
};
@@ -20,6 +20,7 @@ pub enum Error {
2020
InvalidLength(FieldSymbol),
2121
}
2222

23+
#[cfg(feature = "std")]
2324
impl std::error::Error for Error {}
2425

2526
#[derive(Debug, Eq, PartialEq, Clone, Copy, Ord, PartialOrd)]

components/datetime/src/fields/symbols.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
44

55
use crate::fields::FieldLength;
6+
use core::{cmp::Ordering, convert::TryFrom};
67
use displaydoc::Display;
7-
use std::{cmp::Ordering, convert::TryFrom};
88

99
#[derive(Display, Debug, PartialEq)]
1010
pub enum SymbolError {
@@ -16,6 +16,7 @@ pub enum SymbolError {
1616
Invalid(char),
1717
}
1818

19+
#[cfg(feature = "std")]
1920
impl std::error::Error for SymbolError {}
2021

2122
#[derive(Debug, Eq, PartialEq, Clone, Copy)]

components/datetime/src/format/datetime.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ use crate::fields::{self, Field, FieldLength, FieldSymbol};
99
use crate::pattern::{Pattern, PatternItem};
1010
use crate::provider;
1111
use crate::provider::helpers::DateTimeSymbols;
12+
13+
use alloc::string::ToString;
14+
use core::fmt;
1215
use icu_locid::Locale;
13-
use std::fmt;
1416
use writeable::Writeable;
1517

1618
/// [`FormattedDateTime`] is a intermediate structure which can be retrieved as
@@ -57,7 +59,7 @@ where
5759
{
5860
fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
5961
write_pattern(self.pattern, self.symbols, self.datetime, self.locale, sink)
60-
.map_err(|_| std::fmt::Error)
62+
.map_err(|_| core::fmt::Error)
6163
}
6264

6365
// TODO(#489): Implement write_len
@@ -69,12 +71,12 @@ where
6971
{
7072
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7173
write_pattern(self.pattern, self.symbols, self.datetime, self.locale, f)
72-
.map_err(|_| std::fmt::Error)
74+
.map_err(|_| core::fmt::Error)
7375
}
7476
}
7577

7678
// Temporary formatting number with length.
77-
fn format_number<W>(result: &mut W, num: isize, length: FieldLength) -> Result<(), std::fmt::Error>
79+
fn format_number<W>(result: &mut W, num: isize, length: FieldLength) -> Result<(), core::fmt::Error>
7880
where
7981
W: fmt::Write + ?Sized,
8082
{

components/datetime/src/format/time_zone.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// called LICENSE at the top level of the ICU4X source tree
33
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
44

5-
use std::fmt;
5+
use core::fmt;
66

77
use crate::error::DateTimeFormatError as Error;
88
use crate::fields::{self, FieldSymbol};
@@ -26,7 +26,7 @@ where
2626
T: TimeZoneInput,
2727
{
2828
fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
29-
write_pattern(self.time_zone_format, self.time_zone, sink).map_err(|_| std::fmt::Error)
29+
write_pattern(self.time_zone_format, self.time_zone, sink).map_err(|_| core::fmt::Error)
3030
}
3131

3232
// TODO(#489): Implement write_len
@@ -37,7 +37,7 @@ where
3737
T: TimeZoneInput,
3838
{
3939
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40-
write_pattern(self.time_zone_format, self.time_zone, f).map_err(|_| std::fmt::Error)
40+
write_pattern(self.time_zone_format, self.time_zone, f).map_err(|_| core::fmt::Error)
4141
}
4242
}
4343

components/datetime/src/format/zoned_datetime.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::error::DateTimeFormatError as Error;
77
use crate::fields::{self, FieldSymbol};
88
use crate::pattern::PatternItem;
99
use crate::{date::ZonedDateTimeInput, zoned_datetime::ZonedDateTimeFormat};
10-
use std::fmt;
10+
use core::fmt;
1111
use writeable::Writeable;
1212

1313
use super::datetime;
@@ -27,7 +27,7 @@ where
2727
{
2828
fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
2929
write_pattern(self.zoned_datetime_format, self.zoned_datetime, sink)
30-
.map_err(|_| std::fmt::Error)
30+
.map_err(|_| core::fmt::Error)
3131
}
3232

3333
// TODO(#489): Implement write_len
@@ -39,7 +39,7 @@ where
3939
{
4040
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4141
write_pattern(self.zoned_datetime_format, self.zoned_datetime, f)
42-
.map_err(|_| std::fmt::Error)
42+
.map_err(|_| core::fmt::Error)
4343
}
4444
}
4545

components/datetime/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@
6969
//! [`ICU4X`]: ../icu/index.html
7070
//! [`Length`]: options::length
7171
//! [`MockDateTime`]: mock::datetime::MockDateTime
72+
73+
#![cfg_attr(not(any(test, feature = "std")), no_std)]
74+
75+
extern crate alloc;
76+
7277
mod arithmetic;
7378
pub mod date;
7479
pub mod datetime;

components/datetime/src/mock/datetime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
use crate::arithmetic;
66
use crate::date::*;
7-
use std::convert::TryInto;
8-
use std::str::FromStr;
7+
use core::convert::TryInto;
8+
use core::str::FromStr;
99
use tinystr::tinystr8;
1010

1111
/// A temporary struct that implements [`DateTimeInput`]

components/datetime/src/mock/time_zone.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
// called LICENSE at the top level of the ICU4X source tree
33
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
44

5+
use alloc::string::String;
56
use tinystr::TinyStr8;
67

78
use crate::date::*;
8-
use std::str::FromStr;
9+
use core::str::FromStr;
910

1011
// TODO(#622) link [`TimeZoneFormat`] appropriately once it is public.
1112
/// A temporary struct that implements [`TimeZoneInput`]

components/datetime/src/mock/zoned_datetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use tinystr::TinyStr8;
66

77
use crate::date::*;
8-
use std::str::FromStr;
8+
use core::str::FromStr;
99

1010
use super::{datetime::MockDateTime, time_zone::MockTimeZone};
1111

components/datetime/src/options/components.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
//! and it is strongly recommended to never write tests that expect a particular formatted output.
7575
use crate::fields::{self, Field, FieldLength, FieldSymbol};
7676

77+
use alloc::vec::Vec;
78+
7779
use super::preferences;
7880
#[cfg(feature = "serde")]
7981
use serde::{Deserialize, Serialize};

components/datetime/src/pattern/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub enum Error {
2222
UnclosedPlaceholder,
2323
}
2424

25+
#[cfg(feature = "std")]
2526
impl std::error::Error for Error {}
2627

2728
impl From<fields::Error> for Error {

components/datetime/src/pattern/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@ mod error;
66
mod parser;
77

88
use crate::fields::{self, Field, FieldLength, FieldSymbol};
9+
#[cfg(feature = "provider_serde")]
10+
use alloc::format;
11+
use alloc::string::String;
12+
#[cfg(feature = "provider_serde")]
13+
use alloc::string::ToString;
14+
use alloc::vec;
15+
use alloc::vec::Vec;
16+
use core::{convert::TryFrom, fmt};
17+
use core::{fmt::Write, iter::FromIterator};
918
pub use error::Error;
1019
use parser::Parser;
11-
use std::{convert::TryFrom, fmt};
12-
use std::{fmt::Write, iter::FromIterator};
1320

1421
#[cfg(feature = "provider_serde")]
1522
use serde::{

components/datetime/src/pattern/parser.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
use super::error::Error;
66
use super::{Pattern, PatternItem};
77
use crate::fields::FieldSymbol;
8-
use std::convert::{TryFrom, TryInto};
8+
use alloc::string::String;
9+
use alloc::vec;
10+
use alloc::vec::Vec;
11+
use core::convert::{TryFrom, TryInto};
912

1013
#[derive(Debug, PartialEq)]
1114
enum Segment {
@@ -32,7 +35,7 @@ impl<'p> Parser<'p> {
3235
fn handle_quoted_literal(
3336
&mut self,
3437
ch: char,
35-
chars: &mut std::iter::Peekable<std::str::Chars>,
38+
chars: &mut core::iter::Peekable<core::str::Chars>,
3639
result: &mut Vec<PatternItem>,
3740
) -> Result<bool, Error> {
3841
if ch == '\'' {

components/datetime/src/provider/gregory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// called LICENSE at the top level of the ICU4X source tree
33
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
44

5+
use alloc::borrow::Cow;
56
use icu_provider::yoke::{self, *};
6-
use std::borrow::Cow;
77

88
#[icu_provider::data_struct]
99
#[derive(Debug, PartialEq, Clone, Default)]
@@ -139,8 +139,8 @@ pub mod patterns {
139139
pattern::{self, Pattern},
140140
skeleton::{Skeleton, SkeletonError},
141141
};
142+
use core::convert::TryFrom;
142143
use litemap::LiteMap;
143-
use std::convert::TryFrom;
144144

145145
#[derive(Debug, PartialEq, Clone, Default)]
146146
#[cfg_attr(

components/datetime/src/provider/helpers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use crate::options::{components, length, DateTimeFormatOptions};
99
use crate::pattern::Pattern;
1010
use crate::provider;
1111
use crate::skeleton;
12-
use std::borrow::Cow;
12+
use alloc::borrow::Cow;
1313

14-
type Result<T> = std::result::Result<T, DateTimeFormatError>;
14+
type Result<T> = core::result::Result<T, DateTimeFormatError>;
1515

1616
pub trait DateTimePatterns {
1717
fn get_pattern_for_options(&self, options: &DateTimeFormatOptions) -> Result<Option<Pattern>>;

0 commit comments

Comments
 (0)