Skip to content

Commit 5d04768

Browse files
committed
Redo FromSql
Still use from_sql_nullable internally to preserve back compat, but implementation should use from_sql_null instead
1 parent 78818f9 commit 5d04768

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

src/types/mod.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -593,21 +593,12 @@ impl error::Error for WasNull {
593593
/// `Option<T>` where `T` implements `FromSql`. An `Option<T>` represents a
594594
/// nullable Postgres value.
595595
pub trait FromSql: Sized {
596-
/// Creates a new value of this type from a `Read`er of Postgres data.
597-
///
598-
/// If the value was `NULL`, the `Read`er will be `None`.
599-
///
600-
/// The caller of this method is responsible for ensuring that this type
601-
/// is compatible with the Postgres `Type`.
602-
///
603-
/// The default implementation calls `FromSql::from_sql` when `raw` is
604-
/// `Some` and returns `Err(Error::Conversion(Box::new(WasNull))` when
605-
/// `raw` is `None`. It does not typically need to be overridden.
596+
/// ### Deprecated
606597
fn from_sql_nullable<R: Read>(ty: &Type, raw: Option<&mut R>, ctx: &SessionInfo)
607598
-> Result<Self> {
608599
match raw {
609600
Some(raw) => FromSql::from_sql(ty, raw, ctx),
610-
None => Err(Error::Conversion(Box::new(WasNull))),
601+
None => FromSql::from_sql_null(ty, ctx),
611602
}
612603
}
613604

@@ -618,24 +609,31 @@ pub trait FromSql: Sized {
618609
/// is compatible with the Postgres `Type`.
619610
fn from_sql<R: Read>(ty: &Type, raw: &mut R, ctx: &SessionInfo) -> Result<Self>;
620611

612+
/// Creates a new value of this type from a `NULL` SQL value.
613+
///
614+
/// The caller of this method is responsible for ensuring that this type
615+
/// is compatible with the Postgres `Type`.
616+
///
617+
/// The default implementation returns
618+
/// `Err(Error::Conversion(Box::new(WasNull))`.
619+
fn from_sql_null(ty: &Type, ctx: &SessionInfo) -> Result<Self> {
620+
Err(Error::Conversion(Box::new(WasNull)))
621+
}
622+
621623
/// Determines if a value of this type can be created from the specified
622624
/// Postgres `Type`.
623625
fn accepts(ty: &Type) -> bool;
624626
}
625627

626628
impl<T: FromSql> FromSql for Option<T> {
627-
fn from_sql_nullable<R: Read>(ty: &Type, raw: Option<&mut R>, ctx: &SessionInfo)
628-
-> Result<Option<T>> {
629-
match raw {
630-
Some(raw) => <T as FromSql>::from_sql(ty, raw, ctx).map(Some),
631-
None => Ok(None),
632-
}
633-
}
634-
635629
fn from_sql<R: Read>(ty: &Type, raw: &mut R, ctx: &SessionInfo) -> Result<Option<T>> {
636630
<T as FromSql>::from_sql(ty, raw, ctx).map(Some)
637631
}
638632

633+
fn from_sql_null(_: &Type, _: &SessionInfo) -> Result<Option<T>> {
634+
Ok(None)
635+
}
636+
639637
fn accepts(ty: &Type) -> bool {
640638
<T as FromSql>::accepts(ty)
641639
}

0 commit comments

Comments
 (0)