Trait postgres::types::FromSql

pub trait FromSql<'a>: Sized {
    // Required methods
    fn from_sql(
        ty: &Type,
        raw: &'a [u8]
    ) -> Result<Self, Box<dyn Error + Send + Sync, Global>>;
    fn accepts(ty: &Type) -> bool;

    // Provided methods
    fn from_sql_null(
        ty: &Type
    ) -> Result<Self, Box<dyn Error + Send + Sync, Global>> { ... }
    fn from_sql_nullable(
        ty: &Type,
        raw: Option<&'a [u8]>
    ) -> Result<Self, Box<dyn Error + Send + Sync, Global>> { ... }
}
Expand description

A trait for types that can be created from a Postgres value.

Types

The following implementations are provided by this crate, along with the corresponding Postgres types:

Rust typePostgres type(s)
boolBOOL
i8“char”
i16SMALLINT, SMALLSERIAL
i32INT, SERIAL
u32OID
i64BIGINT, BIGSERIAL
f32REAL
f64DOUBLE PRECISION
&str/StringVARCHAR, CHAR(n), TEXT, CITEXT, NAME, UNKNOWN
LTREE, LQUERY, LTXTQUERY
&[u8]/Vec<u8>BYTEA
HashMap<String, Option<String>>HSTORE
SystemTimeTIMESTAMP, TIMESTAMP WITH TIME ZONE
IpAddrINET

In addition, some implementations are provided for types in third party crates. These are disabled by default; to opt into one of these implementations, activate the Cargo feature corresponding to the crate’s name prefixed by with-. For example, the with-serde_json-1 feature enables the implementation for the serde_json::Value type.

Rust typePostgres type(s)
chrono::NaiveDateTimeTIMESTAMP
chrono::DateTime<Utc>TIMESTAMP WITH TIME ZONE
chrono::DateTime<Local>TIMESTAMP WITH TIME ZONE
chrono::DateTime<FixedOffset>TIMESTAMP WITH TIME ZONE
chrono::NaiveDateDATE
chrono::NaiveTimeTIME
time::PrimitiveDateTimeTIMESTAMP
time::OffsetDateTimeTIMESTAMP WITH TIME ZONE
time::DateDATE
time::TimeTIME
eui48::MacAddressMACADDR
geo_types::Point<f64>POINT
geo_types::Rect<f64>BOX
geo_types::LineString<f64>PATH
serde_json::ValueJSON, JSONB
uuid::UuidUUID
bit_vec::BitVecBIT, VARBIT
eui48::MacAddressMACADDR
cidr::InetCidrCIDR
cidr::InetAddrINET
smol_str::SmolStrVARCHAR, CHAR(n), TEXT, CITEXT,
NAME, UNKNOWN, LTREE, LQUERY,
LTXTQUERY

Nullability

In addition to the types listed above, FromSql is implemented for Option<T> where T implements FromSql. An Option<T> represents a nullable Postgres value.

Arrays

FromSql is implemented for Vec<T>, Box<[T]> and [T; N] where T implements FromSql, and corresponds to one-dimensional Postgres arrays.

Note: the impl for arrays only exist when the Cargo feature array-impls is enabled.

Required Methods§

