Skip to content

Commit fcbed91

Browse files
committed
Allow FromSql to borrow from the buffer
This allows for in-place deserialization of text and bytea values in particular. Row::get_bytes is removed since it previously existed for this use case. Closes sfackler#281
1 parent 520aaca commit fcbed91

File tree

21 files changed

+201
-194
lines changed

21 files changed

+201
-194
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
build:
2424
working_directory: ~/build
2525
docker:
26-
- image: rust:1.21.0
26+
- image: rust:1.23.0
2727
environment:
2828
RUSTFLAGS: -D warnings
2929
- image: sfackler/rust-postgres-test:3

codegen/src/main.rs

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ extern crate marksman_escape;
33
extern crate phf_codegen;
44
extern crate regex;
55

6-
#[allow(unused_imports)]
7-
use std::ascii::AsciiExt;
86
use std::path::Path;
97

108
mod sqlstate;

codegen/src/type_gen.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1+
use marksman_escape::Escape;
12
use regex::Regex;
2-
#[allow(unused_imports)]
3-
use std::ascii::AsciiExt;
43
use std::collections::BTreeMap;
54
use std::fs::File;
65
use std::io::{BufWriter, Write};
76
use std::path::Path;
8-
use marksman_escape::Escape;
97

108
use snake_to_camel;
119

@@ -178,8 +176,7 @@ fn make_impl(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
178176
w,
179177
" {} => Some(Inner::{}),
180178
",
181-
oid,
182-
type_.variant
179+
oid, type_.variant
183180
).unwrap();
184181
}
185182

@@ -194,14 +191,12 @@ fn make_impl(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
194191
",
195192
).unwrap();
196193

197-
198194
for (oid, type_) in types {
199195
write!(
200196
w,
201197
" Inner::{} => {},
202198
",
203-
type_.variant,
204-
oid
199+
type_.variant, oid
205200
).unwrap();
206201
}
207202

@@ -231,8 +226,7 @@ fn make_impl(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
231226
V
232227
}}
233228
",
234-
type_.variant,
235-
kind
229+
type_.variant, kind
236230
).unwrap();
237231
}
238232

@@ -252,8 +246,7 @@ fn make_impl(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
252246
w,
253247
r#" Inner::{} => "{}",
254248
"#,
255-
type_.variant,
256-
type_.name
249+
type_.variant, type_.name
257250
).unwrap();
258251
}
259252

postgres-shared/src/rows.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use fallible_iterator::FallibleIterator;
22
use postgres_protocol::message::backend::DataRowBody;
3-
#[allow(unused_imports)]
4-
use std::ascii::AsciiExt;
53
use std::io;
64
use std::ops::Range;
75

8-
use stmt::Column;
96
use rows::sealed::Sealed;
7+
use stmt::Column;
108

