easy-cast 0.7.0

Type conversions which are expected to succeed
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
//     https://www.apache.org/licenses/LICENSE-2.0

//! Traits
//!
//! This module only contains traits, allowing relatively safe glob-import:
//! ```
//! use easy_cast::{Nearest, traits::*};
//!
//! fn nth_power<X: CastApprox<f64>>(x: X, n: u32) {
//!     let x = x.cast_approx();    // Into-like approximate conversion
//!
//!     let power = i32::conv(n);  // From-like exact conversion
//!     let z = x.powi(power);
//!     println!("The {n}-th power of {x} is {z}");
//!
//!     // TryFrom-like approximate (nearest) conversion
//!     if let Ok(nearest) = isize::try_conv_to(Nearest, z) {
//!         println!("Nearest integer: {nearest}");
//!     }
//! }
//! ```
//!

use crate::{Approx, Error, Exact, RangeError, Rounding};
#[allow(unused)]
use core::convert::Infallible;

/// Generic "from" conversion trait for exact conversions
///
/// This trait is provided as an implementation aid only, hence there is no
/// `CastExact` (in most cases you can just use [`Cast`]).
///
/// ## Implementing exact conversions
///
/// Implement conversions which cannot lose precision using this trait.
/// Implementations of <code>[ConvTo]&lt;S, R&gt;</code> are implied for all
/// <code>R: [Rounding]</code> modes provided by this crate (see
/// [§ Implied implementations](Rounding#implied-implementations)).
///
/// ### Example
///
/// ```
/// use easy_cast::ConvExact;
/// use std::convert::Infallible;
///
/// struct MyBigInt { /* details */ }
///
/// // Support conversion from i32:
/// impl ConvExact<i32> for MyBigInt {
///     type Error = Infallible;
///
///     fn try_conv_exact(i: i32) -> Result<Self, Infallible> {
///         Ok(todo!())
///     }
///
///     // optionally also impl fn conv_exact
/// }
/// ```
///
/// Note that in practice you'll probably want to support conversion from many
/// integer types using `macro_rules!`. Or you could "cheat" with a generic
/// `impl<S: Into<i128>> ConvExact<S> for MyBigInt { ... }`.
//
// TODO(specialization): impl<T> ConvExact<T> for T
pub trait ConvExact<S>: Sized {
    /// Conversion error type
    ///
    /// This should be either [`Infallible`] or [`RangeError`].
    type Error: Into<RangeError> + Into<crate::Error> + core::error::Error;

    /// Try converting from `S` to `Self`
    fn try_conv_exact(s: S) -> Result<Self, Self::Error>;

    /// Convert from `S` to `Self`
    ///
    /// Use this method only when success is expected. On error, this method may
    /// panic or may exhibit [§ Fallback behaviour](crate#fallback-behaviour).
    ///
    /// # Implementing
    ///
    /// Implementing this method directly (with fallback behaviour) is optional.
    /// In debug builds, this method must panic on error.
    #[inline]
    fn conv_exact(s: S) -> Self {
        Self::try_conv_exact(s).unwrap_or_else(|e| {
            panic!("ConvExact::conv_exact(_) failed: {}", e);
        })
    }
}

/// Like [`From`], but supports fallible conversions
///
/// This trait has similarities to [`From`] and [`TryFrom`], but is limited to
/// numeric conversions:
/// -   Conversions may be *fallible*, like [`TryFrom`].
/// -   Conversions must be *lossless*, like [`From`]; this corresponds to the
///     [`Exact`] "rounding" mode. (See also [`ConvApprox`].)
/// -   Conversions must be *value-preserving*, like [`From`]. For example,
///     `-1_i8` and `-1_i32` are conceptually the same value while `255_u8` is
///     conceptually a different value.
///
/// The sister-trait [`Cast`] supports "into" style usage.
///
/// This trait should not be implemented directly; instead implement either
/// [`ConvExact`] or [`ConvTo`].
pub trait Conv<S>: Sized {
    /// Conversion error type
    ///
    /// This should be one of [`Infallible`], [`RangeError`] or [`Error`].
    type Error: Into<Error> + core::error::Error;

