From 5d047683f02f4fbaa7b341f63733c02ce78a835f Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 24 Oct 2015 19:24:00 -0700 Subject: [PATCH] Redo FromSql Still use from_sql_nullable internally to preserve back compat, but implementation should use from_sql_null instead --- src/types/mod.rs | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/types/mod.rs b/src/types/mod.rs index f1875464..04149b33 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -593,21 +593,12 @@ impl error::Error for WasNull { /// `Option` where `T` implements `FromSql`. An `Option` represents a /// nullable Postgres value. pub trait FromSql: Sized { - /// Creates a new value of this type from a `Read`er of Postgres data. - /// - /// If the value was `NULL`, the `Read`er will be `None`. - /// - /// The caller of this method is responsible for ensuring that this type - /// is compatible with the Postgres `Type`. - /// - /// The default implementation calls `FromSql::from_sql` when `raw` is - /// `Some` and returns `Err(Error::Conversion(Box::new(WasNull))` when - /// `raw` is `None`. It does not typically need to be overridden. + /// ### Deprecated fn from_sql_nullable(ty: &Type, raw: Option<&mut R>, ctx: &SessionInfo) -> Result { match raw { Some(raw) => FromSql::from_sql(ty, raw, ctx), - None => Err(Error::Conversion(Box::new(WasNull))), + None => FromSql::from_sql_null(ty, ctx), } } @@ -618,24 +609,31 @@ pub trait FromSql: Sized { /// is compatible with the Postgres `Type`. fn from_sql(ty: &Type, raw: &mut R, ctx: &SessionInfo) -> Result; + /// Creates a new value of this type from a `NULL` SQL value. + /// + /// The caller of this method is responsible for ensuring that this type + /// is compatible with the Postgres `Type`. + /// + /// The default implementation returns + /// `Err(Error::Conversion(Box::new(WasNull))`. + fn from_sql_null(ty: &Type, ctx: &SessionInfo) -> Result { + Err(Error::Conversion(Box::new(WasNull))) + } + /// Determines if a value of this type can be created from the specified /// Postgres `Type`. fn accepts(ty: &Type) -> bool; } impl FromSql for Option { - fn from_sql_nullable(ty: &Type, raw: Option<&mut R>, ctx: &SessionInfo) - -> Result> { - match raw { - Some(raw) => ::from_sql(ty, raw, ctx).map(Some), - None => Ok(None), - } - } - fn from_sql(ty: &Type, raw: &mut R, ctx: &SessionInfo) -> Result> { ::from_sql(ty, raw, ctx).map(Some) } + fn from_sql_null(_: &Type, _: &SessionInfo) -> Result> { + Ok(None) + } + fn accepts(ty: &Type) -> bool { ::accepts(ty) }