From 46f314103aa2eca5ab24e2e88de76344a25b85d2 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Thu, 18 Feb 2021 14:11:35 -0800 Subject: [PATCH] feat(core): add TypeEncode::compatible --- sqlx-core/src/type.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/sqlx-core/src/type.rs b/sqlx-core/src/type.rs index 6a48c1f8..f3784401 100644 --- a/sqlx-core/src/type.rs +++ b/sqlx-core/src/type.rs @@ -33,11 +33,19 @@ pub trait Type { #[allow(clippy::module_name_repetitions)] pub trait TypeEncode: Type + Encode { - /// Returns the SQL type identifier that best hints to the database - /// at the incoming value for a bind parameter. + /// Returns the canonical SQL type identifier for this Rust type. #[allow(unused_variables)] fn type_id(&self, ty: &Db::TypeInfo) -> Db::TypeId; + /// Determines if this Rust type is compatible with the specified SQL type. + /// + /// To be compatible, the Rust type must support encoding _and_ decoding + /// from the specified SQL type. + /// + fn compatible(&self, ty: &Db::TypeInfo) -> bool { + ty.id() == self.type_id(ty) + } + /// Returns the Rust type name of this. #[doc(hidden)] #[inline]