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
//! A crate that provides a simple interface to primitive ints to allow explicit widening,
//! truncating, and sign casting.
//!
//! To get started, import the prelude:
//! ```
//! use explicit_cast::prelude::*;
//!
//! assert_eq!(5u8.widen::<u16>().sign_cast().widen::<i32>().truncate::<i8>(), 5i8);
//! ```
//!
//! # Stability
//! This crate is 1.0 as in being **stable and or finished**, as there is no other functionality to be had than
//! allowing explicit casting of integers. As such, a prelude has been included that imports [`Widen`],
//! [`Truncate`], and [`SignCast`] for you. **No new methods** will be added to these traits, and **no
//! new traits** will be added to the prelude, without a 2.0 release, that theoretically should never
//! happen.
//!
//! Documentation updates may be published under a 1.0.X patch release, but no new functionality is
//! planned.
/// Sealed module
use Sealed;
/// The inner trait of [`Widen`] that allows it to have a generic function signature.
///
/// This may be useful to import yourself if you wish to use it in API's, but it is only a
/// byproduct of this crate.
/// The inner trait of [`Truncate`] that allows it to have a generic function signature.
///
/// This may be useful to import yourself if you wish to use it in API's, but it is only a
/// byproduct of this crate.
/// Trait to sign cast an integer to/from signed/unsigned
///
/// This is better than `as` casting because:
/// - It is explicitly only casting signs, and will not change integer width
/// - It is method chainable
/// Trait to truncate an integer from a larger size.
///
/// This is better than `as` casting because:
/// - It is explicitly a truncating operation, and will *only* truncate
/// - It only supports similar signs, i/e `u16` to `i8` will *not* compile
/// - It is method chainable
/// - You can use turbofishy or type inference :D
///
/// Error messages should also be clear in the event of an invalid operation, so you will not be
/// left wondering what went wrong, this is mostly thanks to rusts great error messages though
/// Trait to widen an integer from a smaller size, either zero extending or sign extending
/// depending on whether the integer is signed.
///
/// This is better than `as` casting because:
/// - It is explicitly a widening operation, and will *only* widen
/// - It only supports similar signs, i/e `u8` to `i16` will *not* compile
/// - It is method chainable
/// - You can use turbofishy :D or type inference, unlike [`into`](Into::into) which only supports type inference
///
/// Error messages should also be clear in the event of an invalid operation, so you will not be
/// left wondering what went wrong, this is mostly thanks to rusts great error messages though