sqlx_postgres/types/
str.rs1use crate::decode::Decode;
2use crate::encode::{Encode, IsNull};
3use crate::error::BoxDynError;
4use crate::types::array_compatible;
5use crate::types::Type;
6use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueRef, Postgres};
7use std::borrow::Cow;
8use std::rc::Rc;
9use std::sync::Arc;
10
11impl Type<Postgres> for str {
12 fn type_info() -> PgTypeInfo {
13 PgTypeInfo::TEXT
14 }
15
16 fn compatible(ty: &PgTypeInfo) -> bool {
17 [
18 PgTypeInfo::TEXT,
19 PgTypeInfo::NAME,
20 PgTypeInfo::BPCHAR,
21 PgTypeInfo::VARCHAR,
22 PgTypeInfo::UNKNOWN,
23 PgTypeInfo::with_name("citext"),
24 ]
25 .contains(ty)
26 }
27}
28
29impl Type<Postgres> for String {
30 fn type_info() -> PgTypeInfo {
31 <&str as Type<Postgres>>::type_info()
32 }
33
34 fn compatible(ty: &PgTypeInfo) -> bool {
35 <&str as Type<Postgres>>::compatible(ty)
36 }
37}
38
39impl PgHasArrayType for &'_ str {
40 fn array_type_info() -> PgTypeInfo {
41 PgTypeInfo::TEXT_ARRAY
42 }
43
44 fn array_compatible(ty: &PgTypeInfo) -> bool {
45 array_compatible::<&str>(ty)
46 }
47}
48
49impl PgHasArrayType for Cow<'_, str> {
50 fn array_type_info() -> PgTypeInfo {
51 <&str as PgHasArrayType>::array_type_info()
52 }
53
54 fn array_compatible(ty: &PgTypeInfo) -> bool {
55 <&str as PgHasArrayType>::array_compatible(ty)
56 }
57}
58
59impl PgHasArrayType for Box<str> {
60 fn array_type_info() -> PgTypeInfo {
61 <&str as PgHasArrayType>::array_type_info()
62 }
63
64 fn array_compatible(ty: &PgTypeInfo) -> bool {
65 <&str as PgHasArrayType>::array_compatible(ty)
66 }
67}
68
69impl PgHasArrayType for String {
70 fn array_type_info() -> PgTypeInfo {
71 <&str as PgHasArrayType>::array_type_info()
72 }
73
74 fn array_compatible(ty: &PgTypeInfo) -> bool {
75 <&str as PgHasArrayType>::array_compatible(ty)
76 }
77}
78
79impl Encode<'_, Postgres> for &'_ str {
80 fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
81 buf.extend(self.as_bytes());
82
83 Ok(IsNull::No)
84 }
85}
86
87impl<'r> Decode<'r, Postgres> for &'r str {
88 fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
89 value.as_str()
90 }
91}
92
93impl Decode<'_, Postgres> for String {
94 fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
95 Ok(value.as_str()?.to_owned())
96 }
97}
98
99forward_encode_impl!(Arc<str>, &str, Postgres);
100forward_encode_impl!(Rc<str>, &str, Postgres);
101forward_encode_impl!(Cow<'_, str>, &str, Postgres);
102forward_encode_impl!(Box<str>, &str, Postgres);
103forward_encode_impl!(String, &str, Postgres);