pub trait FromDatum {
const GET_TYPOID: bool = false;
// Required method
unsafe fn from_polymorphic_datum(
datum: Datum,
is_null: bool,
typoid: Oid
) -> Option<Self>
where Self: Sized;
// Provided methods
unsafe fn from_datum(datum: Datum, is_null: bool) -> Option<Self>
where Self: Sized { ... }
unsafe fn from_datum_in_memory_context(
memory_context: PgMemoryContexts,
datum: Datum,
is_null: bool,
typoid: Oid
) -> Option<Self>
where Self: Sized { ... }
unsafe fn try_from_datum(
datum: Datum,
is_null: bool,
type_oid: Oid
) -> Result<Option<Self>, TryFromDatumError>
where Self: Sized + IntoDatum { ... }
unsafe fn try_from_datum_in_memory_context(
memory_context: PgMemoryContexts,
datum: Datum,
is_null: bool,
type_oid: Oid
) -> Result<Option<Self>, TryFromDatumError>
where Self: Sized + IntoDatum { ... }
}
Expand description
Convert a (pg_sys::Datum, is_null:bool)
pair into a Rust type
Default implementations are provided for the common Rust types.
If implementing this, also implement IntoDatum
for the reverse
conversion.
Provided Associated Constants§
sourceconst GET_TYPOID: bool = false
const GET_TYPOID: bool = false
Should a type OID be fetched when calling from_datum
?
Required Methods§
Provided Methods§
sourceunsafe fn from_datum(datum: Datum, is_null: bool) -> Option<Self>where
Self: Sized,
unsafe fn from_datum(datum: Datum, is_null: bool) -> Option<Self>where
Self: Sized,
§Safety
This method is inherently unsafe as the datum
argument can represent an arbitrary
memory address in the case of pass-by-reference Datums. Referencing that memory address
can cause Postgres to crash if it’s invalid.
If the (datum, is_null)
pair comes from Postgres, it’s generally okay to consider this
a safe call (ie, wrap it in unsafe {}
) and move on with life.
If, however, you’re providing an arbitrary datum value, it needs to be considered unsafe and that unsafeness should be propagated through your API.
sourceunsafe fn from_datum_in_memory_context(
memory_context: PgMemoryContexts,
datum: Datum,
is_null: bool,
typoid: Oid
) -> Option<Self>where
Self: Sized,
unsafe fn from_datum_in_memory_context(
memory_context: PgMemoryContexts,
datum: Datum,
is_null: bool,
typoid: Oid
) -> Option<Self>where
Self: Sized,
Default implementation switched to the specified memory context and then simply calls
FromDatum::from_datum(...)
from within that context.
For certain Datums (such as &str
), this is likely not enough and this function
should be overridden in the type’s trait implementation.
The intent here is that the returned Rust type, which might be backed by a pass-by-reference Datum, be copied into the specified memory context, and then the Rust type constructed from that pointer instead.
§Safety
Same caveats as FromDatum::from_datum(...)
sourceunsafe fn try_from_datum(
datum: Datum,
is_null: bool,
type_oid: Oid
) -> Result<Option<Self>, TryFromDatumError>
unsafe fn try_from_datum( datum: Datum, is_null: bool, type_oid: Oid ) -> Result<Option<Self>, TryFromDatumError>
try_from_datum
is a convenience wrapper around FromDatum::from_datum
that returns a
a Result
around an Option
, as a Datum can be null. It’s intended to be used in
situations where the caller needs to know whether the type conversion succeeded or failed.
§Safety
Same caveats as FromDatum::from_datum(...)
sourceunsafe fn try_from_datum_in_memory_context(
memory_context: PgMemoryContexts,
datum: Datum,
is_null: bool,
type_oid: Oid
) -> Result<Option<Self>, TryFromDatumError>
unsafe fn try_from_datum_in_memory_context( memory_context: PgMemoryContexts, datum: Datum, is_null: bool, type_oid: Oid ) -> Result<Option<Self>, TryFromDatumError>
A version of try_from_datum
that switches to the given context to convert from Datum
Object Safety§
Implementations on Foreign Types§
source§impl FromDatum for ()
impl FromDatum for ()
for VOID – always converts to Some(())
, even if the “is_null” argument is true
source§impl FromDatum for String
impl FromDatum for String
For Postgres text, varchar, or any pg_sys::varlena
-based type
This returns a copy, allocated and managed by Rust, of the underlying varlena
Datum
Note that while these conversions are inherently unsafe, they still enforce UTF-8 correctness, so they may panic if you use PGRX with a database that has non-UTF-8 data. The details of this are subject to change.
source§impl<'a> FromDatum for &'a str
impl<'a> FromDatum for &'a str
For Postgres text and varchar
Note that while these conversions are inherently unsafe, they still enforce UTF-8 correctness, so they may panic if you use PGRX with a database that has non-UTF-8 data. The details of this are subject to change.
Implementors§
impl FromDatum for BOX
impl FromDatum for Datum
for pg_sys::Datum
impl FromDatum for ItemPointerData
impl FromDatum for Oid
impl FromDatum for Point
impl FromDatum for PgRelation
impl FromDatum for AnyNumeric
source§impl FromDatum for AnyElement
impl FromDatum for AnyElement
const GET_TYPOID: bool = true
impl FromDatum for Date
impl FromDatum for Inet
impl FromDatum for Internal
impl FromDatum for Interval
impl FromDatum for Json
for json
impl FromDatum for JsonB
for jsonb
impl FromDatum for JsonString
for json
types to be represented as a wholly-owned Rust String copy
This returns a copy, allocated and managed by Rust, of the underlying varlena
Datum
impl FromDatum for Time
impl FromDatum for TimeWithTimeZone
impl FromDatum for Timestamp
impl FromDatum for TimestampWithTimeZone
impl FromDatum for Uuid
impl<'a> FromDatum for PgHeapTuple<'a, AllocatedByRust>
impl<'a, T: FromDatum> FromDatum for Array<'a, T>
impl<'a, T: FromDatum> FromDatum for VariadicArray<'a, T>
impl<'de, T> FromDatum for Twhere
T: PostgresType + Deserialize<'de>,
impl<T> FromDatum for PgBox<T, AllocatedByPostgres>
for user types