Skip to content

Make sure temporal_capi can be built no_std #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 5, 2025
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
6 changes: 6 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ jobs:
- name: Makefile tests
run: cd temporal_capi/cpp_tests && make

# There's no guarantee that dependencies are no_std unless you test with a toolchain without `std`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, thanks! Nice catch!

- name: Install no_std toolchain
run: rustup target add thumbv7m-none-eabi
- name: Run no_std tests
run: cargo check -p temporal_capi --target thumbv7m-none-eabi

docs:
name: Documentation
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ temporal_provider = { version = "~0.0.6", path = "./provider" }
tinystr = "0.8.1"
icu_calendar = { version = "2.0.0-beta2", default-features = false }
rustc-hash = "2.1.0"
num-traits = "0.2.19"
num-traits = { version = "0.2.19", default-features = false }
ixdtf = "0.4.0"
iana-time-zone = "0.1.63"
log = "0.4.27"
Expand Down Expand Up @@ -71,6 +71,7 @@ combine = { workspace = true, optional = true }

# System time feature
web-time = { workspace = true, optional = true }
core_maths = "0.1.1"

[features]
default = ["sys"]
Expand Down
3 changes: 2 additions & 1 deletion src/options/increment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::num::{NonZeroU128, NonZeroU32};

use crate::{TemporalError, TemporalResult};
use num_traits::float::FloatCore;

// ==== RoundingIncrement option ====

Expand Down Expand Up @@ -31,7 +32,7 @@ impl TryFrom<f64> for RoundingIncrement {
}

