@@ -5,8 +5,11 @@ import (
55 "reflect"
66 "unsafe"
77 "github.com/v2pro/plz/reflect2"
8+ "strconv"
89)
910
11+ const ptrSize = 32 << uintptr (^ uintptr (0 )>> 63 )
12+
1013func createEncoderOfNative (ctx * ctx , typ reflect2.Type ) ValEncoder {
1114 if typ .Kind () == reflect .Slice && typ .(reflect2.SliceType ).Elem ().Kind () == reflect .Uint8 {
1215 sliceDecoder := decoderOfSlice (ctx , typ )
@@ -24,7 +27,10 @@ func createEncoderOfNative(ctx *ctx, typ reflect2.Type) ValEncoder {
2427 if typeName != "int" {
2528 return encoderOfType (ctx , reflect2 .TypeOfPtr ((* int )(nil )).Elem ())
2629 }
27- return & intCodec {}
30+ if strconv .IntSize == 32 {
31+ return & int32Codec {}
32+ }
33+ return & int64Codec {}
2834 case reflect .Int8 :
2935 if typeName != "int8" {
3036 return encoderOfType (ctx , reflect2 .TypeOfPtr ((* int8 )(nil )).Elem ())
@@ -49,7 +55,10 @@ func createEncoderOfNative(ctx *ctx, typ reflect2.Type) ValEncoder {
4955 if typeName != "uint" {
5056 return encoderOfType (ctx , reflect2 .TypeOfPtr ((* uint )(nil )).Elem ())
5157 }
52- return & uintCodec {}
58+ if strconv .IntSize == 32 {
59+ return & uint32Codec {}
60+ }
61+ return & uint64Codec {}
5362 case reflect .Uint8 :
5463 if typeName != "uint8" {
5564 return encoderOfType (ctx , reflect2 .TypeOfPtr ((* uint8 )(nil )).Elem ())
@@ -69,7 +78,10 @@ func createEncoderOfNative(ctx *ctx, typ reflect2.Type) ValEncoder {
6978 if typeName != "uintptr" {
7079 return encoderOfType (ctx , reflect2 .TypeOfPtr ((* uintptr )(nil )).Elem ())
7180 }
72- return & uintptrCodec {}
81+ if ptrSize == 32 {
82+ return & uint32Codec {}
83+ }
84+ return & uint64Codec {}
7385 case reflect .Uint64 :
7486 if typeName != "uint64" {
7587 return encoderOfType (ctx , reflect2 .TypeOfPtr ((* uint64 )(nil )).Elem ())
@@ -110,7 +122,10 @@ func createDecoderOfNative(ctx *ctx, typ reflect2.Type) ValDecoder {
110122 if typeName != "int" {
111123 return decoderOfType (ctx , reflect2 .TypeOfPtr ((* int )(nil )).Elem ())
112124 }
113- return & intCodec {}
125+ if strconv .IntSize == 32 {
126+ return & int32Codec {}
127+ }
128+ return & int64Codec {}
114129 case reflect .Int8 :
115130 if typeName != "int8" {
116131 return decoderOfType (ctx , reflect2 .TypeOfPtr ((* int8 )(nil )).Elem ())
@@ -135,7 +150,10 @@ func createDecoderOfNative(ctx *ctx, typ reflect2.Type) ValDecoder {
135150 if typeName != "uint" {
136151 return decoderOfType (ctx , reflect2 .TypeOfPtr ((* uint )(nil )).Elem ())
137152 }
138- return & uintCodec {}
153+ if strconv .IntSize == 32 {
154+ return & uint32Codec {}
155+ }
156+ return & uint64Codec {}
139157 case reflect .Uint8 :
140158 if typeName != "uint8" {
141159 return decoderOfType (ctx , reflect2 .TypeOfPtr ((* uint8 )(nil )).Elem ())
@@ -155,7 +173,10 @@ func createDecoderOfNative(ctx *ctx, typ reflect2.Type) ValDecoder {
155173 if typeName != "uintptr" {
156174 return decoderOfType (ctx , reflect2 .TypeOfPtr ((* uintptr )(nil )).Elem ())
157175 }
158- return & uintptrCodec {}
176+ if ptrSize == 32 {
177+ return & uint32Codec {}
178+ }
179+ return & uint64Codec {}
159180 case reflect .Uint64 :
160181 if typeName != "uint64" {
161182 return decoderOfType (ctx , reflect2 .TypeOfPtr ((* uint64 )(nil )).Elem ())
@@ -196,40 +217,6 @@ func (codec *stringCodec) IsEmpty(ptr unsafe.Pointer) bool {
196217 return * ((* string )(ptr )) == ""
197218}
198219
199- type intCodec struct {
200- }
201-
202- func (codec * intCodec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
203- if ! iter .ReadNil () {
204- * ((* int )(ptr )) = iter .ReadInt ()
205- }
206- }
207-
208- func (codec * intCodec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
209- stream .WriteInt (* ((* int )(ptr )))
210- }
211-
212- func (codec * intCodec ) IsEmpty (ptr unsafe.Pointer ) bool {
213- return * ((* int )(ptr )) == 0
214- }
215-
216- type uintptrCodec struct {
217- }
218-
219- func (codec * uintptrCodec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
220- if ! iter .ReadNil () {
221- * ((* uintptr )(ptr )) = uintptr (iter .ReadUint64 ())
222- }
223- }
224-
225- func (codec * uintptrCodec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
226- stream .WriteUint64 (uint64 (* ((* uintptr )(ptr ))))
227- }
228-
229- func (codec * uintptrCodec ) IsEmpty (ptr unsafe.Pointer ) bool {
230- return * ((* uintptr )(ptr )) == 0
231- }
232-
233220type int8Codec struct {
234221}
235222
@@ -298,24 +285,6 @@ func (codec *int64Codec) IsEmpty(ptr unsafe.Pointer) bool {
298285 return * ((* int64 )(ptr )) == 0
299286}
300287
301- type uintCodec struct {
302- }
303-
304- func (codec * uintCodec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
305- if ! iter .ReadNil () {
306- * ((* uint )(ptr )) = iter .ReadUint ()
307- return
308- }
309- }
310-
311- func (codec * uintCodec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
312- stream .WriteUint (* ((* uint )(ptr )))
313- }
314-
315- func (codec * uintCodec ) IsEmpty (ptr unsafe.Pointer ) bool {
316- return * ((* uint )(ptr )) == 0
317- }
318-
319288type uint8Codec struct {
320289}
321290
0 commit comments