Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compact_str/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ sqlx = ["dep:sqlx", "std"]
sqlx-mysql = ["sqlx", "sqlx/mysql"]
sqlx-postgres = ["sqlx", "sqlx/postgres"]
sqlx-sqlite = ["sqlx", "sqlx/sqlite"]
postgres-types = ["dep:postgres-types"]
utoipa = ["dep:utoipa", "std"]
valuable = ["dep:valuable"]
zeroize = ["dep:zeroize"]
Expand All @@ -60,6 +61,7 @@ serde = { version = "1", optional = true, default-features = false, features = [
] }
smallvec = { version = "1", optional = true, features = ["union"] }
sqlx = { version = "0.9", optional = true, default-features = false }
postgres-types = { version = "0.2.14", optional = true, default-features = false }
utoipa = { version = "5", optional = true }
valuable = { version = "0.1.1", optional = true, default-features = false }
zeroize = { version = "1", optional = true, default-features = false }
Expand Down
1 change: 1 addition & 0 deletions compact_str/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ This crate exposes one macro `format_compact!` that can be used to create `Compa
* `markup`, which implements [`Render`](https://docs.rs/markup/0.16/markup/trait.Render.html) trait, so `CompactString`s can be used in templates as HTML escaped strings
* `diesel`, which allows using CompactStrings in [`diesel`](https://diesel.rs/) text columns
* `sqlx-mysql` / `sqlx-postgres` / `sqlx-sqlite`, which allows using CompactStrings in [`sqlx`](https://github.com/launchbadge/sqlx) text columns
* `postgres-types`, which allows using CompactStrings in [`postgres-types`](https://github.com/rust-postgres/rust-postgres) text columns
* `arbitrary`, which implements the [`arbitrary::Arbitrary`](https://docs.rs/arbitrary/1/arbitrary/trait.Arbitrary.html) trait for fuzzing
* `proptest`, which implements the [`proptest::arbitrary::Arbitrary`](https://docs.rs/proptest/1/proptest/arbitrary/trait.Arbitrary.html) trait for fuzzing
* `quickcheck`, which implements the [`quickcheck::Arbitrary`](https://docs.rs/quickcheck/1/quickcheck/trait.Arbitrary.html) trait for fuzzing
Expand Down
2 changes: 2 additions & 0 deletions compact_str/src/features/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ mod serde;
mod smallvec;
#[cfg(feature = "sqlx")]
mod sqlx;
#[cfg(feature = "postgres-types")]
mod postgres_types;
#[cfg(feature = "utoipa")]
mod utoipa;
#[cfg(feature = "valuable")]
Expand Down
47 changes: 47 additions & 0 deletions compact_str/src/features/postgres_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use postgres_types::{private::BytesMut, to_sql_checked, Format, FromSql, IsNull, ToSql, Type};
use std::{boxed::Box, error::Error};

use crate::CompactString;

#[cfg(feature = "postgres-types")]
impl<'a> FromSql<'a> for CompactString {
fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
<&str as FromSql>::from_sql(ty, raw).map(CompactString::from)
}

fn accepts(ty: &Type) -> bool {
<&str as FromSql>::accepts(ty)
}

fn from_sql_null(ty: &Type) -> Result<Self, Box<dyn Error + Sync + Send>> {
<&str as FromSql>::from_sql_null(ty).map(CompactString::from)
}

fn from_sql_nullable(
ty: &Type,
raw: Option<&'a [u8]>,
) -> Result<Self, Box<dyn Error + Sync + Send>> {
<&str as FromSql>::from_sql_nullable(ty, raw).map(CompactString::from)
}
}

#[cfg(feature = "postgres-types")]
impl ToSql for CompactString {
fn to_sql(
&self,
ty: &Type,
out: &mut BytesMut,
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
<&str as ToSql>::to_sql(&&**self, ty, out)
}

fn accepts(ty: &Type) -> bool {
<&str as ToSql>::accepts(ty)
}

to_sql_checked!();

fn encode_format(&self, ty: &Type) -> Format {
<&str as ToSql>::encode_format(&&**self, ty)
}
}
Loading