Skip to content

Commit 97eea50

Browse files
committed
Remove some macro spam
Yay for multi-item macros!
1 parent d5a125f commit 97eea50

1 file changed

Lines changed: 14 additions & 74 deletions

File tree

types/mod.rs

Lines changed: 14 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,14 @@ macro_rules! from_map_impl(
260260
raw.as_ref().map($blk)
261261
}
262262
}
263+
264+
impl FromSql for $t {
265+
fn from_sql(ty: &PostgresType, raw: &Option<~[u8]>) -> $t {
266+
// FIXME when you can specify Self types properly
267+
let ret: Option<$t> = FromSql::from_sql(ty, raw);
268+
ret.unwrap()
269+
}
270+
}
263271
)
264272
)
265273

@@ -272,52 +280,28 @@ macro_rules! from_raw_from_impl(
272280
)
273281
)
274282

275-
macro_rules! from_option_impl(
276-
($t:ty) => (
277-
impl FromSql for $t {
278-
fn from_sql(ty: &PostgresType, raw: &Option<~[u8]>) -> $t {
279-
// FIXME when you can specify Self types properly
280-
let ret: Option<$t> = FromSql::from_sql(ty, raw);
281-
ret.unwrap()
282-
}
283-
}
284-
)
285-
)
286-
287283
from_raw_from_impl!(PgBool, bool)
288-
from_option_impl!(bool)
289284
from_raw_from_impl!(PgByteA, ~[u8])
290-
from_option_impl!(~[u8])
291285
from_raw_from_impl!(PgChar, i8)
292-
from_option_impl!(i8)
293286
from_raw_from_impl!(PgInt2, i16)
294-
from_option_impl!(i16)
295287
from_raw_from_impl!(PgInt4, i32)
296-
from_option_impl!(i32)
297288
from_raw_from_impl!(PgInt8, i64)
298-
from_option_impl!(i64)
299289
from_raw_from_impl!(PgFloat4, f32)
300-
from_option_impl!(f32)
301290
from_raw_from_impl!(PgFloat8, f64)
302-
from_option_impl!(f64)
303291

304292
from_map_impl!(PgVarchar | PgText | PgCharN, ~str, |buf| {
305293
str::from_utf8_owned(buf.clone())
306294
})
307-
from_option_impl!(~str)
308295

309296
from_map_impl!(PgJson, Json, |buf| {
310297
json::from_str(str::from_utf8(buf.as_slice())).unwrap()
311298
})
312-
from_option_impl!(Json)
313299

314300
from_map_impl!(PgUuid, Uuid, |buf| {
315301
Uuid::from_bytes(buf.as_slice()).unwrap()
316302
})
317-
from_option_impl!(Uuid)
318303

319304
from_raw_from_impl!(PgTimestamp | PgTimestampZ, Timespec)
320-
from_option_impl!(Timespec)
321305

