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
// 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
//! # Converting values
//!
//! This library exists to make numeric type conversions **easy** and
//! **generic** without resorting to the `as` keyword.
//!
//! - Use [`Cast`] and [`Conv`] instead of [`Into`] and [`From`] for exact
//! conversions
//! - Use [`CastApprox`] and [`ConvApprox`] for approximate conversions
//! (rounding mode is implementation-defined just like `as`)
//! - Use [`CastTo`] and [`ConvTo`] for conversions with a specific rounding
//! mode (see [§ Rounding modes](#rounding-modes)).
//!
//! If this sounds like a lot of traits, consider the above are all essentially
//! syntactic sugar for [`ConvTo`] (see
//! [§ Implementing traits](#implementing-traits)).
//!
//! ### Quick example
//!
//! ```
//! use easy_cast::{Cast, Conv, CastApprox, CastTo, Nearest};
//! let _: i32 = 15_usize.cast(); // exact conversion
//! let _ = usize::conv(20_u32); // exact conversion
//! let _: f32 = u32::MAX.cast_approx(); // approximates to 2^32
//! let _: i32 = 11.9_f32.cast_to(Nearest); // rounds to 12
//! ```
//!
//! ## Rounding modes
//!
//! The [`Rounding`] trait (used with [`CastTo`] and [`ConvTo`]) supports
//! genericity over rounding modes:
//!
//! - [`Exact`] specifies that no rounding is allowed (loss of precision is an
//! error)
//! - [`Approx`] specifies that rounding is allowed. The rounding mode used is
//! a property of the implementation, but usually aligns with
//! [`as` numeric casts].
//! - [`Trunc`], [`Floor`], [`Ceil`] and [`Nearest`] allow more precise
//! control over rounding
//!
//! All rounding modes require that the result is close to the input value. For
//! a more precise definition, see
//! [`§ Limits of approximation`](Approx#limits-of-approximation).
//!
//! ## Error handling
//!
//! Unlike [`From`] or [`TryFrom`], this library's traits are implemented
//! regardless of fallibility. All conversion traits have an associated `Error`
//! type which is expected to be one of:
//!
//! - [`std::convert::Infallible`] for infallible conversions
//! - [`RangeError`] for conversions which may fail due to domain errors
//! - [`Error`] for conversions which may fail due to domain or
//! loss-of-precision errors.
//!
//! Further, all traits have two methods:
//!
//! - A `try_` method (e.g. [`Cast::try_cast`]) which returns a [`Result`]
//! - A "derived" method (e.g. [`Cast::cast`]) with
//! [§ Fallback behaviour](#fallback-behaviour)
//!
//! ### Fallback behaviour
//!
//! In debug builds, the "derived" method must panic on failure. This is also
//! the case if the `always_assert` feature flag is enabled (for this library's
//! implementations).
//!
//! Otherwise (in release builds without extra assertions enabled), more
//! flexible behaviour of the "derived" methods is allowed. The implementations
//! provided by `easy-cast` mostly reduce to [`as` numeric casts] (with extra
//! rounding where required).
//!
//! ## Implementing traits
//!
//! Implement conversions which cannot lose precision using [`ConvExact`].
//! Implement all other conversions using [`ConvTo`] for one or several
//! [`Rounding`] modes.
//!
//! [`TryFrom`]: core::convert::TryFrom
//! [`TryInto`]: core::convert::TryInto
//! [`as` numeric casts]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#r-expr.as.numeric
pub use *;
pub use *;
use Infallible;
/// Source value lies outside of target type's range
///
/// This error indicates that the input value is outside the range (domain) of
/// the target type. This error type is used for both conversions where
/// loss-of-precision is impossible and those where rounding is intended.
;
/// Error types for conversions