119
mod sealed {
1210
use stmt::Column;

postgres-shared/src/types/bit_vec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
extern crate bit_vec;
22

3-
use postgres_protocol::types;
43
use self::bit_vec::BitVec;
4+
use postgres_protocol::types;
55
use std::error::Error;
66

77
use types::{FromSql, IsNull, ToSql, Type, BIT, VARBIT};
88

9-
impl FromSql for BitVec {
9+
impl<'a> FromSql<'a> for BitVec {
1010
fn from_sql(_: &Type, raw: &[u8]) -> Result<BitVec, Box<Error + Sync + Send>> {
1111
let varbit = types::varbit_from_sql(raw)?;
1212
let mut bitvec = BitVec::from_bytes(varbit.bytes());

postgres-shared/src/types/chrono.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
extern crate chrono;
22

3-
use postgres_protocol::types;
43
use self::chrono::{DateTime, Duration, FixedOffset, Local, NaiveDate, NaiveDateTime, NaiveTime,
54
Utc};
5+
use postgres_protocol::types;
66
use std::error::Error;
77

88
use types::{FromSql, IsNull, ToSql, Type, DATE, TIME, TIMESTAMP, TIMESTAMPTZ};
@@ -11,7 +11,7 @@ fn base() -> NaiveDateTime {
1111
NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0)
1212
}
1313

14-
impl FromSql for NaiveDateTime {
14+
impl<'a> FromSql<'a> for NaiveDateTime {
1515
fn from_sql(_: &Type, raw: &[u8]) -> Result<NaiveDateTime, Box<Error + Sync + Send>> {
1616
let t = types::timestamp_from_sql(raw)?;
1717
Ok(base() + Duration::microseconds(t))
@@ -34,7 +34,7 @@ impl ToSql for NaiveDateTime {
3434
to_sql_checked!();
3535
}
3636

37-
impl FromSql for DateTime<Utc> {
37+
impl<'a> FromSql<'a> for DateTime<Utc> {
3838
fn from_sql(type_: &Type, raw: &[u8]) -> Result<DateTime<Utc>, Box<Error + Sync + Send>> {
3939
let naive = NaiveDateTime::from_sql(type_, raw)?;
4040
Ok(DateTime::from_utc(naive, Utc))
@@ -52,7 +52,7 @@ impl ToSql for DateTime<Utc> {
5252
to_sql_checked!();
5353
}
5454

55-
impl FromSql for DateTime<Local> {
55+
impl<'a> FromSql<'a> for DateTime<Local> {
5656
fn from_sql(type_: &Type, raw: &[u8]) -> Result<DateTime<Local>, Box<Error + Sync + Send>> {
5757
let utc = DateTime::<Utc>::from_sql(type_, raw)?;
5858
Ok(utc.with_timezone(&Local))
@@ -70,7 +70,7 @@ impl ToSql for DateTime<Local> {
7070
to_sql_checked!();
7171
}
7272

73-
impl FromSql for DateTime<FixedOffset> {
73+
impl<'a> FromSql<'a> for DateTime<FixedOffset> {
7474
fn from_sql(
7575
type_: &Type,
7676
raw: &[u8],
@@ -91,7 +91,7 @@ impl ToSql for DateTime<FixedOffset> {
9191
to_sql_checked!();
9292
}
9393

94-
impl FromSql for NaiveDate {
94+
impl<'a> FromSql<'a> for NaiveDate {
9595
fn from_sql(_: &Type, raw: &[u8]) -> Result<NaiveDate, Box<Error + Sync + Send>> {
9696
let jd = types::date_from_sql(raw)?;
9797
Ok(base().date() + Duration::days(jd as i64))
@@ -115,7 +115,7 @@ impl ToSql for NaiveDate {
115115
to_sql_checked!();
116116
}
117117

118-
impl FromSql for NaiveTime {
118+
impl<'a> FromSql<'a> for NaiveTime {
119119
fn from_sql(_: &Type, raw: &[u8]) -> Result<NaiveTime, Box<Error + Sync + Send>> {
120120
let usec = types::time_from_sql(raw)?;
121121
Ok(NaiveTime::from_hms(0, 0, 0) + Duration::microseconds(usec))

postgres-shared/src/types/eui48.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
extern crate eui48;
22

33
use self::eui48::MacAddress;
4-
use std::error::Error;
54
use postgres_protocol::types;
5+
use std::error::Error;
66

7-
use types::{FromSql, ToSql, Type, IsNull, MACADDR};
7+
use types::{FromSql, IsNull, ToSql, Type, MACADDR};
88

9-
impl FromSql for MacAddress {
9+
impl<'a> FromSql<'a> for MacAddress {
1010
fn from_sql(_: &Type, raw: &[u8]) -> Result<MacAddress, Box<Error + Sync + Send>> {
1111
let bytes = types::macaddr_from_sql(raw)?;
1212
Ok(MacAddress::new(bytes))

postgres-shared/src/types/geo.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
extern crate geo;
22

3-
use postgres_protocol::types;
43
use self::geo::{Bbox, LineString, Point};
5-
use std::error::Error;
64
use fallible_iterator::FallibleIterator;
5+
use postgres_protocol::types;
6+
use std::error::Error;
77

8-
use types::{FromSql, ToSql, IsNull, Type, POINT, BOX, PATH};
8+
use types::{FromSql, IsNull, ToSql, Type, BOX, PATH, POINT};
99

10-
impl FromSql for Point<f64> {
10+
impl<'a> FromSql<'a> for Point<f64> {
1111
fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<Error + Sync + Send>> {
1212
let point = types::point_from_sql(raw)?;
1313
Ok(Point::new(point.x(), point.y()))
@@ -26,7 +26,7 @@ impl ToSql for Point<f64> {
2626
to_sql_checked!();
2727
}
2828

29-
impl FromSql for Bbox<f64> {
29+
impl<'a> FromSql<'a> for Bbox<f64> {
3030
fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<Error + Sync + Send>> {
3131
let bbox = types::box_from_sql(raw)?;
3232
Ok(Bbox {
@@ -50,7 +50,7 @@ impl ToSql for Bbox<f64> {
5050
to_sql_checked!();
5151
}
5252

53-
impl FromSql for LineString<f64> {
53+
impl<'a> FromSql<'a> for LineString<f64> {
5454
fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<Error + Sync + Send>> {
5555
let path = types::path_from_sql(raw)?;
5656
let points = path.points().map(|p| Point::new(p.x(), p.y())).collect()?;

0 commit comments

Comments
 (0)