use std::cmp::{ PartialEq, PartialOrd, Ordering };
use std::ops::{ BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not,
Shl, ShlAssign, Shr, ShrAssign,
Add, AddAssign, Sub, SubAssign, Mul, MulAssign,
Div, DivAssign, Rem, RemAssign };
use std::fmt::{ self, Alignment, Error, Formatter, Display, Debug, Pointer,
Binary, Octal, LowerHex, UpperHex, LowerExp, UpperExp };
use crate::number::{ SmallUInt, IntUnion, LongUnion, LongerUnion, SizeUnion };
use crate::number::{ union_calc_assign_to_calc, union_fmt_with_radix, union_fmt_with_exponent };
#[derive(Copy, Clone, Eq)]
#[allow(dead_code)]
pub union ShortUnion
{
this: u16,
that: i16,
ushort: u16,
sshort: i16,
ubyte: [u8; 2],
sbyte: [i8; 2],
#[cfg(target_pointer_width = "16")] u_size: usize,
#[cfg(target_pointer_width = "16")] s_size: isize,
}
impl ShortUnion
{
#[inline] pub const fn new() -> Self { Self { ushort: 0 } }
#[inline] pub const fn new_with(ushort: u16) -> Self { Self { ushort } }
#[inline] pub const fn new_with_signed(sshort: i16) -> Self { Self { sshort } }
#[inline] pub const fn new_with_ubytes(ubyte: [u8; 2]) -> Self { Self { ubyte } }
#[inline] pub const fn new_with_u128(num: u128) -> Self { Self { ushort: num as u16 } }
#[inline] pub const fn new_with_bool(b: bool) -> Self { Self { ushort: b as u16 } }
#[inline] pub fn get(self) -> u16 { unsafe { self.this } }
#[inline] pub fn set(&mut self, val: u16) { self.this = val; }
#[inline] pub fn get_signed(self) -> i16 { unsafe { self.that } }
#[inline] pub fn set_signed(&mut self, val: i16) { self.that = val; }
crate::number::get_set_short_fit!();
crate::number::get_set_byte!(2);
#[cfg(target_pointer_width = "16")] crate::number::get_set_size_fit!();
crate::number::integer_union_methods!(u16);
#[inline] pub fn as_ptr(&self) -> *const u16 { unsafe { self.ubyte.as_ptr() as *const u16 } }
#[inline] pub fn as_mut_ptr(&mut self) -> *mut u16 { unsafe { self.ubyte.as_mut_ptr() as *mut u16 } }
}
crate::number::operators_for_integer_unions_impl! { ShortUnion }
crate::number::shift_ops_for_integer_unions_impl! { ShortUnion, i8 }
crate::number::shift_ops_for_integer_unions_impl! { ShortUnion, i16 }
crate::number::shift_ops_for_integer_unions_impl! { ShortUnion, i32 }
crate::number::shift_ops_for_integer_unions_impl! { ShortUnion, i64 }
crate::number::shift_ops_for_integer_unions_impl! { ShortUnion, i128 }
crate::number::shift_ops_for_integer_unions_impl! { ShortUnion, isize }
crate::number::shift_ops_for_integer_unions_impl! { ShortUnion, u8 }
crate::number::shift_ops_for_integer_unions_impl! { ShortUnion, u16 }
crate::number::shift_ops_for_integer_unions_impl! { ShortUnion, u32 }
crate::number::shift_ops_for_integer_unions_impl! { ShortUnion, u64 }
crate::number::shift_ops_for_integer_unions_impl! { ShortUnion, u128 }
crate::number::shift_ops_for_integer_unions_impl! { ShortUnion, usize }
crate::number::shift_ops_for_integer_unions_by_union_impl! { ShortUnion, ShortUnion }
crate::number::shift_ops_for_integer_unions_by_union_impl! { ShortUnion, IntUnion }
crate::number::shift_ops_for_integer_unions_by_union_impl! { ShortUnion, LongUnion }
crate::number::shift_ops_for_integer_unions_by_union_impl! { ShortUnion, LongerUnion }
crate::number::shift_ops_for_integer_unions_by_union_impl! { ShortUnion, SizeUnion }
crate::number::format_for_integer_unions_impl! { ShortUnion }
impl Ord for ShortUnion
{
#[inline]
fn cmp(&self, other: &Self) -> Ordering
{
self.get().cmp(&other.get())
}
}
impl Debug for ShortUnion
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
let mut ff = f.debug_struct("ShortUnion");
ff.field("this", &self.get())
.field("that", &self.get_signed())
.field("ushort", &self.get_ushort())
.field("sshort", &self.get_sshort())
.field("ubyte", &[self.get_ubyte_(0), self.get_ubyte_(1)])
.field("sbyte", &[self.get_sbyte_(0), self.get_sbyte_(1)]);
#[cfg(target_pointer_width = "16")] ff.field("u_size", unsafe { &self.get_usize() } )
.field("s_size", unsafe { &self.get_ssize() } );
ff.finish()
}
}