@@ -14,151 +14,113 @@ pub trait FromBytes: Sized {
14
14
fn read < R : Read > ( reader : R ) -> IoResult < Self > ;
15
15
}
16
16
17
- macro_rules! array_bytes {
18
- ( $N: expr) => {
19
- impl ToBytes for [ u8 ; $N] {
20
- #[ inline]
21
- fn write<W : Write >( & self , mut writer: W ) -> IoResult <( ) > {
22
- writer. write_all( self )
23
- }
24
- }
17
+ impl < const N : usize > ToBytes for [ u8 ; N ] {
18
+ #[ inline]
19
+ fn write < W : Write > ( & self , mut writer : W ) -> IoResult < ( ) > {
20
+ writer. write_all ( self )
21
+ }
22
+ }
25
23
26
- impl FromBytes for [ u8 ; $ N] {
27
- #[ inline]
28
- fn read<R : Read >( mut reader: R ) -> IoResult <Self > {
29
- let mut arr = [ 0u8 ; $ N] ;
30
- reader. read_exact( & mut arr) ?;
31
- Ok ( arr)
32
- }
33
- }
24
+ impl < const N : usize > FromBytes for [ u8 ; N ] {
25
+ #[ inline]
26
+ fn read < R : Read > ( mut reader : R ) -> IoResult < Self > {
27
+ let mut arr = [ 0u8 ; N ] ;
28
+ reader. read_exact ( & mut arr) ?;
29
+ Ok ( arr)
30
+ }
31
+ }
34
32
35
- impl ToBytes for [ u16 ; $N] {
36
- #[ inline]
37
- fn write<W : Write >( & self , mut writer: W ) -> IoResult <( ) > {
38
- for num in self {
39
- writer. write_all( & num. to_le_bytes( ) ) ?;
40
- }
41
- Ok ( ( ) )
42
- }
33
+ impl < const N : usize > ToBytes for [ u16 ; N ] {
34
+ #[ inline]
35
+ fn write < W : Write > ( & self , mut writer : W ) -> IoResult < ( ) > {
36
+ for num in self {
37
+ writer. write_all ( & num. to_le_bytes ( ) ) ?;
43
38
}
39
+ Ok ( ( ) )
40
+ }
41
+ }
44
42
45
- impl FromBytes for [ u16 ; $N] {
46
- #[ inline]
47
- fn read<R : Read >( mut reader: R ) -> IoResult <Self > {
48
- let mut res = [ 0u16 ; $N] ;
49
- for num in res. iter_mut( ) {
50
- let mut bytes = [ 0u8 ; 2 ] ;
51
- reader. read_exact( & mut bytes) ?;
52
- * num = u16 :: from_le_bytes( bytes) ;
53
- }
54
- Ok ( res)
55
- }
43
+ impl < const N : usize > FromBytes for [ u16 ; N ] {
44
+ #[ inline]
45
+ fn read < R : Read > ( mut reader : R ) -> IoResult < Self > {
46
+ let mut res = [ 0u16 ; N ] ;
47
+ for num in res. iter_mut ( ) {
48
+ let mut bytes = [ 0u8 ; 2 ] ;
49
+ reader. read_exact ( & mut bytes) ?;
50
+ * num = u16:: from_le_bytes ( bytes) ;
56
51
}
52
+ Ok ( res)
53
+ }
54
+ }
57
55
58
- impl ToBytes for [ u32 ; $N] {
59
- #[ inline]
60
- fn write<W : Write >( & self , mut writer: W ) -> IoResult <( ) > {
61
- for num in self {
62
- writer. write_all( & num. to_le_bytes( ) ) ?;
63
- }
64
- Ok ( ( ) )
65
- }
56
+ impl < const N : usize > ToBytes for [ u32 ; N ] {
57
+ #[ inline]
58
+ fn write < W : Write > ( & self , mut writer : W ) -> IoResult < ( ) > {
59
+ for num in self {
60
+ writer. write_all ( & num. to_le_bytes ( ) ) ?;
66
61
}
62
+ Ok ( ( ) )
63
+ }
64
+ }
67
65
68
- impl FromBytes for [ u32 ; $N] {
69
- #[ inline]
70
- fn read<R : Read >( mut reader: R ) -> IoResult <Self > {
71
- let mut res = [ 0u32 ; $N] ;
72
- for num in res. iter_mut( ) {
73
- let mut bytes = [ 0u8 ; 4 ] ;
74
- reader. read_exact( & mut bytes) ?;
75
- * num = u32 :: from_le_bytes( bytes) ;
76
- }
77
- Ok ( res)
78
- }
66
+ impl < const N : usize > FromBytes for [ u32 ; N ] {
67
+ #[ inline]
68
+ fn read < R : Read > ( mut reader : R ) -> IoResult < Self > {
69
+ let mut res = [ 0u32 ; N ] ;
70
+ for num in res. iter_mut ( ) {
71
+ let mut bytes = [ 0u8 ; 4 ] ;
72
+ reader. read_exact ( & mut bytes) ?;
73
+ * num = u32:: from_le_bytes ( bytes) ;
79
74
}
75
+ Ok ( res)
76
+ }
77
+ }
80
78
81
- impl ToBytes for [ u64 ; $N] {
82
- #[ inline]
83
- fn write<W : Write >( & self , mut writer: W ) -> IoResult <( ) > {
84
- for num in self {
85
- writer. write_all( & num. to_le_bytes( ) ) ?;
86
- }
87
- Ok ( ( ) )
88
- }
79
+ impl < const N : usize > ToBytes for [ u64 ; N ] {
80
+ #[ inline]
81
+ fn write < W : Write > ( & self , mut writer : W ) -> IoResult < ( ) > {
82
+ for num in self {
83
+ writer. write_all ( & num. to_le_bytes ( ) ) ?;
89
84
}
85
+ Ok ( ( ) )
86
+ }
87
+ }
90
88
91
- impl FromBytes for [ u64 ; $N] {
92
- #[ inline]
93
- fn read<R : Read >( mut reader: R ) -> IoResult <Self > {
94
- let mut res = [ 0u64 ; $N] ;
95
- for num in res. iter_mut( ) {
96
- let mut bytes = [ 0u8 ; 8 ] ;
97
- reader. read_exact( & mut bytes) ?;
98
- * num = u64 :: from_le_bytes( bytes) ;
99
- }
100
- Ok ( res)
101
- }
89
+ impl < const N : usize > FromBytes for [ u64 ; N ] {
90
+ #[ inline]
91
+ fn read < R : Read > ( mut reader : R ) -> IoResult < Self > {
92
+ let mut res = [ 0u64 ; N ] ;
93
+ for num in res. iter_mut ( ) {
94
+ let mut bytes = [ 0u8 ; 8 ] ;
95
+ reader. read_exact ( & mut bytes) ?;
96
+ * num = u64:: from_le_bytes ( bytes) ;
102
97
}
98
+ Ok ( res)
99
+ }
100
+ }
103
101
104
- impl ToBytes for [ u128 ; $N] {
105
- #[ inline]
106
- fn write<W : Write >( & self , mut writer: W ) -> IoResult <( ) > {
107
- for num in self {
108
- writer. write_all( & num. to_le_bytes( ) ) ?;
109
- }
110
- Ok ( ( ) )
111
- }
102
+ impl < const N : usize > ToBytes for [ u128 ; N ] {
103
+ #[ inline]
104
+ fn write < W : Write > ( & self , mut writer : W ) -> IoResult < ( ) > {
105
+ for num in self {
106
+ writer. write_all ( & num. to_le_bytes ( ) ) ?;
112
107
}
108
+ Ok ( ( ) )
109
+ }
110
+ }
113
111
114
- impl FromBytes for [ u128 ; $N] {
115
- #[ inline]
116
- fn read<R : Read >( mut reader: R ) -> IoResult <Self > {
117
- let mut res = [ 0u128 ; $N] ;
118
- for num in res. iter_mut( ) {
119
- let mut bytes = [ 0u8 ; 16 ] ;
120
- reader. read_exact( & mut bytes) ?;
121
- * num = u128 :: from_le_bytes( bytes) ;
122
- }
123
- Ok ( res)
124
- }
112
+ impl < const N : usize > FromBytes for [ u128 ; N ] {
113
+ #[ inline]
114
+ fn read < R : Read > ( mut reader : R ) -> IoResult < Self > {
115
+ let mut res = [ 0u128 ; N ] ;
116
+ for num in res. iter_mut ( ) {
117
+ let mut bytes = [ 0u8 ; 16 ] ;
118
+ reader. read_exact ( & mut bytes) ?;
119
+ * num = u128:: from_le_bytes ( bytes) ;
125
120
}
126
- } ;
127
- }
128
-
129
- array_bytes ! ( 0 ) ;
130
- array_bytes ! ( 1 ) ;
131
- array_bytes ! ( 2 ) ;
132
- array_bytes ! ( 3 ) ;
133
- array_bytes ! ( 4 ) ;
134
- array_bytes ! ( 5 ) ;
135
- array_bytes ! ( 6 ) ;
136
- array_bytes ! ( 7 ) ;
137
- array_bytes ! ( 8 ) ;
138
- array_bytes ! ( 9 ) ;
139
- array_bytes ! ( 10 ) ;
140
- array_bytes ! ( 11 ) ;
141
- array_bytes ! ( 12 ) ;
142
- array_bytes ! ( 13 ) ;
143
- array_bytes ! ( 14 ) ;
144
- array_bytes ! ( 15 ) ;
145
- array_bytes ! ( 16 ) ;
146
- array_bytes ! ( 17 ) ;
147
- array_bytes ! ( 18 ) ;
148
- array_bytes ! ( 19 ) ;
149
- array_bytes ! ( 20 ) ;
150
- array_bytes ! ( 21 ) ;
151
- array_bytes ! ( 22 ) ;
152
- array_bytes ! ( 23 ) ;
153
- array_bytes ! ( 24 ) ;
154
- array_bytes ! ( 25 ) ;
155
- array_bytes ! ( 26 ) ;
156
- array_bytes ! ( 27 ) ;
157
- array_bytes ! ( 28 ) ;
158
- array_bytes ! ( 29 ) ;
159
- array_bytes ! ( 30 ) ;
160
- array_bytes ! ( 31 ) ;
161
- array_bytes ! ( 32 ) ;
121
+ Ok ( res)
122
+ }
123
+ }
162
124
163
125
/// Takes as input a sequence of structs, and converts them to a series of
164
126
/// bytes. All traits that implement `Bytes` can be automatically converted to
0 commit comments