cryptocol/number/size_union.rs
1// Copyright 2023, 2024 PARK Youngho.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6// This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! The module that contains unions of primitive signed/unsigned integral
10//! data types used in a lot of modules of the crate Cryptocol.
11//! __These unions are for segmentation.__
12
13// #![warn(missing_docs)]
14// #![warn(rustdoc::missing_doc_code_examples)]
15// #![allow(missing_docs)]
16// #![allow(rustdoc::missing_doc_code_examples)]
17
18use std::cmp::{ PartialEq, PartialOrd, Ordering };
19use std::ops::{ BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not,
20 Shl, ShlAssign, Shr, ShrAssign,
21 Add, AddAssign, Sub, SubAssign, Mul, MulAssign,
22 Div, DivAssign, Rem, RemAssign };
23use std::fmt::{ self, Alignment, Error, Formatter, Display, Debug, Pointer,
24 Binary, Octal, LowerHex, UpperHex, LowerExp, UpperExp };
25
26use crate::number::{ SmallUInt, ShortUnion, IntUnion, LongUnion, LongerUnion };
27use crate::number::{ union_calc_assign_to_calc, union_fmt_with_radix, union_fmt_with_exponent };
28
29/// # Introduction
30/// This union `SizeUnion` is for slicing `usize` into two `u64`s, two `i64`s,
31/// four `u32`s, four `i32`s, eight `u16`s, eight `i16`s, fourteen `u8`,
32/// and/or fourteen `u8`.
33///
34/// Sometimes, for example, we need to slice `usize` data into two `u64` pieces
35/// which include a higher eight-byte word and a lower eight-byte word, and/or
36/// into eight `u16` pieces which include a first four-byte word, a second
37/// four-byte word, a third four-byte word and a fourth four-byte word.
38/// In that case, `SizeUnion` will be very helpful.
39///
40/// # Quick Start
41/// In order to use this union, you have to import (use)
42/// `cryptocol::number::SizeUnion` as follows.
43///
44/// ## Example 1
45/// ```
46/// use cryptocol::number::SizeUnion;
47/// ```
48/// You can use the methods `get()`, `get_signed()`, `get_ulonger()`, and
49/// `get_slonger()` in order to obtain the data of `usize` in various types.
50/// And, you can also slice the data of `usize` into two `u64` type data or
51/// two`i64` by using the methods `get_ulong()`, `get_slong()`, `get_ulong_()`,
52/// and `get_slong_()`. Or, you can also slice the data of `usize` into four
53/// `u32` type data or four `i32` type databy using the methods `get_uint()`,
54/// `get_sint()`, `get_uint_()`, and `get_sint_()`. Or, you can also slice the
55/// data of `usize` into eight `u16` type data or eight `i16` type data by using
56/// the methods `get_ushort()`, `get_sshort()`, `get_ushort_()`, and
57/// `get_sshort_()`. Or, you can also slice the data of `usize` into fourteen
58/// `u8` type data or fourteen `i8` type data by using the methods
59/// `get_ubyte()`, `get_sbyte()`, `get_ubyte_()`, and `get_sbyte_()`.
60///
61/// ## Example 2
62/// ```
63/// use cryptocol::number::SizeUnion;
64///
65/// let a = SizeUnion::new_with_signed(-1234567890987654321012345678987654321_isize);
66/// println!("a.get() = {}", a.get());
67/// println!("a.get_signed() = {}", a.get_signed());
68/// println!("a.get_usize() = {}", a.get_usize());
69/// println!("a.get_ssize() = {}", a.get_ssize());
70/// println!("a.get_ulonger() = {}", a.get_ulonger());
71/// println!("a.get_slonger() = {}", a.get_slonger());
72/// assert_eq!(a.get(), 339047799029950809142362261752780557135_usize);
73/// assert_eq!(a.get_signed(), -1234567890987654321012345678987654321_isize);
74/// assert_eq!(a.get_usize(), 339047799029950809142362261752780557135_usize);
75/// assert_eq!(a.get_ssize(), -1234567890987654321012345678987654321_isize);
76/// assert_eq!(a.get_ulonger(), 339047799029950809142362261752780557135_u128);
77/// assert_eq!(a.get_slonger(), -1234567890987654321012345678987654321_i128);
78///
79/// for i in 0..2
80/// { println!("a.get_ulong_({}) = {}", i, a.get_ulong_(i)); }
81/// for i in 0..2
82/// { println!("a.get_slong_({}) = {}", i, a.get_slong_(i)); }
83/// for i in 0..4
84/// { println!("a.get_uint_({}) = {}", i, a.get_uint_(i)); }
85/// for i in 0..4
86/// { println!("a.get_sint_({}) = {}", i, a.get_sint_(i)); }
87/// for i in 0..8
88/// { println!("a.get_ushort_({}) = {}", i, a.get_ushort_(i)); }
89/// for i in 0..8
90/// { println!("a.get_sshort_({}) = {}", i, a.get_sshort_(i)); }
91/// for i in 0..16
92/// { println!("a.get_ubyte_({}) = {}", i, a.get_ubyte_(i)); }
93/// for i in 0..16
94/// { println!("a.get_sbyte_({}) = {}", i, a.get_sbyte_(i)); }
95/// assert_eq!(a.get_ulong_(0), 13664881099896654671_u64);
96/// assert_eq!(a.get_ulong_(1), 18379818014235068504_u64);
97/// assert_eq!(a.get_slong_(0), -4781862973812896945_i64);
98/// assert_eq!(a.get_slong_(1), -66926059474483112_i64);
99/// assert_eq!(a.get_uint_(0), 4048161615_u32);
100/// assert_eq!(a.get_uint_(1), 3181603061_u32);
101/// assert_eq!(a.get_uint_(2), 2127464536_u32);
102/// assert_eq!(a.get_uint_(3), 4279384858_u32);
103/// assert_eq!(a.get_sint_(0), -246805681_i32);
104/// assert_eq!(a.get_sint_(1), -1113364235_i32);
105/// assert_eq!(a.get_sint_(2), 2127464536_i32);
106/// assert_eq!(a.get_sint_(3), -15582438_i32);
107/// assert_eq!(a.get_ushort_(0), 2895_u16);
108/// assert_eq!(a.get_ushort_(1), 61770_u16);
109/// assert_eq!(a.get_ushort_(2), 26869_u16);
110/// assert_eq!(a.get_ushort_(3), 48547_u16);
111/// assert_eq!(a.get_ushort_(4), 34904_u16);
112/// assert_eq!(a.get_ushort_(5), 32462_u16);
113/// assert_eq!(a.get_ushort_(6), 15130_u16);
114/// assert_eq!(a.get_ushort_(7), 65298_u16);
115/// assert_eq!(a.get_sshort_(0), 2895_i16);
116/// assert_eq!(a.get_sshort_(1), -3766_i16);
117/// assert_eq!(a.get_sshort_(2), 26869_i16);
118/// assert_eq!(a.get_sshort_(3), -16989_i16);
119/// assert_eq!(a.get_sshort_(4), -30632_i16);
120/// assert_eq!(a.get_sshort_(5), 32462_i16);
121/// assert_eq!(a.get_sshort_(6), 15130_i16);
122/// assert_eq!(a.get_sshort_(7), -238_i16);
123/// assert_eq!(a.get_ubyte_(0), 79_u8);
124/// assert_eq!(a.get_ubyte_(1), 11_u8);
125/// assert_eq!(a.get_ubyte_(2), 74_u8);
126/// assert_eq!(a.get_ubyte_(3), 241_u8);
127/// assert_eq!(a.get_ubyte_(4), 245_u8);
128/// assert_eq!(a.get_ubyte_(5), 104_u8);
129/// assert_eq!(a.get_ubyte_(6), 163_u8);
130/// assert_eq!(a.get_ubyte_(7), 189_u8);
131/// assert_eq!(a.get_ubyte_(8), 88_u8);
132/// assert_eq!(a.get_ubyte_(9), 136_u8);
133/// assert_eq!(a.get_ubyte_(10), 206_u8);
134/// assert_eq!(a.get_ubyte_(11), 126_u8);
135/// assert_eq!(a.get_ubyte_(12), 26_u8);
136/// assert_eq!(a.get_ubyte_(13), 59_u8);
137/// assert_eq!(a.get_ubyte_(14), 18_u8);
138/// assert_eq!(a.get_ubyte_(15), 255_u8);
139/// assert_eq!(a.get_sbyte_(0), 79_i8);
140/// assert_eq!(a.get_sbyte_(1), 11_i8);
141/// assert_eq!(a.get_sbyte_(2), 74_i8);
142/// assert_eq!(a.get_sbyte_(3), -15_i8);
143/// assert_eq!(a.get_sbyte_(4), -11_i8);
144/// assert_eq!(a.get_sbyte_(5), 104_i8);
145/// assert_eq!(a.get_sbyte_(6), -93_i8);
146/// assert_eq!(a.get_sbyte_(7), -67_i8);
147/// assert_eq!(a.get_sbyte_(8), 88_i8);
148/// assert_eq!(a.get_sbyte_(9), -120_i8);
149/// assert_eq!(a.get_sbyte_(10), -50_i8);
150/// assert_eq!(a.get_sbyte_(11), 126_i8);
151/// assert_eq!(a.get_sbyte_(12), 26_i8);
152/// assert_eq!(a.get_sbyte_(13), 59_i8);
153/// assert_eq!(a.get_sbyte_(14), 18_i8);
154/// assert_eq!(a.get_sbyte_(15), -1_i8);
155/// ```
156/// You can use `SizeUnion` as if you used `usize`. You can perform all kinds
157/// of arithmetic operations such as addition, subtraction, multiplication,
158/// and division (div and rem), and other operations which are available for
159/// `usize`. If you use `SizeUnion` with the help of `SmallUInt`, it will be
160/// even more powerful and convenient. In this case, you don't even have to
161/// import (use) `cryptocol::number::SizeUnion`.
162///
163/// ## Example 3
164/// ```
165/// use cryptocol::number::SmallUInt;
166///
167/// let a_sizeunion = 123456789876543212345678987654321_usize.into_sizeunion();
168/// let b_sizeunion = 876543210123456787654321012345678_usize.into_sizeunion();
169/// let c_sizeunion = a_sizeunion.wrapping_add(b_sizeunion);
170///
171/// println!("{} + {} = {}", a_sizeunion, b_sizeunion, c_sizeunion);
172/// assert_eq!(c_sizeunion.get(), 999999999999999999999999999999999_usize);
173///
174/// for i in 0..2
175/// { println!("c_sizeunion.get_ulong_({}) = {}", i, c_sizeunion.get_ulong_(i)); }
176/// assert_eq!(c_sizeunion.get_ulong_(0), 4089650035136921599_u64);
177/// assert_eq!(c_sizeunion.get_ulong_(1), 54210108624275_u64);
178///
179/// for i in 0..4
180/// { println!("c_sizeunion.get_uint_({}) = {}", i, c_sizeunion.get_uint_(i)); }
181/// assert_eq!(c_sizeunion.get_uint_(0), 4294967295_u32);
182/// assert_eq!(c_sizeunion.get_uint_(1), 952195849_u32);
183/// assert_eq!(c_sizeunion.get_uint_(2), 3326381459_u32);
184/// assert_eq!(c_sizeunion.get_uint_(3), 12621_u32);
185///
186/// for i in 0..8
187/// { println!("c_sizeunion.get_ushort_({}) = {}", i, c_sizeunion.get_ushort_(i)); }
188/// assert_eq!(c_sizeunion.get_ushort_(0), 65535_u16);
189/// assert_eq!(c_sizeunion.get_ushort_(1), 65535_u16);
190/// assert_eq!(c_sizeunion.get_ushort_(2), 23305_u16);
191/// assert_eq!(c_sizeunion.get_ushort_(3), 14529_u16);
192/// assert_eq!(c_sizeunion.get_ushort_(4), 36243_u16);
193/// assert_eq!(c_sizeunion.get_ushort_(5), 50756_u16);
194/// assert_eq!(c_sizeunion.get_ushort_(6), 12621_u16);
195/// assert_eq!(c_sizeunion.get_ushort_(7), 0_u16);
196///
197/// for i in 0..16
198/// { println!("c_sizeunion.get_ubyte_({}) = {}", i, c_sizeunion.get_ubyte_(i)); }
199/// assert_eq!(c_sizeunion.get_ubyte_(0), 255_u8);
200/// assert_eq!(c_sizeunion.get_ubyte_(1), 255_u8);
201/// assert_eq!(c_sizeunion.get_ubyte_(2), 255_u8);
202/// assert_eq!(c_sizeunion.get_ubyte_(3), 255_u8);
203/// assert_eq!(c_sizeunion.get_ubyte_(4), 9_u8);
204/// assert_eq!(c_sizeunion.get_ubyte_(5), 91_u8);
205/// assert_eq!(c_sizeunion.get_ubyte_(6), 193_u8);
206/// assert_eq!(c_sizeunion.get_ubyte_(7), 56_u8);
207/// assert_eq!(c_sizeunion.get_ubyte_(8), 147_u8);
208/// assert_eq!(c_sizeunion.get_ubyte_(9), 141_u8);
209/// assert_eq!(c_sizeunion.get_ubyte_(10), 68_u8);
210/// assert_eq!(c_sizeunion.get_ubyte_(11), 198_u8);
211/// assert_eq!(c_sizeunion.get_ubyte_(12), 77_u8);
212/// assert_eq!(c_sizeunion.get_ubyte_(13), 49_u8);
213/// assert_eq!(c_sizeunion.get_ubyte_(14), 0_u8);
214/// assert_eq!(c_sizeunion.get_ubyte_(15), 0_u8);
215///
216/// let d_sizeunion = b_sizeunion - a_sizeunion;
217/// println!("{} - {} = {}", b_sizeunion, a_sizeunion, d_sizeunion);
218/// assert_eq!(d_sizeunion.get(), 753086420246913575308642024691357_usize);
219///
220/// for i in 0..2
221/// { println!("d_sizeunion.get_ulong_({}) = {}", i, d_sizeunion.get_ulong_(i)); }
222/// assert_eq!(d_sizeunion.get_ulong_(0), 14084888390109238941_u64);
223/// assert_eq!(d_sizeunion.get_ulong_(1), 40824896645051_u64);
224///
225/// for i in 0..4
226/// { println!("d_sizeunion.get_uint_({}) = {}", i, d_sizeunion.get_uint_(i)); }
227/// assert_eq!(d_sizeunion.get_uint_(0), 2843481757_u32);
228/// assert_eq!(d_sizeunion.get_uint_(1), 3279393629_u32);
229/// assert_eq!(d_sizeunion.get_uint_(2), 1232496571_u32);
230/// assert_eq!(d_sizeunion.get_uint_(3), 9505_u32);
231///
232/// for i in 0..8
233/// { println!("d_sizeunion.get_ushort_({}) = {}", i, d_sizeunion.get_ushort_(i)); }
234/// assert_eq!(d_sizeunion.get_ushort_(0), 5789_u16);
235/// assert_eq!(d_sizeunion.get_ushort_(1), 43388_u16);
236/// assert_eq!(d_sizeunion.get_ushort_(2), 37725_u16);
237/// assert_eq!(d_sizeunion.get_ushort_(3), 50039_u16);
238/// assert_eq!(d_sizeunion.get_ushort_(4), 26555_u16);
239/// assert_eq!(d_sizeunion.get_ushort_(5), 18806_u16);
240/// assert_eq!(d_sizeunion.get_ushort_(6), 9505_u16);
241/// assert_eq!(d_sizeunion.get_ushort_(7), 0_u16);
242///
243/// for i in 0..16
244/// { println!("d_sizeunion.get_ubyte_({}) = {}", i, d_sizeunion.get_ubyte_(i)); }
245/// assert_eq!(d_sizeunion.get_ubyte_(0), 157_u8);
246/// assert_eq!(d_sizeunion.get_ubyte_(1), 22_u8);
247/// assert_eq!(d_sizeunion.get_ubyte_(2), 124_u8);
248/// assert_eq!(d_sizeunion.get_ubyte_(3), 169_u8);
249/// assert_eq!(d_sizeunion.get_ubyte_(4), 93_u8);
250/// assert_eq!(d_sizeunion.get_ubyte_(5), 147_u8);
251/// assert_eq!(d_sizeunion.get_ubyte_(6), 119_u8);
252/// assert_eq!(d_sizeunion.get_ubyte_(7), 195_u8);
253/// assert_eq!(d_sizeunion.get_ubyte_(8), 187_u8);
254/// assert_eq!(d_sizeunion.get_ubyte_(9), 103_u8);
255/// assert_eq!(d_sizeunion.get_ubyte_(10), 118_u8);
256/// assert_eq!(d_sizeunion.get_ubyte_(11), 73_u8);
257/// assert_eq!(d_sizeunion.get_ubyte_(12), 33_u8);
258/// assert_eq!(d_sizeunion.get_ubyte_(13), 37_u8);
259/// assert_eq!(d_sizeunion.get_ubyte_(14), 0_u8);
260/// assert_eq!(d_sizeunion.get_ubyte_(15), 0_u8);
261///
262/// let e_sizeunion = d_sizeunion * 3_usize.into_sizeunion();
263/// println!("{} * {} = {}", d_sizeunion, 3_usize.into_sizeunion(), e_sizeunion);
264/// assert_eq!(e_sizeunion.get(), 2259259260740740725925926074074071_usize);
265///
266/// let f_sizeunion = c_sizeunion / 10_usize.into_sizeunion();
267/// println!("{} / {} = {}", c_sizeunion, 10_usize.into_sizeunion(), f_sizeunion);
268/// assert_eq!(f_sizeunion.get(), 99999999999999999999999999999999_usize);
269///
270/// let g_sizeunion = c_sizeunion % 10_usize.into_sizeunion();
271/// println!("{} % {} = {}", c_sizeunion, 10_usize.into_sizeunion(), g_sizeunion);
272/// assert_eq!(g_sizeunion.get(), 9_usize);
273/// ```
274///
275/// # Big-endian issue
276/// It is just experimental for Big Endian CPUs. So, you are not encouraged
277/// to use it for serious purpose. Only use this crate for Big-endian CPUs
278/// with your own full responsibility.
279// #[cfg(target_pointer_width = "128")]
280// #[derive(Copy, Clone)]
281// #[allow(dead_code)]
282// pub union SizeUnion
283// {
284// /// The biggest unsigned element for compatibility with other unions
285// this: usize,
286
287// /// The biggest signed element for compatibility with other unions
288// that: isize,
289
290// /// The usize type element whose size is the same as the SizeUnion
291// pub u_size: usize,
292
293// /// The isize type element whose size is the same as the SizeUnion
294// pub s_size: isize,
295
296// /// The biggest unsigned element which is 128-bit unsigned integer
297// pub ulonger: u128,
298
299// /// The biggest signed element which is 128-bit unsigned integer
300// pub slonger: i128,
301
302// /// The secondly biggest unsigned element array whose elements are
303// /// 64-bit unsigned integer
304// pub ulong: [u64; 2],
305
306// /// The secondly biggest unsigned element array whose elements are
307// /// 64-bit signed integer
308// pub slong: [i64; 2],
309
310// /// The thirdly biggest unsigned element array whose elements are
311// /// 32-bit unsigned integer
312// pub uint: [u32; 4],
313
314// /// The thirdly biggest unsigned element array whose elements are
315// /// 32-bit signed integer
316// pub sint: [i32; 4],
317
318// /// The fourthly biggest unsigned element array whose elements are
319// /// 16-bit unsigned integer
320// pub ushort: [u16; 8],
321
322// /// The fourthly biggest unsigned element array whose elements are
323// /// 16-bit signed integer
324// pub sshort: [i16; 8],
325
326// /// The fifthly biggest unsigned element array whose elements are
327// /// 8-bit unsigned integer
328// pub ubyte: [u8; 16],
329
330// /// The fifthly biggest unsigned element array whose elements are
331// /// 8-bit signed integer
332// pub sbyte: [i8; 16],
333// }
334
335
336/// # Introduction
337/// This union `SizeUnion` is for slicing `usize` into two `u32`s, two `i32`s,
338/// four `u16`s, four `i16`s, eight `u8`s and/or eight `i8`s.
339///
340/// Sometimes, for example, we need to slice `usize` data into two `u32` pieces
341/// which include a higher four-byte word and a lower four-byte word, and/or
342/// into four `u16` pieces which include a first two-byte word, a second
343/// two-byte word, a third two-byte word and a fourth two-byte word.
344/// In that case, `SizeUnion` will be very helpful.
345///
346/// # Quick Start
347/// In order to use this union, you have to import (use)
348/// `cryptocol::number::SizeUnion` as follows.
349///
350/// ## Example 1
351/// ```
352/// use cryptocol::number::SizeUnion;
353/// ```
354/// You can use the methods `get()`, `get_signed()`, `get_ulong()`, and
355/// `get_slong()` in order to obtain the data of `u64` in various types.
356/// And, you can also slice the data of `usize` into two `u32` type data
357/// or two `i32` type data by using the methods `get_uint()`, `get_sint()`,
358/// `get_uint_()`, and `get_sint_()`. Or, you can also slice the data of
359/// `usize` into four `u16` type data or four `i16` type data by using the
360/// methods `get_ushort()`, `get_sshort()`, `get_ushort_()`, and
361/// `get_sshort_()`. Or, you can also slice the data of `usize` into eight
362/// `u8` type data by using the methods `get_ubyte()`, `get_sbyte()`,
363/// `get_ubyte_()`, and `get_sbyte_()`.
364///
365/// ## Example 2
366/// ```
367/// use cryptocol::number::SizeUnion;
368///
369/// let a = SizeUnion::new_with_signed(-4781862973812896945_isize);
370/// println!("a.get() = {}", a.get());
371/// println!("a.get_signed() = {}", a.get_signed());
372/// println!("a.get_usize() = {}", a.get_usize());
373/// println!("a.get_ssize() = {}", a.get_ssize());
374/// println!("a.get_ulong() = {}", a.get_ulong());
375/// println!("a.get_slong() = {}", a.get_slong());
376/// assert_eq!(a.get(), 13664881099896654671_usize);
377/// assert_eq!(a.get_signed(), -4781862973812896945_isize);
378/// assert_eq!(a.get_usize(), 13664881099896654671_usize);
379/// assert_eq!(a.get_ssize(), -4781862973812896945_isize);
380/// assert_eq!(a.get_ulong(), 13664881099896654671_u64);
381/// assert_eq!(a.get_slong(), -4781862973812896945_i64);
382///
383/// for i in 0..2
384/// { println!("a.get_uint_({}) = {}", i, a.get_uint_(i)); }
385/// for i in 0..2
386/// { println!("a.get_sint_({}) = {}", i, a.get_sint_(i)); }
387/// for i in 0..4
388/// { println!("a.get_ushort_({}) = {}", i, a.get_ushort_(i)); }
389/// for i in 0..4
390/// { println!("a.get_sshort_({}) = {}", i, a.get_sshort_(i)); }
391/// for i in 0..8
392/// { println!("a.get_ubyte_({}) = {}", i, a.get_ubyte_(i)); }
393/// for i in 0..8
394/// { println!("a.get_sbyte_({}) = {}", i, a.get_sbyte_(i)); }
395/// assert_eq!(a.get_uint_(0), 4048161615_u32);
396/// assert_eq!(a.get_uint_(1), 3181603061_u32);
397/// assert_eq!(a.get_sint_(0), -246805681_i32);
398/// assert_eq!(a.get_sint_(1), -1113364235_i32);
399/// assert_eq!(a.get_ushort_(0), 2895_u16);
400/// assert_eq!(a.get_ushort_(1), 61770_u16);
401/// assert_eq!(a.get_ushort_(2), 26869_u16);
402/// assert_eq!(a.get_ushort_(3), 48547_u16);
403/// assert_eq!(a.get_sshort_(0), 2895_i16);
404/// assert_eq!(a.get_sshort_(1), -3766_i16);
405/// assert_eq!(a.get_sshort_(2), 26869_i16);
406/// assert_eq!(a.get_sshort_(3), -16989_i16);
407/// assert_eq!(a.get_ubyte_(0), 79_u8);
408/// assert_eq!(a.get_ubyte_(1), 11_u8);
409/// assert_eq!(a.get_ubyte_(2), 74_u8);
410/// assert_eq!(a.get_ubyte_(3), 241_u8);
411/// assert_eq!(a.get_ubyte_(4), 245_u8);
412/// assert_eq!(a.get_ubyte_(5), 104_u8);
413/// assert_eq!(a.get_ubyte_(6), 163_u8);
414/// assert_eq!(a.get_ubyte_(7), 189_u8);
415/// assert_eq!(a.get_sbyte_(0), 79_i8);
416/// assert_eq!(a.get_sbyte_(1), 11_i8);
417/// assert_eq!(a.get_sbyte_(2), 74_i8);
418/// assert_eq!(a.get_sbyte_(3), -15_i8);
419/// assert_eq!(a.get_sbyte_(4), -11_i8);
420/// assert_eq!(a.get_sbyte_(5), 104_i8);
421/// assert_eq!(a.get_sbyte_(6), -93_i8);
422/// assert_eq!(a.get_sbyte_(7), -67_i8);
423/// ```
424/// You can use `SizeUnion` as if you used `usize`. You can perform all kinds
425/// of arithmetic operations such as addition, subtraction, multiplication,
426/// and division (div and rem), and other operations which are available for
427/// `usize`. If you use `SizeUnion` with the help of `SmallUInt`, it will be
428/// even more powerful and convenient. In this case, you don't even have to
429/// import (use) `cryptocol::number::SizeUnion`.
430///
431/// ## Example 3
432/// ```
433/// use cryptocol::number::SmallUInt;
434///
435/// let a_sizeunion = 12345678987654321_usize.into_sizeunion();
436/// let b_sizeunion = 87654321012345678_usize.into_sizeunion();
437/// let c_sizeunion = a_sizeunion.wrapping_add(b_sizeunion);
438///
439/// println!("{} + {} = {}", a_sizeunion, b_sizeunion, c_sizeunion);
440/// assert_eq!(c_sizeunion.get(), 99999999999999999_usize);
441///
442/// for i in 0..2
443/// { println!("c_sizeunion.get_uint_({}) = {}", i, c_sizeunion.get_uint_(i)); }
444/// assert_eq!(c_sizeunion.get_uint_(0), 1569325055_u32);
445/// assert_eq!(c_sizeunion.get_uint_(1), 23283064_u32);
446///
447/// for i in 0..4
448/// { println!("c_sizeunion.get_ushort_({}) = {}", i, c_sizeunion.get_ushort_(i)); }
449/// assert_eq!(c_sizeunion.get_ushort_(0), 65535_u16);
450/// assert_eq!(c_sizeunion.get_ushort_(1), 23945_u16);
451/// assert_eq!(c_sizeunion.get_ushort_(2), 17784_u16);
452/// assert_eq!(c_sizeunion.get_ushort_(3), 355_u16);
453///
454/// for i in 0..8
455/// { println!("c_sizeunion.get_ubyte_({}) = {}", i, c_sizeunion.get_ubyte_(i)); }
456/// assert_eq!(c_sizeunion.get_ubyte_(0), 255_u8);
457/// assert_eq!(c_sizeunion.get_ubyte_(1), 255_u8);
458/// assert_eq!(c_sizeunion.get_ubyte_(2), 137_u8);
459/// assert_eq!(c_sizeunion.get_ubyte_(3), 93_u8);
460/// assert_eq!(c_sizeunion.get_ubyte_(4), 120_u8);
461/// assert_eq!(c_sizeunion.get_ubyte_(5), 69_u8);
462/// assert_eq!(c_sizeunion.get_ubyte_(6), 99_u8);
463/// assert_eq!(c_sizeunion.get_ubyte_(7), 1_u8);
464///
465/// let d_sizeunion = b_sizeunion - a_sizeunion;
466/// println!("{} - {} = {}", b_sizeunion, a_sizeunion, d_sizeunion);
467/// assert_eq!(d_sizeunion.get(), 75308642024691357_usize);
468///
469/// for i in 0..2
470/// { println!("d_longunion.get_uint_({}) = {}", i, d_sizeunion.get_uint_(i)); }
471/// assert_eq!(d_sizeunion.get_uint_(0), 2556827293_u32);
472/// assert_eq!(d_sizeunion.get_uint_(1), 17534159_u32);
473///
474/// for i in 0..4
475/// { println!("d_sizeunion.get_ushort_({}) = {}", i, d_sizeunion.get_ushort_(i)); }
476/// assert_eq!(d_sizeunion.get_ushort_(0), 5789_u16);
477/// assert_eq!(d_sizeunion.get_ushort_(1), 39014_u16);
478/// assert_eq!(d_sizeunion.get_ushort_(2), 36047_u16);
479/// assert_eq!(d_sizeunion.get_ushort_(3), 267_u16);
480///
481/// for i in 0..8
482/// { println!("d_sizeunion.get_ubyte_({}) = {}", i, d_sizeunion.get_ubyte_(i)); }
483/// assert_eq!(d_sizeunion.get_ubyte_(0), 157_u8);
484/// assert_eq!(d_sizeunion.get_ubyte_(1), 22_u8);
485/// assert_eq!(d_sizeunion.get_ubyte_(2), 102_u8);
486/// assert_eq!(d_sizeunion.get_ubyte_(3), 152_u8);
487/// assert_eq!(d_sizeunion.get_ubyte_(4), 207_u8);
488/// assert_eq!(d_sizeunion.get_ubyte_(5), 140_u8);
489/// assert_eq!(d_sizeunion.get_ubyte_(6), 11_u8);
490/// assert_eq!(d_sizeunion.get_ubyte_(7), 1_u8);
491///
492/// let e_sizeunion = d_sizeunion * 3_usize.into_sizeunion();
493/// println!("{} * {} = {}", d_sizeunion, 3_usize.into_sizeunion(), e_sizeunion);
494/// assert_eq!(e_sizeunion.get(), 225925926074074071_usize);
495///
496/// let f_sizeunion = c_sizeunion / 10_usize.into_sizeunion();
497/// println!("{} / {} = {}", c_sizeunion, 10_usize.into_sizeunion(), f_sizeunion);
498/// assert_eq!(f_sizeunion.get(), 9999999999999999_usize);
499///
500/// let g_sizeunion = c_sizeunion % 10_usize.into_sizeunion();
501/// println!("{} % {} = {}", c_sizeunion, 10_usize.into_sizeunion(), g_sizeunion);
502/// assert_eq!(g_sizeunion.get(), 9_usize);
503/// ```
504///
505/// # Big-endian issue
506/// It is just experimental for Big Endian CPUs. So, you are not encouraged
507/// to use it for serious purpose. Only use this crate for Big-endian CPUs
508/// with your own full responsibility.
509#[cfg(target_pointer_width = "64")]
510#[derive(Copy, Clone)]
511#[allow(dead_code)]
512pub union SizeUnion
513{
514 /// The biggest unsigned element for compatibility with other unions
515 this: usize,
516
517 /// The biggest signed element for compatibility with other unions
518 that: isize,
519
520 /// The usize type element whose size is the same as the SizeUnion
521 u_size: usize,
522
523 /// The isize type element whose size is the same as the SizeUnion
524 s_size: isize,
525
526 /// The biggest unsigned element which is 64-bit unsigned integer
527 ulong: u64,
528
529 /// The biggest signed element which is 64-bit unsigned integer
530 slong: i64,
531
532 /// The secondly biggest unsigned element array whose elements are
533 /// 32-bit unsigned integer
534 uint: [u32; 2],
535
536 /// The secondly biggest unsigned element array whose elements are
537 /// 32-bit signed integer
538 sint: [i32; 2],
539
540 /// The thirdly biggest unsigned element array whose elements are
541 /// 16-bit unsigned integer
542 ushort: [u16; 4],
543
544 /// The thirdly biggest unsigned element array whose elements are
545 /// 16-bit signed integer
546 sshort: [i16; 4],
547
548 /// The fourthly biggest unsigned element array whose elements are
549 /// 8-bit unsigned integer
550 ubyte: [u8; 8],
551
552 /// The fourthly biggest unsigned element array whose elements are
553 /// 8-bit signed integer
554 sbyte: [i8; 8],
555}
556
557
558/// # Introduction
559/// This union `SizeUnion` is for slicing `usize` into two `u16`s, two `i16`s,
560/// four `u8`s, and/or four `i8`s.
561///
562/// Sometimes, for example, we need to slice `usize` data into two `u16` pieces
563/// which include a higher two-byte word and a lower two-byte word, and/or
564/// into four `u8` pieces which include a first byte, a second byte, a third
565/// byte, and a fourth byte. In that case, `SizeUnion` will be very helpful.
566///
567/// # Quick Start
568/// In order to use this union, you have to import (use)
569/// `cryptocol::number::SizeUnion` as follows.
570///
571/// ## Example 1
572/// ```
573/// use cryptocol::number::SizeUnion;
574/// ```
575/// You can use the methods `get()`, `get_signed()`, `get_uint()`, and
576/// `get_sint()` in order to obtain the data of `usize` in various types.
577/// And, you can also slice the data of `usize` into two `u16` type data
578/// or two `i16` type data by using the methods `get_uint()`, `get_sint()`,
579/// `get_uint_()`, and `get_sint_()`. Or, you can also slice the data of
580/// `usize` into four `u8` type data or four `i8` type data by using the
581/// methods `get_ubyte()`, `get_sbyte()`, `get_ubyte_()`, and `get_sbyte_()`.
582///
583/// ## Example 2
584/// ```
585/// use cryptocol::number::SizeUnion;
586///
587/// let a = SizeUnion::new_with_signed(-246805681_isize);
588///
589/// println!("a.get() = {}", a.get());
590/// println!("a.get_signed() = {}", a.get_signed());
591/// println!("a.get_usize() = {}", a.get_usize());
592/// println!("a.get_ssize() = {}", a.get_ssize());
593/// println!("a.get_uint() = {}", a.get_uint());
594/// println!("a.get_sint() = {}", a.get_sint());
595/// assert_eq!(a.get(), 4048161615_usize);
596/// assert_eq!(a.get_signed(), -246805681_isize);
597/// assert_eq!(a.get_usize(), 4048161615_usize);
598/// assert_eq!(a.get_ssize(), -246805681_isize);
599/// assert_eq!(a.get_uint(), 4048161615_u32);
600/// assert_eq!(a.get_sint(), -246805681_i32);
601///
602/// for i in 0..2
603/// { println!("a.get_ushort_({}) = {}", i, a.get_ushort_(i)); }
604/// for i in 0..2
605/// { println!("a.get_sshort_({}) = {}", i, a.get_sshort_(i)); }
606/// for i in 0..4
607/// { println!("a.get_ubyte_({}) = {}", i, a.get_ubyte_(i)); }
608/// for i in 0..4
609/// { println!("a.get_sbyte_({}) = {}", i, a.get_sbyte_(i)); }
610/// assert_eq!(a.get_ushort_(0), 2895_u16);
611/// assert_eq!(a.get_ushort_(1), 61770_u16);
612/// assert_eq!(a.get_sshort_(0), 2895_i16);
613/// assert_eq!(a.get_sshort_(1), -3766_i16);
614/// assert_eq!(a.get_ubyte_(0), 79_u8);
615/// assert_eq!(a.get_ubyte_(1), 11_u8);
616/// assert_eq!(a.get_ubyte_(2), 74_u8);
617/// assert_eq!(a.get_ubyte_(3), 241_u8);
618/// assert_eq!(a.get_sbyte_(0), 79_i8);
619/// assert_eq!(a.get_sbyte_(1), 11_i8);
620/// assert_eq!(a.get_sbyte_(2), 74_i8);
621/// assert_eq!(a.get_sbyte_(3), -15_i8);
622/// ```
623/// You can use `SizeUnion` as if you used `usize`. You can perform all kinds
624/// of arithmetic operations such as addition, subtraction, multiplication,
625/// and division (div and rem), and other operations which are available for
626/// `usize`. If you use `SizeUnion` with the help of `SmallUInt`, it will be
627/// even more powerful and convenient. In this case, you don't even have to
628/// import (use) `cryptocol::number::SizeUnion`.
629///
630/// ## Example 3
631/// ```
632/// use cryptocol::number::SmallUInt;
633///
634/// let a_sizeunion = 12345678_usize.into_sizeunion();
635/// let b_sizeunion = 87654321_usize.into_sizeunion();
636/// let c_sizeunion = a_sizeunion.wrapping_add(b_sizeunion);
637///
638/// println!("{} + {} = {}", a_sizeunion, b_sizeunion, c_sizeunion);
639/// assert_eq!(c_sizeunion.get(), 99999999_usize);
640///
641/// for i in 0..2
642/// { println!("c_sizeunion.get_ushort_({}) = {}", i, c_sizeunion.get_ushort_(i)); }
643/// assert_eq!(c_sizeunion.get_ushort_(0), 57599_u16);
644/// assert_eq!(c_sizeunion.get_ushort_(1), 1525_u16);
645///
646/// for i in 0..4
647/// { println!("c_sizeunion.get_ubyte_({}) = {}", i, c_sizeunion.get_ubyte_(i)); }
648/// assert_eq!(c_sizeunion.get_ubyte_(0), 255_u8);
649/// assert_eq!(c_sizeunion.get_ubyte_(1), 224_u8);
650/// assert_eq!(c_sizeunion.get_ubyte_(2), 245_u8);
651/// assert_eq!(c_sizeunion.get_ubyte_(3), 5_u8);
652///
653/// let d_sizeunion = b_sizeunion - a_sizeunion;
654/// println!("{} - {} = {}", b_sizeunion, a_sizeunion, d_sizeunion);
655/// assert_eq!(d_sizeunion.get(), 75308643_usize);
656///
657/// for i in 0..2
658/// { println!("d_sizeunion.get_ushort_({}) = {}", i, d_sizeunion.get_ushort_(i)); }
659/// assert_eq!(d_sizeunion.get_ushort_(0), 7779_u16);
660/// assert_eq!(d_sizeunion.get_ushort_(1), 1149_u16);
661///
662/// for i in 0..4
663/// { println!("d_sizeunion.get_ubyte_({}) = {}", i, d_sizeunion.get_ubyte_(i)); }
664/// assert_eq!(d_sizeunion.get_ubyte_(0), 99_u8);
665/// assert_eq!(d_sizeunion.get_ubyte_(1), 30_u8);
666/// assert_eq!(d_sizeunion.get_ubyte_(2), 125_u8);
667/// assert_eq!(d_sizeunion.get_ubyte_(3), 4_u8);
668///
669/// let e_sizeunion = d_sizeunion * 3_usize.into_sizeunion();
670/// println!("{} * {} = {}", d_sizeunion, 3_usize.into_sizeunion(), e_sizeunion);
671/// assert_eq!(e_sizeunion.get(), 225925929_usize);
672///
673/// let f_sizeunion = c_sizeunion / 10_usize.into_sizeunion();
674/// println!("{} / {} = {}", c_sizeunion, 10_usize.into_sizeunion(), f_sizeunion);
675/// assert_eq!(f_sizeunion.get(), 9999999_usize);
676///
677/// let g_sizeunion = c_sizeunion % 10_usize.into_sizeunion();
678/// println!("{} % {} = {}", c_sizeunion, 10_usize.into_sizeunion(), g_sizeunion);
679/// assert_eq!(g_sizeunion.get(), 9_usize);
680/// ```
681///
682/// # Big-endian issue
683/// It is just experimental for Big Endian CPUs. So, you are not encouraged
684/// to use it for serious purpose. Only use this crate for Big-endian CPUs
685/// with your own full responsibility.
686#[cfg(target_pointer_width = "32")]
687#[derive(Copy, Clone)]
688#[allow(dead_code)]
689pub union SizeUnion
690{
691 /// The biggest unsigned element for compatibility with other unions
692 this: usize,
693
694 /// The biggest signed element for compatibility with other unions
695 that: isize,
696
697 /// The usize type element whose size is the same as the SizeUnion
698 pub u_size: usize,
699
700 /// The isize type element whose size is the same as the SizeUnion
701 pub s_size: isize,
702
703 /// The biggest unsigned element which is 32-bit unsigned integer
704 pub uint: u32,
705
706 /// The biggest signed element which is 32-bit unsigned integer
707 pub sint: i32,
708
709 /// The secondly biggest unsigned element array whose elements are
710 /// 16-bit unsigned integer
711 pub ushort: [u16; 2],
712
713 /// The secondly biggest unsigned element array whose elements are
714 /// 16-bit signed integer
715 pub sshort: [i16; 2],
716
717 /// The thirdly biggest unsigned element array whose elements are
718 /// 8-bit unsigned integer
719 pub ubyte: [u8; 4],
720
721 /// The thirdly biggest unsigned element array whose elements are
722 /// 8-bit signed integer
723 pub sbyte: [i8; 4],
724}
725
726
727/// # Introduction
728/// This union `SizeUnion` is for slicing `usize` into two `u8`s, and/or
729/// two `i8`s.
730///
731/// Sometimes, for example, we need to slice `usize` data into two `u8` pieces
732/// which include a higher byte and a lower byte.
733/// In that case, `SizeUnion` will be very helpful.
734///
735/// # Quick Start
736/// In order to use this union, you have to import (use)
737/// `cryptocol::number::SizeUnion` as follows.
738///
739/// ## Example 1
740/// ```
741/// use cryptocol::number::SizeUnion;
742/// ```
743/// You can use the methods `get()`, `get_signed()`, `get_ushort()`, and
744/// `get_sshort()` in order to obtain the data of `usize` in various types.
745/// And, you can also slice the data of `usize` into two `u8` type data
746/// or two `i8` type data by using the methods `get_ubyte()`, `get_sbyte()`,
747/// `get_ubyte_()`, and `get_sbyte_()`.
748///
749/// ## Example 2
750/// ```
751/// use cryptocol::number::SizeUnion;
752///
753/// let a = SizeUnion::new_with_signed(2895_isize);
754///
755/// println!("a.get() = {}", a.get());
756/// println!("a.get_signed() = {}", a.get_signed());
757/// println!("a.get_usize() = {}", a.get_usize());
758/// println!("a.get_ssize() = {}", a.get_ssize());
759/// println!("a.get_ushort() = {}", a.get_ushort());
760/// println!("a.get_sshort() = {}", a.get_sshort());
761/// assert_eq!(a.get(), 2895_usize);
762/// assert_eq!(a.get_signed(), 2895_isize);
763/// assert_eq!(a.get_usize(), 2895_usize);
764/// assert_eq!(a.get_ssize(), 2895_isize);
765/// assert_eq!(a.get_ushort(), 2895_u16);
766/// assert_eq!(a.get_sshort(), 2895_i16);
767///
768/// for i in 0..2
769/// { println!("a.get_ubyte_({}) = {}", i, a.get_ubyte_(i)); }
770/// for i in 0..2
771/// { println!("a.get_sbyte_({}) = {}", i, a.get_sbyte_(i)); }
772/// assert_eq!(a.get_ubyte_(0), 79_u8);
773/// assert_eq!(a.get_ubyte_(1), 11_u8);
774/// assert_eq!(a.get_sbyte_(0), 79_i8);
775/// assert_eq!(a.get_sbyte_(1), 11_i8);
776/// ```
777/// You can use `SizeUnion` as if you used `usize`. You can perform all kinds
778/// of arithmetic operations such as addition, subtraction, multiplication,
779/// and division (div and rem), and other operations which are available for
780/// `usize`. If you use `SizeUnion` with the help of `SmallUInt`, it will be
781/// even more powerful and convenient. In this case, you don't even have to
782/// import (use) `cryptocol::number::SizeUnion`.
783///
784/// ## Example 3
785/// ```
786/// use cryptocol::number::SmallUInt;
787///
788/// let a_sizeunion = 1234_usize.into_sizeunion();
789/// let b_sizeunion = 4321_usize.into_sizeunion();
790/// let c_sizeunion = a_sizeunion.wrapping_add(b_sizeunion);
791///
792/// println!("{} + {} = {}", a_sizeunion, b_sizeunion, c_sizeunion);
793/// assert_eq!(c_sizeunion.get(), 5555_usize);
794///
795/// for i in 0..2
796/// { println!("c_sizeunion.get_ubyte_({}) = {}", i, c_sizeunion.get_ubyte_(i)); }
797/// assert_eq!(c_sizeunion.get_ubyte_(0), 179_u8);
798/// assert_eq!(c_sizeunion.get_ubyte_(1), 21_u8);
799///
800/// let d_sizeunion = b_sizeunion - a_sizeunion;
801/// println!("{} - {} = {}", b_sizeunion, a_sizeunion, d_sizeunion);
802/// assert_eq!(d_sizeunion.get(), 3087_usize);
803///
804/// for i in 0..2
805/// { println!("d_sizeunion.get_ubyte_({}) = {}", i, d_sizeunion.get_ubyte_(i)); }
806/// assert_eq!(d_sizeunion.get_ubyte_(0), 15_u8);
807/// assert_eq!(d_sizeunion.get_ubyte_(1), 12_u8);
808///
809/// let e_sizeunion = d_sizeunion * 3_usize.into_sizeunion();
810/// println!("{} * {} = {}", d_sizeunion, 3_usize.into_sizeunion(), e_sizeunion);
811/// assert_eq!(e_sizeunion.get(), 9261_usize);
812///
813/// let f_sizeunion = c_sizeunion / 10_usize.into_sizeunion();
814/// println!("{} / {} = {}", c_sizeunion, 10_usize.into_sizeunion(), f_sizeunion);
815/// assert_eq!(f_shortunion.get(), 555_usize);
816///
817/// let g_sizeunion = c_sizeunion % 10_usize.into_sizeunion();
818/// println!("{} % {} = {}", c_sizeunion, 10_usize.into_sizeunion(), g_sizeunion);
819/// assert_eq!(g_sizeunion.get(), 5_usize);
820/// ```
821///
822/// # Big-endian issue
823/// It is just experimental for Big Endian CPUs. So, you are not encouraged
824/// to use it for serious purpose. Only use this crate for Big-endian CPUs
825/// with your own full responsibility.
826#[cfg(target_pointer_width = "16")]
827#[derive(Copy, Clone)]
828#[allow(dead_code)]
829#[allow(non_camel_case_types)]
830pub union SizeUnion
831{
832 /// The biggest unsigned element for compatibility with other unions
833 this: usize,
834
835 /// The biggest signed element for compatibility with other unions
836 pub that: isize,
837
838 /// The usize type element whose size is the same as the SizeUnion
839 pub u_size: usize,
840
841 /// The isize type element whose size is the same as the SizeUnion
842 pub s_size: isize,
843
844 /// The biggest unsigned element which is 16-bit unsigned integer
845 pub ushort: u16,
846
847 /// The biggest signed element which is 16-bit unsigned integer
848 pub sshort: i16,
849
850 /// The secondly biggest unsigned element array whose elements are
851 /// 8-bit unsigned integer
852 pub ubyte: [u8; 2],
853
854 /// The secondly biggest signed element array whose elements are
855 /// 8-bit unsigned integer
856 pub sbyte: [i8; 2],
857}
858
859
860// /// # Introduction
861// /// This union `SizeUnion` is for using with `u8` or `i8`.
862// ///
863// /// # Quick Start
864// /// In order to use this union, you have to import (use)
865// /// `cryptocol::number::SizeUnion` as follows.
866// ///
867// /// ## Example 1
868// /// ```
869// /// use cryptocol::number::SizeUnion;
870// /// ```
871// /// You can use the methods `get()`, `get_signed()`, `get_ubyte()`, and
872// /// `get_sbyte()` in order to obtain the data of `usize` in various types.
873// ///
874// /// ## Example 2
875// /// ```
876// /// use cryptocol::number::SizeUnion;
877// ///
878// /// let a = SizeUnion::new_with_signed(79_isize);
879// ///
880// /// println!("a.get() = {}", a.get());
881// /// println!("a.get_signed() = {}", a.get_signed());
882// /// println!("a.get_usize() = {}", a.get_usize());
883// /// println!("a.get_ssize() = {}", a.get_ssize());
884// /// println!("a.get_ubyte() = {}", a.get_ubyte());
885// /// println!("a.get_sbyte() = {}", a.get_sbyte());
886// /// assert_eq!(a.get(), 79_usize);
887// /// assert_eq!(a.get_signed(), 79_isize);
888// /// assert_eq!(a.get_usize(), 79_usize);
889// /// assert_eq!(a.get_ssize(), 79_isize);
890// /// assert_eq!(a.get_ubyte(), 79_u8);
891// /// assert_eq!(a.get_sbyte(), 79_u8);
892// /// ```
893// /// You can use `SizeUnion` as if you used `usize`. You can perform all kinds
894// /// of arithmetic operations such as addition, subtraction, multiplication,
895// /// and division (div and rem), and other operations which are available for
896// /// `usize`. If you use `SizeUnion` with the help of `SmallUInt`, it will be
897// /// even more powerful and convenient. In this case, you don't even have to
898// /// import (use) `cryptocol::number::SizeUnion`.
899// ///
900// /// ## Example 3
901// /// ```
902// /// use cryptocol::number::SmallUInt;
903// ///
904// /// let a_sizeunion = 12_usize.into_sizeunion();
905// /// let b_sizeunion = 87_usize.into_sizeunion();
906// /// let c_sizeunion = a_sizeunion.wrapping_add(b_sizeunion);
907// ///
908// /// println!("{} + {} = {}", a_sizeunion, b_sizeunion, c_sizeunion);
909// /// assert_eq!(c_sizeunion.get(), 55_usize);
910// ///
911// /// let d_sizeunion = b_sizeunion - a_sizeunion;
912// /// println!("{} - {} = {}", b_sizeunion, a_sizeunion, d_sizeunion);
913// /// assert_eq!(d_sizeunion.get(), 75_usize);
914// ///
915// /// let e_sizeunion = d_sizeunion * 3_usize.into_sizeunion();
916// /// println!("{} * {} = {}", d_sizeunion, 3_usize.into_sizeunion(), e_sizeunion);
917// /// assert_eq!(e_sizeunion.get(), 225_usize);
918// ///
919// /// let f_sizeunion = c_sizeunion / 10_usize.into_sizeunion();
920// /// println!("{} / {} = {}", c_sizeunion, 10_usize.into_sizeunion(), f_sizeunion);
921// /// assert_eq!(f_shortunion.get(), 9_usize);
922// ///
923// /// let g_sizeunion = c_sizeunion % 10_usize.into_sizeunion();
924// /// println!("{} % {} = {}", c_sizeunion, 10_usize.into_sizeunion(), g_sizeunion);
925// /// assert_eq!(g_sizeunion.get(), 9_usize);
926// /// ```
927// ///
928// /// # Big-endian issue
929// /// It is just experimental for Big Endian CPUs. So, you are not encouraged
930// /// to use it for serious purpose. Only use this crate for Big-endian CPUs
931// /// with your own full responsibility.
932// #[cfg(target_pointer_width = "8")]
933// #[derive(Copy, Clone)]
934// #[allow(dead_code)]
935// #[allow(non_camel_case_types)]
936// pub union SizeUnion
937// {
938// /// The biggest unsigned element for compatibility with other unions
939// pub this: usize,
940
941// /// The biggest signed element for compatibility with other unions
942// pub that: isize,
943
944// /// The usize type element whose size is the same as the SizeUnion
945// pub u_size: usize,
946
947// /// The isize type element whose size is the same as the SizeUnion
948// pub s_size: isize,
949
950// /// The biggest unsigned element which is 8-bit unsigned integer
951// pub ubyte: u8,
952
953// /// The biggest signed element which is 8-bit unsigned integer
954// pub sbyte: i8,
955// }
956
957
958impl SizeUnion
959{
960 // pub const fn new() -> Self
961 /// Constructs a new `SizeUnion`.
962 ///
963 /// # Output
964 /// A new object of `SizeUnion`.
965 ///
966 /// # Initialization
967 /// All the fields of the constructed object will be
968 /// initialized with `0`.
969 ///
970 /// # Example
971 /// ```
972 /// use cryptocol::number::SizeUnion;
973 /// let a = SizeUnion::new();
974 /// println!("a = {}", a.get());
975 /// assert_eq!(a.get(), 0_usize);
976 /// ```
977 #[inline] pub const fn new() -> Self { Self { u_size: 0 } }
978
979 // pub const fn new_with(u_size: usize) -> Self
980 /// Constructs a new `SizeUnion` with initializing it with `u_size`.
981 ///
982 /// # Output
983 /// A new object of `SizeUnion` initialized with the value `u_size`.
984 ///
985 /// # Initialization
986 /// The field of the constructed object will be initialized with `u_size`.
987 ///
988 /// Example
989 /// ```
990 /// use cryptocol::number::SizeUnion;
991 /// let a = SizeUnion::new_with(234_usize);
992 /// println!("a = {}", a.get());
993 /// assert_eq!(a.get(), 234_usize);
994 /// ```
995 #[inline] pub const fn new_with(u_size: usize) -> Self { Self { u_size } }
996
997 // pub const fn new_with_signed(s_size: isize) -> Self
998 /// Constructs a new `SizeUnion` with initializing it with `isize`.
999 ///
1000 /// # Output
1001 /// A new object of `SizeUnion` initialized with the value `isize`.
1002 ///
1003 /// # Initialization
1004 /// The field of the constructed object will be initialized with `isize`.
1005 ///
1006 /// Example
1007 /// ```
1008 /// use cryptocol::number::SizeUnion;
1009 /// let a = SizeUnion::new_with_signed(-123_isize);
1010 /// println!("a = {}", a.get_signed());
1011 /// assert_eq!(a.get_signed(), -123_isize);
1012 /// ```
1013 #[inline] pub const fn new_with_signed(s_size: isize) -> Self { Self { s_size } }
1014
1015 crate::number::new_with_small_uint!();
1016
1017 // pub const fn new_with_u128(num: u128) -> Self
1018 /// Constructs a new `SizeUnion` with initializing it with the lowest
1019 /// `usize`-length part of `num`.
1020 ///
1021 /// # Output
1022 /// A new object of `SizeUnion` initialized with the value of
1023 /// the lowest `usize`-length part of `num`.
1024 ///
1025 /// # Initialization
1026 /// The field of the constructed object will be initialized with
1027 /// the value of the lowest `usize`-length part of `num`.
1028 ///
1029 /// Example
1030 /// ```
1031 /// use cryptocol::number::SizeUnion;
1032 /// let a = SizeUnion::new_with_u128(123456789012345678901234567890123456789_u128);
1033 /// println!("a = {}", a.get());
1034 /// #[cfg(target_pointer_width = "128")] assert_eq!(a.get(), 123456789012345678901234567890123456789_usize);
1035 /// #[cfg(target_pointer_width = "64")] assert_eq!(a.get(), 12312739301371248917_usize);
1036 /// #[cfg(target_pointer_width = "32")] assert_eq!(a.get(), 2923004181_usize);
1037 /// #[cfg(target_pointer_width = "16")] assert_eq!(a.get(), 33045_usize);
1038 /// #[cfg(target_pointer_width = "8")] assert_eq!(a.get(), 21_usize);
1039 /// ```
1040 #[inline] pub const fn new_with_u128(num: u128) -> Self { Self { u_size: num as usize } }
1041
1042 // pub const fn new_with_bool(b: bool) -> Self
1043 /// Constructs a new `SizeUnion` with initializing it with the value of `b`.
1044 ///
1045 /// # Output
1046 /// A new object of `SizeUnion` initialized with the value of `b`
1047 ///
1048 /// # Initialization
1049 /// The field of the constructed object will be initialized with
1050 /// the value of `b`.
1051 /// If `b` is `true`, `self` will have the value `1`.
1052 /// If `b` is `false`, `self` will have the value `0`.
1053 ///
1054 /// Example
1055 /// ```
1056 /// use cryptocol::number::SizeUnion;
1057 /// let a = SizeUnion::new_with_bool(true);
1058 /// let b = SizeUnion::new_with_bool(false);
1059 /// println!("a = {}", a.get());
1060 /// println!("b = {}", b.get());
1061 /// assert_eq!(a.get(), 1_usize);
1062 /// assert_eq!(b.get(), 0_usize);
1063 /// ```
1064 #[inline] pub const fn new_with_bool(b: bool) -> Self { Self { u_size: b as usize } }
1065
1066 // pub fn get(self) -> usize
1067 /// Returns its value as `usize`.
1068 ///
1069 /// # Output
1070 /// Its value as `usize`
1071 ///
1072 /// Example
1073 /// ```
1074 /// use cryptocol::number::SizeUnion;
1075 /// let a = SizeUnion::new_with(250_usize);
1076 /// println!("a = {}", a.get());
1077 /// assert_eq!(a.get(), 250_usize);
1078 /// ```
1079 #[inline] pub fn get(self) -> usize { unsafe { self.u_size } }
1080
1081 // pub fn set(&mut self, val: usize)
1082 /// Sets its value with `val` of type `usize`
1083 ///
1084 /// Example
1085 /// ```
1086 /// use cryptocol::number::SizeUnion;
1087 /// let mut a = SizeUnion::new();
1088 /// a.set(234_usize);
1089 /// println!("a = {}", a.get());
1090 /// assert_eq!(a.get(), 234_usize);
1091 /// ```
1092 #[inline] pub fn set(&mut self, val: usize) { self.u_size = val; }
1093
1094 // pub fn get_signed(self) -> isize
1095 /// Returns its value as `isize`.
1096 ///
1097 /// # Output
1098 /// Its value as `isize`
1099 ///
1100 /// Example
1101 /// ```
1102 /// use cryptocol::number::SizeUnion;
1103 /// let a = SizeUnion::new_with_signed(-123_isize);
1104 /// println!("a = {}", a.get_signed());
1105 /// assert_eq!(a.get_signed(), -123_isize);
1106 /// ```
1107 #[inline] pub fn get_signed(self) -> isize { unsafe { self.s_size } }
1108
1109 // pub fn set_signed(&mut self, val: isize)
1110 /// Sets its value with `val` of type `isize`
1111 ///
1112 /// Example
1113 /// ```
1114 /// use cryptocol::number::SizeUnion;
1115 /// let mut a = SizeUnion::new();
1116 /// a.set_signed(-123_isize);
1117 /// println!("a = {}", a.get_signed());
1118 /// assert_eq!(a.get_signed(), -123_isize);
1119 /// ```
1120 #[inline] pub fn set_signed(&mut self, val: isize) { self.s_size = val; }
1121
1122 crate::number::get_set_size_fit!();
1123
1124 // #[cfg(target_pointer_width = "128")] crate::number::get_set_byte!(16);
1125 #[cfg(target_pointer_width = "64")] crate::number::get_set_byte!(8);
1126 #[cfg(target_pointer_width = "32")] crate::number::get_set_byte!(4);
1127 #[cfg(target_pointer_width = "16")] crate::number::get_set_byte!(2);
1128 // #[cfg(target_pointer_width = "8")] crate::number::get_set_byte_fit!();
1129
1130 // #[cfg(target_pointer_width = "128")] crate::number::get_set_short!(8);
1131 #[cfg(target_pointer_width = "64")] crate::number::get_set_short!(4);
1132 #[cfg(target_pointer_width = "32")] crate::number::get_set_short!(2);
1133 #[cfg(target_pointer_width = "16")] crate::number::get_set_short_fit!();
1134
1135 // #[cfg(target_pointer_width = "128")] crate::number::get_set_int!(4);
1136 #[cfg(target_pointer_width = "64")] crate::number::get_set_int!(2);
1137 #[cfg(target_pointer_width = "32")] crate::number::get_set_int_fit!();
1138
1139 // #[cfg(target_pointer_width = "128")] crate::number::get_set_long!(2);
1140 #[cfg(target_pointer_width = "64")] crate::number::get_set_long_fit!();
1141
1142 // #[cfg(target_pointer_width = "128")] crate::number::get_set_longer_fit!();
1143
1144 crate::number::integer_union_methods!(usize);
1145
1146 // pub fn as_ptr(&self) -> *const usize
1147 /// Returns its pointer as *const usize
1148 #[inline] pub fn as_ptr(&self) -> *const usize { unsafe { self.ubyte.as_ptr() as *const usize } }
1149
1150 // pub fn as_mut_ptr(&self) -> *mut usize
1151 /// Returns its pointer as *mut usize
1152 #[inline] pub fn as_mut_ptr(&mut self) -> *mut usize { unsafe { self.ubyte.as_mut_ptr() as *mut usize } }
1153}
1154
1155
1156
1157crate::number::operators_for_integer_unions_impl! { SizeUnion }
1158
1159crate::number::shift_ops_for_integer_unions_impl! { SizeUnion, i8 }
1160crate::number::shift_ops_for_integer_unions_impl! { SizeUnion, i16 }
1161crate::number::shift_ops_for_integer_unions_impl! { SizeUnion, i32 }
1162crate::number::shift_ops_for_integer_unions_impl! { SizeUnion, i64 }
1163crate::number::shift_ops_for_integer_unions_impl! { SizeUnion, i128 }
1164crate::number::shift_ops_for_integer_unions_impl! { SizeUnion, isize }
1165
1166crate::number::shift_ops_for_integer_unions_impl! { SizeUnion, u8 }
1167crate::number::shift_ops_for_integer_unions_impl! { SizeUnion, u16 }
1168crate::number::shift_ops_for_integer_unions_impl! { SizeUnion, u32 }
1169crate::number::shift_ops_for_integer_unions_impl! { SizeUnion, u64 }
1170crate::number::shift_ops_for_integer_unions_impl! { SizeUnion, u128 }
1171crate::number::shift_ops_for_integer_unions_impl! { SizeUnion, usize }
1172
1173crate::number::format_for_integer_unions_impl! { SizeUnion }
1174
1175
1176crate::number::shift_ops_for_integer_unions_by_union_impl! { SizeUnion, ShortUnion }
1177crate::number::shift_ops_for_integer_unions_by_union_impl! { SizeUnion, IntUnion }
1178crate::number::shift_ops_for_integer_unions_by_union_impl! { SizeUnion, LongUnion }
1179crate::number::shift_ops_for_integer_unions_by_union_impl! { SizeUnion, LongerUnion }
1180crate::number::shift_ops_for_integer_unions_by_union_impl! { SizeUnion, SizeUnion }
1181
1182impl Debug for SizeUnion
1183{
1184 /// Formats the value using the given formatter.
1185 ///
1186 /// # Features
1187 /// When used with format specifier :?, the output is printed for debug.
1188 /// When used with the alternate format specifier #?, the output is
1189 /// pretty-printed.
1190 ///
1191 /// # Example for the format specifier :?
1192 /// ```
1193 /// use cryptocol::number::*;
1194 /// let a_size = SizeUnion::new_with_signed(-1234567890123456789_isize);
1195 /// println!("a_size = {:?}", a_size);
1196 /// #[cfg(target_pointer_width = "64")] assert_eq!(format!("{a_size:?}"), "SizeUnion { this: 17212176183586094827, that: -1234567890123456789, u_size: 17212176183586094827, s_size: -1234567890123456789, ulong: 17212176183586094827, slong: -1234567890123456789, uint: [2182512363, 4007522059], sint: [-2112454933, -287445237], ushort: [32491, 33302, 61195, 61149], sshort: [32491, -32234, -4341, -4387], ubyte: [235, 126, 22, 130, 11, 239, 221, 238], sbyte: [-21, 126, 22, -126, 11, -17, -35, -18] }");
1197 /// ```
1198 ///
1199 /// # Example for the format specifier :#?
1200 /// ```
1201 /// use cryptocol::number::*;
1202 /// let a_size = SizeUnion::new_with_signed(-1234567890123456789_isize);
1203 /// println!("a_size = {:#?}", a_size);
1204 /// #[cfg(target_pointer_width = "64")] assert_eq!(format!("{a_size:#?}"), r#"SizeUnion {
1205 /// this: 17212176183586094827,
1206 /// that: -1234567890123456789,
1207 /// u_size: 17212176183586094827,
1208 /// s_size: -1234567890123456789,
1209 /// ulong: 17212176183586094827,
1210 /// slong: -1234567890123456789,
1211 /// uint: [
1212 /// 2182512363,
1213 /// 4007522059,
1214 /// ],
1215 /// sint: [
1216 /// -2112454933,
1217 /// -287445237,
1218 /// ],
1219 /// ushort: [
1220 /// 32491,
1221 /// 33302,
1222 /// 61195,
1223 /// 61149,
1224 /// ],
1225 /// sshort: [
1226 /// 32491,
1227 /// -32234,
1228 /// -4341,
1229 /// -4387,
1230 /// ],
1231 /// ubyte: [
1232 /// 235,
1233 /// 126,
1234 /// 22,
1235 /// 130,
1236 /// 11,
1237 /// 239,
1238 /// 221,
1239 /// 238,
1240 /// ],
1241 /// sbyte: [
1242 /// -21,
1243 /// 126,
1244 /// 22,
1245 /// -126,
1246 /// 11,
1247 /// -17,
1248 /// -35,
1249 /// -18,
1250 /// ],
1251 /// }"#);
1252 /// ```
1253 ///
1254 /// # Plagiarism in descryption
1255 /// This method works exactly the same way as the normal method fmt() of
1256 /// Debug. So, all the description of this method is mainly the
1257 /// same as that of the implementation of the method fmt() of Debug for the
1258 /// primitive unsigned integer types except example codes. Confer to the
1259 /// descryptions that are linked to in the section _Reference_. This
1260 /// plagiarism is not made maliciously but is made for the reason of
1261 /// effectiveness and efficiency so that users may understand better and
1262 /// easily how to use this method with simiilarity to the method
1263 /// Debug() of implementation for the primitive unsigned integer
1264 /// types.
1265 ///
1266 /// # References
1267 /// - If you want to know about the method of Debug for the primitive type,
1268 /// read [here](https://doc.rust-lang.org/std/fmt/trait.Debug.html).
1269 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
1270 {
1271 let mut ff = f.debug_struct("SizeUnion");
1272 ff.field("this", &self.get())
1273 .field("that", &self.get_signed())
1274 .field("u_size", &self.get_usize())
1275 .field("s_size", &self.get_ssize());
1276
1277 // #[cfg(target_pointer_width = "128")]
1278 // ff.field("ulonger", unsafe { &self.ulonger } )
1279 // .field("slonger", unsafe { &self.slonger } )
1280 // .field("ulong", unsafe { &self.ulong } )
1281 // .field("slong", unsafe { &self.slong } )
1282 // .field("uint", unsafe { &self.uint } )
1283 // .field("sint", unsafe { &self.sint } )
1284 // .field("ushort", unsafe { &self.ushort } )
1285 // .field("sshort", unsafe { &self.sshort } )
1286 // .field("ubyte", unsafe { &self.ubyte } )
1287 // .field("sbyte", unsafe { &self.sbyte } );
1288
1289 #[cfg(target_pointer_width = "64")]
1290 ff.field("ulong", unsafe { &self.ulong } )
1291 .field("slong", unsafe { &self.slong } )
1292 .field("uint", unsafe { &self.uint } )
1293 .field("sint", unsafe { &self.sint } )
1294 .field("ushort", unsafe { &self.ushort } )
1295 .field("sshort", unsafe { &self.sshort } )
1296 .field("ubyte", unsafe { &self.ubyte } )
1297 .field("sbyte", unsafe { &self.sbyte } );
1298
1299 #[cfg(target_pointer_width = "32")]
1300 ff.field("uint", unsafe { &self.uint } )
1301 .field("sint", unsafe { &self.sint } )
1302 .field("ushort", unsafe { &self.ushort } )
1303 .field("sshort", unsafe { &self.sshort } )
1304 .field("ubyte", unsafe { &self.ubyte } )
1305 .field("sbyte", unsafe { &self.sbyte } );
1306
1307 #[cfg(target_pointer_width = "16")]
1308 ff.field("ushort", unsafe { &self.ushort } )
1309 .field("sshort", unsafe { &self.sshort } )
1310 .field("ubyte", unsafe { &self.ubyte } )
1311 .field("sbyte", unsafe { &self.sbyte } );
1312
1313 // #[cfg(target_pointer_width = "8")]
1314 // ff.field("ubyte", unsafe { &self.ubyte } )
1315 // .field("sbyte", unsafe { &self.sbyte } );
1316
1317 ff.finish()
1318 }
1319}