Support Rust arrays in Postgres (#1953)

This commit is contained in:
Valentin
2022-07-09 01:56:47 +02:00
committed by GitHub
parent cfef70a796
commit 9ca1fbf2ca
3 changed files with 106 additions and 9 deletions

View File

@@ -55,6 +55,19 @@ where
}
}
impl<T, const N: usize> Type<Postgres> for [T; N]
where
T: PgHasArrayType,
{
fn type_info() -> PgTypeInfo {
T::array_type_info()
}
fn compatible(ty: &PgTypeInfo) -> bool {
T::array_compatible(ty)
}
}
impl<'q, T> Encode<'q, Postgres> for Vec<T>
where
for<'a> &'a [T]: Encode<'q, Postgres>,
@@ -66,6 +79,16 @@ where
}
}
impl<'q, T, const N: usize> Encode<'q, Postgres> for [T; N]
where
for<'a> &'a [T]: Encode<'q, Postgres>,
T: Encode<'q, Postgres>,
{
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
self.as_slice().encode_by_ref(buf)
}
}
impl<'q, T> Encode<'q, Postgres> for &'_ [T]
where
T: Encode<'q, Postgres> + Type<Postgres>,
@@ -100,6 +123,19 @@ where
}
}
impl<'r, T, const N: usize> Decode<'r, Postgres> for [T; N]
where
T: for<'a> Decode<'a, Postgres> + Type<Postgres>,
{
fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
// This could be done more efficiently by refactoring the Vec decoding below so that it can
// be used for arrays and Vec.
let vec: Vec<T> = Decode::decode(value)?;
let array: [T; N] = vec.try_into().map_err(|_| "wrong number of elements")?;
Ok(array)
}
}
impl<'r, T> Decode<'r, Postgres> for Vec<T>
where
T: for<'a> Decode<'a, Postgres> + Type<Postgres>,

View File

@@ -24,6 +24,12 @@ impl PgHasArrayType for Vec<u8> {
}
}
impl<const N: usize> PgHasArrayType for [u8; N] {
fn array_type_info() -> PgTypeInfo {
<[&[u8]] as Type<Postgres>>::type_info()
}
}
impl Encode<'_, Postgres> for &'_ [u8] {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
buf.extend_from_slice(self);
@@ -38,6 +44,12 @@ impl Encode<'_, Postgres> for Vec<u8> {
}
}
impl<const N: usize> Encode<'_, Postgres> for [u8; N] {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
<&[u8] as Encode<Postgres>>::encode(self.as_slice(), buf)
}
}
impl<'r> Decode<'r, Postgres> for &'r [u8] {
fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
match value.format() {
@@ -49,18 +61,33 @@ impl<'r> Decode<'r, Postgres> for &'r [u8] {
}
}
fn text_hex_decode_input(value: PgValueRef<'_>) -> Result<&[u8], BoxDynError> {
// BYTEA is formatted as \x followed by hex characters
value
.as_bytes()?
.strip_prefix(b"\\x")
.ok_or("text does not start with \\x")
.map_err(Into::into)
}
impl Decode<'_, Postgres> for Vec<u8> {
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
Ok(match value.format() {
PgValueFormat::Binary => value.as_bytes()?.to_owned(),
PgValueFormat::Text => {
// BYTEA is formatted as \x followed by hex characters
let text = value
.as_bytes()?
.strip_prefix(b"\\x")
.ok_or("text does not start with \\x")?;
hex::decode(text)?
}
PgValueFormat::Text => hex::decode(text_hex_decode_input(value)?)?,
})
}
}
impl<const N: usize> Decode<'_, Postgres> for [u8; N] {
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
let mut bytes = [0u8; N];
match value.format() {
PgValueFormat::Binary => {
bytes = value.as_bytes()?.try_into()?;
}
PgValueFormat::Text => hex::decode_to_slice(text_hex_decode_input(value)?, &mut bytes)?,
};
Ok(bytes)
}
}