Skip to content

Commit d0d1aac

Browse files
authored
Make numeric constructors and arithmetic const. (#3900)
## Motivation It should be possible to e.g. define a `const x: Amount` using some addition and multiplication. (I ran into this in #3893.) ## Proposal Make more functions `const`. (Also fix a typo.) ## Test Plan N/A ## Release Plan - Nothing to do / These changes follow the usual release cycle. ## Links - [reviewer checklist](https://github.com/linera-io/linera-protocol/blob/main/CONTRIBUTING.md#reviewer-checklist)
1 parent a95ee7f commit d0d1aac

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

linera-base/src/data_types.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,17 @@ pub struct TimeDelta(u64);
142142

143143
impl TimeDelta {
144144
/// Returns the given number of microseconds as a [`TimeDelta`].
145-
pub fn from_micros(micros: u64) -> Self {
145+
pub const fn from_micros(micros: u64) -> Self {
146146
TimeDelta(micros)
147147
}
148148

149149
/// Returns the given number of milliseconds as a [`TimeDelta`].
150-
pub fn from_millis(millis: u64) -> Self {
150+
pub const fn from_millis(millis: u64) -> Self {
151151
TimeDelta(millis.saturating_mul(1_000))
152152
}
153153

154154
/// Returns the given number of seconds as a [`TimeDelta`].
155-
pub fn from_secs(secs: u64) -> Self {
155+
pub const fn from_secs(secs: u64) -> Self {
156156
TimeDelta(secs.saturating_mul(1_000_000))
157157
}
158158

@@ -163,12 +163,12 @@ impl TimeDelta {
163163
}
164164

165165
/// Returns this [`TimeDelta`] as a number of microseconds.
166-
pub fn as_micros(&self) -> u64 {
166+
pub const fn as_micros(&self) -> u64 {
167167
self.0
168168
}
169169

170170
/// Returns this [`TimeDelta`] as a [`Duration`].
171-
pub fn as_duration(&self) -> Duration {
171+
pub const fn as_duration(&self) -> Duration {
172172
Duration::from_micros(self.as_micros())
173173
}
174174
}
@@ -206,41 +206,41 @@ impl Timestamp {
206206
}
207207

208208
/// Returns the number of microseconds since the Unix epoch.
209-
pub fn micros(&self) -> u64 {
209+
pub const fn micros(&self) -> u64 {
210210
self.0
211211
}
212212

213213
/// Returns the [`TimeDelta`] between `other` and `self`, or zero if `other` is not earlier
214214
/// than `self`.
215-
pub fn delta_since(&self, other: Timestamp) -> TimeDelta {
215+
pub const fn delta_since(&self, other: Timestamp) -> TimeDelta {
216216
TimeDelta::from_micros(self.0.saturating_sub(other.0))
217217
}
218218

219219
/// Returns the [`Duration`] between `other` and `self`, or zero if `other` is not
220220
/// earlier than `self`.
221-
pub fn duration_since(&self, other: Timestamp) -> Duration {
221+
pub const fn duration_since(&self, other: Timestamp) -> Duration {
222222
Duration::from_micros(self.0.saturating_sub(other.0))
223223
}
224224

225225
/// Returns the timestamp that is `duration` later than `self`.
226-
pub fn saturating_add(&self, duration: TimeDelta) -> Timestamp {
226+
pub const fn saturating_add(&self, duration: TimeDelta) -> Timestamp {
227227
Timestamp(self.0.saturating_add(duration.0))
228228
}
229229

230230
/// Returns the timestamp that is `duration` earlier than `self`.
231-
pub fn saturating_sub(&self, duration: TimeDelta) -> Timestamp {
231+
pub const fn saturating_sub(&self, duration: TimeDelta) -> Timestamp {
232232
Timestamp(self.0.saturating_sub(duration.0))
233233
}
234234

235235
/// Returns a timestamp `micros` microseconds later than `self`, or the highest possible value
236236
/// if it would overflow.
237-
pub fn saturating_add_micros(&self, micros: u64) -> Timestamp {
237+
pub const fn saturating_add_micros(&self, micros: u64) -> Timestamp {
238238
Timestamp(self.0.saturating_add(micros))
239239
}
240240

241241
/// Returns a timestamp `micros` microseconds earlier than `self`, or the lowest possible value
242242
/// if it would underflow.
243-
pub fn saturating_sub_micros(&self, micros: u64) -> Timestamp {
243+
pub const fn saturating_sub_micros(&self, micros: u64) -> Timestamp {
244244
Timestamp(self.0.saturating_sub(micros))
245245
}
246246
}
@@ -374,7 +374,7 @@ macro_rules! impl_wrapped_number {
374374
}
375375

376376
/// Saturating addition.
377-
pub fn saturating_add(self, other: Self) -> Self {
377+
pub const fn saturating_add(self, other: Self) -> Self {
378378
let val = self.0.saturating_add(other.0);
379379
Self(val)
380380
}
@@ -395,7 +395,7 @@ macro_rules! impl_wrapped_number {
395395
}
396396

397397
/// Saturating subtraction.
398-
pub fn saturating_sub(self, other: Self) -> Self {
398+
pub const fn saturating_sub(self, other: Self) -> Self {
399399
let val = self.0.saturating_sub(other.0);
400400
Self(val)
401401
}
@@ -416,7 +416,7 @@ macro_rules! impl_wrapped_number {
416416
}
417417

418418
/// Saturating in-place addition.
419-
pub fn saturating_add_assign(&mut self, other: Self) {
419+
pub const fn saturating_add_assign(&mut self, other: Self) {
420420
self.0 = self.0.saturating_add(other.0);
421421
}
422422

@@ -430,7 +430,7 @@ macro_rules! impl_wrapped_number {
430430
}
431431

432432
/// Saturating multiplication.
433-
pub fn saturating_mul(&self, other: $wrapped) -> Self {
433+
pub const fn saturating_mul(&self, other: $wrapped) -> Self {
434434
Self(self.0.saturating_mul(other))
435435
}
436436

@@ -665,37 +665,37 @@ impl Amount {
665665
pub const ONE: Amount = Amount(10u128.pow(Amount::DECIMAL_PLACES as u32));
666666

667667
/// Returns an `Amount` corresponding to that many tokens, or `Amount::MAX` if saturated.
668-
pub fn from_tokens(tokens: u128) -> Amount {
668+
pub const fn from_tokens(tokens: u128) -> Amount {
669669
Self::ONE.saturating_mul(tokens)
670670
}
671671

672672
/// Returns an `Amount` corresponding to that many millitokens, or `Amount::MAX` if saturated.
673-
pub fn from_millis(millitokens: u128) -> Amount {
673+
pub const fn from_millis(millitokens: u128) -> Amount {
674674
Amount(10u128.pow(Amount::DECIMAL_PLACES as u32 - 3)).saturating_mul(millitokens)
675675
}
676676

677677
/// Returns an `Amount` corresponding to that many microtokens, or `Amount::MAX` if saturated.
678-
pub fn from_micros(microtokens: u128) -> Amount {
678+
pub const fn from_micros(microtokens: u128) -> Amount {
679679
Amount(10u128.pow(Amount::DECIMAL_PLACES as u32 - 6)).saturating_mul(microtokens)
680680
}
681681

682682
/// Returns an `Amount` corresponding to that many nanotokens, or `Amount::MAX` if saturated.
683-
pub fn from_nanos(nanotokens: u128) -> Amount {
683+
pub const fn from_nanos(nanotokens: u128) -> Amount {
684684
Amount(10u128.pow(Amount::DECIMAL_PLACES as u32 - 9)).saturating_mul(nanotokens)
685685
}
686686

687687
/// Returns an `Amount` corresponding to that many attotokens.
688-
pub fn from_attos(attotokens: u128) -> Amount {
688+
pub const fn from_attos(attotokens: u128) -> Amount {
689689
Amount(attotokens)
690690
}
691691

692692
/// Helper function to obtain the 64 most significant bits of the balance.
693-
pub fn upper_half(self) -> u64 {
693+
pub const fn upper_half(self) -> u64 {
694694
(self.0 >> 64) as u64
695695
}
696696

697697
/// Helper function to obtain the 64 least significant bits of the balance.
698-
pub fn lower_half(self) -> u64 {
698+
pub const fn lower_half(self) -> u64 {
699699
self.0 as u64
700700
}
701701

linera-service/tests/linera_net_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3231,7 +3231,7 @@ async fn test_end_to_end_faucet(config: impl LineraNetConfig) -> Result<()> {
32313231
Ok(())
32323232
}
32333233

3234-
/// Tests creating a new wallet using a Faucet that has already created a lot of microchains.
3234+
/// Tests creating a new wallet using a faucet that has already created a lot of microchains.
32353235
#[cfg_attr(feature = "storage-service", test_case(LocalNetConfig::new_test(Database::Service, Network::Grpc) ; "storage_test_service_grpc"))]
32363236
#[cfg_attr(feature = "scylladb", test_case(LocalNetConfig::new_test(Database::ScyllaDb, Network::Grpc) ; "scylladb_grpc"))]
32373237
#[cfg_attr(feature = "dynamodb", test_case(LocalNetConfig::new_test(Database::DynamoDb, Network::Grpc) ; "aws_grpc"))]

0 commit comments

Comments
 (0)