fn from_sql( ty: &Type, raw: &'a [u8] ) -> Result<Self, Box<dyn Error + Send + Sync, Global>>

Creates a new value of this type from a buffer of data of the specified Postgres Type in its binary format.

The caller of this method is responsible for ensuring that this type is compatible with the Postgres Type.

fn accepts(ty: &Type) -> bool

Determines if a value of this type can be created from the specified Postgres Type.

Provided Methods§

fn from_sql_null( ty: &Type ) -> Result<Self, Box<dyn Error + Send + Sync, Global>>

Creates a new value of this type from a NULL SQL value.

The caller of this method is responsible for ensuring that this type is compatible with the Postgres Type.

The default implementation returns Err(Box::new(WasNull)).

fn from_sql_nullable( ty: &Type, raw: Option<&'a [u8]> ) -> Result<Self, Box<dyn Error + Send + Sync, Global>>

A convenience function that delegates to from_sql and from_sql_null depending on the value of raw.

Implementations on Foreign Types§

§

impl<'a> FromSql<'a> for SmolStr

§

fn from_sql( ty: &Type, raw: &'a [u8] ) -> Result<SmolStr, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for MacAddress

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<MacAddress, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for i8

§

fn from_sql( _: &Type, raw: &'a [u8] ) -> Result<i8, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for NaiveDate

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<NaiveDate, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for Uuid

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<Uuid, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for Date

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<Date, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for OffsetDateTime

§

fn from_sql( type_: &Type, raw: &[u8] ) -> Result<OffsetDateTime, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for OffsetDateTime

§

fn from_sql( type_: &Type, raw: &[u8] ) -> Result<OffsetDateTime, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a, S> FromSql<'a> for HashMap<String, Option<String>, S>where S: Default + BuildHasher,

§

fn from_sql( _: &Type, raw: &'a [u8] ) -> Result<HashMap<String, Option<String>, S>, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for SystemTime

§

fn from_sql( _: &Type, raw: &'a [u8] ) -> Result<SystemTime, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a, T> FromSql<'a> for Box<[T], Global>where T: FromSql<'a>,

§

fn from_sql( ty: &Type, raw: &'a [u8] ) -> Result<Box<[T], Global>, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for Time

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<Time, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for &'a str

§

fn from_sql( ty: &Type, raw: &'a [u8] ) -> Result<&'a str, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a, T> FromSql<'a> for Option<T>where T: FromSql<'a>,

§

fn from_sql( ty: &Type, raw: &'a [u8] ) -> Result<Option<T>, Box<dyn Error + Send + Sync, Global>>

§

fn from_sql_null( _: &Type ) -> Result<Option<T>, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for NaiveDateTime

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<NaiveDateTime, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for DateTime<FixedOffset>

§

fn from_sql( type_: &Type, raw: &[u8] ) -> Result<DateTime<FixedOffset>, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for Vec<u8, Global>

§

fn from_sql( _: &Type, raw: &'a [u8] ) -> Result<Vec<u8, Global>, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for LineString<f64>

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<LineString<f64>, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for BitVec<u32>

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<BitVec<u32>, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for NaiveTime

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<NaiveTime, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a, T> FromSql<'a> for Vec<T, Global>where T: FromSql<'a>,

§

fn from_sql( ty: &Type, raw: &'a [u8] ) -> Result<Vec<T, Global>, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for Value

§

fn from_sql( ty: &Type, raw: &[u8] ) -> Result<Value, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for DateTime<Utc>

§

fn from_sql( type_: &Type, raw: &[u8] ) -> Result<DateTime<Utc>, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for Date

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<Date, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for Point<f64>

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<Point<f64>, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for f32

§

fn from_sql( _: &Type, raw: &'a [u8] ) -> Result<f32, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for LineString<f64>

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<LineString<f64>, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for Point<f64>

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<Point<f64>, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for String

§

fn from_sql( ty: &Type, raw: &'a [u8] ) -> Result<String, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for DateTime<Local>

§

fn from_sql( type_: &Type, raw: &[u8] ) -> Result<DateTime<Local>, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for Time

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<Time, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for u32

§

fn from_sql( _: &Type, raw: &'a [u8] ) -> Result<u32, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for f64

§

fn from_sql( _: &Type, raw: &'a [u8] ) -> Result<f64, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for Box<str, Global>

§

fn from_sql( ty: &Type, raw: &'a [u8] ) -> Result<Box<str, Global>, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for Rect<f64>

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<Rect<f64>, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for bool

§

fn from_sql( _: &Type, raw: &'a [u8] ) -> Result<bool, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for Rect<f64>

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<Rect<f64>, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for MacAddress

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<MacAddress, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for &'a [u8]

§

fn from_sql( _: &Type, raw: &'a [u8] ) -> Result<&'a [u8], Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for IpAddr

§

fn from_sql( _: &Type, raw: &'a [u8] ) -> Result<IpAddr, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a, T, const N: usize> FromSql<'a> for [T; N]where T: FromSql<'a>,

§

fn from_sql( ty: &Type, raw: &'a [u8] ) -> Result<[T; N], Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for PrimitiveDateTime

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<PrimitiveDateTime, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for i32

§

fn from_sql( _: &Type, raw: &'a [u8] ) -> Result<i32, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for i64

§

fn from_sql( _: &Type, raw: &'a [u8] ) -> Result<i64, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for Uuid

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<Uuid, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for PrimitiveDateTime

§

fn from_sql( _: &Type, raw: &[u8] ) -> Result<PrimitiveDateTime, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

§

impl<'a> FromSql<'a> for i16

§

fn from_sql( _: &Type, raw: &'a [u8] ) -> Result<i16, Box<dyn Error + Send + Sync, Global>>

§

fn accepts(ty: &Type) -> bool

Implementors§

§

impl<'a> FromSql<'a> for PgLsn

§

impl<'a, T> FromSql<'a> for postgres::types::Date<T>where T: FromSql<'a>,

§

impl<'a, T> FromSql<'a> for Timestamp<T>where T: FromSql<'a>,

§

impl<'a, T> FromSql<'a> for Json<T>where T: Deserialize<'a>,