2
2
3
3
use super :: { code, prelude:: * , Datum , Format , FromDataType , Numeric , RowDescription } ;
4
4
use bytes:: BytesMut ;
5
- use std:: ops:: Deref ;
5
+ use std:: ops:: { Deref , DerefMut } ;
6
+
7
+ #[ derive( Debug , Clone ) ]
8
+ pub struct Data {
9
+ data : Bytes ,
10
+ is_null : bool ,
11
+ }
12
+
13
+ impl Deref for Data {
14
+ type Target = Bytes ;
15
+
16
+ fn deref ( & self ) -> & Self :: Target {
17
+ & self . data
18
+ }
19
+ }
20
+
21
+ impl DerefMut for Data {
22
+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
23
+ & mut self . data
24
+ }
25
+ }
26
+
27
+ impl From < Bytes > for Data {
28
+ fn from ( value : Bytes ) -> Self {
29
+ Self {
30
+ data : value,
31
+ is_null : false ,
32
+ }
33
+ }
34
+ }
35
+
36
+ impl Data {
37
+ pub fn null ( ) -> Self {
38
+ Self {
39
+ data : Bytes :: new ( ) ,
40
+ is_null : true ,
41
+ }
42
+ }
43
+ }
6
44
7
45
/// DataRow message.
8
46
#[ derive( Debug , Clone ) ]
9
47
pub struct DataRow {
10
- columns : Vec < Bytes > ,
48
+ columns : Vec < Data > ,
11
49
}
12
50
13
51
/// Convert value to data row column
14
52
/// using text formatting.
15
53
pub trait ToDataRowColumn {
16
- fn to_data_row_column ( & self ) -> Bytes ;
54
+ fn to_data_row_column ( & self ) -> Data ;
17
55
}
18
56
19
57
impl ToDataRowColumn for String {
20
- fn to_data_row_column ( & self ) -> Bytes {
21
- Bytes :: copy_from_slice ( self . as_bytes ( ) )
58
+ fn to_data_row_column ( & self ) -> Data {
59
+ Bytes :: copy_from_slice ( self . as_bytes ( ) ) . into ( )
22
60
}
23
61
}
24
62
25
63
impl ToDataRowColumn for & String {
26
- fn to_data_row_column ( & self ) -> Bytes {
27
- Bytes :: copy_from_slice ( self . as_bytes ( ) )
64
+ fn to_data_row_column ( & self ) -> Data {
65
+ Bytes :: copy_from_slice ( self . as_bytes ( ) ) . into ( )
28
66
}
29
67
}
30
68
31
69
impl ToDataRowColumn for & str {
32
- fn to_data_row_column ( & self ) -> Bytes {
33
- Bytes :: copy_from_slice ( self . as_bytes ( ) )
70
+ fn to_data_row_column ( & self ) -> Data {
71
+ Bytes :: copy_from_slice ( self . as_bytes ( ) ) . into ( )
34
72
}
35
73
}
36
74
37
75
impl ToDataRowColumn for i64 {
38
- fn to_data_row_column ( & self ) -> Bytes {
39
- Bytes :: copy_from_slice ( self . to_string ( ) . as_bytes ( ) )
76
+ fn to_data_row_column ( & self ) -> Data {
77
+ Bytes :: copy_from_slice ( self . to_string ( ) . as_bytes ( ) ) . into ( )
40
78
}
41
79
}
42
80
43
81
impl ToDataRowColumn for usize {
44
- fn to_data_row_column ( & self ) -> Bytes {
45
- Bytes :: copy_from_slice ( self . to_string ( ) . as_bytes ( ) )
82
+ fn to_data_row_column ( & self ) -> Data {
83
+ Bytes :: copy_from_slice ( self . to_string ( ) . as_bytes ( ) ) . into ( )
46
84
}
47
85
}
48
86
49
87
impl ToDataRowColumn for u64 {
50
- fn to_data_row_column ( & self ) -> Bytes {
51
- Bytes :: copy_from_slice ( self . to_string ( ) . as_bytes ( ) )
88
+ fn to_data_row_column ( & self ) -> Data {
89
+ Bytes :: copy_from_slice ( self . to_string ( ) . as_bytes ( ) ) . into ( )
52
90
}
53
91
}
54
92
55
93
impl ToDataRowColumn for bool {
56
- fn to_data_row_column ( & self ) -> Bytes {
57
- Bytes :: copy_from_slice ( if * self { b"t" } else { b"f" } )
94
+ fn to_data_row_column ( & self ) -> Data {
95
+ Bytes :: copy_from_slice ( if * self { b"t" } else { b"f" } ) . into ( )
58
96
}
59
97
}
60
98
61
99
impl ToDataRowColumn for f64 {
62
- fn to_data_row_column ( & self ) -> Bytes {
100
+ fn to_data_row_column ( & self ) -> Data {
63
101
let number = format ! ( "{:.5}" , self ) ;
64
- Bytes :: copy_from_slice ( number. as_bytes ( ) )
102
+ Bytes :: copy_from_slice ( number. as_bytes ( ) ) . into ( )
65
103
}
66
104
}
67
105
68
106
impl ToDataRowColumn for u128 {
69
- fn to_data_row_column ( & self ) -> Bytes {
70
- Bytes :: copy_from_slice ( self . to_string ( ) . as_bytes ( ) )
107
+ fn to_data_row_column ( & self ) -> Data {
108
+ Bytes :: copy_from_slice ( self . to_string ( ) . as_bytes ( ) ) . into ( )
71
109
}
72
110
}
73
111
@@ -93,7 +131,7 @@ impl DataRow {
93
131
/// columns will be prefilled with NULLs.
94
132
pub fn insert ( & mut self , index : usize , value : impl ToDataRowColumn ) -> & mut Self {
95
133
while self . columns . len ( ) <= index {
96
- self . columns . push ( Bytes :: new ( ) ) ;
134
+ self . columns . push ( Data :: null ( ) ) ;
97
135
}
98
136
self . columns [ index] = value. to_data_row_column ( ) ;
99
137
self
@@ -111,7 +149,7 @@ impl DataRow {
111
149
/// Get data for column at index.
112
150
#[ inline]
113
151
pub fn column ( & self , index : usize ) -> Option < Bytes > {
114
- self . columns . get ( index) . cloned ( )
152
+ self . columns . get ( index) . cloned ( ) . map ( |d| d . data )
115
153
}
116
154
117
155
/// Get integer at index with text/binary encoding.
@@ -207,6 +245,7 @@ impl FromBytes for DataRow {
207
245
208
246
column. freeze ( )
209
247
} )
248
+ . map ( Data :: from)
210
249
. collect ( ) ;
211
250
212
251
Ok ( Self { columns } )
@@ -219,8 +258,12 @@ impl ToBytes for DataRow {
219
258
payload. put_i16 ( self . columns . len ( ) as i16 ) ;
220
259
221
260
for column in & self . columns {
222
- payload. put_i32 ( column. len ( ) as i32 ) ;
223
- payload. put ( & column[ ..] ) ;
261
+ if column. is_null {
262
+ payload. put_i32 ( -1 ) ;
263
+ } else {
264
+ payload. put_i32 ( column. len ( ) as i32 ) ;
265
+ payload. put ( & column[ ..] ) ;
266
+ }
224
267
}
225
268
226
269
Ok ( payload. freeze ( ) )
0 commit comments