Skip to content

Commit 7245866

Browse files
committed
Add rkyv 0.8 and rend 0.5 support
1 parent 6fd0444 commit 7245866

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed

postgres-types/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ with-uuid-0_8 = ["uuid-08"]
2828
with-uuid-1 = ["uuid-1"]
2929
with-time-0_2 = ["time-02"]
3030
with-time-0_3 = ["time-03"]
31+
with-rkyv-0_8 = ["rkyv-08"]
32+
with-rend-0_5 = ["rend-05"]
33+
3134

3235
[dependencies]
3336
bytes = "1.0"
@@ -55,3 +58,5 @@ uuid-1 = { version = "1.0", package = "uuid", optional = true }
5558
time-02 = { version = "0.2", package = "time", optional = true }
5659
time-03 = { version = "0.3", package = "time", default-features = false, optional = true }
5760
smol_str-01 = { version = "0.1.23", package = "smol_str", default-features = false, optional = true }
61+
rkyv-08 = { version = "0.8.0-rc.1", package = "rkyv", optional = true, default-features = false }
62+
rend-05 = { version = "0.5.0-rc.1", package = "rend", optional = true, default-features = false }

postgres-types/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ mod geo_types_06;
278278
mod geo_types_07;
279279
#[cfg(feature = "with-jiff-0_1")]
280280
mod jiff_01;
281+
#[cfg(feature = "with-rend-0_5")]
282+
mod rend_05;
283+
#[cfg(feature = "with-rkyv-0_8")]
284+
mod rkyv_08;
281285
#[cfg(feature = "with-serde_json-1")]
282286
mod serde_json_1;
283287
#[cfg(feature = "with-smol_str-01")]

postgres-types/src/rend_05.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use crate::{IsNull, ToSql, Type};
2+
use bytes::BytesMut;
3+
4+
macro_rules! impl_rend {
5+
($($ty:ty => $orig:ty,)*) => {$(
6+
impl ToSql for $ty {
7+
#[inline]
8+
fn to_sql(
9+
&self,
10+
ty: &Type,
11+
out: &mut BytesMut,
12+
) -> Result<IsNull, Box<dyn std::error::Error + Sync + Send>> {
13+
self.to_native().to_sql(ty, out)
14+
}
15+
16+
#[inline]
17+
fn accepts(ty: &Type) -> bool {
18+
<$orig as ToSql>::accepts(ty)
19+
}
20+
21+
to_sql_checked!();
22+
}
23+
)*};
24+
}
25+
26+
impl_rend! {
27+
rend_05::f32_le => f32,
28+
rend_05::f32_be => f32,
29+
rend_05::f64_le => f64,
30+
rend_05::f64_be => f64,
31+
32+
rend_05::i16_le => i16,
33+
rend_05::i16_be => i16,
34+
rend_05::i32_le => i32,
35+
rend_05::i32_be => i32,
36+
rend_05::i64_le => i64,
37+
rend_05::i64_be => i64,
38+
39+
rend_05::u32_le => u32,
40+
rend_05::u32_be => u32,
41+
}

postgres-types/src/rkyv_08.rs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
use std::{collections::HashMap, error::Error, hash::Hasher, net::IpAddr};
2+
3+
use crate::{Format, IsNull, ToSql, Type};
4+
use bytes::BytesMut;
5+
use postgres_protocol::types;
6+
7+
use rkyv_08::{
8+
collections::swiss_table::ArchivedHashMap, net::ArchivedIpAddr,
9+
niche::option_box::ArchivedOptionBox, option::ArchivedOption, string::ArchivedString,
10+
vec::ArchivedVec,
11+
};
12+
13+
macro_rules! fwd_accepts {
14+
($ty:ty) => {
15+
#[inline]
16+
fn accepts(ty: &Type) -> bool {
17+
<$ty as ToSql>::accepts(ty)
18+
}
19+
};
20+
}
21+
22+
impl ToSql for ArchivedString {
23+
fn to_sql(
24+
&self,
25+
ty: &Type,
26+
out: &mut BytesMut,
27+
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
28+
self.as_str().to_sql(ty, out)
29+
}
30+
31+
fwd_accepts!(&str);
32+
to_sql_checked!();
33+
}
34+
35+
impl<T> ToSql for ArchivedVec<T>
36+
where
37+
T: ToSql,
38+
{
39+
fn to_sql(
40+
&self,
41+
ty: &Type,
42+
out: &mut BytesMut,
43+
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
44+
self.as_slice().to_sql(ty, out)
45+
}
46+
47+
fwd_accepts!(&[T]);
48+
to_sql_checked!();
49+
}
50+
51+
impl<T> ToSql for ArchivedOption<T>
52+
where
53+
T: ToSql,
54+
{
55+
fn to_sql(
56+
&self,
57+
ty: &Type,
58+
out: &mut BytesMut,
59+
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
60+
match self {
61+
ArchivedOption::Some(value) => value.to_sql(ty, out),
62+
ArchivedOption::None => Ok(IsNull::Yes),
63+
}
64+
}
65+
66+
fn encode_format(&self, ty: &Type) -> Format {
67+
match self {
68+
ArchivedOption::Some(ref val) => val.encode_format(ty),
69+
ArchivedOption::None => Format::Binary,
70+
}
71+
}
72+
73+
fwd_accepts!(Option<T>);
74+
to_sql_checked!();
75+
}
76+
77+
impl<T> ToSql for ArchivedOptionBox<T>
78+
where
79+
T: ToSql,
80+
{
81+
fn to_sql(
82+
&self,
83+
ty: &Type,
84+
out: &mut BytesMut,
85+
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
86+
match self.as_ref() {
87+
Some(value) => value.to_sql(ty, out),
88+
None => Ok(IsNull::Yes),
89+
}
90+
}
91+
92+
fn encode_format(&self, ty: &Type) -> Format {
93+
match self.as_ref() {
94+
Some(val) => val.encode_format(ty),
95+
None => Format::Binary,
96+
}
97+
}
98+
99+
fwd_accepts!(Option<T>);
100+
to_sql_checked!();
101+
}
102+
103+
impl ToSql for ArchivedIpAddr {
104+
fn to_sql(
105+
&self,
106+
ty: &Type,
107+
out: &mut BytesMut,
108+
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
109+
self.as_ipaddr().to_sql(ty, out)
110+
}
111+
112+
fwd_accepts!(IpAddr);
113+
to_sql_checked!();
114+
}
115+
116+
impl<H> ToSql for ArchivedHashMap<ArchivedString, ArchivedOption<ArchivedString>, H>
117+
where
118+
H: Hasher,
119+
{
120+
fn to_sql(
121+
&self,
122+
_ty: &Type,
123+
out: &mut BytesMut,
124+
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
125+
types::hstore_to_sql(
126+
self.iter()
127+
.map(|(k, v)| (k.as_ref(), v.as_ref().map(|v| v.as_ref()))),
128+
out,
129+
)?;
130+
131+
Ok(IsNull::No)
132+
}
133+
134+
fwd_accepts!(HashMap<String, Option<String>>);
135+
to_sql_checked!();
136+
}

0 commit comments

Comments
 (0)