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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use std::mem;


pub trait CS{fn c_size(&self)->usize;}

pub trait CCP<T>{fn c_ptr(&self)->*const T;}

pub trait CCNP<T,U>{fn cn_ptr(&self)->*const U;}

pub trait CL{fn c_len(&self)->usize;}

pub trait C8CP{fn c8_ptr(&self)->*const u8;}
pub trait C8MP{fn c8_mut_ptr(&mut self)->*mut u8;}
pub trait C8L{fn c8_len(&self)->usize;}



macro_rules! cs_impls_2{($type:ty,$($N:expr)+)=>{$(
    impl CS for [$type;$N]{fn c_size(&self)->usize{mem::size_of::<$type>()}}
)+}}
macro_rules! cs_impls{($($type:ty)+)=>{$(
    impl CS for $type{fn c_size(&self)->usize{mem::size_of::<$type>()}}
    impl CS for Vec<$type>{fn c_size(&self)->usize{mem::size_of::<$type>()}}
    cs_impls_2!($type,
        00 01 02 03 04 05 06 07 08 09
        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
    );
)+}}


macro_rules! ccp_impls_2{($type:ty,$($N:expr)+)=>{$(
    impl CCP<$type> for [$type;$N]{fn c_ptr(&self)->*const $type{self[..].as_ptr()}}
)+}}
macro_rules! ccp_impls{($($type:ty)+)=>{$(
    impl CCP<$type> for $type{fn c_ptr(&self)->*const $type{self as *const $type}}
    impl CCP<$type> for Vec<$type>{fn c_ptr(&self)->*const $type{self.as_ptr()}}
    ccp_impls_2!($type,
        00 01 02 03 04 05 06 07 08 09
        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
    );
)+}}


// +CCPP
pub trait CCPP<T,U>{fn cp_ptr(&self)->*const U;}
macro_rules! ccpp_impls_2{($t1:ty,$t2:ty,$($N:expr)+)=>{$(
    impl CCPP<$t1,$t2> for [$t1;$N]{fn cp_ptr(&self)->*const $t2{self[..].as_ptr() as *const $t2}}
)+}}
macro_rules! ccpp_impls{
    ($([$t1:ty,$t2:ty]),*)=>{
        $(
            impl CCPP<$t1,$t2> for $t1{fn cp_ptr(&self)->*const $t2{(self as *const $t1) as *const $t2}}
            impl CCPP<$t1,$t2> for Vec<$t1>{fn cp_ptr(&self)->*const $t2{self.as_ptr() as *const $t2}}
            ccpp_impls_2!($t1,$t2,
                00 01 02 03 04 05 06 07 08 09
                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
            );
        )+
    }
}
ccpp_impls!([u8,u8],[i8,u8],[u16,u16],[i16,u16],[u32,u32],[i32,u32],[u64,u64],[i64,u64]);
impl CCPP<u8,u8> for String{fn cp_ptr(&self)->*const u8{self.as_ptr()}}
impl CCPP<u8,u8> for str{fn cp_ptr(&self)->*const u8{self.as_ptr()}}
impl<'a,T,U> CCPP<T,U> for &'a T where T:CCPP<T,U>{fn cp_ptr(&self)->*const U{(*self).cp_ptr()}}
// -CCPP

// +C8CPP
pub trait C8CPP<T,U>{fn c8p_ptr(&self)->*const U;}
macro_rules! c8cpp_impls_2{($t1:ty,$t2:ty,$($N:expr)+)=>{$(
    impl C8CPP<$t1,$t2> for [$t1;$N]{fn c8p_ptr(&self)->*const $t2{self[..].as_ptr() as *const $t2}}
)+}}
macro_rules! c8cpp_impls{
    ($([$t1:ty,$t2:ty]),*)=>{
        $(
            impl C8CPP<$t1,$t2> for $t1{fn c8p_ptr(&self)->*const $t2{(self as *const $t1) as *const $t2}}
            impl C8CPP<$t1,$t2> for Vec<$t1>{fn c8p_ptr(&self)->*const $t2{self.as_ptr() as *const $t2}}
            c8cpp_impls_2!($t1,$t2,
                00 01 02 03 04 05 06 07 08 09
                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
            );
        )+
    }
}
c8cpp_impls!([u8,u8],[i8,u8]);
impl C8CPP<u8,u8> for String{fn c8p_ptr(&self)->*const u8{self.as_ptr()}}
impl C8CPP<u8,u8> for str{fn c8p_ptr(&self)->*const u8{self.as_ptr()}}
impl<'a,T,U> C8CPP<T,U> for &'a T where T:C8CPP<T,U>{fn c8p_ptr(&self)->*const U{(*self).c8p_ptr()}}
// -C8CPP

