1
#![cfg_attr(docsrs, feature(doc_cfg))]
2
#![doc = include_str!("../README.md")]
3
// @@ begin lint list maintained by maint/add_warning @@
4
#![allow(renamed_and_removed_lints)] // @@REMOVE_WHEN(ci_arti_stable)
5
#![allow(unknown_lints)] // @@REMOVE_WHEN(ci_arti_nightly)
6
#![warn(missing_docs)]
7
#![warn(noop_method_call)]
8
#![warn(unreachable_pub)]
9
#![warn(clippy::all)]
10
#![deny(clippy::await_holding_lock)]
11
#![deny(clippy::cargo_common_metadata)]
12
#![deny(clippy::cast_lossless)]
13
#![deny(clippy::checked_conversions)]
14
#![warn(clippy::cognitive_complexity)]
15
#![deny(clippy::debug_assert_with_mut_call)]
16
#![deny(clippy::exhaustive_enums)]
17
#![deny(clippy::exhaustive_structs)]
18
#![deny(clippy::expl_impl_clone_on_copy)]
19
#![deny(clippy::fallible_impl_from)]
20
#![deny(clippy::implicit_clone)]
21
#![deny(clippy::large_stack_arrays)]
22
#![warn(clippy::manual_ok_or)]
23
#![deny(clippy::missing_docs_in_private_items)]
24
#![warn(clippy::needless_borrow)]
25
#![warn(clippy::needless_pass_by_value)]
26
#![warn(clippy::option_option)]
27
#![deny(clippy::print_stderr)]
28
#![deny(clippy::print_stdout)]
29
#![warn(clippy::rc_buffer)]
30
#![deny(clippy::ref_option_ref)]
31
#![warn(clippy::semicolon_if_nothing_returned)]
32
#![warn(clippy::trait_duplication_in_bounds)]
33
#![deny(clippy::unchecked_time_subtraction)]
34
#![deny(clippy::unnecessary_wraps)]
35
#![warn(clippy::unseparated_literal_suffix)]
36
#![deny(clippy::unwrap_used)]
37
#![deny(clippy::mod_module_files)]
38
#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
39
#![allow(clippy::uninlined_format_args)]
40
#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
41
#![allow(clippy::result_large_err)] // temporary workaround for arti#587
42
#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
43
#![allow(clippy::needless_lifetimes)] // See arti#1765
44
#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
45
#![allow(clippy::collapsible_if)] // See arti#2342
46
#![deny(clippy::unused_async)]
47
#![deny(clippy::string_slice)] // See arti#2571
48
//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
49

            
50
use derive_more::{Add, Display, Div, From, FromStr, Mul};
51

            
52
use serde::{Deserialize, Serialize};
53
use std::time::Duration;
54
use thiserror::Error;
55

            
56
#[cfg(feature = "memquota-memcost")]
57
use {derive_deftly::Deftly, tor_memquota::derive_deftly_template_HasMemoryCost};
58

            
59
/// Conversion errors from converting a value into a [`BoundedInt32`].
60
#[derive(Debug, Clone, PartialEq, Eq, Error)]
61
#[non_exhaustive]
62
pub enum Error {
63
    /// A passed value was below the lower bound for the type.
64
    #[error("Value {0} was below the lower bound {1} for this type")]
65
    BelowLowerBound(i32, i32),
66
    /// A passed value was above the upper bound for the type.
67
    #[error("Value {0} was above the lower bound {1} for this type")]
68
    AboveUpperBound(i32, i32),
69
    /// Tried to convert a negative value to an unsigned type.
70
    #[error("Tried to convert a negative value to an unsigned type")]
71
    Negative,
72
    /// Tried to parse a value that was not representable as the
73
    /// underlying type.
74
    #[error("Value could not be represented as an i32")]
75
    Unrepresentable,
76
    /// We encountered some kind of integer overflow when converting a number.
77
    #[error("Integer overflow")]
78
    Overflow,
79
}
80

            
81
/// A 32-bit signed integer with a restricted range.
82
///
83
/// This type holds an i32 value such that `LOWER` <= value <= `UPPER`
84
///
85
/// # Limitations
86
///
87
/// If you were to try to instantiate this type with LOWER > UPPER,
88
/// you would get an uninhabitable type.
89
/// Attempting to construct a value with a type with LOWER > UPPER
90
/// will result in a compile-time error;
91
/// though there may not be a compiler error if the code that constructs the value is
92
/// dead code and is optimized away.
93
/// It would be better if we could prevent such types from being named.
94
//
95
// [TODO: If you need a Bounded* for some type other than i32, ask nickm:
96
// he has an implementation kicking around.]
97
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
98
#[cfg_attr(
99
    feature = "memquota-memcost",
100
    derive(Deftly),
101
    derive_deftly(HasMemoryCost)
