Trait pgrx::datum::FromDatum

source ·
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§

source

const GET_TYPOID: bool = false

Should a type OID be fetched when calling from_datum?

Required Methods§

source

unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, typoid: Oid ) -> Option<Self>
where Self: Sized,

Like from_datum for instantiating polymorphic types which require preserving the dynamic type metadata.

§Safety

Same caveats as FromDatum::from_datum(...).

Provided Methods§

source

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.

source

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(...)

source

unsafe fn try_from_datum( datum: Datum, is_null: bool, type_oid: Oid ) -> Result<Option<Self>, TryFromDatumError>
where Self: Sized + IntoDatum,

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(...)

source

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,

A version of try_from_datum that switches to the given context to convert from Datum

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl FromDatum for bool

for bool

source§

unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, _: Oid ) -> Option<bool>

source§

impl FromDatum for char

source§

unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, typoid: Oid ) -> Option<char>

source§

impl FromDatum for f32

for real

source§

unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, _: Oid ) -> Option<f32>

source§

impl FromDatum for f64

for double precision

source§

unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, _: Oid ) -> Option<f64>

source§

impl FromDatum for i8

for "char"

source§

unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, _: Oid ) -> Option<i8>

source§

impl FromDatum for i16

for smallint

source§

unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, _: Oid ) -> Option<i16>

source§

impl FromDatum for i32

for integer

source§

unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, _: Oid ) -> Option<i32>

source§

impl FromDatum for i64

for bigint

source§

unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, _: Oid ) -> Option<i64>

source§

impl FromDatum for u32

for oid

source§

unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, _: Oid ) -> Option<u32>

source§

impl FromDatum for ()

for VOID – always converts to Some(()), even if the “is_null” argument is true

source§

unsafe fn from_polymorphic_datum( _datum: Datum, _is_null: bool, _: Oid ) -> Option<()>

source§

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§

unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, typoid: Oid ) -> Option<String>

source§

impl FromDatum for Vec<u8>

source§

unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, typoid: Oid ) -> Option<Vec<u8>>

source§

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.

source§

unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, _: Oid ) -> Option<&'a str>

source§

unsafe fn from_datum_in_memory_context( memory_context: PgMemoryContexts, datum: Datum, is_null: bool, _typoid: Oid ) -> Option<Self>
where Self: Sized,

source§

impl<'a> FromDatum for &'a CStr

for cstring

source§

unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, _: Oid ) -> Option<&'a CStr>

source§

unsafe fn from_datum_in_memory_context( memory_context: PgMemoryContexts, datum: Datum, is_null: bool, _typoid: Oid ) -> Option<Self>
where Self: Sized,

source§

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

for bytea

source§

unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, _typoid: Oid ) -> Option<&'a [u8]>

source§

unsafe fn from_datum_in_memory_context( memory_context: PgMemoryContexts, datum: Datum, is_null: bool, _typoid: Oid ) -> Option<Self>
where Self: Sized,

source§

impl<A, B> FromDatum for (Option<A>, Option<B>)

source§

unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, typoid: Oid ) -> Option<Self>
where Self: Sized,

source§

impl<A, B, C> FromDatum for (Option<A>, Option<B>, Option<C>)

source§

unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, typoid: Oid ) -> Option<Self>
where Self: Sized,

source§

impl<T: FromDatum> FromDatum for Vec<Option<T>>

source§

unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, typoid: Oid ) -> Option<Vec<Option<T>>>

source§

unsafe fn from_datum_in_memory_context( memory_context: PgMemoryContexts, datum: Datum, is_null: bool, typoid: Oid ) -> Option<Self>
where Self: Sized,

source§

impl<T: FromDatum> FromDatum for Vec<T>

source§

unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, typoid: Oid ) -> Option<Vec<T>>

source§

unsafe fn from_datum_in_memory_context( memory_context: PgMemoryContexts, datum: Datum, is_null: bool, typoid: Oid ) -> Option<Self>
where Self: Sized,

Implementors§

source§

impl FromDatum for BOX

source§

impl FromDatum for Datum

for pg_sys::Datum

source§

impl FromDatum for ItemPointerData

source§

impl FromDatum for Oid

source§

impl FromDatum for Point

source§

impl FromDatum for PgRelation

source§

impl FromDatum for AnyNumeric

source§

impl FromDatum for AnyArray

source§

const GET_TYPOID: bool = true

source§

impl FromDatum for AnyElement

source§

const GET_TYPOID: bool = true

source§

impl FromDatum for Date

source§

impl FromDatum for Inet

source§

impl FromDatum for Internal

source§

impl FromDatum for Interval

source§

impl FromDatum for Json

for json

source§

impl FromDatum for JsonB

for jsonb

source§

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

source§

impl FromDatum for Time

source§

impl FromDatum for TimeWithTimeZone

source§

impl FromDatum for Timestamp

source§

impl FromDatum for TimestampWithTimeZone

source§

impl FromDatum for Uuid

source§

impl<'a> FromDatum for PgHeapTuple<'a, AllocatedByRust>

source§

impl<'a, T: FromDatum> FromDatum for Array<'a, T>

source§

impl<'a, T: FromDatum> FromDatum for VariadicArray<'a, T>

source§

impl<'de, T> FromDatum for T
where T: PostgresType + Deserialize<'de>,

source§

impl<T> FromDatum for PgBox<T, AllocatedByPostgres>

for user types

source§

impl<T> FromDatum for PgVarlena<T>
where T: Copy + Sized,

source§

impl<T> FromDatum for Range<T>
where T: RangeSubType,

source§

impl<const P: u32, const S: u32> FromDatum for Numeric<P, S>