Implement Decode, Encode and Type for Box, Arc, Cow and Rc (#3674)

* feat: implement Decode,Encode,Type for Box,Arc,Cow

* feat implement Encode,Type for Rc

* feat: implement Decode for Rc

* chore: make tests more concise

* chore: use macro's

* chore: remove conflicting impls

* chore: more macro's

* Relax Sized bound for Decode, Encode

* update unit tests

* fixes after review

* add comment in `Decode` impl

* add comment about `ToOwned` trait bound

* add comment explaining why decoding to `Cow::Owned` was chosen

* Remove unnecessary Decode impls
This commit is contained in:
Joey de Waal
2025-07-01 02:03:48 +02:00
committed by GitHub
parent 64e154abfa
commit 8602d94c4d
12 changed files with 269 additions and 136 deletions

View File

@@ -34,16 +34,6 @@ 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<'_>>) -> Result<IsNull, BoxDynError> {
args.push(SqliteArgumentValue::Blob(Cow::Owned(self.into_vec())));
@@ -63,12 +53,6 @@ impl Encode<'_, Sqlite> for Box<[u8]> {
}
}
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()
@@ -101,3 +85,20 @@ impl<'r> Decode<'r, Sqlite> for Vec<u8> {
Ok(value.blob().to_owned())
}
}
impl<'q> Encode<'q, Sqlite> for Cow<'q, [u8]> {
fn encode(self, args: &mut Vec<SqliteArgumentValue<'q>>) -> Result<IsNull, BoxDynError> {
args.push(SqliteArgumentValue::Blob(self));
Ok(IsNull::No)
}
fn encode_by_ref(
&self,
args: &mut Vec<SqliteArgumentValue<'q>>,
) -> Result<IsNull, BoxDynError> {
args.push(SqliteArgumentValue::Blob(self.clone()));
Ok(IsNull::No)
}
}

View File

@@ -30,12 +30,6 @@ 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<'_>>) -> Result<IsNull, BoxDynError> {
args.push(SqliteArgumentValue::Text(Cow::Owned(self.into_string())));
@@ -55,12 +49,6 @@ impl Encode<'_, Sqlite> for Box<str> {
}
}
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()
@@ -90,16 +78,6 @@ impl<'r> Decode<'r, Sqlite> for String {
}
}
impl Type<Sqlite> for Cow<'_, str> {
fn type_info() -> SqliteTypeInfo {
<&str as Type<Sqlite>>::type_info()
}
fn compatible(ty: &SqliteTypeInfo) -> bool {
<&str as Type<Sqlite>>::compatible(ty)
}
}
impl<'q> Encode<'q, Sqlite> for Cow<'q, str> {
fn encode(self, args: &mut Vec<SqliteArgumentValue<'q>>) -> Result<IsNull, BoxDynError> {
args.push(SqliteArgumentValue::Text(self));
@@ -116,9 +94,3 @@ impl<'q> Encode<'q, Sqlite> for Cow<'q, str> {
Ok(IsNull::No)
}
}
impl<'r> Decode<'r, Sqlite> for Cow<'r, str> {
fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
value.text().map(Cow::Borrowed)
}
}