Skip to content

Commit ea5be58

Browse files
Manishearthnekevss
authored andcommitted
Make sure temporal_capi can be built no_std (#281)
Ensures that no sys deps get pulled in
1 parent 843e2f4 commit ea5be58

File tree

17 files changed

+38
-15
lines changed

17 files changed

+38
-15
lines changed

.github/workflows/rust.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ jobs:
125125
- name: Makefile tests
126126
run: cd temporal_capi/cpp_tests && make
127127

128+
# There's no guarantee that dependencies are no_std unless you test with a toolchain without `std`
129+
- name: Install no_std toolchain
130+
run: rustup target add thumbv7m-none-eabi
131+
- name: Run no_std tests
132+
run: cargo check -p temporal_capi --target thumbv7m-none-eabi
133+
128134
docs:
129135
name: Documentation
130136
runs-on: ubuntu-latest

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ temporal_provider = { version = "~0.0.6", path = "./provider" }
2828
tinystr = "0.8.1"
2929
icu_calendar = { version = "2.0.0-beta2", default-features = false }
3030
rustc-hash = "2.1.0"
31-
num-traits = "0.2.19"
31+
num-traits = { version = "0.2.19", default-features = false }
3232
ixdtf = "0.4.0"
3333
iana-time-zone = "0.1.63"
3434
log = "0.4.27"
@@ -71,6 +71,7 @@ combine = { workspace = true, optional = true }
7171

7272
# System time feature
7373
web-time = { workspace = true, optional = true }
74+
core_maths = "0.1.1"
7475

7576
[features]
7677
default = ["sys"]

src/options/increment.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use core::num::{NonZeroU128, NonZeroU32};
22

33
use crate::{TemporalError, TemporalResult};
4+
use num_traits::float::FloatCore;
45

56
// ==== RoundingIncrement option ====
67

@@ -31,7 +32,7 @@ impl TryFrom<f64> for RoundingIncrement {
3132
}
3233

3334
// 5. Let integerIncrement be truncate(ℝ(increment)).
34-
let integer_increment = value.trunc();
35+
let integer_increment = FloatCore::trunc(value);
3536
// 6. If integerIncrement < 1 or integerIncrement > 10**9, throw a RangeError exception.
3637
if !(1.0..=1_000_000_000.0).contains(&integer_increment) {
3738
return Err(TemporalError::range()

src/primitive.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Implementation of the FiniteF64 primitive
22
33
use crate::{TemporalError, TemporalResult};
4+
use num_traits::float::FloatCore;
45
use num_traits::{AsPrimitive, FromPrimitive, PrimInt};
56

67
#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd)]
@@ -48,7 +49,7 @@ impl FiniteF64 {
4849

4950
#[inline]
5051
pub fn checked_mul_add(&self, a: FiniteF64, b: FiniteF64) -> TemporalResult<Self> {
51-
let result = Self(self.0.mul_add(a.0, b.0));
52+
let result = Self(core_maths::CoreFloat::mul_add(self.0, a.0, b.0));
5253
if !result.0.is_finite() {
5354
return Err(TemporalError::range().with_message("number value is not a finite value."));
5455
}
@@ -77,7 +78,7 @@ impl FiniteF64 {
7778
where
7879
f64: AsPrimitive<T>,
7980
{
80-
if self.0 != self.0.trunc() {
81+
if self.0 != FloatCore::trunc(self.0) {
8182
return Err(TemporalError::range().with_message("value must be integral."));
8283
}
8384
Ok(self.0.as_())

src/rounding.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use core::{
1111
ops::{Div, Neg},
1212
};
1313

14+
use num_traits::float::FloatCore;
1415
use num_traits::{ConstZero, Euclid, FromPrimitive, NumCast, Signed, ToPrimitive};
1516

1617
pub(crate) trait Roundable:
@@ -98,14 +99,16 @@ impl Roundable for f64 {
9899

99100
fn compare_remainder(dividend: Self, divisor: Self) -> Option<Ordering> {
100101
let quotient = Roundable::quotient_abs(dividend, divisor);
101-
let d1 = quotient - quotient.floor();
102-
let d2 = quotient.ceil() - quotient;
102+
let d1 = quotient - FloatCore::floor(quotient);
103+
let d2 = FloatCore::ceil(quotient) - quotient;
103104
d1.partial_cmp(&d2)
104105
}
105106

106107
fn is_even_cardinal(dividend: Self, divisor: Self) -> bool {
107108
let quotient = Roundable::quotient_abs(dividend, divisor);
108-
(quotient.floor() / (quotient.ceil() - quotient.floor()) % 2.0) == 0.0
109+
(FloatCore::floor(quotient) / (FloatCore::ceil(quotient) - FloatCore::floor(quotient))
110+
% 2.0)
111+
== 0.0
109112
}
110113

111114
fn result_floor(dividend: Self, divisor: Self) -> u128 {

temporal_capi/src/calendar.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ pub mod ffi {
99
use crate::plain_date::ffi::{PartialDate, PlainDate};
1010
use crate::plain_month_day::ffi::PlainMonthDay;
1111
use crate::plain_year_month::ffi::PlainYearMonth;
12+
use alloc::boxed::Box;
13+
use core::fmt::Write;
1214
use diplomat_runtime::DiplomatStr;
13-
use std::fmt::Write;
1415

1516
#[diplomat::enum_convert(icu_calendar::any_calendar::AnyCalendarKind, needs_wildcard)]
1617
pub enum AnyCalendarKind {

temporal_capi/src/duration.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::error::ffi::TemporalError;
55
#[diplomat::attr(auto, namespace = "temporal_rs")]
66
pub mod ffi {
77
use crate::error::ffi::TemporalError;
8+
use alloc::boxed::Box;
89
use diplomat_runtime::DiplomatOption;
910
use num_traits::FromPrimitive;
1011

temporal_capi/src/error.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#[diplomat::abi_rename = "temporal_rs_{0}"]
33
#[diplomat::attr(auto, namespace = "temporal_rs")]
44
pub mod ffi {
5-
65
#[diplomat::enum_convert(temporal_rs::error::ErrorKind)]
76
pub enum ErrorKind {
87
Generic,

temporal_capi/src/instant.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub mod ffi {
55
use crate::duration::ffi::{Duration, TimeDuration};
66
use crate::error::ffi::TemporalError;
77
use crate::options::ffi::{DifferenceSettings, RoundingOptions};
8+
use alloc::boxed::Box;
89

910
#[diplomat::opaque]
1011
pub struct Instant(pub temporal_rs::Instant);

temporal_capi/src/iso.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use temporal_rs::iso;
44
#[diplomat::abi_rename = "temporal_rs_{0}"]
55
#[diplomat::attr(auto, namespace = "temporal_rs")]
66
pub mod ffi {
7-
87
pub struct IsoDate {
98
pub year: i32,
109
pub month: u8,

temporal_capi/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
//!
1414
//! [`temporal_rs`]: http://crates.io/crates/temporal_rs
1515
16+
#![no_std]
17+
18+
extern crate alloc;
19+
1620
pub mod calendar;
1721
pub mod duration;
1822
pub mod error;

temporal_capi/src/plain_date.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ pub mod ffi {
1414
use crate::plain_month_day::ffi::PlainMonthDay;
1515
use crate::plain_time::ffi::PlainTime;
1616
use crate::plain_year_month::ffi::PlainYearMonth;
17+
use alloc::boxed::Box;
18+
use core::fmt::Write;
1719
use diplomat_runtime::{DiplomatOption, DiplomatStrSlice, DiplomatWrite};
18-
use std::fmt::Write;
1920

2021
#[diplomat::opaque]
2122
pub struct PlainDate(pub(crate) temporal_rs::PlainDate);

temporal_capi/src/plain_date_time.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ pub mod ffi {
77
use crate::calendar::ffi::Calendar;
88
use crate::duration::ffi::Duration;
99
use crate::error::ffi::TemporalError;
10+
use alloc::boxed::Box;
1011

1112
use crate::options::ffi::{
1213
ArithmeticOverflow, DifferenceSettings, DisplayCalendar, RoundingOptions,
1314
ToStringRoundingOptions,
1415
};
1516
use crate::plain_date::ffi::{PartialDate, PlainDate};
1617
use crate::plain_time::ffi::{PartialTime, PlainTime};
18+
use core::fmt::Write;
1719
use diplomat_runtime::DiplomatWrite;
18-
use std::fmt::Write;
1920

2021
#[diplomat::opaque]
2122
pub struct PlainDateTime(pub(crate) temporal_rs::PlainDateTime);

temporal_capi/src/plain_month_day.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
pub mod ffi {
55
use crate::calendar::ffi::Calendar;
66
use crate::error::ffi::TemporalError;
7+
use alloc::boxed::Box;
78

89
use crate::options::ffi::ArithmeticOverflow;
910
use crate::plain_date::ffi::{PartialDate, PlainDate};
1011

12+
use core::fmt::Write;
1113
use diplomat_runtime::DiplomatWrite;
12-
use std::fmt::Write;
1314

1415
#[diplomat::opaque]
1516
pub struct PlainMonthDay(pub(crate) temporal_rs::PlainMonthDay);

temporal_capi/src/plain_time.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
#[diplomat::abi_rename = "temporal_rs_{0}"]
33
#[diplomat::attr(auto, namespace = "temporal_rs")]
44
pub mod ffi {
5+
use alloc::boxed::Box;
56

67
use crate::duration::ffi::{Duration, TimeDuration};
78
use crate::error::ffi::TemporalError;
89
use crate::options::ffi::{
910
ArithmeticOverflow, DifferenceSettings, RoundingMode, ToStringRoundingOptions, Unit,
1011
};
12+
use core::fmt::Write;
1113
use diplomat_runtime::{DiplomatOption, DiplomatWrite};
12-
use std::fmt::Write;
1314

1415
#[diplomat::opaque]
1516
pub struct PlainTime(pub(crate) temporal_rs::PlainTime);

temporal_capi/src/plain_year_month.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ pub mod ffi {
55
use crate::calendar::ffi::Calendar;
66
use crate::duration::ffi::Duration;
77
use crate::error::ffi::TemporalError;
8+
use alloc::boxed::Box;
89

910
use crate::options::ffi::{ArithmeticOverflow, DifferenceSettings};
1011
use crate::plain_date::ffi::{PartialDate, PlainDate};
12+
use core::fmt::Write;
1113
use diplomat_runtime::DiplomatWrite;
12-
use std::fmt::Write;
1314

1415
#[diplomat::opaque]
1516
pub struct PlainYearMonth(pub(crate) temporal_rs::PlainYearMonth);

0 commit comments

Comments
 (0)