Skip to content

Commit 7c54eea

Browse files
committed
Add type aliases for the return types of ToSql and FromSql
These types were annoying to write out, and often involved importing several types you wouldn't otherwise need to import. These aliases are `serialize::Result` and `deserialize::Result`. I intend to eventually move the relevant traits into these modules, but this felt like a good standalone change.
1 parent afb1322 commit 7c54eea

37 files changed

Lines changed: 217 additions & 383 deletions

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
1919
* `HasSqlType`, `NotNull`, and `SingleValue` can now be derived with
2020
`#[derive(SqlType)]`. See the docs for those traits for more information.
2121

22+
* The return type of `FromSql`, `FromSqlRow`, and `QueryableByName` can now be
23+
written as `deserialize::Result<Self>`.
24+
25+
* The return type of `ToSql` can now be written as `serialize::Result`.
26+
2227
### Deprecated
2328

2429
* Deprecated `impl_query_id!` in favor of `#[derive(QueryId)]`

diesel/src/deserialize.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//! Types and traits related to deserializing values from the database
2+
3+
use std::error::Error;
4+
use std::result;
5+
6+
/// A specialized result type representing the result of deserializing
7+
/// a value from the database.
8+
pub type Result<T> = result::Result<T, Box<Error + Send + Sync>>;

diesel/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,14 @@ pub mod associations;
136136
pub mod backend;
137137
#[doc(hidden)]
138138
pub mod connection;
139+
pub mod deserialize;
139140
#[macro_use]
140141
pub mod expression;
141142
pub mod expression_methods;
142143
#[doc(hidden)]
143144
pub mod insertable;
144145
pub mod query_builder;
146+
pub mod serialize;
145147
#[macro_use]
146148
pub mod types;
147149

diesel/src/mysql/types/date_and_time.rs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ extern crate chrono;
22
extern crate mysqlclient_sys as ffi;
33

44
use self::chrono::*;
5-
use std::error::Error;
65
use std::io::Write;
76
use std::os::raw as libc;
87
use std::{mem, ptr, slice};
98

109
use mysql::Mysql;
1110
use types::{Date, Datetime, FromSql, IsNull, Time, Timestamp, ToSql, ToSqlOutput};
11+
use {deserialize, serialize};
1212