102
)]
103
pub struct BoundedInt32<const LOWER: i32, const UPPER: i32> {
104
    /// Interior Value
105
    value: i32,
106
}
107

            
108
impl<const LOWER: i32, const UPPER: i32> BoundedInt32<LOWER, UPPER> {
109
    /// Lower bound
110
    pub const LOWER: i32 = LOWER;
111
    /// Upper bound
112
    pub const UPPER: i32 = UPPER;
113

            
114
    /// Private constructor function for this type.
115
1840951
    fn unchecked_new(value: i32) -> Self {
116
        // If there is a code path leading to this function that remains after dead code elimination,
117
        // this will ensures LOWER <= UPPER at build time.
118
        const { assert!(LOWER <= UPPER) };
119

            
120
1840951
        BoundedInt32 { value }
121
1840951
    }
122

            
123
    /// Return the lower bound value of this bounded i32.
124
    ///
125
    /// This always return [`Self::LOWER`].
126
110
    pub const fn lower(&self) -> i32 {
127
110
        LOWER
128
110
    }
129

            
130
    /// Return the lower bound value of this bounded i32.
131
    ///
132
    /// This always return [`Self::LOWER`].
133
110
    pub const fn upper(&self) -> i32 {
134
110
        UPPER
135
110
    }
136

            
137
    /// Return the underlying i32 value.
138
    ///
139
    /// This value will always be between [`Self::LOWER`] and [`Self::UPPER`],
140
    /// inclusive.
141
48928
    pub fn get(&self) -> i32 {
142
48928
        self.value
143
48928
    }
144

            
145
    /// Return the underlying u32 value, if [`Self::LOWER`] is non-negative.
146
    ///
147
    /// If [`Self::LOWER`] is negative, this will panic at build-time.
148
    ///
149
    /// This value will always be between [`Self::LOWER`] and [`Self::UPPER`],
150
    /// inclusive.
151
550
    pub fn get_u32(&self) -> u32 {
152
        const { assert!(LOWER >= 0) };
153
550
        self.value as u32
154
550
    }
155

            
156
    /// If `val` is within range, return a new `BoundedInt32` wrapping
157
    /// it; otherwise, clamp it to the upper or lower bound as
158
    /// appropriate.
159
60
    pub fn saturating_new(val: i32) -> Self {
160
60
        Self::unchecked_new(Self::clamp(val))
161
60
    }
162

            
163
    /// If `val` is an acceptable value inside the range for this type,
164
    /// return a new [`BoundedInt32`].  Otherwise return an error.
165
1840923
    pub fn checked_new(val: i32) -> Result<Self, Error> {
166
1840923
        if val > UPPER {
167
66
            Err(Error::AboveUpperBound(val, UPPER))
168
1840857
        } else if val < LOWER {
169
4
            Err(Error::BelowLowerBound(val, LOWER))
170
        } else {
171
1840853
            Ok(BoundedInt32::unchecked_new(val))
172
        }
173
1840923
    }
174

            
175
    /// This private function clamps an input to the acceptable range.
176
98
    fn clamp(val: i32) -> i32 {
177
98
        Ord::clamp(val, LOWER, UPPER)
178
98
    }
179

            
180
    /// Convert from the underlying type, clamping to the upper or
181
    /// lower bound if needed.
182
    ///
183
    /// # Panics
184
    ///
185
    /// This function will panic if UPPER < LOWER.
186
38
    pub fn saturating_from(val: i32) -> Self {
187
38
        Self::unchecked_new(Self::clamp(val))
188
38
    }
189

            
190
    /// Convert from a string, clamping to the upper or lower bound if needed.
191
    ///
192
    /// # Limitations
193
    ///
194
    /// If the input is a number that cannot be represented as an i32,
195
    /// then we return an error instead of clamping it.
196
4
    pub fn saturating_from_str(s: &str) -> Result<Self, Error> {
197
4
        let val: i32 = s.parse().map_err(|_| Error::Unrepresentable)?;
198
4
        Ok(Self::saturating_from(val))
199
4
    }
200
}
201

            
202
impl<const L: i32, const U: i32> std::fmt::Display for BoundedInt32<L, U> {
203
2
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
204
2
        write!(f, "{}", self.value)
205
2
    }
206
}
207

            
208
impl<const L: i32, const U: i32> From<BoundedInt32<L, U>> for i32 {
209
10
    fn from(val: BoundedInt32<L, U>) -> i32 {
210
10
        val.value
211
10
    }
212
}
213

            
214
impl<const L: i32, const U: i32> From<BoundedInt32<L, U>> for f64 {
215
16438
    fn from(val: BoundedInt32<L, U>) -> f64 {
216
16438
        val.value.into()
217
16438
    }
218
}
219

            
220
impl<const L: i32, const H: i32> TryFrom<i32> for BoundedInt32<L, H> {
221
    type Error = Error;
222
1741222
    fn try_from(val: i32) -> Result<Self, Self::Error> {
223
1741222
        Self::checked_new(val)
224
1741222
    }
225
}
226

            
227
impl<const L: i32, const H: i32> std::str::FromStr for BoundedInt32<L, H> {
228
    type Err = Error;
229
20
    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
230
20
        Self::checked_new(s.parse().map_err(|_| Error::Unrepresentable)?)
231
20
    }
