3030//! io::copy(&mut large_object, &mut file).unwrap();
3131//! }
3232//! ```
33- #![ doc( html_root_url="https://sfackler.github.io/rust-postgres-large-object/doc/v0.3.3 " ) ]
33+ #![ doc( html_root_url="https://sfackler.github.io/rust-postgres-large-object/doc/v0.3.4 " ) ]
3434
3535extern crate postgres;
36- extern crate debug_builders;
3736
38- use debug_builders:: DebugStruct ;
3937use postgres:: { Result , Transaction , GenericConnection } ;
4038use postgres:: error:: Error ;
4139use postgres:: types:: Oid ;
4240use std:: cmp;
4341use std:: fmt;
4442use std:: i32;
45- use std:: io;
43+ use std:: io:: { self , Write } ;
4644
4745/// An extension trait adding functionality to create and delete large objects.
4846pub trait LargeObjectExt {
@@ -133,10 +131,10 @@ pub struct LargeObject<'a> {
133131
134132impl < ' a > fmt:: Debug for LargeObject < ' a > {
135133 fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
136- DebugStruct :: new ( fmt, "LargeObject" )
137- . field ( "fd" , & self . fd )
138- . field ( "transaction" , & self . trans )
139- . finish ( )
134+ fmt. debug_struct ( "LargeObject" )
135+ . field ( "fd" , & self . fd )
136+ . field ( "transaction" , & self . trans )
137+ . finish ( )
140138 }
141139}
142140
@@ -147,6 +145,11 @@ impl<'a> Drop for LargeObject<'a> {
147145}
148146
149147impl < ' a > LargeObject < ' a > {
148+ /// Returns the file descriptor of the opened object.
149+ pub fn fd ( & self ) -> i32 {
150+ self . fd
151+ }
152+
150153 /// Truncates the object to the specified size.
151154 ///
152155 /// If `len` is larger than the size of the object, it will be padded with
@@ -156,13 +159,12 @@ impl<'a> LargeObject<'a> {
156159 let stmt = try!( self . trans . prepare_cached ( "SELECT pg_catalog.lo_truncate64($1, $2)" ) ) ;
157160 stmt. execute ( & [ & self . fd , & len] ) . map ( |_| ( ) )
158161 } else {
159- let len: i32 = if len <= i32:: max_value ( ) as i64 {
162+ let len = if len <= i32:: max_value ( ) as i64 {
160163 len as i32
161164 } else {
162- return Err ( Error :: IoError ( io:: Error :: new (
163- io:: ErrorKind :: InvalidInput ,
164- "The database does not support objects larger than 2GB"
165- ) ) )
165+ return Err ( Error :: IoError ( io:: Error :: new ( io:: ErrorKind :: InvalidInput ,
166+ "The database does not support objects \
167+ larger than 2GB") ) ) ;
166168 } ;
167169 let stmt = try!( self . trans . prepare_cached ( "SELECT pg_catalog.lo_truncate($1, $2)" ) ) ;
168170 stmt. execute ( & [ & self . fd , & len] ) . map ( |_| ( ) )
@@ -189,16 +191,12 @@ impl<'a> LargeObject<'a> {
189191}
190192
191193impl < ' a > io:: Read for LargeObject < ' a > {
192- fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
194+ fn read ( & mut self , mut buf : & mut [ u8 ] ) -> io:: Result < usize > {
193195 let stmt = try_io ! ( self . trans. prepare_cached( "SELECT pg_catalog.loread($1, $2)" ) ) ;
194196 let cap = cmp:: min ( buf. len ( ) , i32:: MAX as usize ) as i32 ;
195197 let row = try_io ! ( stmt. query( & [ & self . fd, & cap] ) ) . into_iter ( ) . next ( ) . unwrap ( ) ;
196198 let out = row. get_bytes ( 0 ) . unwrap ( ) ;
197-
198- for ( i, o) in out. iter ( ) . zip ( buf. iter_mut ( ) ) {
199- * o = * i;
200- }
201- Ok ( out. len ( ) )
199+ buf. write ( out)
202200 }
203201}
204202
@@ -232,8 +230,11 @@ impl<'a> io::Seek for LargeObject<'a> {
232230 } ;
233231
234232 if self . has_64 {
235- let stmt = try_io ! ( self . trans. prepare_cached( "SELECT pg_catalog.lo_lseek64($1, $2, $3)" ) ) ;
236- Ok ( try_io ! ( stmt. query( & [ & self . fd, & pos, & kind] ) ) . iter ( ) . next ( ) . unwrap ( ) . get :: < _ , i64 > ( 0 ) as u64 )
233+ let stmt = try_io ! ( self . trans
234+ . prepare_cached( "SELECT pg_catalog.lo_lseek64($1, $2, $3)" ) ) ;
235+ let rows = try_io ! ( stmt. query( & [ & self . fd, & pos, & kind] ) ) ;
236+ let pos: i64 = rows. iter ( ) . next ( ) . unwrap ( ) . get ( 0 ) ;
237+ Ok ( pos as u64 )
237238 } else {
238239 let pos = if pos <= i32:: max_value ( ) as i64 {
239240 pos as i32
@@ -242,7 +243,9 @@ impl<'a> io::Seek for LargeObject<'a> {
242243 "cannot seek more than 2^31 bytes" ) ) ;
243244 } ;
244245 let stmt = try_io ! ( self . trans. prepare_cached( "SELECT pg_catalog.lo_lseek($1, $2, $3)" ) ) ;
245- Ok ( try_io ! ( stmt. query( & [ & self . fd, & pos, & kind] ) ) . iter ( ) . next ( ) . unwrap ( ) . get :: < _ , i32 > ( 0 ) as u64 )
246+ let rows = try_io ! ( stmt. query( & [ & self . fd, & pos, & kind] ) ) ;
247+ let pos: i32 = rows. iter ( ) . next ( ) . unwrap ( ) . get ( 0 ) ;
248+ Ok ( pos as u64 )
246249 }
247250 }
248251}
0 commit comments