    /// Try converting from `S` to `Self`
    fn try_conv(s: S) -> Result<Self, Self::Error>;

    /// Convert from `S` to `Self`
    ///
    /// Use this method only when success is expected. On error, this method may
    /// panic or may exhibit [§ Fallback behaviour](crate#fallback-behaviour).
    fn conv(s: S) -> Self {
        Self::try_conv(s).unwrap_or_else(|e| {
            panic!("Conv::conv(_) failed: {}", e);
        })
    }
}

impl<S, T: ConvTo<S, Exact>> Conv<S> for T {
    type Error = T::Error;

    #[inline]
    fn try_conv(s: S) -> Result<Self, Self::Error> {
        T::try_conv_to(Exact, s)
    }

    #[inline]
    fn conv(s: S) -> Self {
        T::conv_to(Exact, s)
    }
}

/// Like [`Into`], but for [`Conv`]
///
/// This trait has similarities to [`Into`] and [`TryInto`], but limited to
/// numeric conversions:
/// -   Conversions may be *fallible*, like [`TryInto`].
/// -   Conversions must be *lossless*, like [`Into`]; this corresponds to the
///     [`Exact`] "rounding" mode. (See also [`CastApprox`].)
/// -   Conversions must be *value-preserving*, like [`Into`]. For example,
///     `-1_i8` and `-1_i32` are conceptually the same value while `255_u8` is
///     conceptually a different value.
///
/// This trait is automatically implemented for every implementation of
/// [`Conv`].
pub trait Cast<T> {
    /// Conversion error type
    ///
    /// This should be one of [`Infallible`], [`RangeError`] or [`Error`].
    type Error: Into<Error> + core::error::Error;

    /// Try converting from `Self` to `T`
    fn try_cast(self) -> Result<T, Self::Error>;

    /// Cast from `Self` to `T`
    ///
    /// Use this method only when success is expected. On error, this method may
    /// panic or may exhibit [§ Fallback behaviour](crate#fallback-behaviour).
    fn cast(self) -> T;
}

impl<S, T: Conv<S>> Cast<T> for S {
    type Error = T::Error;

    #[inline]
    fn cast(self) -> T {
        T::conv(self)
    }
    #[inline]
    fn try_cast(self) -> Result<T, Self::Error> {
        T::try_conv(self)
    }
}

/// Like [`From`], but for approximate numerical conversions
///
/// This trait supports [`From`]- and [`TryFrom`]-like conversions, but allowing
/// approximation:
/// -   Conversions may be *fallible*, like [`TryFrom`].
/// -   Conversions may be *lossy*, provided that the result is close to the
///     input value (see
///     [`§ Limits of approximation`](Approx#limits-of-approximation)).
/// -   Conversions must be *value-preserving*, like [`From`]. For example,
///     `-1_i8` and `-1_i32` are conceptually the same value while `255_u8` is
///     conceptually a different value.
///
/// The rounding mode used is implementation-defined. Conversions provided by
/// this crate use the same behaviour as [`as` numeric casts]: float-to-int
/// conversions round towards zero while conversions to floating-point formats
/// produce the closest possible float (rounding ties to even).
/// Use [`ConvTo`] where specific rounding is required.
///
/// The sister-trait [`CastApprox`] supports "into" style usage.
///
/// This trait should not be implemented directly; instead implement [`ConvTo`]
/// using the [`Approx`] rounding mode.
///
/// [`as` numeric casts]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#r-expr.as.numeric
pub trait ConvApprox<S>: Sized {
    /// Conversion error type
    ///
    /// This should be either [`Infallible`] or [`RangeError`].
    type Error: Into<RangeError> + core::error::Error;

    /// Try converting from `S` to `Self`, allowing approximation
    fn try_conv_approx(s: S) -> Result<Self, Self::Error>;

    /// Convert from `S` to `Self`, allowing approximation
    ///
    /// Use this method only when success is expected. On error, this method may
    /// panic or may exhibit [§ Fallback behaviour](crate#fallback-behaviour).
    #[inline]
    fn conv_approx(s: S) -> Self {
        Self::try_conv_approx(s).unwrap_or_else(|e| {
            panic!("ConvApprox::conv_approx(_) failed: {}", e);
        })
    }
}

