From c7abba1e135e0be13cc857c4b8f9e86d8fc51b45 Mon Sep 17 00:00:00 2001 From: Orion Kindel Date: Fri, 7 Jul 2023 12:17:52 -0500 Subject: [PATCH] feat: compile-time assoc of rust <> postgres types --- postgres-types/src/lib.rs | 95 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/postgres-types/src/lib.rs b/postgres-types/src/lib.rs index b095c1cd..f3c0620a 100644 --- a/postgres-types/src/lib.rs +++ b/postgres-types/src/lib.rs @@ -276,6 +276,101 @@ pub mod private; mod special; 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. #[derive(PartialEq, Eq, Clone, Hash)] pub struct Type(Inner);