1
1
use crate :: {
2
- arrays:: Array , errors:: Throwable , sys:: * , types:: get_type_by_const,
3
- utils:: ensure_end_with_zero, TypeError ,
2
+ arrays:: Array ,
3
+ errors:: Throwable ,
4
+ sys:: * ,
5
+ types:: { get_type_by_const, Type } ,
6
+ utils:: ensure_end_with_zero,
7
+ TypeError ,
4
8
} ;
5
9
use std:: {
6
10
mem:: zeroed,
@@ -119,28 +123,31 @@ impl Val {
119
123
}
120
124
121
125
#[ inline]
122
- fn type_info ( & self ) -> u32 {
123
- unsafe { self . inner . u1 . type_info }
126
+ fn get_type ( & self ) -> Type {
127
+ let t = unsafe { self . inner . u1 . type_info } ;
128
+ t. into ( )
124
129
}
125
130
126
- fn type_name ( & self ) -> crate :: Result < String > {
127
- get_type_by_const ( unsafe { self . type_info ( ) } )
131
+ fn get_type_name ( & self ) -> crate :: Result < String > {
132
+ get_type_by_const ( unsafe { self . get_type ( ) as u32 } )
128
133
}
129
134
130
- unsafe fn type_info_mut ( & mut self ) -> & mut u32 {
131
- & mut self . inner . u1 . type_info
135
+ fn set_type ( & mut self , t : Type ) {
136
+ unsafe {
137
+ self . inner . u1 . type_info = t as u32 ;
138
+ }
132
139
}
133
140
134
141
pub fn as_bool ( & self ) -> crate :: Result < bool > {
135
- match self . type_info ( ) {
136
- IS_TRUE => Ok ( true ) ,
137
- IS_FALSE => Ok ( false ) ,
142
+ match self . get_type ( ) {
143
+ Type :: True => Ok ( true ) ,
144
+ Type :: False => Ok ( false ) ,
138
145
_ => Err ( self . must_be_type_error ( "bool" ) . into ( ) ) ,
139
146
}
140
147
}
141
148
142
149
pub fn as_i64 ( & self ) -> crate :: Result < i64 > {
143
- if self . type_info ( ) == IS_LONG {
150
+ if self . get_type ( ) == Type :: Long {
144
151
unsafe { Ok ( self . inner . value . lval ) }
145
152
} else {
146
153
Err ( self . must_be_type_error ( "long" ) . into ( ) )
@@ -152,15 +159,15 @@ impl Val {
152
159
}
153
160
154
161
pub fn as_f64 ( & self ) -> crate :: Result < f64 > {
155
- if self . type_info ( ) == IS_DOUBLE {
162
+ if self . get_type ( ) == Type :: Double {
156
163
unsafe { Ok ( self . inner . value . dval ) }
157
164
} else {
158
165
Err ( self . must_be_type_error ( "float" ) . into ( ) )
159
166
}
160
167
}
161
168
162
169
pub fn as_string ( & self ) -> crate :: Result < String > {
163
- if self . type_info ( ) == IS_STRING {
170
+ if self . get_type ( ) == Type :: String {
164
171
unsafe {
165
172
let s = self . inner . value . str ;
166
173
let buf = from_raw_parts ( & ( * s) . val as * const c_char as * const u8 , ( * s) . len ) ;
@@ -185,7 +192,7 @@ impl Val {
185
192
pub fn as_array ( & self ) { }
186
193
187
194
fn must_be_type_error ( & self , expect_type : & str ) -> crate :: Error {
188
- match self . type_name ( ) {
195
+ match self . get_type_name ( ) {
189
196
Ok ( type_name) => {
190
197
let message = format ! ( "must be of type {}, {} given" , expect_type, type_name) ;
191
198
TypeError :: new ( message) . into ( )
@@ -201,17 +208,13 @@ pub trait SetVal {
201
208
202
209
impl SetVal for ( ) {
203
210
fn set_val ( self , val : & mut Val ) {
204
- unsafe {
205
- * val. type_info_mut ( ) = IS_NULL ;
206
- }
211
+ val. set_type ( Type :: Null ) ;
207
212
}
208
213
}
209
214
210
215
impl SetVal for bool {
211
216
fn set_val ( self , val : & mut Val ) {
212
- unsafe {
213
- * val. type_info_mut ( ) = if self { IS_TRUE } else { IS_FALSE } ;
214
- }
217
+ val. set_type ( if self { Type :: True } else { Type :: False } ) ;
215
218
}
216
219
}
217
220
@@ -229,18 +232,18 @@ impl SetVal for u32 {
229
232
230
233
impl SetVal for i64 {
231
234
fn set_val ( self , val : & mut Val ) {
235
+ val. set_type ( Type :: Long ) ;
232
236
unsafe {
233
237
( * val. as_mut_ptr ( ) ) . value . lval = self ;
234
- ( * val. as_mut_ptr ( ) ) . u1 . type_info = IS_LONG ;
235
238
}
236
239
}
237
240
}
238
241
239
242
impl SetVal for f64 {
240
243
fn set_val ( self , val : & mut Val ) {
244
+ val. set_type ( Type :: Double ) ;
241
245
unsafe {
242
246
( * val. as_mut_ptr ( ) ) . value . dval = self ;
243
- ( * val. as_mut_ptr ( ) ) . u1 . type_info = IS_DOUBLE ;
244
247
}
245
248
}
246
249
}
0 commit comments