File tree Expand file tree Collapse file tree 1 file changed +27
-3
lines changed Expand file tree Collapse file tree 1 file changed +27
-3
lines changed Original file line number Diff line number Diff line change 1
- use bytes:: BytesMut ;
1
+ use bytes:: { BufMut , BytesMut } ;
2
2
use serde_1:: { Deserialize , Serialize } ;
3
3
use serde_json_1:: Value ;
4
4
use std:: error:: Error ;
@@ -7,6 +7,30 @@ use std::io::Read;
7
7
8
8
use crate :: { FromSql , IsNull , ToSql , Type } ;
9
9
10
+ // https://github.com/tokio-rs/bytes/issues/170
11
+ struct B < ' a > ( & ' a mut BytesMut ) ;
12
+
13
+ impl < ' a > BufMut for B < ' a > {
14
+ #[ inline]
15
+ fn remaining_mut ( & self ) -> usize {
16
+ usize:: max_value ( ) - self . 0 . len ( )
17
+ }
18
+
19
+ #[ inline]
20
+ unsafe fn advance_mut ( & mut self , cnt : usize ) {
21
+ self . 0 . advance_mut ( cnt) ;
22
+ }
23
+
24
+ #[ inline]
25
+ unsafe fn bytes_mut ( & mut self ) -> & mut [ u8 ] {
26
+ if !self . 0 . has_remaining_mut ( ) {
27
+ self . 0 . reserve ( 64 ) ;
28
+ }
29
+
30
+ self . 0 . bytes_mut ( )
31
+ }
32
+ }
33
+
10
34
/// A wrapper type to allow arbitrary `Serialize`/`Deserialize` types to convert to Postgres JSON values.
11
35
#[ derive( Debug ) ]
12
36
pub struct Json < T > ( pub T ) ;
42
66
out : & mut BytesMut ,
43
67
) -> Result < IsNull , Box < dyn Error + Sync + Send > > {
44
68
if * ty == Type :: JSONB {
45
- out. push ( 1 ) ;
69
+ B ( out) . put_u8 ( 1 ) ;
46
70
}
47
- serde_json_1:: ser:: to_writer ( out, & self . 0 ) ?;
71
+ serde_json_1:: ser:: to_writer ( B ( out) . writer ( ) , & self . 0 ) ?;
48
72
Ok ( IsNull :: No )
49
73
}
50
74
You can’t perform that action at this time.
0 commit comments