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
//!Slice accessors
//!
//!Provides traits to access integers as byte slices.
//!
//!# Usage
//!
//!```rust
//!extern crate lazy_bytes_cast;
//!use lazy_bytes_cast::slice::{
//!   ByteSlice,
//!   ByteIndex
//!};
//!
//!fn main() {
//!    let some_int = 666;
//!
//!    for (idx, byte) in some_int.byte_slice().iter().enumerate() {
//!        assert_eq!(some_int.byte(idx).unwrap(), *byte);
//!        println!("bytes[{}]={}", idx, byte);
//!    }
//!}
//!```

use ::mem;

/// Slice Accessor.
pub trait ByteSlice {
    /// Returns read-only slice over integer bytes
    fn byte_slice(&self) -> &[u8];
    /// Returns mutable slice over integer bytes
    fn byte_mut_slice(&mut self) -> &mut [u8];
}

/// Indexing Accessor.
pub trait ByteIndex : ByteSlice {
    /// Returns byte from integer by index.
    ///
    ///# Parameters:
    ///
    ///* `idx` - Index of byte starting from 0.
    ///
    ///# Result:
    ///
    ///* `Some` - Contains byte.
    ///* `None` - Index out of bounds.
    fn byte(&self, idx: usize) -> Option<u8>;
}

macro_rules! impl_index_trait1
{
    ($($t:ty), +) => {
        $(
            impl ByteSlice for $t {
                fn byte_slice(&self) -> &[u8] {
                    let bytes: &[u8; 1] = unsafe { mem::transmute(self) };
                    &bytes[..]
                }

                fn byte_mut_slice(&mut self) -> &mut [u8] {
                    let bytes: &mut [u8; 1] = unsafe { mem::transmute(self) };
                    &mut bytes[..]
                }
            }

            impl ByteIndex for $t {
                fn byte(&self, idx: usize) -> Option<u8> {
                    if idx >= mem::size_of::<$t>() {
                        return None;
                    }

                    Some(self.byte_slice()[idx])
                }
            }
        )+
    };
}

macro_rules! impl_index_trait2
{
    ($($t:ty), +) => {
        $(
            impl ByteSlice for $t {
                fn byte_slice(&self) -> &[u8] {
                    let bytes: &[u8; 2] = unsafe { mem::transmute(self) };
                    &bytes[..]
                }

                fn byte_mut_slice(&mut self) -> &mut [u8] {
                    let bytes: &mut [u8; 2] = unsafe { mem::transmute(self) };
                    &mut bytes[..]
                }
            }

            impl ByteIndex for $t {
                fn byte(&self, idx: usize) -> Option<u8> {
                    if idx >= mem::size_of::<$t>() {
                        return None;
                    }

                    Some(self.byte_slice()[idx])
                }
            }
        )+
    };
}

macro_rules! impl_index_trait4
{
    ($($t:ty), +) => {
        $(
            impl ByteSlice for $t {
                fn byte_slice(&self) -> &[u8] {
                    let bytes: &[u8; 4] = unsafe { mem::transmute(self) };
                    &bytes[..]
                }

                fn byte_mut_slice(&mut self) -> &mut [u8] {
                    let bytes: &mut [u8; 4] = unsafe { mem::transmute(self) };
                    &mut bytes[..]
                }
            }
            impl ByteIndex for $t {
                fn byte(&self, idx: usize) -> Option<u8> {
                    if idx >= mem::size_of::<$t>() {
                        return None;
                    }

                    Some(self.byte_slice()[idx])
                }
            }
        )+
    };
}

macro_rules! impl_index_trait8
{
    ($($t:ty), +) => {
        $(
            impl ByteSlice for $t {
                fn byte_slice(&self) -> &[u8] {
                    let bytes: &[u8; 8] = unsafe { mem::transmute(self) };
                    &bytes[..]
                }

                fn byte_mut_slice(&mut self) -> &mut [u8] {
                    let bytes: &mut [u8; 8] = unsafe { mem::transmute(self) };
                    &mut bytes[..]
                }
            }
            impl ByteIndex for $t {
                fn byte(&self, idx: usize) -> Option<u8> {
                    if idx >= mem::size_of::<$t>(){
                        return None;
                    }

                    Some(self.byte_slice()[idx])
                }
            }
        )+
    };
}

impl_index_trait1!(i8, u8);
impl_index_trait2!(i16, u16);
impl_index_trait4!(i32, u32, f32);
impl_index_trait8!(i64, u64, f64);

#[cfg(target_pointer_width = "64")]
impl_index_trait8!(isize, usize);

#[cfg(target_pointer_width = "32")]
impl_index_trait4!(isize, usize);