use postgres_types::{Type, ToSql, FromSql, IsNull, to_sql_checked}; use bytes::BytesMut; use std::error::Error; #[derive(Debug)] struct RawValue<'a> { type_: Type, raw: Option<&'a [u8]>, } impl<'a> FromSql<'a> for RawValue<'a> { fn from_sql( type_: &Type, raw: &'a [u8] ) -> Result> { Ok(RawValue { type_: type_.clone(), raw: Some(raw) }) } fn accepts(_: &Type) -> bool { true } fn from_sql_null( type_: &Type ) -> Result> { Ok(RawValue { type_: type_.clone(), raw: None, }) } } impl ToSql for RawValue<'_> { fn to_sql( &self, type_: &Type, out: &mut BytesMut ) -> Result> { if self.type_ != *type_ { return Err(format!("expected type {} but saw {}", self.type_, type_).into()); } match self.raw { Some(raw) => { out.extend_from_slice(raw); Ok(IsNull::No) } None => Ok(IsNull::Yes) } } fn accepts(_: &Type) -> bool { true } to_sql_checked!(); }