1- use std:: cell:: RefCell ;
1+ use std:: cell:: Cell ;
22
33use nanorand:: { Rng , WyRand } ;
4- use ntex:: util:: { Bytes , BytesMut } ;
4+ use ntex:: util:: { Bytes , BytesVec } ;
55use smallvec:: SmallVec ;
66use tokio_postgres:: { connect, Client , Statement } ;
77use yarte:: TemplateBytesTrait ;
@@ -33,7 +33,7 @@ pub struct PgConnection {
3333 world : Statement ,
3434 rng : WyRand ,
3535 updates : Statement ,
36- buf : RefCell < BytesMut > ,
36+ buf : Cell < Option < BytesVec > > ,
3737}
3838
3939impl PgConnection {
@@ -55,7 +55,7 @@ impl PgConnection {
5555 world,
5656 updates,
5757 rng : WyRand :: new ( ) ,
58- buf : RefCell :: new ( BytesMut :: with_capacity ( 10 * 1024 * 1024 ) ) ,
58+ buf : Cell :: new ( Some ( BytesVec :: with_capacity ( 10 * 1024 * 1024 ) ) ) ,
5959 }
6060 }
6161}
@@ -66,17 +66,18 @@ impl PgConnection {
6666
6767 let row = self . cl . query_one ( & self . world , & [ & random_id] ) . await . unwrap ( ) ;
6868
69- let mut body = self . buf . borrow_mut ( ) ;
70- utils:: reserve ( & mut body, 1024 ) ;
69+ let mut body = self . buf . take ( ) . unwrap ( ) ;
7170 sonic_rs:: to_writer (
72- utils:: BytesWriter ( & mut body) ,
71+ utils:: BVecWriter :: new ( & mut body) ,
7372 & World {
7473 id : row. get ( 0 ) ,
7574 randomnumber : row. get ( 1 ) ,
7675 } ,
7776 )
7877 . unwrap ( ) ;
79- body. split ( ) . freeze ( )
78+ let result = body. take_bytes ( ) ;
79+ self . buf . set ( Some ( body) ) ;
80+ result
8081 }
8182
8283 pub async fn get_worlds ( & self , num : usize ) -> Bytes {
@@ -96,10 +97,11 @@ impl PgConnection {
9697 } )
9798 }
9899
99- let mut body = self . buf . borrow_mut ( ) ;
100- utils:: reserve ( & mut body, 2 * 1024 ) ;
101- sonic_rs:: to_writer ( utils:: BytesWriter ( & mut body) , & worlds[ ..] ) . unwrap ( ) ;
102- body. split ( ) . freeze ( )
100+ let mut body = self . buf . take ( ) . unwrap ( ) ;
101+ sonic_rs:: to_writer ( utils:: BVecWriter :: new ( & mut body) , & worlds[ ..] ) . unwrap ( ) ;
102+ let result = body. take_bytes ( ) ;
103+ self . buf . set ( Some ( body) ) ;
104+ result
103105 }
104106
105107 pub async fn update ( & self , num : usize ) -> Bytes {
@@ -131,10 +133,11 @@ impl PgConnection {
131133
132134 update. await . unwrap ( ) ;
133135
134- let mut body = self . buf . borrow_mut ( ) ;
135- utils:: reserve ( & mut body, 2 * 1024 ) ;
136- sonic_rs:: to_writer ( utils:: BytesWriter ( & mut body) , & worlds[ ..] ) . unwrap ( ) ;
137- body. split ( ) . freeze ( )
136+ let mut body = self . buf . take ( ) . unwrap ( ) ;
137+ sonic_rs:: to_writer ( utils:: BVecWriter :: new ( & mut body) , & worlds[ ..] ) . unwrap ( ) ;
138+ let result = body. take_bytes ( ) ;
139+ self . buf . set ( Some ( body) ) ;
140+ result
138141 }
139142
140143 pub async fn tell_fortune ( & self ) -> Bytes {
@@ -151,17 +154,16 @@ impl PgConnection {
151154 } ) ) ;
152155 fortunes. sort_by ( |it, next| it. message . cmp ( & next. message ) ) ;
153156
154- let mut body = std :: mem :: replace ( & mut * self . buf . borrow_mut ( ) , BytesMut :: new ( ) ) ;
157+ let mut body = self . buf . take ( ) . unwrap ( ) ;
155158 utils:: reserve ( & mut body, 4 * 1024 ) ;
156-
157159 FortunesTemplate {
158160 fortunes : & fortunes,
159161 }
160162 . write_call ( & mut body) ;
161163 fortunes. clear ( ) ;
162164
163- let result = body. split ( ) . freeze ( ) ;
164- let _ = std :: mem :: replace ( & mut * self . buf . borrow_mut ( ) , body) ;
165+ let result = body. take_bytes ( ) ;
166+ self . buf . set ( Some ( body) ) ;
165167 result
166168 }
167169}
0 commit comments