-
-
Notifications
You must be signed in to change notification settings - Fork 443
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use crate::{IsNull, ToSql, Type}; | ||
use bytes::BytesMut; | ||
|
||
macro_rules! impl_rend { | ||
($($ty:ty => $orig:ty,)*) => {$( | ||
impl ToSql for $ty { | ||
#[inline] | ||
fn to_sql( | ||
&self, | ||
ty: &Type, | ||
out: &mut BytesMut, | ||
) -> Result<IsNull, Box<dyn std::error::Error + Sync + Send>> { | ||
self.to_native().to_sql(ty, out) | ||
} | ||
|
||
#[inline] | ||
fn accepts(ty: &Type) -> bool { | ||
<$orig as ToSql>::accepts(ty) | ||
} | ||
|
||
to_sql_checked!(); | ||
} | ||
)*}; | ||
} | ||
|
||
impl_rend! { | ||
rend_05::f32_le => f32, | ||
rend_05::f32_be => f32, | ||
rend_05::f64_le => f64, | ||
rend_05::f64_be => f64, | ||
|
||
rend_05::i16_le => i16, | ||
rend_05::i16_be => i16, | ||
rend_05::i32_le => i32, | ||
rend_05::i32_be => i32, | ||
rend_05::i64_le => i64, | ||
rend_05::i64_be => i64, | ||
|
||
rend_05::u32_le => u32, | ||
rend_05::u32_be => u32, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
use std::{collections::HashMap, error::Error, hash::Hasher, net::IpAddr}; | ||
|
||
use crate::{Format, IsNull, ToSql, Type}; | ||
use bytes::BytesMut; | ||
use postgres_protocol::types; | ||
|
||
use rkyv_08::{ | ||
collections::swiss_table::ArchivedHashMap, net::ArchivedIpAddr, | ||
niche::option_box::ArchivedOptionBox, option::ArchivedOption, string::ArchivedString, | ||
vec::ArchivedVec, | ||
}; | ||
|
||
macro_rules! fwd_accepts { | ||
($ty:ty) => { | ||
#[inline] | ||
fn accepts(ty: &Type) -> bool { | ||
<$ty as ToSql>::accepts(ty) | ||
} | ||
}; | ||
} | ||
|
||
impl ToSql for ArchivedString { | ||
fn to_sql( | ||
&self, | ||
ty: &Type, | ||
out: &mut BytesMut, | ||
) -> Result<IsNull, Box<dyn Error + Sync + Send>> { | ||
self.as_str().to_sql(ty, out) | ||
} | ||
|
||
fwd_accepts!(&str); | ||
to_sql_checked!(); | ||
} | ||
|
||
impl<T> ToSql for ArchivedVec<T> | ||
where | ||
T: ToSql, | ||
{ | ||
fn to_sql( | ||
&self, | ||
ty: &Type, | ||
out: &mut BytesMut, | ||
) -> Result<IsNull, Box<dyn Error + Sync + Send>> { | ||
self.as_slice().to_sql(ty, out) | ||
} | ||
|
||
fwd_accepts!(&[T]); | ||
to_sql_checked!(); | ||
} | ||
|
||
impl<T> ToSql for ArchivedOption<T> | ||
where | ||
T: ToSql, | ||
{ | ||
fn to_sql( | ||
&self, | ||
ty: &Type, | ||
out: &mut BytesMut, | ||
) -> Result<IsNull, Box<dyn Error + Sync + Send>> { | ||
match self { | ||
ArchivedOption::Some(value) => value.to_sql(ty, out), | ||
ArchivedOption::None => Ok(IsNull::Yes), | ||
} | ||
} | ||
|
||
fn encode_format(&self, ty: &Type) -> Format { | ||
match self { | ||
ArchivedOption::Some(ref val) => val.encode_format(ty), | ||
ArchivedOption::None => Format::Binary, | ||
} | ||
} | ||
|
||
fwd_accepts!(Option<T>); | ||
to_sql_checked!(); | ||
} | ||
|
||
impl<T> ToSql for ArchivedOptionBox<T> | ||
where | ||
T: ToSql, | ||
{ | ||
fn to_sql( | ||
&self, | ||
ty: &Type, | ||
out: &mut BytesMut, | ||
) -> Result<IsNull, Box<dyn Error + Sync + Send>> { | ||
match self.as_ref() { | ||
Some(value) => value.to_sql(ty, out), | ||
None => Ok(IsNull::Yes), | ||
} | ||
} | ||
|
||
fn encode_format(&self, ty: &Type) -> Format { | ||
match self.as_ref() { | ||
Some(val) => val.encode_format(ty), | ||
None => Format::Binary, | ||
} | ||
} | ||
|
||
fwd_accepts!(Option<T>); | ||
to_sql_checked!(); | ||
} | ||
|
||
impl ToSql for ArchivedIpAddr { | ||
fn to_sql( | ||
&self, | ||
ty: &Type, | ||
out: &mut BytesMut, | ||
) -> Result<IsNull, Box<dyn Error + Sync + Send>> { | ||
self.as_ipaddr().to_sql(ty, out) | ||
} | ||
|
||
fwd_accepts!(IpAddr); | ||
to_sql_checked!(); | ||
} | ||
|
||
impl<H> ToSql for ArchivedHashMap<ArchivedString, ArchivedOption<ArchivedString>, H> | ||
where | ||
H: Hasher, | ||
{ | ||
fn to_sql( | ||
&self, | ||
_ty: &Type, | ||
out: &mut BytesMut, | ||
) -> Result<IsNull, Box<dyn Error + Sync + Send>> { | ||
types::hstore_to_sql( | ||
self.iter() | ||
.map(|(k, v)| (k.as_ref(), v.as_ref().map(|v| v.as_ref()))), | ||
out, | ||
)?; | ||
|
||
Ok(IsNull::No) | ||
} | ||
|
||
fwd_accepts!(HashMap<String, Option<String>>); | ||
to_sql_checked!(); | ||
} |