// 5. Let integerIncrement be truncate(ℝ(increment)).
let integer_increment = value.trunc();
let integer_increment = FloatCore::trunc(value);
// 6. If integerIncrement < 1 or integerIncrement > 10**9, throw a RangeError exception.
if !(1.0..=1_000_000_000.0).contains(&integer_increment) {
return Err(TemporalError::range()
Expand Down
5 changes: 3 additions & 2 deletions src/primitive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Implementation of the FiniteF64 primitive

use crate::{TemporalError, TemporalResult};
use num_traits::float::FloatCore;
use num_traits::{AsPrimitive, FromPrimitive, PrimInt};

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

#[inline]
pub fn checked_mul_add(&self, a: FiniteF64, b: FiniteF64) -> TemporalResult<Self> {
let result = Self(self.0.mul_add(a.0, b.0));
let result = Self(core_maths::CoreFloat::mul_add(self.0, a.0, b.0));
if !result.0.is_finite() {
return Err(TemporalError::range().with_message("number value is not a finite value."));
}
Expand Down Expand Up @@ -77,7 +78,7 @@ impl FiniteF64 {
where
f64: AsPrimitive<T>,
{
if self.0 != self.0.trunc() {
if self.0 != FloatCore::trunc(self.0) {
return Err(TemporalError::range().with_message("value must be integral."));
}
Ok(self.0.as_())
Expand Down
9 changes: 6 additions & 3 deletions src/rounding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use core::{
ops::{Div, Neg},
};

use num_traits::float::FloatCore;
use num_traits::{ConstZero, Euclid, FromPrimitive, NumCast, Signed, ToPrimitive};

pub(crate) trait Roundable:
Expand Down Expand Up @@ -98,14 +99,16 @@ impl Roundable for f64 {

fn compare_remainder(dividend: Self, divisor: Self) -> Option<Ordering> {
let quotient = Roundable::quotient_abs(dividend, divisor);
let d1 = quotient - quotient.floor();
let d2 = quotient.ceil() - quotient;
let d1 = quotient - FloatCore::floor(quotient);
let d2 = FloatCore::ceil(quotient) - quotient;
d1.partial_cmp(&d2)
}

fn is_even_cardinal(dividend: Self, divisor: Self) -> bool {
let quotient = Roundable::quotient_abs(dividend, divisor);
(quotient.floor() / (quotient.ceil() - quotient.floor()) % 2.0) == 0.0
(FloatCore::floor(quotient) / (FloatCore::ceil(quotient) - FloatCore::floor(quotient))
% 2.0)
== 0.0
}

fn result_floor(dividend: Self, divisor: Self) -> u128 {
Expand Down
3 changes: 2 additions & 1 deletion temporal_capi/src/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ pub mod ffi {
use crate::plain_date::ffi::{PartialDate, PlainDate};
use crate::plain_month_day::ffi::PlainMonthDay;
use crate::plain_year_month::ffi::PlainYearMonth;
use alloc::boxed::Box;
use core::fmt::Write;
use diplomat_runtime::DiplomatStr;
use std::fmt::Write;

#[diplomat::enum_convert(icu_calendar::any_calendar::AnyCalendarKind, needs_wildcard)]
pub enum AnyCalendarKind {
Expand Down
1 change: 1 addition & 0 deletions temporal_capi/src/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::error::ffi::TemporalError;
#[diplomat::attr(auto, namespace = "temporal_rs")]
pub mod ffi {
use crate::error::ffi::TemporalError;
use alloc::boxed::Box;
use diplomat_runtime::DiplomatOption;
use num_traits::FromPrimitive;

Expand Down
1 change: 0 additions & 1 deletion temporal_capi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#[diplomat::abi_rename = "temporal_rs_{0}"]
#[diplomat::attr(auto, namespace = "temporal_rs")]
pub mod ffi {

#[diplomat::enum_convert(temporal_rs::error::ErrorKind)]
pub enum ErrorKind {
Generic,
Expand Down
1 change: 1 addition & 0 deletions temporal_capi/src/instant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod ffi {
use crate::duration::ffi::{Duration, TimeDuration};
use crate::error::ffi::TemporalError;
use crate::options::ffi::{DifferenceSettings, RoundingOptions};
use alloc::boxed::Box;

#[diplomat::opaque]
pub struct Instant(pub temporal_rs::Instant);
Expand Down
1 change: 0 additions & 1 deletion temporal_capi/src/iso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use temporal_rs::iso;
#[diplomat::abi_rename = "temporal_rs_{0}"]
#[diplomat::attr(auto, namespace = "temporal_rs")]
pub mod ffi {

pub struct IsoDate {
pub year: i32,
pub month: u8,
Expand Down
4 changes: 4 additions & 0 deletions temporal_capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
//!
//! [`temporal_rs`]: http://crates.io/crates/temporal_rs

#![no_std]

extern crate alloc;

pub mod calendar;
pub mod duration;
pub mod error;
Expand Down
3 changes: 2 additions & 1 deletion temporal_capi/src/plain_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ pub mod ffi {
use crate::plain_month_day::ffi::PlainMonthDay;
use crate::plain_time::ffi::PlainTime;
use crate::plain_year_month::ffi::PlainYearMonth;
use alloc::boxed::Box;
use core::fmt::Write;
use diplomat_runtime::{DiplomatOption, DiplomatStrSlice, DiplomatWrite};
use std::fmt::Write;

#[diplomat::opaque]
pub struct PlainDate(pub(crate) temporal_rs::PlainDate);
Expand Down
3 changes: 2 additions & 1 deletion temporal_capi/src/plain_date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ pub mod ffi {
use crate::calendar::ffi::Calendar;
use crate::duration::ffi::Duration;
use crate::error::ffi::TemporalError;
use alloc::boxed::Box;

use crate::options::ffi::{
ArithmeticOverflow, DifferenceSettings, DisplayCalendar, RoundingOptions,
ToStringRoundingOptions,
};
use crate::plain_date::ffi::{PartialDate, PlainDate};
use crate::plain_time::ffi::{PartialTime, PlainTime};
use core::fmt::Write;
use diplomat_runtime::DiplomatWrite;
use std::fmt::Write;

#[diplomat::opaque]
pub struct PlainDateTime(pub(crate) temporal_rs::PlainDateTime);
Expand Down
3 changes: 2 additions & 1 deletion temporal_capi/src/plain_month_day.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
pub mod ffi {
use crate::calendar::ffi::Calendar;
use crate::error::ffi::TemporalError;
use alloc::boxed::Box;

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

use core::fmt::Write;
use diplomat_runtime::DiplomatWrite;
use std::fmt::Write;

#[diplomat::opaque]
pub struct PlainMonthDay(pub(crate) temporal_rs::PlainMonthDay);
Expand Down
3 changes: 2 additions & 1 deletion temporal_capi/src/plain_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#[diplomat::abi_rename = "temporal_rs_{0}"]
#[diplomat::attr(auto, namespace = "temporal_rs")]
pub mod ffi {
use alloc::boxed::Box;

use crate::duration::ffi::{Duration, TimeDuration};
use crate::error::ffi::TemporalError;
use crate::options::ffi::{
ArithmeticOverflow, DifferenceSettings, RoundingMode, ToStringRoundingOptions, Unit,
};
use core::fmt::Write;
use diplomat_runtime::{DiplomatOption, DiplomatWrite};
use std::fmt::Write;

#[diplomat::opaque]
pub struct PlainTime(pub(crate) temporal_rs::PlainTime);
Expand Down
3 changes: 2 additions & 1 deletion temporal_capi/src/plain_year_month.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ pub mod ffi {
use crate::calendar::ffi::Calendar;
use crate::duration::ffi::Duration;
use crate::error::ffi::TemporalError;
use alloc::boxed::Box;

use crate::options::ffi::{ArithmeticOverflow, DifferenceSettings};
use crate::plain_date::ffi::{PartialDate, PlainDate};
use core::fmt::Write;
use diplomat_runtime::DiplomatWrite;
use std::fmt::Write;

#[diplomat::opaque]
pub struct PlainYearMonth(pub(crate) temporal_rs::PlainYearMonth);
Expand Down