232
}
233

            
234
impl From<BoundedInt32<0, 1>> for bool {
235
1261540
    fn from(val: BoundedInt32<0, 1>) -> bool {
236
1261540
        val.value == 1
237
1261540
    }
238
}
239

            
240
impl From<BoundedInt32<0, 255>> for u8 {
241
8
    fn from(val: BoundedInt32<0, 255>) -> u8 {
242
8
        val.value as u8
243
8
    }
244
}
245

            
246
impl<const L: i32, const H: i32> From<BoundedInt32<L, H>> for u32 {
247
20896
    fn from(val: BoundedInt32<L, H>) -> u32 {
248
20896
        val.value as u32
249
20896
    }
250
}
251

            
252
impl<const L: i32, const H: i32> TryFrom<BoundedInt32<L, H>> for u64 {
253
    type Error = Error;
254
44464
    fn try_from(val: BoundedInt32<L, H>) -> Result<Self, Self::Error> {
255
44464
        if val.value < 0 {
256
2
            Err(Error::Negative)
257
        } else {
258
44462
            Ok(val.value as u64)
259
        }
260
44464
    }
261
}
262

            
263
impl<const L: i32, const H: i32> TryFrom<BoundedInt32<L, H>> for usize {
264
    type Error = Error;
265
15515
    fn try_from(val: BoundedInt32<L, H>) -> Result<Self, Self::Error> {
266
15515
        if val.value < 0 {
267
2
            Err(Error::Negative)
268
        } else {
269
15513
            Ok(val.value as usize)
270
        }
271
15515
    }
272
}
273

            
274
/// A percentage value represented as a number.
275
///
276
/// This type wraps an underlying numeric type, and ensures that callers
277
/// are clear whether they want a _fraction_, or a _percentage_.
278
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
279
pub struct Percentage<T: Copy + Into<f64>> {
280
    /// The underlying percentage value.
281
    value: T,
282
}
283

            
284
impl<T: Copy + Into<f64>> Percentage<T> {
285
    /// Create a new `IntPercentage` from the underlying percentage.
286
194724
    pub fn new(value: T) -> Self {
287
194724
        Self { value }
288
194724
    }
289

            
290
    /// Return this value as a (possibly improper) fraction.
291
    ///
292
    /// ```
293
    /// use tor_units::Percentage;
294
    /// let pct_200 = Percentage::<u8>::new(200);
295
    /// let pct_100 = Percentage::<u8>::new(100);
296
    /// let pct_50 = Percentage::<u8>::new(50);
297
    ///
298
    /// assert_eq!(pct_200.as_fraction(), 2.0);
299
    /// assert_eq!(pct_100.as_fraction(), 1.0);
300
    /// assert_eq!(pct_50.as_fraction(), 0.5);
301
    /// // Note: don't actually compare f64 with ==.
302
    /// ```
303
16442
    pub fn as_fraction(self) -> f64 {
304
16442
        self.value.into() / 100.0
305
16442
    }
306

            
307
    /// Return this value as a percentage.
308
    ///
309
    /// ```
310
    /// use tor_units::Percentage;
311
    /// let pct_200 = Percentage::<u8>::new(200);
312
    /// let pct_100 = Percentage::<u8>::new(100);
313
    /// let pct_50 = Percentage::<u8>::new(50);
314
    ///
315
    /// assert_eq!(pct_200.as_percent(), 200);
316
    /// assert_eq!(pct_100.as_percent(), 100);
317
    /// assert_eq!(pct_50.as_percent(), 50);
318
    /// ```
319
762
    pub fn as_percent(self) -> T {
320
762
        self.value
321
762
    }
322
}
323

            
324
impl<const H: i32, const L: i32> TryFrom<i32> for Percentage<BoundedInt32<H, L>> {
325
    type Error = Error;
326
191534
    fn try_from(v: i32) -> Result<Self, Error> {
327
191534
        Ok(Percentage::new(v.try_into()?))
328
191534
    }
329
}
330

            
331
// TODO: There is a bunch of code duplication among these "IntegerTimeUnits"
332
// section.
333

            
334
#[derive(
335
    Add, Copy, Clone, Mul, Div, From, FromStr, Display, Debug, PartialEq, Eq, Ord, PartialOrd, Hash,