// +C16CPP
pub trait C16CPP<T,U>{fn c16p_ptr(&self)->*const U;}
macro_rules! c16cpp_impls_2{($t1:ty,$t2:ty,$($N:expr)+)=>{$(
    impl C16CPP<$t1,$t2> for [$t1;$N]{fn c16p_ptr(&self)->*const $t2{self[..].as_ptr() as *const $t2}}
)+}}
macro_rules! c16cpp_impls{
    ($([$t1:ty,$t2:ty]),*)=>{
        $(
            impl C16CPP<$t1,$t2> for $t1{fn c16p_ptr(&self)->*const $t2{(self as *const $t1) as *const $t2}}
            impl C16CPP<$t1,$t2> for Vec<$t1>{fn c16p_ptr(&self)->*const $t2{self.as_ptr() as *const $t2}}
            c16cpp_impls_2!($t1,$t2,
                00 01 02 03 04 05 06 07 08 09
                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
            );
        )+
    }
}
c16cpp_impls!([u16,u16],[i16,u16]);
impl<'a,T,U> C16CPP<T,U> for &'a T where T:C16CPP<T,U>{fn c16p_ptr(&self)->*const U{(*self).c16p_ptr()}}
// -C16CPP




macro_rules! cl_impls_2{($type:ty,$($N:expr)+)=>{$(
    impl CL for [$type;$N]{fn c_len(&self)->usize{$N}}
)+}}
macro_rules! cl_impls{($($type:ty)+)=>{$(
    impl CL for $type{fn c_len(&self)->usize{1}}
    impl CL for Vec<$type>{fn c_len(&self)->usize{self.len()}}
    cl_impls_2!($type,
        00 01 02 03 04 05 06 07 08 09
        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
    );
)+}}


macro_rules! c8cp_impls_2{($type:ty,$($N:expr)+)=>{$(
    impl C8CP for [$type;$N]{fn c8_ptr(&self)->*const u8{self[..].as_ptr() as *const u8}}
)+}}
macro_rules! c8cp_impls{($($type:ty)+)=>{$(
    impl C8CP for $type{fn c8_ptr(&self)->*const u8{(self as *const $type) as *const u8}}
    impl C8CP for Vec<$type>{fn c8_ptr(&self)->*const u8{self.as_ptr() as *const u8}}
    c8cp_impls_2!($type,
        00 01 02 03 04 05 06 07 08 09
        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
    );
)+}}

macro_rules! c8mp_impls_2{($type:ty,$($N:expr)+)=>{$(
    impl C8MP for [$type;$N]{fn c8_mut_ptr(&mut self)->*mut u8{self[..].as_mut_ptr() as *mut u8}}
)+}}
macro_rules! c8mp_impls{($($type:ty)+)=>{$(
    impl C8MP for $type{fn c8_mut_ptr(&mut self)->*mut u8{(self as *mut $type) as *mut u8}}
    impl C8MP for Vec<$type>{fn c8_mut_ptr(&mut self)->*mut u8{self.as_mut_ptr() as *mut u8}}
    c8mp_impls_2!($type,
        00 01 02 03 04 05 06 07 08 09
        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
    );
)+}}

macro_rules! c8l_impls_2{($type:ty,$($N:expr)+)=>{$(
    impl C8L for [$type;$N]{fn c8_len(&self)->usize{$N*mem::size_of::<$type>()}}
)+}}
macro_rules! c8l_impls{($($type:ty)+)=>{$(
    impl C8L for $type{fn c8_len(&self)->usize{mem::size_of::<$type>()}}
    impl C8L for Vec<$type>{fn c8_len(&self)->usize{self.len()*mem::size_of::<$type>()}}
    c8l_impls_2!($type,
        00 01 02 03 04 05 06 07 08 09
        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
    );
)+}}


cs_impls!(u8 i8 u16 i16 u32 i32 u64 i64);
impl CS for String{fn c_size(&self)->usize{1}}
impl CS for str{fn c_size(&self)->usize{1}}
impl<'a,T> CS for &'a T where T:CS+?Sized{fn c_size(&self)->usize{(*self).c_size()}}

ccp_impls!(u8 i8 u16 i16 u32 i32 u64 i64);
impl CCP<u8> for String{fn c_ptr(&self)->*const u8{self.as_ptr()}}
impl CCP<u8> for str{fn c_ptr(&self)->*const u8{self.as_ptr()}}
impl<'a,T> CCP<T> for &'a T where T:CCP<T>{fn c_ptr(&self)->*const T{(*self).c_ptr()}}


cl_impls!(u8 i8 u16 i16 u32 i32 u64 i64);
impl CL for String{fn c_len(&self)->usize{self.len()}}
impl CL for str{fn c_len(&self)->usize{self.len()}}
impl<'a,T> CL for &'a T where T:CL+?Sized{fn c_len(&self)->usize{(*self).c_len()}}


c8cp_impls!(u8 i8 u16 i16 u32 i32 u64 i64);
impl C8CP for String{fn c8_ptr(&self)->*const u8{self.as_ptr()}}
impl C8CP for str{fn c8_ptr(&self)->*const u8{self.as_ptr()}}
impl<'a,T> C8CP for &'a T where T:C8CP+?Sized{fn c8_ptr(&self)->*const u8{(*self).c8_ptr()}}

c8mp_impls!(u8 i8 u16 i16 u32 i32 u64 i64);

c8l_impls!(u8 i8 u16 i16 u32 i32 u64 i64);
impl C8L for String{fn c8_len(&self)->usize{self.len()}}
impl C8L for str{fn c8_len(&self)->usize{self.len()}}
impl<'a,T> C8L for &'a T where T:C8L+?Sized{fn c8_len(&self)->usize{(*self).c8_len()}}