Skip to content

Commit 742657b

Browse files
committed
feat(lazer): add a few more functions and costs
1 parent 1daa25f commit 742657b

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

lazer/sdk/rust/protocol/src/time.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,49 +24,73 @@ impl TimestampUs {
2424

2525
impl TimestampUs {
2626
pub const UNIX_EPOCH: Self = Self(0);
27+
pub const MAX: Self = Self(u64::MAX);
2728

29+
#[inline]
2830
pub const fn from_micros(micros: u64) -> Self {
2931
Self(micros)
3032
}
3133

34+
#[inline]
3235
pub const fn as_micros(self) -> u64 {
3336
self.0
3437
}
3538

39+
#[inline]
3640
pub fn as_nanos(self) -> u128 {
3741
// never overflows
3842
u128::from(self.0) * 1000
3943
}
4044

45+
#[inline]
46+
pub fn as_nanos_i128(self) -> i128 {
47+
// never overflows
48+
i128::from(self.0) * 1000
49+
}
50+
51+
#[inline]
4152
pub fn from_nanos(nanos: u128) -> anyhow::Result<Self> {
4253
let micros = nanos
4354
.checked_div(1000)
4455
.context("nanos.checked_div(1000) failed")?;
4556
Ok(Self::from_micros(micros.try_into()?))
4657
}
4758

59+
#[inline]
60+
pub fn from_nanos_i128(nanos: i128) -> anyhow::Result<Self> {
61+
let micros = nanos
62+
.checked_div(1000)
63+
.context("nanos.checked_div(1000) failed")?;
64+
Ok(Self::from_micros(micros.try_into()?))
65+
}
66+
67+
#[inline]
4868
pub fn as_millis(self) -> u64 {
4969
self.0 / 1000
5070
}
5171

72+
#[inline]
5273
pub fn from_millis(millis: u64) -> anyhow::Result<Self> {
5374
let micros = millis
5475
.checked_mul(1000)
5576
.context("millis.checked_mul(1000) failed")?;
5677
Ok(Self::from_micros(micros))
5778
}
5879

80+
#[inline]
5981
pub fn as_secs(self) -> u64 {
6082
self.0 / 1_000_000
6183
}
6284

85+
#[inline]
6386
pub fn from_secs(secs: u64) -> anyhow::Result<Self> {
6487
let micros = secs
6588
.checked_mul(1_000_000)
6689
.context("secs.checked_mul(1_000_000) failed")?;
6790
Ok(Self::from_micros(micros))
6891
}
6992

93+
#[inline]
7094
pub fn duration_since(self, other: Self) -> anyhow::Result<DurationUs> {
7195
Ok(DurationUs(
7296
self.0
@@ -75,26 +99,32 @@ impl TimestampUs {
7599
))
76100
}
77101

102+
#[inline]
78103
pub fn saturating_duration_since(self, other: Self) -> DurationUs {
79104
DurationUs(self.0.saturating_sub(other.0))
80105
}
81106

107+
#[inline]
82108
pub fn elapsed(self) -> anyhow::Result<DurationUs> {
83109
self.duration_since(Self::now())
84110
}
85111

112+
#[inline]
86113
pub fn saturating_elapsed(self) -> DurationUs {
87114
self.saturating_duration_since(Self::now())
88115
}
89116

117+
#[inline]
90118
pub fn saturating_add(self, duration: DurationUs) -> TimestampUs {
91119
TimestampUs(self.0.saturating_add(duration.0))
92120
}
93121

122+
#[inline]
94123
pub fn saturating_sub(self, duration: DurationUs) -> TimestampUs {
95124
TimestampUs(self.0.saturating_sub(duration.0))
96125
}
97126

127+
#[inline]
98128
pub fn is_multiple_of(self, duration: DurationUs) -> bool {
99129
match self.0.checked_rem(duration.0) {
100130
Some(rem) => rem == 0,
@@ -103,6 +133,7 @@ impl TimestampUs {
103133
}
104134

105135
/// Calculates the smallest value greater than or equal to self that is a multiple of `duration`.
136+
#[inline]
106137
pub fn next_multiple_of(self, duration: DurationUs) -> anyhow::Result<TimestampUs> {
107138
Ok(TimestampUs(
108139
self.0
@@ -112,6 +143,7 @@ impl TimestampUs {
112143
}
113144

114145
/// Calculates the smallest value less than or equal to self that is a multiple of `duration`.
146+
#[inline]
115147
pub fn previous_multiple_of(self, duration: DurationUs) -> anyhow::Result<TimestampUs> {
116148
Ok(TimestampUs(
117149
self.0
@@ -122,6 +154,7 @@ impl TimestampUs {
122154
))
123155
}
124156

157+
#[inline]
125158
pub fn checked_add(self, duration: DurationUs) -> anyhow::Result<Self> {
126159
Ok(TimestampUs(
127160
self.0
@@ -130,6 +163,7 @@ impl TimestampUs {
130163
))
131164
}
132165

166+
#[inline]
133167
pub fn checked_sub(self, duration: DurationUs) -> anyhow::Result<Self> {
134168
Ok(TimestampUs(
135169
self.0
@@ -142,6 +176,7 @@ impl TimestampUs {
142176
impl TryFrom<ProtobufTimestamp> for TimestampUs {
143177
type Error = anyhow::Error;
144178

179+
#[inline]
145180
fn try_from(timestamp: ProtobufTimestamp) -> anyhow::Result<Self> {
146181
TryFrom::<&ProtobufTimestamp>::try_from(&timestamp)
147182
}
@@ -185,6 +220,7 @@ impl From<TimestampUs> for ProtobufTimestamp {
185220
}
186221

187222
impl From<TimestampUs> for MessageField<ProtobufTimestamp> {
223+
#[inline]
188224
fn from(value: TimestampUs) -> Self {
189225
MessageField::some(value.into())
190226
}
@@ -216,6 +252,7 @@ impl TryFrom<TimestampUs> for SystemTime {
216252
impl TryFrom<&chrono::DateTime<chrono::Utc>> for TimestampUs {
217253
type Error = anyhow::Error;
218254

255+
#[inline]
219256
fn try_from(value: &chrono::DateTime<chrono::Utc>) -> Result<Self, Self::Error> {
220257
Ok(Self(value.timestamp_micros().try_into()?))
221258
}
@@ -224,6 +261,7 @@ impl TryFrom<&chrono::DateTime<chrono::Utc>> for TimestampUs {
224261
impl TryFrom<chrono::DateTime<chrono::Utc>> for TimestampUs {
225262
type Error = anyhow::Error;
226263

264+
#[inline]
227265
fn try_from(value: chrono::DateTime<chrono::Utc>) -> Result<Self, Self::Error> {
228266
TryFrom::<&chrono::DateTime<chrono::Utc>>::try_from(&value)
229267
}
@@ -232,6 +270,7 @@ impl TryFrom<chrono::DateTime<chrono::Utc>> for TimestampUs {
232270
impl TryFrom<TimestampUs> for chrono::DateTime<chrono::Utc> {
233271
type Error = anyhow::Error;
234272

273+
#[inline]
235274
fn try_from(value: TimestampUs) -> Result<Self, Self::Error> {
236275
chrono::DateTime::<chrono::Utc>::from_timestamp_micros(value.as_micros().try_into()?)
237276
.with_context(|| format!("cannot convert timestamp to datetime: {value:?}"))
@@ -245,86 +284,112 @@ pub struct DurationUs(u64);
245284
impl DurationUs {
246285
pub const ZERO: Self = Self(0);
247286

287+
#[inline]
248288
pub const fn from_micros(micros: u64) -> Self {
249289
Self(micros)
250290
}
251291

292+
#[inline]
252293
pub const fn as_micros(self) -> u64 {
253294
self.0
254295
}
255296

297+
#[inline]
256298
pub fn as_nanos(self) -> u128 {
257299
// never overflows
258300
u128::from(self.0) * 1000
259301
}
260302

303+
#[inline]
261304
pub fn from_nanos(nanos: u128) -> anyhow::Result<Self> {
262305
let micros = nanos.checked_div(1000).context("checked_div failed")?;
263306
Ok(Self::from_micros(micros.try_into()?))
264307
}
265308

309+
#[inline]
266310
pub fn as_millis(self) -> u64 {
267311
self.0 / 1000
268312
}
269313

314+
#[inline]
270315
pub const fn from_millis_u32(millis: u32) -> Self {
271316
// never overflows
272317
Self((millis as u64) * 1_000)
273318
}
274319

320+
#[inline]
275321
pub fn from_millis(millis: u64) -> anyhow::Result<Self> {
276322
let micros = millis
277323
.checked_mul(1000)
278324
.context("millis.checked_mul(1000) failed")?;
279325
Ok(Self::from_micros(micros))
280326
}
281327

328+
#[inline]
282329
pub fn as_secs(self) -> u64 {
283330
self.0 / 1_000_000
284331
}
285332

333+
#[inline]
286334
pub const fn from_secs_u32(secs: u32) -> Self {
287335
// never overflows
288336
Self((secs as u64) * 1_000_000)
289337
}
290338

339+
#[inline]
291340
pub fn from_secs(secs: u64) -> anyhow::Result<Self> {
292341
let micros = secs
293342
.checked_mul(1_000_000)
294343
.context("secs.checked_mul(1_000_000) failed")?;
295344
Ok(Self::from_micros(micros))
296345
}
297346

347+
#[inline]
348+
pub fn from_days_u32(days: u32) -> Self {
349+
// never overflows
350+
Self((days as u64) * 24 * 3600 * 1_000_000)
351+
}
352+
353+
#[inline]
298354
pub fn is_multiple_of(self, other: DurationUs) -> bool {
299355
match self.0.checked_rem(other.0) {
300356
Some(rem) => rem == 0,
301357
None => false,
302358
}
303359
}
304360

361+
#[inline]
305362
pub const fn is_zero(self) -> bool {
306363
self.0 == 0
307364
}
308365

366+
#[inline]
309367
pub const fn is_positive(self) -> bool {
310368
self.0 > 0
311369
}
312370

371+
#[inline]
313372
pub fn checked_add(self, other: DurationUs) -> anyhow::Result<Self> {
314373
Ok(DurationUs(
315374
self.0.checked_add(other.0).context("checked_add failed")?,
316375
))
317376
}
377+
378+
#[inline]
318379
pub fn checked_sub(self, other: DurationUs) -> anyhow::Result<Self> {
319380
Ok(DurationUs(
320381
self.0.checked_sub(other.0).context("checked_sub failed")?,
321382
))
322383
}
384+
385+
#[inline]
323386
pub fn checked_mul(self, n: u64) -> anyhow::Result<DurationUs> {
324387
Ok(DurationUs(
325388
self.0.checked_mul(n).context("checked_mul failed")?,
326389
))
327390
}
391+
392+
#[inline]
328393
pub fn checked_div(self, n: u64) -> anyhow::Result<DurationUs> {
329394
Ok(DurationUs(
330395
self.0.checked_div(n).context("checked_div failed")?,
@@ -333,6 +398,7 @@ impl DurationUs {
333398
}
334399

335400
impl From<DurationUs> for Duration {
401+
#[inline]
336402
fn from(value: DurationUs) -> Self {
337403
Duration::from_micros(value.as_micros())
338404
}
@@ -341,6 +407,7 @@ impl From<DurationUs> for Duration {
341407
impl TryFrom<Duration> for DurationUs {
342408
type Error = anyhow::Error;
343409

410+
#[inline]
344411
fn try_from(value: Duration) -> Result<Self, Self::Error> {
345412
Ok(Self(value.as_micros().try_into()?))
346413
}
@@ -349,6 +416,7 @@ impl TryFrom<Duration> for DurationUs {
349416
impl TryFrom<ProtobufDuration> for DurationUs {
350417
type Error = anyhow::Error;
351418

419+
#[inline]
352420
fn try_from(duration: ProtobufDuration) -> anyhow::Result<Self> {
353421
TryFrom::<&ProtobufDuration>::try_from(&duration)
354422
}

0 commit comments

Comments
 (0)