336
)]
337
/// This type represents an integer number of milliseconds.
338
///
339
/// The underlying type should usually implement `TryInto<u64>`.
340
pub struct IntegerMilliseconds<T> {
341
    /// Interior Value. Should implement `TryInto<u64>` to be useful.
342
    value: T,
343
}
344

            
345
impl<T> IntegerMilliseconds<T> {
346
    /// Public Constructor
347
128796
    pub fn new(value: T) -> Self {
348
128796
        IntegerMilliseconds { value }
349
128796
    }
350

            
351
    /// Deconstructor
352
    ///
353
    /// Use only in contexts where it's no longer possible to
354
    /// use the Rust type system to ensure secs vs ms vs us correctness.
355
10180
    pub fn as_millis(self) -> T {
356
10180
        self.value
357
10180
    }
358

            
359
    /// Map the inner value (useful for conversion)
360
    ///
361
    /// # Example
362
    ///
363
    /// ```
364
    /// use tor_units::{BoundedInt32, IntegerMilliseconds};
365
    ///
366
    /// let value: IntegerMilliseconds<i32> = 42.into();
367
    /// let value: IntegerMilliseconds<BoundedInt32<0,1000>>
368
    ///     = value.try_map(TryInto::try_into).unwrap();
369
    /// ```
370
3980
    pub fn try_map<U, F, E>(self, f: F) -> Result<IntegerMilliseconds<U>, E>
371
3980
    where
372
3980
        F: FnOnce(T) -> Result<U, E>,
373
    {
374
3980
        Ok(IntegerMilliseconds::new(f(self.value)?))
375
3980
    }
376
}
377

            
378
impl<T: TryInto<u64>> TryFrom<IntegerMilliseconds<T>> for Duration {
379
    type Error = <T as TryInto<u64>>::Error;
380
32
    fn try_from(val: IntegerMilliseconds<T>) -> Result<Self, <T as TryInto<u64>>::Error> {
381
32
        Ok(Self::from_millis(val.value.try_into()?))
382
32
    }
383
}
384

            
385
impl<const H: i32, const L: i32> TryFrom<i32> for IntegerMilliseconds<BoundedInt32<H, L>> {
386
    type Error = Error;
387
121888
    fn try_from(v: i32) -> Result<Self, Error> {
388
121888
        Ok(IntegerMilliseconds::new(v.try_into()?))
389
121888
    }
390
}
391

            
392
#[derive(
393
    Add, Copy, Clone, Mul, Div, From, FromStr, Display, Debug, PartialEq, Eq, Ord, PartialOrd, Hash,
