Skip to content

Commit 0dc7027

Browse files
Restrict Rata Die API (#6440)
Fixes #6386
1 parent 096214f commit 0dc7027

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

components/calendar/src/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
//! This module contains various types used by `icu_calendar` and `icu::datetime`
66
7+
#[doc(no_inline)]
78
pub use calendrical_calculations::rata_die::RataDie;
89
use core::fmt;
910
use core::num::NonZeroU8;

utils/calendrical_calculations/src/rata_die.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ use core::ops::{Add, AddAssign, Sub, SubAssign};
1212
#[allow(unused_imports)]
1313
use core_maths::*;
1414

15-
/// The *Rata Die*, or *R.D.*, or `fixed_date`: number of days since January 1, 1 CE.
15+
/// The *Rata Die*, or *R.D.*: number of days since January 1, 1 CE.
1616
///
1717
/// See: <https://en.wikipedia.org/wiki/Rata_Die>
1818
///
19-
/// It is a logic error to construct a RataDie
20-
/// except from a date that is in range of one of the official calendars.
19+
/// Typically, one should obtain RataDies from other calendrical code, rather than constructing them from integers.
20+
/// The valid range for direct construction is deliberately not documented as it may change.
2121
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2222
pub struct RataDie(i64);
2323

2424
impl RataDie {
25-
/// Create a RataDie
25+
/// Create a `RataDie`
26+
///
27+
/// Typically, one should obtain `RataDie`s from other calendrical code, rather than
28+
/// constructing them from integers. The valid range for direct construction is
29+
/// deliberately not documented as it may change.
2630
pub const fn new(fixed_date: i64) -> Self {
2731
let result = Self(fixed_date);
2832
#[cfg(debug_assertions)]
@@ -32,7 +36,7 @@ impl RataDie {
3236

3337
/// Check that it is in range
3438
#[cfg(debug_assertions)]
35-
pub const fn check(&self) {
39+
const fn check(self) {
3640
if self.0 > i64::MAX / 256 {
3741
debug_assert!(
3842
false,
@@ -47,34 +51,34 @@ impl RataDie {
4751
}
4852
}
4953

50-
/// A valid RataDie that is intended to be below all dates representable in calendars
51-
#[doc(hidden)] // for testing only
54+
/// A valid `RataDie` that is intended to be below all dates representable in calendars
55+
#[doc(hidden)]
5256
pub const fn big_negative() -> Self {
5357
Self::new(i64::MIN / 256 / 256)
5458
}
5559

56-
/// Convert this to an i64 value representing the RataDie
60+
/// Convert this to an `i64` value representing the `RataDie`
5761
pub const fn to_i64_date(self) -> i64 {
5862
self.0
5963
}
6064

61-
/// Convert this to an f64 value representing the RataDie
62-
pub const fn to_f64_date(self) -> f64 {
65+
/// Convert this to an `f64` value representing the `RataDie`
66+
pub(crate) const fn to_f64_date(self) -> f64 {
6367
self.0 as f64
6468
}
6569

66-
/// Calculate the number of days between two RataDie in a const-friendly way
70+
/// Calculate the number of days between two `RataDie` in a const-friendly way
6771
pub const fn const_diff(self, rhs: Self) -> i64 {
6872
self.0 - rhs.0
6973
}
7074

71-
/// Adds a number of days to this RataDie in a const-friendly way
75+
/// Adds a number of days to this `RataDie` in a const-friendly way
7276
pub const fn const_add(self, rhs: i64) -> Self {
7377
Self(self.0 + rhs)
7478
}
7579

7680
/// Convert this to a [`Moment`]
77-
pub const fn as_moment(&self) -> Moment {
81+
pub(crate) const fn as_moment(self) -> Moment {
7882
Moment::new(self.0 as f64)
7983
}
8084
}
@@ -141,7 +145,7 @@ impl Sub for RataDie {
141145
/// NOTE: This should not cause overflow errors for most cases, but consider
142146
/// alternative implementations if necessary.
143147
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
144-
pub struct Moment(f64);
148+
pub(crate) struct Moment(f64);
145149

146150
/// Add a number of days to a Moment
147151
impl Add<f64> for Moment {
@@ -186,12 +190,12 @@ impl Moment {
186190
}
187191

188192
/// Get the inner field of a Moment
189-
pub const fn inner(&self) -> f64 {
193+
pub const fn inner(self) -> f64 {
190194
self.0
191195
}
192196

193197
/// Get the RataDie of a Moment
194-
pub fn as_rata_die(&self) -> RataDie {
198+
pub fn as_rata_die(self) -> RataDie {
195199
RataDie::new(self.0.floor() as i64)
196200
}
197201
}

0 commit comments

Comments
 (0)