Skip to content

Commit 0d70c17

Browse files
committed
Update for IO Vec changes
1 parent 48501cf commit 0d70c17

File tree

4 files changed

+46
-49
lines changed

4 files changed

+46
-49
lines changed

src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct Person {
1616
id: i32,
1717
name: ~str,
1818
time_created: Timespec,
19-
data: Option<~[u8]>
19+
data: Option<Vec<u8>>
2020
}
2121
2222
fn main() {
@@ -661,8 +661,9 @@ impl InnerPostgresConnection {
661661
match try_pg!(self.read_message()) {
662662
ReadyForQuery { .. } => break,
663663
DataRow { row } =>
664+
// FIXME
664665
result.push(row.move_iter().map(|opt|
665-
opt.map(|b| str::from_utf8_owned(b).unwrap()))
666+
opt.map(|b| str::from_utf8(b.as_slice()).unwrap().to_owned()))
666667
.collect()),
667668
ErrorResponse { fields } => {
668669
try!(self.wait_for_ready());
@@ -1197,7 +1198,7 @@ pub struct ResultDescription {
11971198
pub struct PostgresRows<'stmt> {
11981199
stmt: &'stmt PostgresStatement<'stmt>,
11991200
name: ~str,
1200-
data: RingBuf<Vec<Option<~[u8]>>>,
1201+
data: RingBuf<Vec<Option<Vec<u8>>>>,
12011202
row_limit: uint,
12021203
more_rows: bool,
12031204
finished: bool,
@@ -1314,7 +1315,7 @@ impl<'stmt> Iterator<PostgresRow<'stmt>> for PostgresRows<'stmt> {
13141315
/// A single result row of a query.
13151316
pub struct PostgresRow<'stmt> {
13161317
stmt: &'stmt PostgresStatement<'stmt>,
1317-
data: Vec<Option<~[u8]>>
1318+
data: Vec<Option<Vec<u8>>>
13181319
}
13191320

13201321
impl<'stmt> PostgresRow<'stmt> {

src/message.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub enum BackendMessage {
2828
pub tag: ~str
2929
},
3030
DataRow {
31-
pub row: Vec<Option<~[u8]>>
31+
pub row: Vec<Option<Vec<u8>>>
3232
},
3333
EmptyQueryResponse,
3434
ErrorResponse {
@@ -75,7 +75,7 @@ pub enum FrontendMessage<'a> {
7575
pub portal: &'a str,
7676
pub statement: &'a str,
7777
pub formats: &'a [i16],
78-
pub values: &'a [Option<~[u8]>],
78+
pub values: &'a [Option<Vec<u8>>],
7979
pub result_formats: &'a [i16]
8080
},
8181
CancelRequest {
@@ -159,7 +159,7 @@ impl<W: Writer> WriteMessage for W {
159159
}
160160
Some(ref value) => {
161161
try!(buf.write_be_i32(value.len() as i32));
162-
try!(buf.write(*value));
162+
try!(buf.write(value.as_slice()));
163163
}
164164
}
165165
}
@@ -231,7 +231,7 @@ impl<W: Writer> WriteMessage for W {
231231
let buf = buf.unwrap();
232232
// add size of length value
233233
try!(self.write_be_i32((buf.len() + mem::size_of::<i32>()) as i32));
234-
try!(self.write(buf));
234+
try!(self.write(buf.as_slice()));
235235

236236
Ok(())
237237
}
@@ -246,7 +246,8 @@ impl<R: Buffer> ReadCStr for R {
246246
fn read_cstr(&mut self) -> IoResult<~str> {
247247
let mut buf = try!(self.read_until(0));
248248
buf.pop();
249-
Ok(str::from_utf8_owned(buf).unwrap())
249+
// FIXME
250+
Ok(str::from_utf8(buf.as_slice()).unwrap().to_owned())
250251
}
251252
}
252253

src/test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ fn test_bpchar_params() {
454454
455455
#[test]
456456
fn test_bytea_params() {
457-
test_type("BYTEA", [(Some(~[0u8, 1, 2, 3, 254, 255]), "'\\x00010203feff'"),
457+
test_type("BYTEA", [(Some(vec!(0u8, 1, 2, 3, 254, 255)), "'\\x00010203feff'"),
458458
(None, "NULL")]);
459459
}
460460
@@ -562,8 +562,8 @@ fn test_boolarray_params() {
562562
563563
#[test]
564564
fn test_byteaarray_params() {
565-
test_array_params!("BYTEA", ~[0u8, 1], r#""\\x0001""#, ~[254u8, 255u8],
566-
r#""\\xfeff""#, ~[10u8, 11u8], r#""\\x0a0b""#);
565+
test_array_params!("BYTEA", vec!(0u8, 1), r#""\\x0001""#, vec!(254u8, 255u8),
566+
r#""\\xfeff""#, vec!(10u8, 11u8), r#""\\x0a0b""#);
567567
}
568568
569569
#[test]

src/types/mod.rs

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ pub trait FromSql {
230230
/// Creates a new value of this type from a buffer of Postgres data.
231231
///
232232
/// If the value was `NULL`, the buffer will be `None`.
233-
fn from_sql(ty: &PostgresType, raw: &Option<~[u8]>)
233+
fn from_sql(ty: &PostgresType, raw: &Option<Vec<u8>>)
234234
-> Result<Self, PostgresError>;
235235
}
236236

@@ -257,17 +257,18 @@ impl RawFromSql for bool {
257257
}
258258
}
259259

260-
impl RawFromSql for ~[u8] {
260+
impl RawFromSql for Vec<u8> {
261261
fn raw_from_sql<R: Reader>(raw: &mut R)
262-
-> Result<~[u8], PostgresError> {
262+
-> Result<Vec<u8>, PostgresError> {
263263
Ok(try_pg!(raw.read_to_end()))
264264
}
265265
}
266266

267267
impl RawFromSql for ~str {
268268
fn raw_from_sql<R: Reader>(raw: &mut R)
269269
-> Result<~str, PostgresError> {
270-
Ok(str::from_utf8_owned(try_pg!(raw.read_to_end())).unwrap())
270+
// FIXME
271+
Ok(str::from_utf8(try_pg!(raw.read_to_end()).as_slice()).unwrap().to_owned())
271272
}
272273
}
273274

@@ -297,7 +298,7 @@ impl RawFromSql for Timespec {
297298
impl RawFromSql for Uuid {
298299
fn raw_from_sql<R: Reader>(raw: &mut R)
299300
-> Result<Uuid, PostgresError> {
300-
Ok(Uuid::from_bytes(try_pg!(raw.read_to_end())).unwrap())
301+
Ok(Uuid::from_bytes(try_pg!(raw.read_to_end()).as_slice()).unwrap())
301302
}
302303
}
303304

@@ -363,7 +364,7 @@ impl RawFromSql for Json {
363364
macro_rules! from_map_impl(
364365
($($expected:pat)|+, $t:ty, $blk:expr) => (
365366
impl FromSql for Option<$t> {
366-
fn from_sql(ty: &PostgresType, raw: &Option<~[u8]>)
367+
fn from_sql(ty: &PostgresType, raw: &Option<Vec<u8>>)
367368
-> Result<Option<$t>, PostgresError> {
368369
check_types!($($expected)|+, ty)
369370
match *raw {
@@ -374,7 +375,7 @@ macro_rules! from_map_impl(
374375
}
375376

376377
impl FromSql for $t {
377-
fn from_sql(ty: &PostgresType, raw: &Option<~[u8]>)
378+
fn from_sql(ty: &PostgresType, raw: &Option<Vec<u8>>)
378379
-> Result<$t, PostgresError> {
379380
// FIXME when you can specify Self types properly
380381
let ret: Result<Option<$t>, PostgresError> = FromSql::from_sql(ty, raw);
@@ -390,15 +391,15 @@ macro_rules! from_map_impl(
390391

391392
macro_rules! from_raw_from_impl(
392393
($($expected:pat)|+, $t:ty) => (
393-
from_map_impl!($($expected)|+, $t, |buf: &~[u8]| {
394+
from_map_impl!($($expected)|+, $t, |buf: &Vec<u8>| {
394395
let mut reader = BufReader::new(buf.as_slice());
395396
RawFromSql::raw_from_sql(&mut reader)
396397
})
397398
)
398399
)
399400

400401
from_raw_from_impl!(PgBool, bool)
401-
from_raw_from_impl!(PgByteA, ~[u8])
402+
from_raw_from_impl!(PgByteA, Vec<u8>)
402403
from_raw_from_impl!(PgVarchar | PgText | PgCharN, ~str)
403404
from_raw_from_impl!(PgChar, i8)
404405
from_raw_from_impl!(PgInt2, i16)
@@ -416,7 +417,7 @@ from_raw_from_impl!(PgTsRange | PgTstzRange, Range<Timespec>)
416417

417418
macro_rules! from_array_impl(
418419
($($oid:ident)|+, $t:ty) => (
419-
from_map_impl!($($oid)|+, ArrayBase<Option<$t>>, |buf: &~[u8]| {
420+
from_map_impl!($($oid)|+, ArrayBase<Option<$t>>, |buf: &Vec<u8>| {
420421
let mut rdr = BufReader::new(buf.as_slice());
421422

422423
let ndim = try_pg!(rdr.read_be_i32()) as uint;
@@ -450,7 +451,7 @@ macro_rules! from_array_impl(
450451
)
451452

452453
from_array_impl!(PgBoolArray, bool)
453-
from_array_impl!(PgByteAArray, ~[u8])
454+
from_array_impl!(PgByteAArray, Vec<u8>)
454455
from_array_impl!(PgCharArray, i8)
455456
from_array_impl!(PgInt2Array, i16)
456457
from_array_impl!(PgInt4Array, i32)
@@ -466,7 +467,7 @@ from_array_impl!(PgTsRangeArray | PgTstzRangeArray, Range<Timespec>)
466467
from_array_impl!(PgInt8RangeArray, Range<i64>)
467468

468469
impl FromSql for Option<HashMap<~str, Option<~str>>> {
469-
fn from_sql(ty: &PostgresType, raw: &Option<~[u8]>)
470+
fn from_sql(ty: &PostgresType, raw: &Option<Vec<u8>>)
470471
-> Result<Option<HashMap<~str, Option<~str>>>, PostgresError> {
471472
match *ty {
472473
PgUnknownType { name: ref name, .. } if "hstore" == *name => {}
@@ -482,13 +483,15 @@ impl FromSql for Option<HashMap<~str, Option<~str>>> {
482483

483484
for _ in range(0, count) {
484485
let key_len = try_pg!(rdr.read_be_i32());
485-
let key = str::from_utf8_owned(try_pg!(rdr.read_exact(key_len as uint))).unwrap();
486+
let key = str::from_utf8(try_pg!(rdr.read_exact(key_len as uint)).as_slice())
487+
.unwrap().to_owned();
486488

487489
let val_len = try_pg!(rdr.read_be_i32());
488490
let val = if val_len < 0 {
489491
None
490492
} else {
491-
Some(str::from_utf8_owned(try_pg!(rdr.read_exact(val_len as uint))).unwrap())
493+
Some(str::from_utf8(try_pg!(rdr.read_exact(val_len as uint)).as_slice())
494+
.unwrap().to_owned())
492495
};
493496

494497
map.insert(key, val);
@@ -501,7 +504,7 @@ impl FromSql for Option<HashMap<~str, Option<~str>>> {
501504
}
502505

503506
impl FromSql for HashMap<~str, Option<~str>> {
504-
fn from_sql(ty: &PostgresType, raw: &Option<~[u8]>)
507+
fn from_sql(ty: &PostgresType, raw: &Option<Vec<u8>>)
505508
-> Result<HashMap<~str, Option<~str>>, PostgresError> {
506509
// FIXME when you can specify Self types properly
507510
let ret: Result<Option<HashMap<~str, Option<~str>>>, PostgresError> = FromSql::from_sql(ty, raw);
@@ -518,7 +521,7 @@ pub trait ToSql {
518521
/// Converts the value of `self` into a format appropriate for the Postgres
519522
/// backend.
520523
fn to_sql(&self, ty: &PostgresType)
521-
-> Result<(Format, Option<~[u8]>), PostgresError>;
524+
-> Result<(Format, Option<Vec<u8>>), PostgresError>;
522525
}
523526

524527
#[doc(hidden)]
@@ -543,12 +546,6 @@ impl RawToSql for bool {
543546
}
544547
}
545548

546-
impl RawToSql for ~[u8] {
547-
fn raw_to_sql<W: Writer>(&self, w: &mut W) -> Result<(), PostgresError> {
548-
Ok(try_pg!(w.write(self.as_slice())))
549-
}
550-
}
551-
552549
impl RawToSql for Vec<u8> {
553550
fn raw_to_sql<W: Writer>(&self, w: &mut W) -> Result<(), PostgresError> {
554551
Ok(try_pg!(w.write(self.as_slice())))
@@ -613,7 +610,7 @@ macro_rules! to_range_impl(
613610
try!(bound.value.raw_to_sql(&mut inner_buf));
614611
let inner_buf = inner_buf.unwrap();
615612
try_pg!(buf.write_be_i32(inner_buf.len() as i32));
616-
try_pg!(buf.write(inner_buf));
613+
try_pg!(buf.write(inner_buf.as_slice()));
617614
}
618615
None => {}
619616
}
@@ -623,7 +620,7 @@ macro_rules! to_range_impl(
623620
try!(bound.value.raw_to_sql(&mut inner_buf));
624621
let inner_buf = inner_buf.unwrap();
625622
try_pg!(buf.write_be_i32(inner_buf.len() as i32));
626-
try_pg!(buf.write(inner_buf));
623+
try_pg!(buf.write(inner_buf.as_slice()));
627624
}
628625
None => {}
629626
}
@@ -649,7 +646,7 @@ macro_rules! to_option_impl(
649646
($($oid:pat)|+, $t:ty) => (
650647
impl ToSql for Option<$t> {
651648
fn to_sql(&self, ty: &PostgresType)
652-
-> Result<(Format, Option<~[u8]>), PostgresError> {
649+
-> Result<(Format, Option<Vec<u8>>), PostgresError> {
653650
check_types!($($oid)|+, ty)
654651

655652
match *self {
@@ -665,7 +662,7 @@ macro_rules! to_option_impl_lifetime(
665662
($($oid:pat)|+, $t:ty) => (
666663
impl<'a> ToSql for Option<$t> {
667664
fn to_sql(&self, ty: &PostgresType)
668-
-> Result<(Format, Option<~[u8]>), PostgresError> {
665+
-> Result<(Format, Option<Vec<u8>>), PostgresError> {
669666
check_types!($($oid)|+, ty)
670667

671668
match *self {
@@ -681,7 +678,7 @@ macro_rules! to_raw_to_impl(
681678
($($oid:ident)|+, $t:ty) => (
682679
impl ToSql for $t {
683680
fn to_sql(&self, ty: &PostgresType)
684-
-> Result<(Format, Option<~[u8]>), PostgresError> {
681+
-> Result<(Format, Option<Vec<u8>>), PostgresError> {
685682
check_types!($($oid)|+, ty)
686683

687684
let mut writer = MemWriter::new();
@@ -695,7 +692,6 @@ macro_rules! to_raw_to_impl(
695692
)
696693

697694
to_raw_to_impl!(PgBool, bool)
698-
to_raw_to_impl!(PgByteA, ~[u8])
699695
to_raw_to_impl!(PgByteA, Vec<u8>)
700696
to_raw_to_impl!(PgVarchar | PgText | PgCharN, ~str)
701697
to_raw_to_impl!(PgJson, Json)
@@ -711,19 +707,19 @@ to_raw_to_impl!(PgTsRange | PgTstzRange, Range<Timespec>)
711707

712708
impl<'a> ToSql for &'a str {
713709
fn to_sql(&self, ty: &PostgresType)
714-
-> Result<(Format, Option<~[u8]>), PostgresError> {
710+
-> Result<(Format, Option<Vec<u8>>), PostgresError> {
715711
check_types!(PgVarchar | PgText | PgCharN, ty)
716-
Ok((Text, Some(self.as_bytes().to_owned())))
712+
Ok((Text, Some(Vec::from_slice(self.as_bytes()))))
717713
}
718714
}
719715

720716
to_option_impl_lifetime!(PgVarchar | PgText | PgCharN, &'a str)
721717

722718
impl<'a> ToSql for &'a [u8] {
723719
fn to_sql(&self, ty: &PostgresType)
724-
-> Result<(Format, Option<~[u8]>), PostgresError> {
720+
-> Result<(Format, Option<Vec<u8>>), PostgresError> {
725721
check_types!(PgByteA, ty)
726-
Ok((Binary, Some(self.to_owned())))
722+
Ok((Binary, Some(Vec::from_slice(*self))))
727723
}
728724
}
729725

@@ -736,7 +732,7 @@ macro_rules! to_array_impl(
736732
($($oid:ident)|+, $t:ty) => (
737733
impl ToSql for ArrayBase<Option<$t>> {
738734
fn to_sql(&self, ty: &PostgresType)
739-
-> Result<(Format, Option<~[u8]>), PostgresError> {
735+
-> Result<(Format, Option<Vec<u8>>), PostgresError> {
740736
check_types!($($oid)|+, ty)
741737
let mut buf = MemWriter::new();
742738

@@ -756,7 +752,7 @@ macro_rules! to_array_impl(
756752
try!(val.raw_to_sql(&mut inner_buf));
757753
let inner_buf = inner_buf.unwrap();
758754
try_pg!(buf.write_be_i32(inner_buf.len() as i32));
759-
try_pg!(buf.write(inner_buf));
755+
try_pg!(buf.write(inner_buf.as_slice()));
760756
}
761757
None => try_pg!(buf.write_be_i32(-1))
762758
}
@@ -771,7 +767,6 @@ macro_rules! to_array_impl(
771767
)
772768

773769
to_array_impl!(PgBoolArray, bool)
774-
to_array_impl!(PgByteAArray, ~[u8])
775770
to_array_impl!(PgByteAArray, Vec<u8>)
776771
to_array_impl!(PgCharArray, i8)
777772
to_array_impl!(PgInt2Array, i16)
@@ -789,7 +784,7 @@ to_array_impl!(PgJsonArray, Json)
789784

790785
impl ToSql for HashMap<~str, Option<~str>> {
791786
fn to_sql(&self, ty: &PostgresType)
792-
-> Result<(Format, Option<~[u8]>), PostgresError> {
787+
-> Result<(Format, Option<Vec<u8>>), PostgresError> {
793788
match *ty {
794789
PgUnknownType { name: ref name, .. } if "hstore" == *name => {}
795790
_ => return Err(PgWrongType(ty.clone()))
@@ -818,7 +813,7 @@ impl ToSql for HashMap<~str, Option<~str>> {
818813

819814
impl ToSql for Option<HashMap<~str, Option<~str>>> {
820815
fn to_sql(&self, ty: &PostgresType)
821-
-> Result<(Format, Option<~[u8]>), PostgresError> {
816+
-> Result<(Format, Option<Vec<u8>>), PostgresError> {
822817
match *ty {
823818
PgUnknownType { name: ref name, .. } if "hstore" == *name => {}
824819
_ => return Err(PgWrongType(ty.clone()))

0 commit comments

Comments
 (0)