322306
macro_rules! from_range_impl(
323307
($($oid:ident)|+, $t:ty) => (
@@ -360,13 +344,8 @@ macro_rules! from_range_impl(
360344
)
361345

362346
from_range_impl!(PgInt4Range, i32)
363-
from_option_impl!(Range<i32>)
364-
365347
from_range_impl!(PgInt8Range, i64)
366-
from_option_impl!(Range<i64>)
367-
368348
from_range_impl!(PgTsRange | PgTstzRange, Timespec)
369-
from_option_impl!(Range<Timespec>)
370349

371350
macro_rules! from_array_impl(
372351
($($oid:ident)|+, $t:ty) => (
@@ -403,28 +382,13 @@ macro_rules! from_array_impl(
403382
)
404383

405384
from_array_impl!(PgBoolArray, bool)
406-
from_option_impl!(ArrayBase<Option<bool>>)
407-
408385
from_array_impl!(PgByteAArray, ~[u8])
409-
from_option_impl!(ArrayBase<Option<~[u8]>>)
410-
411386
from_array_impl!(PgCharArray, i8)
412-
from_option_impl!(ArrayBase<Option<i8>>)
413-
414387
from_array_impl!(PgInt2Array, i16)
415-
from_option_impl!(ArrayBase<Option<i16>>)
416-
417388
from_array_impl!(PgInt4Array, i32)
418-
from_option_impl!(ArrayBase<Option<i32>>)
419-
420389
from_array_impl!(PgInt8Array, i64)
421-
from_option_impl!(ArrayBase<Option<i64>>)
422-
423390
from_array_impl!(PgFloat4Array, f32)
424-
from_option_impl!(ArrayBase<Option<f32>>)
425-
426391
from_array_impl!(PgFloat8Array, f64)
427-
from_option_impl!(ArrayBase<Option<f64>>)
428392

429393
from_map_impl!(PgUnknownType { name: ~"hstore", .. },
430394
HashMap<~str, Option<~str>>, |buf| {
@@ -449,7 +413,6 @@ from_map_impl!(PgUnknownType { name: ~"hstore", .. },
449413
450414
map
451415
})
452-
from_option_impl!(HashMap<~str, Option<~str>>)
453416
454417
/// A trait for types that can be converted into Postgres values
455418
pub trait ToSql {
@@ -563,25 +526,19 @@ macro_rules! to_raw_to_impl(
563526
(Binary, Some(writer.inner()))
564527
}
565528
}
529+
530+
to_option_impl!($($oid)|+, $t)
566531
)
567532
)
568533
569534
to_raw_to_impl!(PgBool, bool)
570-
to_option_impl!(PgBool, bool)
571535
to_raw_to_impl!(PgByteA, ~[u8])
572-
to_option_impl!(PgByteA, ~[u8])
573536
to_raw_to_impl!(PgChar, i8)
574-
to_option_impl!(PgChar, i8)
575537
to_raw_to_impl!(PgInt2, i16)
576-
to_option_impl!(PgInt2, i16)
577538
to_raw_to_impl!(PgInt4, i32)
578-
to_option_impl!(PgInt4, i32)
579539
to_raw_to_impl!(PgInt8, i64)
580-
to_option_impl!(PgInt8, i64)
581540
to_raw_to_impl!(PgFloat4, f32)
582-
to_option_impl!(PgFloat4, f32)
583541
to_raw_to_impl!(PgFloat8, f64)
584-
to_option_impl!(PgFloat8, f64)
585542
586543
impl ToSql for ~str {
587544
fn to_sql(&self, ty: &PostgresType) -> (Format, Option<~[u8]>) {
@@ -628,7 +585,6 @@ impl ToSql for Uuid {
628585
to_option_impl!(PgUuid, Uuid)
629586
630587
to_raw_to_impl!(PgTimestamp | PgTimestampZ, Timespec)
631-
to_option_impl!(PgTimestamp | PgTimestampZ, Timespec)
632588
633589
macro_rules! to_range_impl(
634590
($($oid:ident)|+, $t:ty) => (
@@ -675,17 +631,14 @@ macro_rules! to_range_impl(
675631
(Binary, Some(buf.inner()))
676632
}
677633
}
634+
635+
to_option_impl!($($oid)|+, Range<$t>)
678636
)
679637
)
680638
681639
to_range_impl!(PgInt4Range, i32)
682-
to_option_impl!(PgInt4Range, Range<i32>)
683-
684640
to_range_impl!(PgInt8Range, i64)
685-
to_option_impl!(PgInt8Range, Range<i64>)
686-
687641
to_range_impl!(PgTsRange | PgTstzRange, Timespec)
688-
to_option_impl!(PgTsRange | PgTstzRange, Range<Timespec>)
689642
690643
macro_rules! to_array_impl(
691644
($($oid:ident)|+, $base_oid:ident, $t:ty) => (
@@ -716,32 +669,19 @@ macro_rules! to_array_impl(
716669
(Binary, Some(buf.inner()))
717670
}
718671
}
672+
673+
to_option_impl!($($oid)|+, ArrayBase<Option<$t>>)
719674
)
720675
)
721676
722677
to_array_impl!(PgBoolArray, BOOLOID, bool)
723-
to_option_impl!(PgBoolArray, ArrayBase<Option<bool>>)
724-
725678
to_array_impl!(PgByteAArray, BYTEAOID, ~[u8])
726-
to_option_impl!(PgByteAArray, ArrayBase<Option<~[u8]>>)
727-
728679
to_array_impl!(PgCharArray, CHAROID, i8)
729-
to_option_impl!(PgCharArray, ArrayBase<Option<i8>>)
730-
731680
to_array_impl!(PgInt2Array, INT2OID, i16)
732-
to_option_impl!(PgInt2Array, ArrayBase<Option<i16>>)
733-
734681
to_array_impl!(PgInt4Array, INT4OID, i32)
735-
to_option_impl!(PgInt4Array, ArrayBase<Option<i32>>)
736-
737682
to_array_impl!(PgInt8Array, INT8OID, i64)
738-
to_option_impl!(PgInt8Array, ArrayBase<Option<i64>>)
739-
740683
to_array_impl!(PgFloat4Array, FLOAT4OID, f32)
741-
to_option_impl!(PgFloat4Array, ArrayBase<Option<f32>>)
742-
743684
to_array_impl!(PgFloat8Array, FLOAT8OID, f64)
744-
to_option_impl!(PgFloat8Array, ArrayBase<Option<f64>>)
745685
746686
impl<'self> ToSql for HashMap<~str, Option<~str>> {
747687
fn to_sql(&self, ty: &PostgresType) -> (Format, Option<~[u8]>) {

0 commit comments

Comments
 (0)