impl<S, T: ConvTo<S, Approx>> ConvApprox<S> for T {
    type Error = T::Error;

    #[inline]
    fn try_conv_approx(s: S) -> Result<Self, Self::Error> {
        T::try_conv_to(Approx, s)
    }

    #[inline]
    fn conv_approx(s: S) -> Self {
        T::conv_to(Approx, s)
    }
}

/// Like [`Into`], but for [`ConvApprox`]
///
/// This trait supports [`Into`]- and [`TryInto`]-like conversions, but allowing
/// approximation:
/// -   Conversions may be *fallible*, like [`TryInto`].
/// -   Conversions may be *lossy*, provided that the result is close to the
///     input value (see
///     [`§ Limits of approximation`](Approx#limits-of-approximation)).
/// -   Conversions must be *value-preserving*, like [`Into`]. For example,
///     `-1_i8` and `-1_i32` are conceptually the same value while `255_u8` is
///     conceptually a different value.
///
/// The rounding mode used is implementation-defined. Conversions provided by
/// this crate use the same behaviour as [`as` numeric casts]: float-to-int
/// conversions round towards zero while conversions to floating-point formats
/// produce the closest possible float (rounding ties to even).
/// Use [`CastTo`] where specific rounding is required.
///
/// This trait is automatically implemented for every implementation of
/// [`ConvApprox`].
///
/// [`as` numeric casts]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#r-expr.as.numeric
pub trait CastApprox<T> {
    /// Conversion error type
    ///
    /// This should be either [`Infallible`] or [`RangeError`].
    type Error: Into<RangeError> + core::error::Error;

    /// Try approximate conversion from `Self` to `T`
    fn try_cast_approx(self) -> Result<T, Self::Error>;

    /// Cast approximately from `Self` to `T`
    ///
    /// Use this method only when success is expected. On error, this method may
    /// panic or may exhibit [§ Fallback behaviour](crate#fallback-behaviour).
    fn cast_approx(self) -> T;
}

impl<S, T: ConvApprox<S>> CastApprox<T> for S {
    type Error = T::Error;

    #[inline]
    fn try_cast_approx(self) -> Result<T, Self::Error> {
        T::try_conv_approx(self)
    }
    #[inline]
    fn cast_approx(self) -> T {
        T::conv_approx(self)
    }
}

/// Generic "from" conversion trait with specified rounding mode
///
/// This trait supports [`From`]- and [`TryFrom`]-like conversions, but with a
/// specified rounding mode:
/// -   Conversions may be *fallible*, like [`TryFrom`].
/// -   Conversions may be *lossy*, according to the [`Rounding`] mode used.
/// -   Conversions must be *value-preserving*, like [`From`]. For example,
///     `-1_i8` and `-1_i32` are conceptually the same value while `255_u8` is
///     conceptually a different value.
///
/// The [`Rounding`] mode must be specified:
/// ```
/// # use easy_cast::{ConvTo, Exact, Nearest};
/// assert_eq!(i32::conv_to(Nearest, 7.6f32), 8);
/// assert_eq!(f32::conv_to(Exact, 20), 20.0);
/// ```
/// Usage with [`Exact`] and [`Approx`] is equivalent to usage of [`Conv`] and
/// [`ConvApprox`] respectively.
///
/// The sister-trait [`CastTo`] supports "into" style usage.
///
/// ## Implementing conversions
///
/// Implement conversions which cannot lose precision using [`ConvExact`] and
/// other conversions using this trait. Separate implementations may be provided
/// for each [`Rounding`] mode.
///
/// ### Example
///
/// ```
/// use easy_cast::{Approx, ConvTo, Error, Exact, RangeError};
///
/// struct MyFloat { /* details */ }
/// # impl MyFloat {
/// #   fn is_in_range_of<T>(&self) -> bool { todo!() }
/// #   fn is_integral(&self) -> bool { todo!() }
/// # }
///
/// impl ConvTo<MyFloat, Exact> for i32 {
///     type Error = Error;
///
///     fn try_conv_to(_: Exact, f: MyFloat) -> Result<Self, Self::Error> {
///         if f.is_in_range_of::<i32>() {
///             if f.is_integral() {
///                 Ok(todo!())
///             } else {
///                 Err(Error::Inexact)
///             }
///         } else {
///             Err(Error::Range)
///         }
///     }
///
///     // optionally also impl fn conv
/// }
///
/// impl ConvTo<MyFloat, Approx> for i32 {
///     type Error = RangeError;
///
///     fn try_conv_to(_: Approx, f: MyFloat) -> Result<Self, Self::Error> {
///         if f.is_in_range_of::<i32>() {
///             Ok(todo!())
///         } else {
///             Err(RangeError)
///         }
///     }
///
///     // optionally also impl fn conv
/// }
///
/// // optionally also implement ConvTo for other rounding modes
/// ```
pub trait ConvTo<S, R: Rounding>: Sized {
    /// Conversion error type
    ///
    /// This should be one of [`Infallible`], [`RangeError`] or [`Error`].
    type Error: Into<R::MaximumError> + core::error::Error;

