Skip to content

Commit bde8991

Browse files
committed
Add Writeable getters for some types, use in FFI
1 parent 03cdc44 commit bde8991

File tree

7 files changed

+47
-14
lines changed

7 files changed

+47
-14
lines changed

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.

src/builtins/core/date.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::{
1717
use alloc::{format, string::String};
1818
use core::{cmp::Ordering, str::FromStr};
1919
use icu_calendar::AnyCalendarKind;
20+
use writeable::Writeable;
2021

2122
use super::{
2223
calendar::month_to_month_code,
@@ -679,6 +680,13 @@ impl PlainDate {
679680

680681
#[inline]
681682
pub fn to_ixdtf_string(&self, display_calendar: DisplayCalendar) -> String {
683+
self.to_ixdtf_writeable(display_calendar)
684+
.write_to_string()
685+
.into()
686+
}
687+
688+
#[inline]
689+
pub fn to_ixdtf_writeable(&self, display_calendar: DisplayCalendar) -> impl Writeable + '_ {
682690
IxdtfStringBuilder::default()
683691
.with_date(self.iso)
684692
.with_calendar(self.calendar.identifier(), display_calendar)

src/builtins/core/datetime.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::{
2020
use alloc::string::String;
2121
use core::{cmp::Ordering, str::FromStr};
2222
use tinystr::TinyAsciiStr;
23+
use writeable::Writeable;
2324

2425
/// A partial PlainDateTime record
2526
#[derive(Debug, Default, Clone)]
@@ -806,11 +807,11 @@ impl PlainDateTime {
806807
Ok(PlainTime::new_unchecked(self.iso.time))
807808
}
808809

809-
pub fn to_ixdtf_string(
810+
pub fn to_ixdtf_writeable(
810811
&self,
811812
options: ToStringRoundingOptions,
812813
display_calendar: DisplayCalendar,
813-
) -> TemporalResult<String> {
814+
) -> TemporalResult<impl Writeable + '_> {
814815
let resolved_options = options.resolve()?;
815816
let result = self
816817
.iso
@@ -820,12 +821,20 @@ impl PlainDateTime {
820821
if !result.is_within_limits() {
821822
return Err(TemporalError::range().with_message("DateTime is not within valid limits."));
822823
}
823-
let ixdtf_string = IxdtfStringBuilder::default()
824+
let builder = IxdtfStringBuilder::default()
824825
.with_date(result.date)
825826
.with_time(result.time, resolved_options.precision)
826-
.with_calendar(self.calendar.identifier(), display_calendar)
827-
.build();
828-
Ok(ixdtf_string)
827+
.with_calendar(self.calendar.identifier(), display_calendar);
828+
Ok(builder)
829+
}
830+
831+
pub fn to_ixdtf_string(
832+
&self,
833+
options: ToStringRoundingOptions,
834+
display_calendar: DisplayCalendar,
835+
) -> TemporalResult<String> {
836+
self.to_ixdtf_writeable(options, display_calendar)
837+
.map(|x| x.write_to_string().into())
829838
}
830839
}
831840

src/parsers.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ impl<'a> IxdtfStringBuilder<'a> {
9191
}
9292
}
9393

94+
impl<'a> Writeable for IxdtfStringBuilder<'a> {
95+
fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result {
96+
self.inner.write_to(sink)
97+
}
98+
99+
fn writeable_length_hint(&self) -> LengthHint {
100+
self.inner.writeable_length_hint()
101+
}
102+
}
103+
94104
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
95105
pub enum Precision {
96106
#[default]

temporal_capi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ num-traits.workspace = true
2424
temporal_rs = { workspace = true, default-features = false }
2525
icu_calendar = { version = "2.0.2", default-features = false }
2626
icu_locale = { version = "2.0.0" }
27+
writeable = "0.6.1"
2728

2829
[features]
2930
compiled_data = ["temporal_rs/compiled_data"]

temporal_capi/src/plain_date.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub mod ffi {
1919
use core::fmt::Write;
2020
use diplomat_runtime::{DiplomatOption, DiplomatStrSlice, DiplomatWrite};
2121
use diplomat_runtime::{DiplomatStr, DiplomatStr16};
22+
use writeable::Writeable;
2223

2324
use core::str::FromStr;
2425

@@ -277,10 +278,10 @@ pub mod ffi {
277278
display_calendar: DisplayCalendar,
278279
write: &mut DiplomatWrite,
279280
) {
280-
// TODO this double-allocates, an API returning a Writeable or impl Write would be better
281-
let string = self.0.to_ixdtf_string(display_calendar.into());
282-
// throw away the error, this should always succeed
283-
let _ = write.write_str(&string);
281+
let writeable = self.0.to_ixdtf_writeable(display_calendar.into());
282+
// This can only fail in cases where the DiplomatWriteable is capped, we
283+
// don't care about that.
284+
let _ = writeable.write_to(write);
284285
}
285286
}
286287
}

temporal_capi/src/plain_date_time.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub mod ffi {
2020
use core::str::FromStr;
2121
use diplomat_runtime::DiplomatWrite;
2222
use diplomat_runtime::{DiplomatStr, DiplomatStr16};
23+
use writeable::Writeable;
2324

2425
#[diplomat::opaque]
2526
pub struct PlainDateTime(pub(crate) temporal_rs::PlainDateTime);
@@ -320,11 +321,13 @@ pub mod ffi {
320321
write: &mut DiplomatWrite,
321322
) -> Result<(), TemporalError> {
322323
// TODO this double-allocates, an API returning a Writeable or impl Write would be better
323-
let string = self
324+
let writeable = self
324325
.0
325-
.to_ixdtf_string(options.into(), display_calendar.into())?;
326-
// throw away the error, this should always succeed
327-
let _ = write.write_str(&string);
326+
.to_ixdtf_writeable(options.into(), display_calendar.into())?;
327+
328+
// This can only fail in cases where the DiplomatWriteable is capped, we
329+
// don't care about that.
330+
let _ = writeable.write_to(write);
328331
Ok(())
329332
}
330333
}

0 commit comments

Comments
 (0)