394
)]
395
/// This type represents an integer number of seconds.
396
///
397
/// The underlying type should usually implement `TryInto<u64>`.
398
pub struct IntegerSeconds<T> {
399
    /// Interior Value. Should implement `TryInto<u64>` to be useful.
400
    value: T,
401
}
402

            
403
impl<T> IntegerSeconds<T> {
404
    /// Public Constructor
405
226474
    pub fn new(value: T) -> Self {
406
226474
        IntegerSeconds { value }
407
226474
    }
408

            
409
    /// Deconstructor
410
    ///
411
    /// Use only in contexts where it's no longer possible to
412
    /// use the Rust type system to ensure secs vs ms vs us correctness.
413
    pub fn as_secs(self) -> T {
414
        self.value
415
    }
416

            
417
    /// Map the inner value (useful for conversion)
418
    ///
419
    /// ```
420
    /// use tor_units::{BoundedInt32, IntegerSeconds};
421
    ///
422
    /// let value: IntegerSeconds<i32> = 42.into();
423
    /// let value: IntegerSeconds<BoundedInt32<0,1000>>
424
    ///     = value.try_map(TryInto::try_into).unwrap();
425
    /// ```
426
    pub fn try_map<U, F, E>(self, f: F) -> Result<IntegerSeconds<U>, E>
427
    where
428
        F: FnOnce(T) -> Result<U, E>,
429
    {
430
        Ok(IntegerSeconds::new(f(self.value)?))
431
    }
432
}
433

            
434
impl<T: TryInto<u64>> TryFrom<IntegerSeconds<T>> for Duration {
435
    type Error = <T as TryInto<u64>>::Error;
436
17810
    fn try_from(val: IntegerSeconds<T>) -> Result<Self, <T as TryInto<u64>>::Error> {
437
17810
        Ok(Self::from_secs(val.value.try_into()?))
438
17810
    }
439
}
440

            
441
impl<const H: i32, const L: i32> TryFrom<i32> for IntegerSeconds<BoundedInt32<H, L>> {
442
    type Error = Error;
443
226360
    fn try_from(v: i32) -> Result<Self, Error> {
444
226360
        Ok(IntegerSeconds::new(v.try_into()?))
445
226360
    }
446
}
447

            
448
#[derive(Deserialize, Serialize)] //
449
#[derive(Copy, Clone, From, FromStr, Display, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
450
/// This type represents an integer number of minutes.
451
///
452
/// The underlying type should usually implement `TryInto<u64>`.
453
pub struct IntegerMinutes<T> {
454
    /// Interior Value. Should Implement `TryInto<u64>` to be useful.
455
    value: T,
456
}
457

            
458
impl<T> IntegerMinutes<T> {
459
    /// Public Constructor
460
47533
    pub fn new(value: T) -> Self {
461
47533
        IntegerMinutes { value }
462
47533
    }
463

            
464
    /// Deconstructor
465
    ///
466
    /// Use only in contexts where it's no longer possible to
467
    /// use the Rust type system to ensure secs vs ms vs us correctness.
468
289416
    pub fn as_minutes(self) -> T {
469
289416
        self.value
470
289416
    }
471

            
472
    /// Map the inner value (useful for conversion)
473
    ///
474
    /// ```
475
    /// use tor_units::{BoundedInt32, IntegerMinutes};
476
    ///
477
    /// let value: IntegerMinutes<i32> = 42.into();
478
    /// let value: IntegerMinutes<BoundedInt32<0,1000>>
479
    ///     = value.try_map(TryInto::try_into).unwrap();
480
    /// ```
481
    pub fn try_map<U, F, E>(self, f: F) -> Result<IntegerMinutes<U>, E>
482
    where
483
        F: FnOnce(T) -> Result<U, E>,
484
    {
485
        Ok(IntegerMinutes::new(f(self.value)?))
486
    }
487
}
488

            
489
impl<T: TryInto<u64>> TryFrom<IntegerMinutes<T>> for Duration {
490
    type Error = Error;
491
20780
    fn try_from(val: IntegerMinutes<T>) -> Result<Self, Error> {
492
        /// Number of seconds in a single minute.
493
        const SECONDS_PER_MINUTE: u64 = 60;
494
20780
        let minutes: u64 = val.value.try_into().map_err(|_| Error::Overflow)?;
495
20778
        let seconds = minutes
496
20778
            .checked_mul(SECONDS_PER_MINUTE)
497
20778
            .ok_or(Error::Overflow)?;
498
20776
        Ok(Self::from_secs(seconds))
499
20780
    }
500
}
501

            
502
impl<const H: i32, const L: i32> TryFrom<i32> for IntegerMinutes<BoundedInt32<H, L>> {
503
    type Error = Error;
504
17414
    fn try_from(v: i32) -> Result<Self, Error> {
505
17414
        Ok(IntegerMinutes::new(v.try_into()?))
506
17414
    }
507
}
508

            
509
#[derive(Copy, Clone, From, FromStr, Display, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
510
/// This type represents an integer number of days.
511
///
512
/// The underlying type should usually implement `TryInto<u64>`.
513
pub struct IntegerDays<T> {
514
    /// Interior Value. Should Implement `TryInto<u64>` to be useful.
515
    value: T,
516
}
517

            
518
impl<T> IntegerDays<T> {
519
    /// Public Constructor
520
52252
    pub fn new(value: T) -> Self {
521
52252
        IntegerDays { value }
522
52252
    }
523

            
524
    /// Deconstructor
525
    ///
526
    /// Use only in contexts where it's no longer possible to
527
    /// use the Rust type system to ensure secs vs ms vs us correctness.
528
    pub fn as_days(self) -> T {
529
        self.value
530
    }
531

            
532
    /// Map the inner value (useful for conversion)
533
    ///
534
    /// ```
535
    /// use tor_units::{BoundedInt32, IntegerDays};
536
    ///
537
    /// let value: IntegerDays<i32> = 42.into();
538
    /// let value: IntegerDays<BoundedInt32<0,1000>>
539
    ///     = value.try_map(TryInto::try_into).unwrap();
540
    /// ```
541
    pub fn try_map<U, F, E>(self, f: F) -> Result<IntegerDays<U>, E>
542
    where
543
        F: FnOnce(T) -> Result<U, E>,
544
    {
545
        Ok(IntegerDays::new(f(self.value)?))
546
    }
547
}
548

            
549
impl<T: TryInto<u64>> TryFrom<IntegerDays<T>> for Duration {
550
    type Error = Error;
551
5868
    fn try_from(val: IntegerDays<T>) -> Result<Self, Error> {
552
        /// Number of seconds in a single day.
553
        const SECONDS_PER_DAY: u64 = 86400;
554
5868
        let days: u64 = val.value.try_into().map_err(|_| Error::Overflow)?;
555
5866
        let seconds = days.checked_mul(SECONDS_PER_DAY).ok_or(Error::Overflow)?;
556
5864
        Ok(Self::from_secs(seconds))
557
5868
    }
558
}
559

            
560
impl<const H: i32, const L: i32> TryFrom<i32> for IntegerDays<BoundedInt32<H, L>> {
561
    type Error = Error;
562
52238
    fn try_from(v: i32) -> Result<Self, Error> {
563
52238
        Ok(IntegerDays::new(v.try_into()?))
564
52238
    }
565
}
566

            
567
/// A SendMe Version
568
///
569
/// DOCDOC: Explain why this needs to have its own type, or remove it.
570
#[derive(Clone, Copy, From, FromStr, Display, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
571
pub struct SendMeVersion(u8);
572

            
573
impl SendMeVersion {
574
    /// Public Constructor
575
38815
    pub fn new(value: u8) -> Self {
576
38815
        SendMeVersion(value)
577
38815
    }
578

            
579
    /// Helper
580
259
    pub fn get(&self) -> u8 {
581
259
        self.0
582
259
    }
583
}
584

            
585
impl TryFrom<i32> for SendMeVersion {
586
    type Error = Error;
587
38558
    fn try_from(v: i32) -> Result<Self, Error> {
588
38558
        let val_u8 = BoundedInt32::<0, 255>::checked_new(v)?;
589
38558
        Ok(SendMeVersion::new(val_u8.get() as u8))
590
38558
    }
591
}
592

            
593
/// Tests that check whether some code fails to compile as intended.
594
// Unfortunately we can't check the reason that it fails to compile,
595
// so these tests could become stale if the API is changed.
596
// In the future, we may be able to use the (currently nightly):
597
// https://doc.rust-lang.org/rustdoc/unstable-features.html?highlight=compile_fail#error-numbers-for-compile-fail-doctests
598
#[cfg(doc)]
599
#[doc(hidden)]
600
mod compile_fail_tests {
601
    /// ```compile_fail
602
    /// use tor_units::BoundedInt32;
603
    /// let _: BoundedInt32<10, 5> = BoundedInt32::saturating_new(7);
604
    /// ```
605
    fn uninhabited_saturating_new() {}
606

            
607
    /// ```compile_fail
608
    /// use tor_units::BoundedInt32;
609
    /// let _: Result<BoundedInt32<10, 5>, Error> = BoundedInt32::saturating_from_str("7");
610
    /// ```
611
    fn uninhabited_from_string() {}
612
}
613

            
614
#[cfg(test)]
615
mod tests {
616
    #![allow(clippy::unwrap_used)]
617
    use float_cmp::assert_approx_eq;
618

            
619
    use super::*;
620

            
621
    type TestFoo = BoundedInt32<1, 5>;
622
    type TestBar = BoundedInt32<-45, 17>;
623

            
624
    //make_parameter_type! {TestFoo(3,)}
625
    #[test]
626
    fn entire_range_parsed() {
627
        let x: TestFoo = "1".parse().unwrap();
628
        assert!(x.get() == 1);
629
        let x: TestFoo = "2".parse().unwrap();
630
        assert!(x.get() == 2);
631
        let x: TestFoo = "3".parse().unwrap();
632
        assert!(x.get() == 3);
633
        let x: TestFoo = "4".parse().unwrap();
634
        assert!(x.get() == 4);
635
        let x: TestFoo = "5".parse().unwrap();
636
        assert!(x.get() == 5);
637
    }
638

            
639
    #[test]
640
    fn saturating() {
641
        let x: TestFoo = TestFoo::saturating_new(1000);
642
        let x_val: i32 = x.into();
643
        assert!(x_val == TestFoo::UPPER);
644
        let x: TestFoo = TestFoo::saturating_new(0);
645
        let x_val: i32 = x.into();
646
        assert!(x_val == TestFoo::LOWER);
647
    }
648
    #[test]
649
    fn saturating_string() {
650
        let x: TestFoo = TestFoo::saturating_from_str("1000").unwrap();
651
        let x_val: i32 = x.into();
652
        assert!(x_val == TestFoo::UPPER);
653
        let x: TestFoo = TestFoo::saturating_from_str("0").unwrap();
654
        let x_val: i32 = x.into();
655
        assert!(x_val == TestFoo::LOWER);
656
    }
657

            
658
    #[test]
659
    fn errors_correct() {
660
        let x: Result<TestBar, Error> = "1000".parse();
661
        assert!(x.unwrap_err() == Error::AboveUpperBound(1000, TestBar::UPPER));
662
        let x: Result<TestBar, Error> = "-1000".parse();
663
        assert!(x.unwrap_err() == Error::BelowLowerBound(-1000, TestBar::LOWER));
664
        let x: Result<TestBar, Error> = "xyz".parse();
665
        assert!(x.unwrap_err() == Error::Unrepresentable);
666
    }
667

            
668
    #[test]
669
    fn display() {
670
        let v = BoundedInt32::<99, 1000>::checked_new(345).unwrap();
671
        assert_eq!(v.to_string(), "345".to_string());
672
    }
673

            
674
    #[test]
675
    #[should_panic]
676
    fn checked_too_high() {
677
        let _: TestBar = "1000".parse().unwrap();
678
    }
679

            
680
    #[test]
681
    #[should_panic]
682
    fn checked_too_low() {
683
        let _: TestBar = "-46".parse().unwrap();
684
    }
685

            
686
    #[test]
687
    fn bounded_to_u64() {
688
        let b: BoundedInt32<-100, 100> = BoundedInt32::checked_new(77).unwrap();
689
        let u: u64 = b.try_into().unwrap();
690
        assert_eq!(u, 77);
691

            
692
        let b: BoundedInt32<-100, 100> = BoundedInt32::checked_new(-77).unwrap();
693
        let u: Result<u64, Error> = b.try_into();
694
        assert!(u.is_err());
695
    }
696

            
697
    #[test]
698
    fn bounded_to_f64() {
699
        let x: BoundedInt32<-100, 100> = BoundedInt32::checked_new(77).unwrap();
700
        let f: f64 = x.into();
701
        assert_approx_eq!(f64, f, 77.0);
702
    }
703

            
704
    #[test]
705
    fn bounded_from_i32() {
706
        let x: Result<BoundedInt32<-100, 100>, _> = 50.try_into();
707
        let y: i32 = x.unwrap().into();
708
        assert_eq!(y, 50);
709

            
710
        let x: Result<BoundedInt32<-100, 100>, _> = 1000.try_into();
711
        assert!(x.is_err());
712
    }
713

            
714
    #[test]
715
    fn into_bool() {
716
        let zero: BoundedInt32<0, 1> = BoundedInt32::saturating_from(0);
717
        let one: BoundedInt32<0, 1> = BoundedInt32::saturating_from(1);
718

            
719
        let f: bool = zero.into();
720
        let t: bool = one.into();
721
        assert!(!f);
722
        assert!(t);
723
    }
724

            
725
    #[test]
726
    fn into_u8() {
727
        let zero: BoundedInt32<0, 255> = BoundedInt32::saturating_from(0);
728
        let one: BoundedInt32<0, 255> = BoundedInt32::saturating_from(1);
729
        let ninety: BoundedInt32<0, 255> = BoundedInt32::saturating_from(90);
730
        let max: BoundedInt32<0, 255> = BoundedInt32::saturating_from(1000);
731

            
732
        let a: u8 = zero.into();
733
        let b: u8 = one.into();
734
        let c: u8 = ninety.into();
735
        let d: u8 = max.into();
736

            
737
        assert_eq!(a, 0);
738
        assert_eq!(b, 1);
739
        assert_eq!(c, 90);
740
        assert_eq!(d, 255);
741
    }
742

            
743
    #[test]
744
    fn into_u32() {
745
        let zero: BoundedInt32<0, 1000> = BoundedInt32::saturating_from(0);
746
        let one: BoundedInt32<0, 1000> = BoundedInt32::saturating_from(1);
747
        let ninety: BoundedInt32<0, 1000> = BoundedInt32::saturating_from(90);
748
        let max: BoundedInt32<0, 1000> = BoundedInt32::saturating_from(1000);
749

            
750
        assert_eq!(u32::from(zero), 0);
751
        assert_eq!(u32::from(one), 1);
752
        assert_eq!(u32::from(ninety), 90);
753
        assert_eq!(u32::from(max), 1000);
754

            
755
        let zero: BoundedInt32<1, 1000> = BoundedInt32::saturating_from(0);
756
        let one: BoundedInt32<1, 1000> = BoundedInt32::saturating_from(1);
757
        let ninety: BoundedInt32<1, 1000> = BoundedInt32::saturating_from(90);
758
        let max: BoundedInt32<1, 1000> = BoundedInt32::saturating_from(1000);
759

            
760
        assert_eq!(u32::from(zero), 1);
761
        assert_eq!(u32::from(one), 1);
762
        assert_eq!(u32::from(ninety), 90);
763
        assert_eq!(u32::from(max), 1000);
764
    }
765

            
766
    #[test]
767
    fn try_into_usize() {
768
        let b0: BoundedInt32<-10, 300> = BoundedInt32::saturating_from(0);
769
        let b100: BoundedInt32<-10, 300> = BoundedInt32::saturating_from(100);
770
        let bn5: BoundedInt32<-10, 300> = BoundedInt32::saturating_from(-5);
771
        assert_eq!(usize::try_from(b0), Ok(0_usize));
772
        assert_eq!(usize::try_from(b100), Ok(100_usize));
773
        assert_eq!(usize::try_from(bn5), Err(Error::Negative));
774
    }
775

            
776
    #[test]
777
    fn percents() {
778
        type Pct = Percentage<u8>;
779
        let p = Pct::new(100);
780
        assert_eq!(p.as_percent(), 100);
781
        assert_approx_eq!(f64, p.as_fraction(), 1.0);
782

            
783
        let p = Pct::new(0);
784
        assert_eq!(p.as_percent(), 0);
785
        assert_approx_eq!(f64, p.as_fraction(), 0.0);
786

            
787
        let p = Pct::new(25);
788
        assert_eq!(p.as_percent(), 25);
789
        assert_eq!(p.clone(), p);
790
        assert_approx_eq!(f64, p.as_fraction(), 0.25);
791

            
792
        type BPct = Percentage<BoundedInt32<0, 100>>;
793
        assert_eq!(BPct::try_from(99).unwrap().as_percent().get(), 99);
794
    }
795

            
796
    #[test]
797
    fn milliseconds() {
798
        type Msec = IntegerMilliseconds<i32>;
799

            
800
        let ms = Msec::new(500);
801
        let d: Result<Duration, _> = ms.try_into();
802
        assert_eq!(d.unwrap(), Duration::from_millis(500));
803
        assert_eq!(Duration::try_from(ms * 2).unwrap(), Duration::from_secs(1));
804

            
805
        let ms = Msec::new(-100);
806
        let d: Result<Duration, _> = ms.try_into();
807
        assert!(d.is_err());
808

            
809
        type BMSec = IntegerMilliseconds<BoundedInt32<0, 1000>>;
810
        let half_sec = BMSec::try_from(500).unwrap();
811
        assert_eq!(
812
            Duration::try_from(half_sec).unwrap(),
813
            Duration::from_millis(500)
814
        );
815
        assert!(BMSec::try_from(1001).is_err());
816
    }
817

            
818
    #[test]
819
    fn seconds() {
820
        type Sec = IntegerSeconds<i32>;
821

            
822
        let ms = Sec::new(500);
823
        let d: Result<Duration, _> = ms.try_into();
824
        assert_eq!(d.unwrap(), Duration::from_secs(500));
825

            
826
        let ms = Sec::new(-100);
827
        let d: Result<Duration, _> = ms.try_into();
828
        assert!(d.is_err());
829

            
830
        type BSec = IntegerSeconds<BoundedInt32<0, 3600>>;
831
        let half_hour = BSec::try_from(1800).unwrap();
832
        assert_eq!(
833
            Duration::try_from(half_hour).unwrap(),
834
            Duration::from_secs(1800)
835
        );
836
        assert!(BSec::try_from(9999).is_err());
837
        assert_eq!(half_hour.clone(), half_hour);
838
    }
839

            
840
    #[test]
841
    fn minutes() {
842
        type Min = IntegerMinutes<i32>;
843

            
844
        let t = Min::new(500);
845
        let d: Duration = t.try_into().unwrap();
846
        assert_eq!(d, Duration::from_secs(500 * 60));
847

            
848
        let t = Min::new(-100);
849
        let d: Result<Duration, _> = t.try_into();
850
        assert_eq!(d, Err(Error::Overflow));
851

            
852
        let t = IntegerMinutes::<u64>::new(u64::MAX);
853
        let d: Result<Duration, _> = t.try_into();
854
        assert_eq!(d, Err(Error::Overflow));
855

            
856
        type BMin = IntegerMinutes<BoundedInt32<10, 30>>;
857
        assert_eq!(
858
            BMin::new(17_i32.try_into().unwrap()),
859
            BMin::try_from(17).unwrap()
860
        );
861
    }
862

            
863
    #[test]
864
    fn days() {
865
        type Days = IntegerDays<i32>;
866

            
867
        let t = Days::new(500);
868
        let d: Duration = t.try_into().unwrap();
869
        assert_eq!(d, Duration::from_secs(500 * 86400));
870

            
871
        let t = Days::new(-100);
872
        let d: Result<Duration, _> = t.try_into();
873
        assert_eq!(d, Err(Error::Overflow));
874

            
875
        let t = IntegerDays::<u64>::new(u64::MAX);
876
        let d: Result<Duration, _> = t.try_into();
877
        assert_eq!(d, Err(Error::Overflow));
878

            
879
        type BDays = IntegerDays<BoundedInt32<10, 30>>;
880
        assert_eq!(
881
            BDays::new(17_i32.try_into().unwrap()),
882
            BDays::try_from(17).unwrap()
883
        );
884
    }
885

            
886
    #[test]
887
    fn sendme() {
888
        let smv = SendMeVersion::new(5);
889
        assert_eq!(smv.get(), 5);
890
        assert_eq!(smv.clone().get(), 5);
891
        assert_eq!(smv, SendMeVersion::try_from(5).unwrap());
892
    }
893
}