Skip to content

Commit

Permalink
Simplify ToSql lifetimes
Browse files Browse the repository at this point in the history
Because non-mutable references (and other covariant types) always get
their lifetimes automatically downgraded upon function calls, the
ToSql signature can be rewritten in a much simpler way
  • Loading branch information
weiznich committed Nov 22, 2021
1 parent 104550e commit 3e9e97d
Show file tree
Hide file tree
Showing 33 changed files with 88 additions and 170 deletions.
9 changes: 3 additions & 6 deletions diesel/src/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,10 @@ where
Cow<'a, str>: ToSql<Text, DB>,
DB: Backend,
{
fn to_sql<'b, 'c, 'd>(
fn to_sql<'b>(
&'b self,
out: &mut crate::serialize::Output<'c, 'd, DB>,
) -> crate::serialize::Result
where
'b: 'c,
{
out: &mut crate::serialize::Output<'b, '_, DB>,
) -> crate::serialize::Result {
self.0.to_sql(out)
}
}
Expand Down
13 changes: 5 additions & 8 deletions diesel/src/mysql/types/date_and_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl MysqlTimestampType {
macro_rules! mysql_time_impls {
($ty:ty) => {
impl ToSql<$ty, Mysql> for MysqlTime {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
let bytes = unsafe {
let bytes_ptr = self as *const MysqlTime as *const u8;
slice::from_raw_parts(bytes_ptr, mem::size_of::<MysqlTime>())
Expand All @@ -131,7 +131,7 @@ mysql_time_impls!(Date);

#[cfg(feature = "chrono")]
impl ToSql<Datetime, Mysql> for NaiveDateTime {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
<NaiveDateTime as ToSql<Timestamp, Mysql>>::to_sql(self, out)
}
}
Expand All @@ -145,7 +145,7 @@ impl FromSql<Datetime, Mysql> for NaiveDateTime {

#[cfg(feature = "chrono")]
impl ToSql<Timestamp, Mysql> for NaiveDateTime {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
let mysql_time = MysqlTime {
year: self.year() as libc::c_uint,
month: self.month() as libc::c_uint,
Expand Down Expand Up @@ -187,10 +187,7 @@ impl FromSql<Timestamp, Mysql> for NaiveDateTime {

#[cfg(feature = "chrono")]
impl ToSql<Time, Mysql> for NaiveTime {
fn to_sql<'a: 'b, 'b>(
&'a self,
out: &mut serialize::Output<'b, '_, Mysql>,
) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut serialize::Output<'b, '_, Mysql>) -> serialize::Result {
let mysql_time = MysqlTime {
hour: self.hour() as libc::c_uint,
minute: self.minute() as libc::c_uint,
Expand Down Expand Up @@ -223,7 +220,7 @@ impl FromSql<Time, Mysql> for NaiveTime {

#[cfg(feature = "chrono")]
impl ToSql<Date, Mysql> for NaiveDate {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
let mysql_time = MysqlTime {
year: self.year() as libc::c_uint,
month: self.month() as libc::c_uint,
Expand Down
2 changes: 1 addition & 1 deletion diesel/src/mysql/types/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl FromSql<sql_types::Json, Mysql> for serde_json::Value {
}

impl ToSql<sql_types::Json, Mysql> for serde_json::Value {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
serde_json::to_writer(out, self)
.map(|_| IsNull::No)
.map_err(Into::into)
Expand Down
22 changes: 11 additions & 11 deletions diesel/src/mysql/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use byteorder::{NativeEndian, WriteBytesExt};
pub use date_and_time::{MysqlTime, MysqlTimestampType};

impl ToSql<TinyInt, Mysql> for i8 {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
out.write_i8(*self).map(|_| IsNull::No).map_err(Into::into)
}
}
Expand Down Expand Up @@ -66,7 +66,7 @@ where
}

impl ToSql<Unsigned<TinyInt>, Mysql> for u8 {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
ToSql::<TinyInt, Mysql>::to_sql(&(*self as i8), &mut out.reborrow())
}
}
Expand All @@ -79,7 +79,7 @@ impl FromSql<Unsigned<TinyInt>, Mysql> for u8 {
}

impl ToSql<Unsigned<SmallInt>, Mysql> for u16 {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
ToSql::<SmallInt, Mysql>::to_sql(&(*self as i16), &mut out.reborrow())
}
}
Expand All @@ -92,7 +92,7 @@ impl FromSql<Unsigned<SmallInt>, Mysql> for u16 {
}

impl ToSql<Unsigned<Integer>, Mysql> for u32 {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
ToSql::<Integer, Mysql>::to_sql(&(*self as i32), &mut out.reborrow())
}
}
Expand All @@ -105,7 +105,7 @@ impl FromSql<Unsigned<Integer>, Mysql> for u32 {
}

impl ToSql<Unsigned<BigInt>, Mysql> for u64 {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
ToSql::<BigInt, Mysql>::to_sql(&(*self as i64), &mut out.reborrow())
}
}
Expand All @@ -118,7 +118,7 @@ impl FromSql<Unsigned<BigInt>, Mysql> for u64 {
}

impl ToSql<Bool, Mysql> for bool {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
let int_value = if *self { 1 } else { 0 };
<i32 as ToSql<Integer, Mysql>>::to_sql(&int_value, &mut out.reborrow())
}
Expand All @@ -131,39 +131,39 @@ impl FromSql<Bool, Mysql> for bool {
}

impl ToSql<sql_types::SmallInt, Mysql> for i16 {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
out.write_i16::<NativeEndian>(*self)
.map(|_| IsNull::No)
.map_err(|e| Box::new(e) as Box<_>)
}
}

impl ToSql<sql_types::Integer, Mysql> for i32 {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
out.write_i32::<NativeEndian>(*self)
.map(|_| IsNull::No)
.map_err(|e| Box::new(e) as Box<_>)
}
}

impl ToSql<sql_types::BigInt, Mysql> for i64 {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
out.write_i64::<NativeEndian>(*self)
.map(|_| IsNull::No)
.map_err(|e| Box::new(e) as Box<_>)
}
}

impl ToSql<sql_types::Double, Mysql> for f64 {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
out.write_f64::<NativeEndian>(*self)
.map(|_| IsNull::No)
.map_err(|e| Box::new(e) as Box<_>)
}
}

impl ToSql<sql_types::Float, Mysql> for f32 {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
out.write_f32::<NativeEndian>(*self)
.map(|_| IsNull::No)
.map_err(|e| Box::new(e) as Box<_>)
Expand Down
2 changes: 1 addition & 1 deletion diesel/src/mysql/types/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub mod bigdecimal {
use crate::sql_types::Numeric;

impl ToSql<Numeric, Mysql> for BigDecimal {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
write!(out, "{}", *self)
.map(|_| IsNull::No)
.map_err(Into::into)
Expand Down
2 changes: 1 addition & 1 deletion diesel/src/pg/serialize/write_tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::serialize::{self, Output};
/// struct MyStruct<'a>(i32, &'a str);
///
/// impl<'a> ToSql<MyType, Pg> for MyStruct<'a> {
/// fn to_sql<'b: 'c, 'c>(&'b self, out: &mut Output<'c, '_, Pg>) -> serialize::Result {
/// fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
/// WriteTuple::<(Integer, Text)>::write_tuple(
/// &(self.0, self.1),
/// &mut out.reborrow(),
Expand Down
8 changes: 4 additions & 4 deletions diesel/src/pg/types/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ where
Pg: HasSqlType<ST>,
T: ToSql<ST, Pg>,
{
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
let num_dimensions = 1;
out.write_i32::<NetworkEndian>(num_dimensions)?;
let flags = 0;
Expand Down Expand Up @@ -127,7 +127,7 @@ where
[T]: ToSql<Array<ST>, Pg>,
ST: 'static,
{
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
ToSql::<Array<ST>, Pg>::to_sql(self, out)
}
}
Expand All @@ -138,7 +138,7 @@ where
[T]: ToSql<Array<ST>, Pg>,
T: fmt::Debug,
{
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
(self as &[T]).to_sql(out)
}
}
Expand All @@ -148,7 +148,7 @@ where
ST: 'static,
Vec<T>: ToSql<Array<ST>, Pg>,
{
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
ToSql::<Array<ST>, Pg>::to_sql(self, out)
}
}
10 changes: 5 additions & 5 deletions diesel/src/pg/types/date_and_time/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl FromSql<Timestamp, Pg> for NaiveDateTime {
}

impl ToSql<Timestamp, Pg> for NaiveDateTime {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
let time = match (self.signed_duration_since(pg_epoch())).num_microseconds() {
Some(time) => time,
None => {
Expand All @@ -51,7 +51,7 @@ impl FromSql<Timestamptz, Pg> for NaiveDateTime {
}

impl ToSql<Timestamptz, Pg> for NaiveDateTime {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
ToSql::<Timestamp, Pg>::to_sql(self, out)
}
}
Expand All @@ -71,7 +71,7 @@ impl FromSql<Timestamptz, Pg> for DateTime<Local> {
}

impl<TZ: TimeZone> ToSql<Timestamptz, Pg> for DateTime<TZ> {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
ToSql::<Timestamptz, Pg>::to_sql(&self.naive_utc(), &mut out.reborrow())
}
}
Expand All @@ -81,7 +81,7 @@ fn midnight() -> NaiveTime {
}

impl ToSql<Time, Pg> for NaiveTime {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
let duration = self.signed_duration_since(midnight());
match duration.num_microseconds() {
Some(offset) => ToSql::<Time, Pg>::to_sql(&PgTime(offset), &mut out.reborrow()),
Expand All @@ -103,7 +103,7 @@ fn pg_epoch_date() -> NaiveDate {
}

impl ToSql<Date, Pg> for NaiveDate {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
let days_since_epoch = self.signed_duration_since(pg_epoch_date()).num_days();
ToSql::<Date, Pg>::to_sql(&PgDate(days_since_epoch as i32), &mut out.reborrow())
}
Expand Down
10 changes: 5 additions & 5 deletions diesel/src/pg/types/date_and_time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl PgInterval {
}

impl ToSql<sql_types::Timestamp, Pg> for PgTimestamp {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
ToSql::<sql_types::BigInt, Pg>::to_sql(&self.0, out)
}
}
Expand All @@ -93,7 +93,7 @@ impl FromSql<sql_types::Timestamp, Pg> for PgTimestamp {
}

impl ToSql<sql_types::Timestamptz, Pg> for PgTimestamp {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
ToSql::<sql_types::Timestamp, Pg>::to_sql(self, out)
}
}
Expand All @@ -105,7 +105,7 @@ impl FromSql<sql_types::Timestamptz, Pg> for PgTimestamp {
}

impl ToSql<sql_types::Date, Pg> for PgDate {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
ToSql::<sql_types::Integer, Pg>::to_sql(&self.0, out)
}
}
Expand All @@ -117,7 +117,7 @@ impl FromSql<sql_types::Date, Pg> for PgDate {
}

impl ToSql<sql_types::Time, Pg> for PgTime {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
ToSql::<sql_types::BigInt, Pg>::to_sql(&self.0, out)
}
}
Expand All @@ -129,7 +129,7 @@ impl FromSql<sql_types::Time, Pg> for PgTime {
}

impl ToSql<sql_types::Interval, Pg> for PgInterval {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
ToSql::<sql_types::BigInt, Pg>::to_sql(&self.microseconds, out)?;
ToSql::<sql_types::Integer, Pg>::to_sql(&self.days, out)?;
ToSql::<sql_types::Integer, Pg>::to_sql(&self.months, out)?;
Expand Down
2 changes: 1 addition & 1 deletion diesel/src/pg/types/date_and_time/std_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn pg_epoch() -> SystemTime {
}

impl ToSql<sql_types::Timestamp, Pg> for SystemTime {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
let (before_epoch, duration) = match self.duration_since(pg_epoch()) {
Ok(duration) => (false, duration),
Err(time_err) => (true, time_err.duration()),
Expand Down
6 changes: 3 additions & 3 deletions diesel/src/pg/types/floats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl FromSql<sql_types::Numeric, Pg> for PgNumeric {
}

impl ToSql<sql_types::Numeric, Pg> for PgNumeric {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
let sign = match *self {
PgNumeric::Positive { .. } => 0,
PgNumeric::Negative { .. } => 0x4000,
Expand Down Expand Up @@ -149,15 +149,15 @@ impl FromSql<sql_types::Double, Pg> for f64 {
}

impl ToSql<sql_types::Float, Pg> for f32 {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
out.write_f32::<NetworkEndian>(*self)
.map(|_| IsNull::No)
.map_err(|e| Box::new(e) as Box<dyn Error + Send + Sync>)
}
}

impl ToSql<sql_types::Double, Pg> for f64 {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
out.write_f64::<NetworkEndian>(*self)
.map(|_| IsNull::No)
.map_err(|e| Box::new(e) as Box<dyn Error + Send + Sync>)
Expand Down
8 changes: 4 additions & 4 deletions diesel/src/pg/types/integers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl FromSql<sql_types::Oid, Pg> for u32 {
}

impl ToSql<sql_types::Oid, Pg> for u32 {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
out.write_u32::<NetworkEndian>(*self)
.map(|_| IsNull::No)
.map_err(Into::into)
Expand Down Expand Up @@ -78,23 +78,23 @@ impl FromSql<sql_types::BigInt, Pg> for i64 {
}

impl ToSql<sql_types::SmallInt, Pg> for i16 {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
out.write_i16::<NetworkEndian>(*self)
.map(|_| IsNull::No)
.map_err(|e| Box::new(e) as Box<_>)
}
}

impl ToSql<sql_types::Integer, Pg> for i32 {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
out.write_i32::<NetworkEndian>(*self)
.map(|_| IsNull::No)
.map_err(|e| Box::new(e) as Box<_>)
}
}

impl ToSql<sql_types::BigInt, Pg> for i64 {
fn to_sql<'a: 'b, 'b>(&'a self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
out.write_i64::<NetworkEndian>(*self)
.map(|_| IsNull::No)
.map_err(|e| Box::new(e) as Box<_>)
Expand Down
Loading

0 comments on commit 3e9e97d

Please sign in to comment.