    /// Try converting from `S` to `Self`, rounding according to `mode`
    fn try_conv_to(mode: R, s: S) -> Result<Self, Self::Error>;

    /// Convert from `S` to `Self`, rounding according to `mode`
    ///
    /// Use this method only when success is expected. On error, this method may
    /// panic or may exhibit [§ Fallback behaviour](crate#fallback-behaviour).
    ///
    /// # Implementing
    ///
    /// Implementing this method directly (with fallback behaviour) is optional.
    /// In debug builds, this method must panic on error.
    fn conv_to(mode: R, s: S) -> Self {
        Self::try_conv_to(mode, s).unwrap_or_else(|e| panic!("ConvTo::conv_to(_) failed: {e}"))
    }
}

/// Generic "into" conversion trait with specified rounding mode
///
/// This trait supports [`Into`]- and [`TryInto`]-like conversions, but with a
/// specified rounding mode:
/// -   Conversions may be *fallible*, like [`TryInto`].
/// -   Conversions may be *lossy*, according to the [`Rounding`] mode used.
/// -   Conversions must be *value-preserving*, like [`Into`]. For example,
///     `-1_i8` and `-1_i32` are conceptually the same value while `255_u8` is
///     conceptually a different value.
///
/// The [`Rounding`] mode must be specified:
/// ```
/// # use easy_cast::{CastTo, Floor, Nearest};
/// let x: i32 = 3.14192.cast_to(Floor);
/// assert_eq!(x, 3);
///
/// let y = (1i32 << 30) - 1;
/// let z: f32 = y.cast_to(Nearest);  // this example rounds up
/// assert_eq!(z as i32, 1i32 << 30);
/// ```
/// Usage with [`Exact`] and [`Approx`] is equivalent to usage of [`Cast`] and
/// [`CastApprox`] respectively.
///
/// This trait is automatically implemented for every implementation of [`ConvTo`].
pub trait CastTo<T, R: Rounding>: Sized {
    /// Conversion error type
    ///
    /// This should be one of [`Infallible`], [`RangeError`] or [`Error`].
    type Error: Into<R::MaximumError> + core::error::Error;

    /// Try converting from `Self` to `T`, rounding according to `mode`
    fn try_cast_to(self, mode: R) -> Result<T, Self::Error>;

    /// Convert from `Self` to `T`, rounding according to `mode`
    ///
    /// Use this method only when success is expected. On error, this method may
    /// panic or may exhibit [§ Fallback behaviour](crate#fallback-behaviour).
    fn cast_to(self, mode: R) -> T;
}

impl<R: Rounding, S, T: ConvTo<S, R>> CastTo<T, R> for S {
    type Error = T::Error;

    fn try_cast_to(self, mode: R) -> Result<T, Self::Error> {
        T::try_conv_to(mode, self)
    }

    fn cast_to(self, mode: R) -> T {
        T::conv_to(mode, self)
    }
}