pub trait Compatible<Rhs: DataType = Self>: DataType { }Expand description
SQL types that can be compared with Rhs: Self on the left of =, <,
IN, LIKE and similar operators, Rhs on the right.
Comparison is loose: it allows any pair the database can compare without an explicit cast. The rules, per dialect:
- Every dialect: a type is compatible with itself;
Placeholderand the dialect’sAny(untyped SQL) are compatible with every marker of that dialect, both ways; tuples are compatible element by element. - SQLite:
Integer,RealandNumericwith each other;TextwithBlob. - PostgreSQL: all of
Int2,Int4,Int8,Float4,Float8andNumericwith each other;Text,Varchar,CharandEnumwith each other;TextwithDate,Time,TimestampandTimestamptz;TimestampwithTimestamptz;TimewithTimetz;JsonwithJsonb;InetwithCidr;MacAddrwithMacAddr8. - MySQL: signed integers with each other, and unsigned integers with
each other, but not signed with unsigned (cast explicitly);
Year,Float,DoubleandDecimalwith every integer, andFloat,DoubleandDecimalwith each other; text types with each other and withEnumandSet(butEnumnot withSet); binary types andBitwith each other;DateTimewithTimestamp.
Everything else is rejected, for example a number with text, or a
boolean with an integer. An Array<T> is compatible only
with the same Array<T>.
For storing a value in a column, the stricter Assignable applies.
§Examples
use drizzle_types::{Compatible, DataType};
use drizzle_types::postgres::types::{Float8, Int4, Jsonb, Json, Text, Varchar};
fn compatible<L: Compatible<R>, R: DataType>() {}
compatible::<Int4, Float8>(); // int4 = float8
compatible::<Varchar, Text>(); // varchar = text
compatible::<Jsonb, Json>(); // jsonb = json§Type safety
ⓘ
use drizzle_types::{Compatible, DataType};
use drizzle_types::postgres::types::{Int4, Text};
fn compatible<L: Compatible<R>, R: DataType>() {}
compatible::<Int4, Text>(); // an integer cannot be compared with textDyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".