Skip to content

Commit

Permalink
Add impl for Type, Decode, and Encode for Box<str> and `Box<[…
Browse files Browse the repository at this point in the history
…u8]>` (#2712)
  • Loading branch information
grant0417 committed Sep 12, 2023
1 parent a6a2af1 commit c0d4019
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 0 deletions.
22 changes: 22 additions & 0 deletions sqlx-mysql/src/types/bytes.rs
Expand Up @@ -40,6 +40,28 @@ impl<'r> Decode<'r, MySql> for &'r [u8] {
}
}

impl Type<MySql> for Box<[u8]> {
fn type_info() -> MySqlTypeInfo {
<&[u8] as Type<MySql>>::type_info()
}

fn compatible(ty: &MySqlTypeInfo) -> bool {
<&[u8] as Type<MySql>>::compatible(ty)
}
}

impl Encode<'_, MySql> for Box<[u8]> {
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
<&[u8] as Encode<MySql>>::encode(self.as_ref(), buf)
}
}

impl<'r> Decode<'r, MySql> for Box<[u8]> {
fn decode(value: MySqlValueRef<'r>) -> Result<Self, BoxDynError> {
<&[u8] as Decode<MySql>>::decode(value).map(Box::from)
}
}

impl Type<MySql> for Vec<u8> {
fn type_info() -> MySqlTypeInfo {
<[u8] as Type<MySql>>::type_info()
Expand Down
22 changes: 22 additions & 0 deletions sqlx-mysql/src/types/str.rs
Expand Up @@ -62,6 +62,28 @@ impl<'r> Decode<'r, MySql> for &'r str {
}
}

impl Type<MySql> for Box<str> {
fn type_info() -> MySqlTypeInfo {
<&str as Type<MySql>>::type_info()
}

fn compatible(ty: &MySqlTypeInfo) -> bool {
<&str as Type<MySql>>::compatible(ty)
}
}

impl Encode<'_, MySql> for Box<str> {
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
<&str as Encode<MySql>>::encode(&**self, buf)
}
}

impl<'r> Decode<'r, MySql> for Box<str> {
fn decode(value: MySqlValueRef<'r>) -> Result<Self, BoxDynError> {
<&str as Decode<MySql>>::decode(value).map(Box::from)
}
}

impl Type<MySql> for String {
fn type_info() -> MySqlTypeInfo {
<str as Type<MySql>>::type_info()
Expand Down
21 changes: 21 additions & 0 deletions sqlx-postgres/src/types/bytes.rs
Expand Up @@ -22,6 +22,12 @@ impl<const N: usize> PgHasArrayType for &'_ [u8; N] {
}
}

impl PgHasArrayType for Box<[u8]> {
fn array_type_info() -> PgTypeInfo {
<[&[u8]] as Type<Postgres>>::type_info()
}
}

impl PgHasArrayType for Vec<u8> {
fn array_type_info() -> PgTypeInfo {
<[&[u8]] as Type<Postgres>>::type_info()
Expand All @@ -42,6 +48,12 @@ impl Encode<'_, Postgres> for &'_ [u8] {
}
}

impl Encode<'_, Postgres> for Box<[u8]> {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
<&[u8] as Encode<Postgres>>::encode(self.as_ref(), buf)
}
}

impl Encode<'_, Postgres> for Vec<u8> {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
<&[u8] as Encode<Postgres>>::encode(self, buf)
Expand Down Expand Up @@ -74,6 +86,15 @@ fn text_hex_decode_input(value: PgValueRef<'_>) -> Result<&[u8], BoxDynError> {
.map_err(Into::into)
}

impl Decode<'_, Postgres> for Box<[u8]> {
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
Ok(match value.format() {
PgValueFormat::Binary => Box::from(value.as_bytes()?),
PgValueFormat::Text => Box::from(hex::decode(text_hex_decode_input(value)?)?),
})
}
}

impl Decode<'_, Postgres> for Vec<u8> {
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
Ok(match value.format() {
Expand Down
32 changes: 32 additions & 0 deletions sqlx-postgres/src/types/str.rs
Expand Up @@ -33,6 +33,16 @@ impl Type<Postgres> for Cow<'_, str> {
}
}

