Skip to content

Commit c909108

Browse files
authored
Add TryFrom for PartialDuration to Duration (#585)
This PR adds a `TryFrom` implemenation for converting a PartialDuration to Duration. The core reasoning is to simplify the case where someone would want to add a single day in Rust. ```rust use temporal_rs::{Temporal, PlainDate, partial::PartialDuration}; let today = Temporal::now().plain_date_iso(None).unwrap(); let one_day = PartialDuration::empty().with_days(1); // Before: // let tomorrow = today.add(Duration::from_partial_duration(one_day).unwrap()).unwrap() // After: let tomorrow = today.add(one_day.try_into().unwrap()).unwrap(); ```
1 parent 1f6b893 commit c909108

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/builtins/core/duration.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,13 @@ impl core::fmt::Display for Duration {
333333
}
334334
}
335335

336+
impl TryFrom<PartialDuration> for Duration {
337+
type Error = TemporalError;
338+
fn try_from(partial: PartialDuration) -> Result<Self, Self::Error> {
339+
Duration::from_partial_duration(partial)
340+
}
341+
}
342+
336343
// NOTE(nekevss): Structure of the below is going to be a little convoluted,
337344
// but intended to section everything based on the below
338345
//

0 commit comments

Comments
 (0)