2727//! stmt.copy_in(&[], &mut reader).unwrap();
2828//! }
2929//! ```
30- #![ doc( html_root_url="https://sfackler.github.io/rust-postgres-binary-copy/doc/v0.2.0 " ) ]
30+ #![ doc( html_root_url="https://sfackler.github.io/rust-postgres-binary-copy/doc/v0.2.1 " ) ]
3131#![ warn( missing_docs) ]
3232extern crate byteorder;
3333extern crate postgres;
@@ -86,17 +86,21 @@ pub struct BinaryCopyReader<'a, I> {
8686 buf : Cursor < Vec < u8 > > ,
8787}
8888
89- impl < ' a , I > fmt:: Debug for BinaryCopyReader < ' a , I > where I : fmt:: Debug {
89+ impl < ' a , I > fmt:: Debug for BinaryCopyReader < ' a , I >
90+ where I : fmt:: Debug
91+ {
9092 fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
9193 fmt. debug_struct ( "BinaryCopyReader" )
92- . field ( "types" , & self . types )
93- . field ( "state" , & self . state )
94- . field ( "it" , & self . it )
95- . finish ( )
94+ . field ( "types" , & self . types )
95+ . field ( "state" , & self . state )
96+ . field ( "it" , & self . it )
97+ . finish ( )
9698 }
9799}
98100
99- impl < ' a , I > BinaryCopyReader < ' a , I > where I : StreamingIterator < Item = ToSql > {
101+ impl < ' a , I > BinaryCopyReader < ' a , I >
102+ where I : StreamingIterator < Item = ToSql >
103+ {
100104 /// Creates a new `BinaryCopyReader`.
101105 ///
102106 /// The reader will output tuples with a structure described by `types` and
@@ -147,8 +151,8 @@ impl<'a, I> BinaryCopyReader<'a, I> where I: StreamingIterator<Item = ToSql> {
147151 if idx == 0 {
148152 let len = self . types . len ( ) ;
149153 let len = if len > i16:: max_value ( ) as usize {
150- let err: Box < error:: Error + Sync + Send > =
151- "value too large to transmit" . into ( ) ;
154+ let err: Box < error:: Error + Sync + Send > = "value too large to transmit"
155+ . into ( ) ;
152156 return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput ,
153157 Error :: Conversion ( err) ) ) ;
154158 } else {
@@ -166,8 +170,9 @@ impl<'a, I> BinaryCopyReader<'a, I> where I: StreamingIterator<Item = ToSql> {
166170 Ok ( IsNull :: No ) => {
167171 let len = self . buf . position ( ) - 4 - len_pos;
168172 if len > i32:: max_value ( ) as u64 {
169- let err: Box < error:: Error +Sync +Send > =
170- "value too large to transmit" . into ( ) ;
173+ let err: Box < error:: Error + Sync + Send > = "value too large to \
174+ transmit"
175+ . into ( ) ;
171176 return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput ,
172177 Error :: Conversion ( err) ) ) ;
173178 } else {
@@ -190,7 +195,9 @@ impl<'a, I> BinaryCopyReader<'a, I> where I: StreamingIterator<Item = ToSql> {
190195 }
191196}
192197
193- impl < ' a , I > ReadWithInfo for BinaryCopyReader < ' a , I > where I : StreamingIterator < Item = ToSql > {
198+ impl < ' a , I > ReadWithInfo for BinaryCopyReader < ' a , I >
199+ where I : StreamingIterator < Item = ToSql >
200+ {
194201 fn read_with_info ( & mut self , buf : & mut [ u8 ] , info : & CopyInfo ) -> io:: Result < usize > {
195202 if self . buf . position ( ) == self . buf . get_ref ( ) . len ( ) as u64 {
196203 try!( self . fill_buf ( info) ) ;
@@ -222,9 +229,9 @@ pub trait WriteValue {
222229}
223230
224231impl < F > WriteValue for F
225- where F : FnMut ( Option < & mut WriteValueReader > , & CopyInfo ) -> io:: Result < ( ) > {
226- fn write_value ( & mut self , r : & mut WriteValueReader , info : & CopyInfo )
227- -> io:: Result < ( ) > {
232+ where F : FnMut ( Option < & mut WriteValueReader > , & CopyInfo ) -> io:: Result < ( ) >
233+ {
234+ fn write_value ( & mut self , r : & mut WriteValueReader , info : & CopyInfo ) -> io:: Result < ( ) > {
228235 self ( Some ( r) , info)
229236 }
230237
@@ -254,18 +261,22 @@ pub struct BinaryCopyWriter<W> {
254261 buf : Vec < u8 > ,
255262}
256263
257- impl < W > fmt:: Debug for BinaryCopyWriter < W > where W : fmt:: Debug {
264+ impl < W > fmt:: Debug for BinaryCopyWriter < W >
265+ where W : fmt:: Debug
266+ {
258267 fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
259268 fmt. debug_struct ( "BinaryCopyWriter" )
260- . field ( "state" , & self . state )
261- . field ( "has_oids" , & self . has_oids )
262- . field ( "value_writer" , & self . value_writer )
263- . field ( "buf" , & self . buf . len ( ) )
264- . finish ( )
269+ . field ( "state" , & self . state )
270+ . field ( "has_oids" , & self . has_oids )
271+ . field ( "value_writer" , & self . value_writer )
272+ . field ( "buf" , & self . buf . len ( ) )
273+ . finish ( )
265274 }
266275}
267276
268- impl < W > BinaryCopyWriter < W > where W : WriteValue {
277+ impl < W > BinaryCopyWriter < W >
278+ where W : WriteValue
279+ {
269280 /// Creates a new `BinaryCopyWriter`.
270281 ///
271282 /// The writer will forward SQL values to the specified `WriteValue`.
@@ -292,7 +303,7 @@ impl<W> BinaryCopyWriter<W> where W: WriteValue {
292303 }
293304
294305 if & self . buf [ ..HEADER_MAGIC . len ( ) ] != HEADER_MAGIC {
295- let err: Box < error:: Error + Sync + Send > = "Did not receive expected header" . into ( ) ;
306+ let err: Box < error:: Error + Sync + Send > = "Did not receive expected header" . into ( ) ;
296307 return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput , err) ) ;
297308 }
298309
@@ -301,7 +312,7 @@ impl<W> BinaryCopyWriter<W> where W: WriteValue {
301312 self . has_oids = ( flags & 1 << 16 ) != 0 ;
302313
303314 if ( flags & !0 << 17 ) != 0 {
304- let err: Box < error:: Error + Sync + Send > = "Critical file format issue" . into ( ) ;
315+ let err: Box < error:: Error + Sync + Send > = "Critical file format issue" . into ( ) ;
305316 return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput , err) ) ;
306317 }
307318
@@ -331,7 +342,10 @@ impl<W> BinaryCopyWriter<W> where W: WriteValue {
331342 }
332343 }
333344
334- fn read_field_size ( & mut self , buf : & [ u8 ] , info : & CopyInfo , remaining : usize )
345+ fn read_field_size ( & mut self ,
346+ buf : & [ u8 ] ,
347+ info : & CopyInfo ,
348+ remaining : usize )
335349 -> io:: Result < usize > {
336350 let ( done, nread) = try!( self . read_to ( buf, mem:: size_of :: < i32 > ( ) ) ) ;
337351 if !done {
@@ -361,7 +375,11 @@ impl<W> BinaryCopyWriter<W> where W: WriteValue {
361375 } ;
362376 }
363377
364- fn read_field ( & mut self , buf : & [ u8 ] , info : & CopyInfo , size : usize , remaining : usize )
378+ fn read_field ( & mut self ,
379+ buf : & [ u8 ] ,
380+ info : & CopyInfo ,
381+ size : usize ,
382+ remaining : usize )
365383 -> io:: Result < usize > {
366384 let ( done, nread) = try!( self . read_to ( buf, size) ) ;
367385 if !done {
@@ -375,15 +393,18 @@ impl<W> BinaryCopyWriter<W> where W: WriteValue {
375393 }
376394}
377395
378- impl < W > WriteWithInfo for BinaryCopyWriter < W > where W : WriteValue {
396+ impl < W > WriteWithInfo for BinaryCopyWriter < W >
397+ where W : WriteValue
398+ {
379399 fn write_with_info ( & mut self , buf : & [ u8 ] , info : & CopyInfo ) -> io:: Result < usize > {
380400 match self . state {
381401 WriteState :: AtHeader => self . read_header ( buf) ,
382402 WriteState :: AtTuple => self . read_tuple ( buf) ,
383403 WriteState :: AtFieldSize ( remaining) => self . read_field_size ( buf, info, remaining) ,
384404 WriteState :: AtField { size, remaining } => self . read_field ( buf, info, size, remaining) ,
385405 WriteState :: Done => {
386- let err: Box < error:: Error +Sync +Send > = "Unexpected input after stream end" . into ( ) ;
406+ let err: Box < error:: Error + Sync + Send > = "Unexpected input after stream end"
407+ . into ( ) ;
387408 Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput , err) )
388409 }
389410 }
@@ -400,13 +421,17 @@ mod test {
400421 #[ test]
401422 fn write_basic ( ) {
402423 let conn = Connection :: connect ( "postgres://postgres@localhost" , SslMode :: None ) . unwrap ( ) ;
403- conn. execute ( "CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY, bar VARCHAR)" , & [ ] ) . unwrap ( ) ;
424+ conn. execute ( "CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY, bar VARCHAR)" ,
425+ & [ ] )
426+ . unwrap ( ) ;
404427
405428 let stmt = conn. prepare ( "COPY foo (id, bar) FROM STDIN BINARY" ) . unwrap ( ) ;
406429
407430 let types = & [ Type :: Int4 , Type :: Varchar ] ;
408- let values: Vec < Box < ToSql > > = vec ! [ Box :: new( 1i32 ) , Box :: new( "foobar" ) ,
409- Box :: new( 2i32 ) , Box :: new( None :: <String >) ] ;
431+ let values: Vec < Box < ToSql > > = vec ! [ Box :: new( 1i32 ) ,
432+ Box :: new( "foobar" ) ,
433+ Box :: new( 2i32 ) ,
434+ Box :: new( None :: <String >) ] ;
410435 let values = values. iter ( ) . map ( |e| & * * e) ;
411436 let mut reader = BinaryCopyReader :: new ( types, values) ;
412437
@@ -415,16 +440,18 @@ mod test {
415440 let stmt = conn. prepare ( "SELECT id, bar FROM foo ORDER BY id" ) . unwrap ( ) ;
416441 assert_eq ! ( vec![ ( 1i32 , Some ( "foobar" . to_string( ) ) ) , ( 2i32 , None ) ] ,
417442 stmt. query( & [ ] )
418- . unwrap( )
419- . into_iter( )
420- . map( |r| ( r. get( 0 ) , r. get( 1 ) ) )
421- . collect:: <Vec <( i32 , Option <String >) >>( ) ) ;
443+ . unwrap( )
444+ . into_iter( )
445+ . map( |r| ( r. get( 0 ) , r. get( 1 ) ) )
446+ . collect:: <Vec <( i32 , Option <String >) >>( ) ) ;
422447 }
423448
424449 #[ test]
425450 fn write_many_rows ( ) {
426451 let conn = Connection :: connect ( "postgres://postgres@localhost" , SslMode :: None ) . unwrap ( ) ;
427- conn. execute ( "CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY, bar VARCHAR)" , & [ ] ) . unwrap ( ) ;
452+ conn. execute ( "CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY, bar VARCHAR)" ,
453+ & [ ] )
454+ . unwrap ( ) ;
428455
429456 let stmt = conn. prepare ( "COPY foo (id, bar) FROM STDIN BINARY" ) . unwrap ( ) ;
430457
@@ -452,7 +479,9 @@ mod test {
452479 #[ test]
453480 fn write_big_rows ( ) {
454481 let conn = Connection :: connect ( "postgres://postgres@localhost" , SslMode :: None ) . unwrap ( ) ;
455- conn. execute ( "CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY, bar BYTEA)" , & [ ] ) . unwrap ( ) ;
482+ conn. execute ( "CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY, bar BYTEA)" ,
483+ & [ ] )
484+ . unwrap ( ) ;
456485
457486 let stmt = conn. prepare ( "COPY foo (id, bar) FROM STDIN BINARY" ) . unwrap ( ) ;
458487
@@ -480,23 +509,32 @@ mod test {
480509 #[ test]
481510 fn read_basic ( ) {
482511 let conn = Connection :: connect ( "postgres://postgres@localhost" , SslMode :: None ) . unwrap ( ) ;
483- conn. execute ( "CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY, bar INT)" , & [ ] ) . unwrap ( ) ;
512+ conn. execute ( "CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY, bar INT)" ,
513+ & [ ] )
514+ . unwrap ( ) ;
484515 conn. execute ( "INSERT INTO foo (bar) VALUES (1), (2), (NULL), (4)" , & [ ] ) . unwrap ( ) ;
485516
486517 let mut out = vec ! [ ] ;
487518
488519 {
489520 let writer = |r : Option < & mut WriteValueReader > , info : & CopyInfo | {
490521 match r {
491- Some ( r) => out. push ( Option :: < i32 > :: from_sql ( & Type :: Int4 , r, & info. session_info ( ) ) . unwrap ( ) ) ,
492- None => out. push ( Option :: < i32 > :: from_sql_null ( & Type :: Int4 , & info. session_info ( ) ) . unwrap ( ) ) ,
522+ Some ( r) => {
523+ out. push ( Option :: < i32 > :: from_sql ( & Type :: Int4 , r, & info. session_info ( ) )
524+ . unwrap ( ) )
525+ }
526+ None => {
527+ out. push ( Option :: < i32 > :: from_sql_null ( & Type :: Int4 , & info. session_info ( ) )
528+ . unwrap ( ) )
529+ }
493530 }
494531 Ok ( ( ) )
495532 } ;
496533
497534 let mut writer = BinaryCopyWriter :: new ( writer) ;
498535
499- let stmt = conn. prepare ( "COPY (SELECT bar FROM foo ORDER BY id) TO STDOUT BINARY" ) . unwrap ( ) ;
536+ let stmt = conn. prepare ( "COPY (SELECT bar FROM foo ORDER BY id) TO STDOUT BINARY" )
537+ . unwrap ( ) ;
500538 stmt. copy_out ( & [ ] , & mut writer) . unwrap ( ) ;
501539 }
502540
@@ -525,7 +563,8 @@ mod test {
525563
526564 let mut writer = BinaryCopyWriter :: new ( writer) ;
527565
528- let stmt = conn. prepare ( "COPY (SELECT id FROM foo ORDER BY id) TO STDOUT BINARY" ) . unwrap ( ) ;
566+ let stmt = conn. prepare ( "COPY (SELECT id FROM foo ORDER BY id) TO STDOUT BINARY" )
567+ . unwrap ( ) ;
529568 stmt. copy_out ( & [ ] , & mut writer) . unwrap ( ) ;
530569 }
531570
@@ -535,7 +574,9 @@ mod test {
535574 #[ test]
536575 fn read_big_rows ( ) {
537576 let conn = Connection :: connect ( "postgres://postgres@localhost" , SslMode :: None ) . unwrap ( ) ;
538- conn. execute ( "CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY, bar BYTEA)" , & [ ] ) . unwrap ( ) ;
577+ conn. execute ( "CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY, bar BYTEA)" ,
578+ & [ ] )
579+ . unwrap ( ) ;
539580
540581 let mut expected = vec ! [ ] ;
541582 let stmt = conn. prepare ( "INSERT INTO foo (id, bar) VALUES ($1, $2)" ) . unwrap ( ) ;
@@ -549,13 +590,16 @@ mod test {
549590
550591 {
551592 let writer = |r : Option < & mut WriteValueReader > , info : & CopyInfo | {
552- out. push ( Vec :: < u8 > :: from_sql ( & Type :: Bytea , r. unwrap ( ) , & info. session_info ( ) ) . unwrap ( ) ) ;
593+ out. push ( Vec :: < u8 > :: from_sql ( & Type :: Bytea , r. unwrap ( ) , & info. session_info ( ) )
594+ . unwrap ( ) ) ;
553595 Ok ( ( ) )
554596 } ;
555597
556598 let mut writer = BinaryCopyWriter :: new ( writer) ;
557599
558- let stmt = conn. prepare ( "COPY (SELECT bar FROM foo ORDER BY id) TO STDOUT (FORMAT binary)" ) . unwrap ( ) ;
600+ let stmt = conn. prepare ( "COPY (SELECT bar FROM foo ORDER BY id) TO STDOUT (FORMAT \
601+ binary)")
602+ . unwrap ( ) ;
559603 stmt. copy_out ( & [ ] , & mut writer) . unwrap ( ) ;
560604 }
561605
@@ -574,7 +618,8 @@ mod test {
574618 {
575619 let writer = |r : Option < & mut WriteValueReader > , info : & CopyInfo | {
576620 if oids. len ( ) > out. len ( ) {
577- out. push ( i32:: from_sql ( & Type :: Bytea , r. unwrap ( ) , & info. session_info ( ) ) . unwrap ( ) ) ;
621+ out. push ( i32:: from_sql ( & Type :: Bytea , r. unwrap ( ) , & info. session_info ( ) )
622+ . unwrap ( ) ) ;
578623 } else {
579624 oids. push ( u32:: from_sql ( & Type :: Oid , r. unwrap ( ) , & info. session_info ( ) ) . unwrap ( ) ) ;
580625 }
0 commit comments