impl Type<Postgres> for Box<str> {
fn type_info() -> PgTypeInfo {
<&str as Type<Postgres>>::type_info()
}

fn compatible(ty: &PgTypeInfo) -> bool {
<&str as Type<Postgres>>::compatible(ty)
}
}

impl Type<Postgres> for String {
fn type_info() -> PgTypeInfo {
<&str as Type<Postgres>>::type_info()
Expand Down Expand Up @@ -63,6 +73,16 @@ impl PgHasArrayType for Cow<'_, str> {
}
}

impl PgHasArrayType for Box<str> {
fn array_type_info() -> PgTypeInfo {
<&str as PgHasArrayType>::array_type_info()
}

fn array_compatible(ty: &PgTypeInfo) -> bool {
<&str as PgHasArrayType>::array_compatible(ty)
}
}

impl PgHasArrayType for String {
fn array_type_info() -> PgTypeInfo {
<&str as PgHasArrayType>::array_type_info()
Expand Down Expand Up @@ -90,6 +110,12 @@ impl Encode<'_, Postgres> for Cow<'_, str> {
}
}

impl Encode<'_, Postgres> for Box<str> {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
<&str as Encode<Postgres>>::encode(&**self, buf)
}
}

impl Encode<'_, Postgres> for String {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
<&str as Encode<Postgres>>::encode(&**self, buf)
Expand All @@ -108,6 +134,12 @@ impl<'r> Decode<'r, Postgres> for Cow<'r, str> {
}
}

impl<'r> Decode<'r, Postgres> for Box<str> {
fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
Ok(Box::from(value.as_str()?))
}
}

impl Decode<'_, Postgres> for String {
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
Ok(value.as_str()?.to_owned())
Expand Down
30 changes: 30 additions & 0 deletions sqlx-sqlite/src/types/bytes.rs
Expand Up @@ -31,6 +31,36 @@ impl<'r> Decode<'r, Sqlite> for &'r [u8] {
}
}

impl Type<Sqlite> for Box<[u8]> {
fn type_info() -> SqliteTypeInfo {
<&[u8] as Type<Sqlite>>::type_info()
}

fn compatible(ty: &SqliteTypeInfo) -> bool {
<&[u8] as Type<Sqlite>>::compatible(ty)
}
}

impl Encode<'_, Sqlite> for Box<[u8]> {
fn encode(self, args: &mut Vec<SqliteArgumentValue<'_>>) -> IsNull {
args.push(SqliteArgumentValue::Blob(Cow::Owned(self.into_vec())));

IsNull::No
}

fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'_>>) -> IsNull {
args.push(SqliteArgumentValue::Blob(Cow::Owned(self.clone().into_vec())));

IsNull::No
}
}

impl Decode<'_, Sqlite> for Box<[u8]> {
fn decode(value: SqliteValueRef<'_>) -> Result<Self, BoxDynError> {
Ok(Box::from(value.blob()))
}
}

impl Type<Sqlite> for Vec<u8> {
fn type_info() -> SqliteTypeInfo {
<&[u8] as Type<Sqlite>>::type_info()
Expand Down
28 changes: 28 additions & 0 deletions sqlx-sqlite/src/types/str.rs
Expand Up @@ -27,6 +27,34 @@ impl<'r> Decode<'r, Sqlite> for &'r str {
}
}

impl Type<Sqlite> for Box<str> {
fn type_info() -> SqliteTypeInfo {
<&str as Type<Sqlite>>::type_info()
}
}

impl Encode<'_, Sqlite> for Box<str> {
fn encode(self, args: &mut Vec<SqliteArgumentValue<'_>>) -> IsNull {
args.push(SqliteArgumentValue::Text(Cow::Owned(self.into_string())));

IsNull::No
}

fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'_>>) -> IsNull {
args.push(SqliteArgumentValue::Text(Cow::Owned(
self.clone().into_string(),
)));

IsNull::No
}
}

impl Decode<'_, Sqlite> for Box<str> {
fn decode(value: SqliteValueRef<'_>) -> Result<Self, BoxDynError> {
value.text().map(Box::from)
}
}

impl Type<Sqlite> for String {
fn type_info() -> SqliteTypeInfo {
<&str as Type<Sqlite>>::type_info()
Expand Down

0 comments on commit c0d4019

Please sign in to comment.