Forgot to check in the ByteSized file in the last commit.
This commit is contained in:
parent
ed44f4f861
commit
8075e68607
116
src/byte_sized.rs
Normal file
116
src/byte_sized.rs
Normal file
@ -0,0 +1,116 @@
|
||||
/// The size of an 8 bit whole number. 1 byte.
|
||||
pub const U8_BYTES: usize = 1usize;
|
||||
|
||||
/// The size of a 16 bit whole number. 2 byte.
|
||||
pub const U16_BYTES: usize = 2usize;
|
||||
|
||||
/// The size of a 32 bit whole number. 4 byte.
|
||||
pub const U32_BYTES: usize = 4usize;
|
||||
|
||||
/// The size of a 64 bit whole number. 8 byte.
|
||||
pub const U64_BYTES: usize = 8usize;
|
||||
|
||||
/// The size of an 8 bit integer. 1 byte.
|
||||
pub const I8_BYTES: usize = 1usize;
|
||||
|
||||
/// The size of a 16 bit integer. 2 bytes.
|
||||
pub const I16_BYTES: usize = 2usize;
|
||||
|
||||
/// The size of a 32 bit integer. 4 bytes.
|
||||
pub const I32_BYTES: usize = 4usize;
|
||||
|
||||
/// The size of a 64 bit integer. 8 bytes.
|
||||
pub const I64_BYTES: usize = 8usize;
|
||||
|
||||
|
||||
/// The size of a 32 bit floating point number. 4 bytes.
|
||||
pub const F32_BYTES: usize = 4usize;
|
||||
|
||||
/// The size of a 64 bit floating point number. 8 bytes.
|
||||
pub const F64_BYTES: usize = 8usize;
|
||||
|
||||
#[cfg(target_pointer_width = "8")]
|
||||
/// This should be the size of the systems pointer width. 1 byte.
|
||||
pub const USIZE_BYTES: usize = 1usize;
|
||||
|
||||
#[cfg(target_pointer_width = "16")]
|
||||
/// This should be the size of the systems pointer width. 2 bytes.
|
||||
pub const USIZE_BYTES: usize = 2usize;
|
||||
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
/// This should be the size of the systems pointer width. 4 bytes.
|
||||
pub const USIZE_BYTES: usize = 4usize;
|
||||
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
/// This should be the size of the systems pointer width. 8 bytes.
|
||||
pub const USIZE_BYTES: usize = 8usize;
|
||||
|
||||
#[cfg(target_pointer_width = "8")]
|
||||
/// This should be the size of the systems pointer width. 1 byte.
|
||||
pub const ISIZE_BYTES: usize = 1usize;
|
||||
|
||||
#[cfg(target_pointer_width = "16")]
|
||||
/// This should be the size of the systems pointer width. 2 bytes.
|
||||
pub const ISIZE_BYTES: usize = 2usize;
|
||||
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
/// This should be the size of the systems pointer width. 4 bytes.
|
||||
pub const ISIZE_BYTES: usize = 4usize;
|
||||
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
/// This should be the size of the systems pointer width. 8 bytes.
|
||||
pub const ISIZE_BYTES: usize = 8usize;
|
||||
|
||||
|
||||
|
||||
// Note: This is named after my sister Jessica "Monkey" Smith.
|
||||
/// Denotes a type that knows how many bits/bytes
|
||||
/// it takes to represent it.
|
||||
pub trait ByteSized
|
||||
{
|
||||
/// The amount of bits it takes to represent this type.
|
||||
const BITS: usize;
|
||||
|
||||
/// The amount of bytes it takes to represent this type.
|
||||
const BYTES: usize;
|
||||
|
||||
|
||||
|
||||
/// The amount of bits it takes to represent this type.
|
||||
fn get_bit_size() -> usize;
|
||||
|
||||
/// The amount of bytes it takes to represent this type.
|
||||
fn get_byte_size() -> usize;
|
||||
}
|
||||
|
||||
|
||||
macro_rules! byte_sized_impl
|
||||
{
|
||||
($(($varType: ty, $numBytes: expr))*) =>
|
||||
{$(
|
||||
impl ByteSized for $varType
|
||||
{
|
||||
const BITS: usize = $numBytes * 8;
|
||||
const BYTES: usize = $numBytes;
|
||||
|
||||
|
||||
|
||||
fn get_bit_size() -> usize
|
||||
{
|
||||
Self::BITS
|
||||
}
|
||||
|
||||
fn get_byte_size() -> usize
|
||||
{
|
||||
Self::BYTES
|
||||
}
|
||||
}
|
||||
)*}
|
||||
}
|
||||
|
||||
byte_sized_impl!((u8, U8_BYTES) (u16, U16_BYTES));
|
||||
byte_sized_impl!((u32, U32_BYTES) (u64, I64_BYTES));
|
||||
byte_sized_impl!((i8, I8_BYTES) (i16, I16_BYTES));
|
||||
byte_sized_impl!((i32, I32_BYTES) (i64, U64_BYTES));
|
||||
byte_sized_impl!((f32, F32_BYTES) (f64, F64_BYTES));
|
||||
byte_sized_impl!((usize, USIZE_BYTES) (isize, ISIZE_BYTES));
|
Loading…
x
Reference in New Issue
Block a user