1313
macro_rules! mysql_time_impls {
1414
($ty:ty) => {
1515
impl ToSql<$ty, Mysql> for ffi::MYSQL_TIME {
16-
fn to_sql<W: Write>(&self, out: &mut ToSqlOutput<W, Mysql>) -> Result<IsNull, Box<Error+Send+Sync>> {
16+
fn to_sql<W: Write>(&self, out: &mut ToSqlOutput<W, Mysql>) -> serialize::Result {
1717
let bytes = unsafe {
1818
let bytes_ptr = self as *const ffi::MYSQL_TIME as *const u8;
1919
slice::from_raw_parts(bytes_ptr, mem::size_of::<ffi::MYSQL_TIME>())
@@ -24,7 +24,7 @@ macro_rules! mysql_time_impls {
2424
}
2525

2626
impl FromSql<$ty, Mysql> for ffi::MYSQL_TIME {
27-
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error+Send+Sync>> {
27+
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
2828
let bytes = not_none!(bytes);
2929
let bytes_ptr = bytes.as_ptr() as *const ffi::MYSQL_TIME;
3030
unsafe {
@@ -47,25 +47,19 @@ mysql_time_impls!(Time);
4747
mysql_time_impls!(Date);
4848

4949
impl ToSql<Datetime, Mysql> for NaiveDateTime {
50-
fn to_sql<W: Write>(
51-
&self,
52-
out: &mut ToSqlOutput<W, Mysql>,
53-
) -> Result<IsNull, Box<Error + Send + Sync>> {
50+
fn to_sql<W: Write>(&self, out: &mut ToSqlOutput<W, Mysql>) -> serialize::Result {
5451
<NaiveDateTime as ToSql<Timestamp, Mysql>>::to_sql(self, out)
5552
}
5653
}
5754

5855
impl FromSql<Datetime, Mysql> for NaiveDateTime {
59-
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error + Send + Sync>> {
56+
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
6057
<NaiveDateTime as FromSql<Timestamp, Mysql>>::from_sql(bytes)
6158
}
6259
}
6360

6461
impl ToSql<Timestamp, Mysql> for NaiveDateTime {
65-
fn to_sql<W: Write>(
66-
&self,
67-
out: &mut ToSqlOutput<W, Mysql>,
68-
) -> Result<IsNull, Box<Error + Send + Sync>> {
62+
fn to_sql<W: Write>(&self, out: &mut ToSqlOutput<W, Mysql>) -> serialize::Result {
6963
let mut mysql_time: ffi::MYSQL_TIME = unsafe { mem::zeroed() };
7064

7165
mysql_time.year = self.year() as libc::c_uint;
@@ -81,7 +75,7 @@ impl ToSql<Timestamp, Mysql> for NaiveDateTime {
8175
}
8276

8377
impl FromSql<Timestamp, Mysql> for NaiveDateTime {
84-
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error + Send + Sync>> {
78+
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
8579
let mysql_time = <ffi::MYSQL_TIME as FromSql<Timestamp, Mysql>>::from_sql(bytes)?;
8680

8781
NaiveDate::from_ymd_opt(
@@ -101,10 +95,7 @@ impl FromSql<Timestamp, Mysql> for NaiveDateTime {
10195
}
10296

10397
impl ToSql<Time, Mysql> for NaiveTime {
104-
fn to_sql<W: Write>(
105-
&self,
106-
out: &mut ToSqlOutput<W, Mysql>,
107-
) -> Result<IsNull, Box<Error + Send + Sync>> {
98+
fn to_sql<W: Write>(&self, out: &mut ToSqlOutput<W, Mysql>) -> serialize::Result {
10899
let mut mysql_time: ffi::MYSQL_TIME = unsafe { mem::zeroed() };
109100

110101
mysql_time.hour = self.hour() as libc::c_uint;
@@ -116,7 +107,7 @@ impl ToSql<Time, Mysql> for NaiveTime {
116107
}
117108

118109
impl FromSql<Time, Mysql> for NaiveTime {
119-
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error + Send + Sync>> {
110+
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
120111
let mysql_time = <ffi::MYSQL_TIME as FromSql<Time, Mysql>>::from_sql(bytes)?;
121112
NaiveTime::from_hms_opt(
122113
mysql_time.hour as u32,
@@ -127,10 +118,7 @@ impl FromSql<Time, Mysql> for NaiveTime {
127118
}
128119

129120
impl ToSql<Date, Mysql> for NaiveDate {
130-
fn to_sql<W: Write>(
131-
&self,
132-
out: &mut ToSqlOutput<W, Mysql>,
133-
) -> Result<IsNull, Box<Error + Send + Sync>> {
121+
fn to_sql<W: Write>(&self, out: &mut ToSqlOutput<W, Mysql>) -> serialize::Result {
134122
let mut mysql_time: ffi::MYSQL_TIME = unsafe { mem::zeroed() };
135123

136124
mysql_time.year = self.year() as libc::c_uint;
@@ -142,7 +130,7 @@ impl ToSql<Date, Mysql> for NaiveDate {
142130
}
143131

144132
impl FromSql<Date, Mysql> for NaiveDate {
145-
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error + Send + Sync>> {
133+
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
146134
let mysql_time = <ffi::MYSQL_TIME as FromSql<Date, Mysql>>::from_sql(bytes)?;
147135
NaiveDate::from_ymd_opt(
148136
mysql_time.year as i32,

diesel/src/mysql/types/mod.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,32 @@ mod numeric;
66

77
use byteorder::WriteBytesExt;
88
use mysql::Mysql;
9-
use std::error::Error as StdError;
109
use std::io::Write;
1110
use types::{self, FromSql, IsNull, ToSql, ToSqlOutput};
11+
use {deserialize, serialize};
1212

1313
impl ToSql<types::Tinyint, Mysql> for i8 {
14-
fn to_sql<W: Write>(
15-
&self,
16-
out: &mut ToSqlOutput<W, Mysql>,
17-
) -> Result<IsNull, Box<StdError + Send + Sync>> {
18-
out.write_i8(*self)
19-
.map(|_| IsNull::No)
20-
.map_err(|e| Box::new(e) as Box<StdError + Send + Sync>)
14+
fn to_sql<W: Write>(&self, out: &mut ToSqlOutput<W, Mysql>) -> serialize::Result {
15+
out.write_i8(*self).map(|_| IsNull::No).map_err(Into::into)
2116
}
2217
}
2318

2419
impl FromSql<types::Tinyint, Mysql> for i8 {
25-
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<StdError + Send + Sync>> {
20+
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
2621
let bytes = not_none!(bytes);
2722
Ok(bytes[0] as i8)
2823
}
2924
}
3025

3126
impl ToSql<types::Bool, Mysql> for bool {
32-
fn to_sql<W: Write>(
33-
&self,
34-
out: &mut ToSqlOutput<W, Mysql>,
35-
) -> Result<IsNull, Box<StdError + Send + Sync>> {
27+
fn to_sql<W: Write>(&self, out: &mut ToSqlOutput<W, Mysql>) -> serialize::Result {
3628
let int_value = if *self { 1 } else { 0 };
3729
<i32 as ToSql<types::Integer, Mysql>>::to_sql(&int_value, out)
3830
}
3931
}
4032

4133
impl FromSql<types::Bool, Mysql> for bool {
42-
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<StdError + Send + Sync>> {
34+
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
4335
Ok(not_none!(bytes).iter().any(|x| *x != 0))
4436
}
4537
}

diesel/src/mysql/types/numeric.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,23 @@
22
pub mod bigdecimal {
33
extern crate bigdecimal;
44

5-
use std::error::Error;
5+
use self::bigdecimal::BigDecimal;
66
use std::io::prelude::*;
77

88
use mysql::Mysql;
9-
10-
use self::bigdecimal::BigDecimal;
11-
129
use types::{self, FromSql, IsNull, ToSql, ToSqlOutput};
10+
use {deserialize, serialize};
1311

1412
impl ToSql<types::Numeric, Mysql> for BigDecimal {
15-
fn to_sql<W: Write>(
16-
&self,
17-
out: &mut ToSqlOutput<W, Mysql>,
18-
) -> Result<IsNull, Box<Error + Send + Sync>> {
13+
fn to_sql<W: Write>(&self, out: &mut ToSqlOutput<W, Mysql>) -> serialize::Result {
1914
write!(out, "{}", *self)
2015
.map(|_| IsNull::No)
2116
.map_err(|e| e.into())
2217
}
2318
}
2419

2520
impl FromSql<types::Numeric, Mysql> for BigDecimal {
26-
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error + Send + Sync>> {
21+
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
2722
let bytes = not_none!(bytes);
2823
BigDecimal::parse_bytes(bytes, 10)
2924
.ok_or_else(|| Box::from(format!("{:?} is not valid decimal number ", bytes)))

diesel/src/pg/types/array.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use byteorder::{NetworkEndian, ReadBytesExt, WriteBytesExt};
2-
use std::error::Error;
32
use std::fmt;
43
use std::io::Write;
54

65
use pg::{Pg, PgMetadataLookup, PgTypeMetadata};
76
use types::*;
7+
use {deserialize, serialize};
88

99
impl<T> HasSqlType<Array<T>> for Pg
1010
where
@@ -26,7 +26,7 @@ impl<T, ST> FromSql<Array<ST>, Pg> for Vec<T>
2626
where
2727
T: FromSql<ST, Pg>,
2828
{
29-
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error + Send + Sync>> {
29+
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
3030
let mut bytes = not_none!(bytes);
3131
let num_dimensions = try!(bytes.read_i32::<NetworkEndian>());
3232
let has_null = try!(bytes.read_i32::<NetworkEndian>()) != 0;
@@ -92,10 +92,7 @@ where
9292
Pg: HasSqlType<ST>,
9393
T: ToSql<ST, Pg>,
9494
{
95-
fn to_sql<W: Write>(
96-
&self,
97-
out: &mut ToSqlOutput<W, Pg>,
98-
) -> Result<IsNull, Box<Error + Send + Sync>> {
95+
fn to_sql<W: Write>(&self, out: &mut ToSqlOutput<W, Pg>) -> serialize::Result {
9996
let num_dimensions = 1;
10097
try!(out.write_i32::<NetworkEndian>(num_dimensions));
10198
let flags = 0;
@@ -127,10 +124,7 @@ impl<ST, T> ToSql<Nullable<Array<ST>>, Pg> for [T]
127124
where
128125
[T]: ToSql<Array<ST>, Pg>,
129126
{
130-
fn to_sql<W: Write>(
131-
&self,
132-
out: &mut ToSqlOutput<W, Pg>,
133-
) -> Result<IsNull, Box<Error + Send + Sync>> {
127+
fn to_sql<W: Write>(&self, out: &mut ToSqlOutput<W, Pg>) -> serialize::Result {
134128
ToSql::<Array<ST>, Pg>::to_sql(self, out)
135129
}
136130
}
@@ -140,10 +134,7 @@ where
140134
[T]: ToSql<Array<ST>, Pg>,
141135
T: fmt::Debug,
142136
{
143-
fn to_sql<W: Write>(
144-
&self,
145-
out: &mut ToSqlOutput<W, Pg>,
146-
) -> Result<IsNull, Box<Error + Send + Sync>> {
137+
fn to_sql<W: Write>(&self, out: &mut ToSqlOutput<W, Pg>) -> serialize::Result {
147138
(self as &[T]).to_sql(out)
148139
}
149140
}
@@ -152,10 +143,7 @@ impl<ST, T> ToSql<Nullable<Array<ST>>, Pg> for Vec<T>
152143
where
153144
Vec<T>: ToSql<Array<ST>, Pg>,
154145
{
155-
fn to_sql<W: Write>(
156-
&self,
157-
out: &mut ToSqlOutput<W, Pg>,
158-
) -> Result<IsNull, Box<Error + Send + Sync>> {
146+
fn to_sql<W: Write>(&self, out: &mut ToSqlOutput<W, Pg>) -> serialize::Result {
159147
ToSql::<Array<ST>, Pg>::to_sql(self, out)
160148
}
161149
}

0 commit comments

Comments
 (0)