Skip to content

Commit c09addf

Browse files
committed
Stabilize
1 parent 59547bc commit c09addf

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

src/lib.rs

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,14 @@
3030
//! io::copy(&mut large_object, &mut file).unwrap();
3131
//! }
3232
//! ```
33-
#![feature(io, core)]
3433
#![doc(html_root_url="https://sfackler.github.io/rust-postgres-large-object/doc")]
3534

3635
extern crate postgres;
3736

3837
use std::cmp;
3938
use std::fmt;
4039
use std::i32;
41-
use std::num::FromPrimitive;
4240
use std::io;
43-
use std::slice::bytes;
4441

4542
use postgres::{Oid, Error, Result, Transaction, GenericConnection};
4643

@@ -118,9 +115,7 @@ macro_rules! try_io {
118115
($e:expr) => {
119116
match $e {
120117
Ok(ok) => ok,
121-
Err(e) => return Err(io::Error::new(io::ErrorKind::Other,
122-
"error communicating with server",
123-
Some(format!("{}", e))))
118+
Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e))
124119
}
125120
}
126121
}
@@ -155,13 +150,13 @@ impl<'a> LargeObject<'a> {
155150
let stmt = try!(self.trans.prepare_cached("SELECT pg_catalog.lo_truncate64($1, $2)"));
156151
stmt.execute(&[&self.fd, &len]).map(|_| ())
157152
} else {
158-
let len: i32 = match FromPrimitive::from_i64(len) {
159-
Some(len) => len,
160-
None => return Err(Error::IoError(io::Error::new(
153+
let len: i32 = if len <= i32::max_value() as i64 {
154+
len as i32
155+
} else {
156+
return Err(Error::IoError(io::Error::new(
161157
io::ErrorKind::InvalidInput,
162-
"The database does not support objects larger than 2GB",
163-
None,
164-
))),
158+
"The database does not support objects larger than 2GB"
159+
)))
165160
};
166161
let stmt = try!(self.trans.prepare_cached("SELECT pg_catalog.lo_truncate($1, $2)"));
167162
stmt.execute(&[&self.fd, &len]).map(|_| ())
@@ -194,7 +189,9 @@ impl<'a> io::Read for LargeObject<'a> {
194189
let row = try_io!(stmt.query(&[&self.fd, &cap])).into_iter().next().unwrap();
195190
let out = row.get_bytes(0).unwrap();
196191

197-
bytes::copy_memory(buf, &out);
192+
for (i, o) in out.iter().zip(buf.iter_mut()) {
193+
*o = *i;
194+
}
198195
Ok(out.len())
199196
}
200197
}
@@ -216,11 +213,11 @@ impl<'a> io::Seek for LargeObject<'a> {
216213
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
217214
let (kind, pos) = match pos {
218215
io::SeekFrom::Start(pos) => {
219-
let pos = match FromPrimitive::from_u64(pos) {
220-
Some(pos) => pos,
221-
None => return Err(io::Error::new(io::ErrorKind::InvalidInput,
222-
"cannot seek more than 2^63 bytes",
223-
None)),
216+
let pos = if pos <= i64::max_value as u64 {
217+
pos as i64
218+
} else {
219+
return Err(io::Error::new(io::ErrorKind::InvalidInput,
220+
"cannot seek more than 2^63 bytes"));
224221
};
225222
(0, pos)
226223
}
@@ -232,11 +229,11 @@ impl<'a> io::Seek for LargeObject<'a> {
232229
let stmt = try_io!(self.trans.prepare_cached("SELECT pg_catalog.lo_lseek64($1, $2, $3)"));
233230
Ok(try_io!(stmt.query(&[&self.fd, &pos, &kind])).iter().next().unwrap().get::<_, i64>(0) as u64)
234231
} else {
235-
let pos: i32 = match FromPrimitive::from_i64(pos) {
236-
Some(pos) => pos,
237-
None => return Err(io::Error::new(io::ErrorKind::InvalidInput,
238-
"cannot seek more than 2^31 bytes",
239-
None)),
232+
let pos = if pos <= i32::max_value() as i64 {
233+
pos as i32
234+
} else {
235+
return Err(io::Error::new(io::ErrorKind::InvalidInput,
236+
"cannot seek more than 2^31 bytes"));
240237
};
241238
let stmt = try_io!(self.trans.prepare_cached("SELECT pg_catalog.lo_lseek($1, $2, $3)"));
242239
Ok(try_io!(stmt.query(&[&self.fd, &pos, &kind])).iter().next().unwrap().get::<_, i32>(0) as u64)
@@ -301,7 +298,7 @@ mod test {
301298
let mut lo = trans.open_large_object(oid, Mode::Read).unwrap();
302299
let mut out = vec![];
303300
lo.read_to_end(&mut out).unwrap();
304-
assert_eq!(b"hello world!!!", out);
301+
assert_eq!(out, b"hello world!!!");
305302
}
306303

307304
#[test]
@@ -353,11 +350,11 @@ mod test {
353350
lo.seek(SeekFrom::Start(0)).unwrap();
354351
let mut buf = vec![];
355352
lo.read_to_end(&mut buf).unwrap();
356-
assert_eq!(b"hello", buf);
353+
assert_eq!(buf, b"hello");
357354
lo.truncate(10).unwrap();
358355
lo.seek(SeekFrom::Start(0)).unwrap();
359356
buf.clear();
360357
lo.read_to_end(&mut buf).unwrap();
361-
assert_eq!(b"hello\0\0\0\0\0", buf);
358+
assert_eq!(buf, b"hello\0\0\0\0\0");
362359
}
363360
}

0 commit comments

Comments
 (0)