feat: compile-time assoc of rust <> postgres types

This commit is contained in:
Orion Kindel 2023-07-07 12:17:52 -05:00
parent 6c6a266c14
commit c7abba1e13
Signed by untrusted user who does not match committer: orion
GPG Key ID: 6D4165AE4C928719

View File

@ -276,6 +276,101 @@ pub mod private;
mod special; mod special;
mod type_gen; mod type_gen;
/// compile-time association between rust type
/// and runtime postgres [`Type`]
pub trait Typed {
/// What [`Type`] is `Self`?
const TYPE: Type;
}
impl Typed for IpAddr {
const TYPE: Type = Type::INET;
}
impl Typed for SystemTime {
const TYPE: Type = Type::TIMESTAMP;
}
impl Typed for &[&str] {
const TYPE: Type = Type::TEXT_ARRAY;
}
impl Typed for &[String] {
const TYPE: Type = Type::TEXT_ARRAY;
}
impl Typed for &[bool] {
const TYPE: Type = Type::BOOL_ARRAY;
}
impl Typed for bool {
const TYPE: Type = Type::BOOL;
}
impl Typed for &str {
const TYPE: Type = Type::TEXT;
}
impl Typed for String {
const TYPE: Type = Type::TEXT;
}
impl Typed for i8 {
const TYPE: Type = Type::CHAR;
}
impl Typed for i16 {
const TYPE: Type = Type::INT2;
}
impl Typed for i32 {
const TYPE: Type = Type::INT4;
}
impl Typed for i64 {
const TYPE: Type = Type::INT8;
}
impl Typed for u32 {
const TYPE: Type = Type::OID;
}
impl Typed for f32 {
const TYPE: Type = Type::FLOAT4;
}
impl Typed for f64 {
const TYPE: Type = Type::FLOAT8;
}
impl Typed for &[i8] {
const TYPE: Type = Type::CHAR_ARRAY;
}
impl Typed for &[i16] {
const TYPE: Type = Type::INT2_ARRAY;
}
impl Typed for &[i32] {
const TYPE: Type = Type::INT4_ARRAY;
}
impl Typed for &[i64] {
const TYPE: Type = Type::INT8_ARRAY;
}
impl Typed for &[u32] {
const TYPE: Type = Type::OID_ARRAY;
}
impl Typed for &[f32] {
const TYPE: Type = Type::FLOAT4_ARRAY;
}
impl Typed for &[f64] {
const TYPE: Type = Type::FLOAT8_ARRAY;
}
/// A Postgres type. /// A Postgres type.
#[derive(PartialEq, Eq, Clone, Hash)] #[derive(PartialEq, Eq, Clone, Hash)]
pub struct Type(Inner); pub struct Type(Inner);