Skip to content

Commit b093672

Browse files
Make Scalar provide pg_sys::Oid constant (pgcentralfoundation#2209)
This makes it simpler to do a variety of things involving arrays of that type that are guaranteed to work at compile-time. Also, I fuss around where it is: I think it should be visible at `pgrx::array::Scalar`.
1 parent fd0a466 commit b093672

2 files changed

Lines changed: 36 additions & 21 deletions

File tree

pgrx/src/array.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,38 @@ impl Toasty for RawArray {
380380
unsafe { pg_sys::pfree(self.ptr.as_ptr().cast()) }
381381
}
382382
}
383+
384+
/// Marker for "simple scalars" in arrays
385+
///
386+
/// A Scalar must have:
387+
/// - A fixed size
388+
/// - No padding bits
389+
/// - All bitpatterns are valid
390+
/// - Postgres runtime handling which respects these properties
391+
/// - ...which also means it must have a statically-known OID
392+
///
393+
/// This allows for it to be copied from a Rust slice to a Postgres array,
394+
/// and obtain a Rust slice from a Postgres array if it contains no nulls.
395+
pub unsafe trait Scalar: Sized + Copy {
396+
const OID: pg_sys::Oid;
397+
}
398+
399+
unsafe impl Scalar for f32 {
400+
const OID: pg_sys::Oid = pg_sys::FLOAT4OID;
401+
}
402+
#[cfg(target_pointer_width = "64")]
403+
unsafe impl Scalar for f64 {
404+
const OID: pg_sys::Oid = pg_sys::FLOAT8OID;
405+
}
406+
unsafe impl Scalar for i8 {
407+
const OID: pg_sys::Oid = pg_sys::CHAROID;
408+
}
409+
unsafe impl Scalar for i16 {
410+
const OID: pg_sys::Oid = pg_sys::INT2OID;
411+
}
412+
unsafe impl Scalar for i32 {
413+
const OID: pg_sys::Oid = pg_sys::INT4OID;
414+
}
415+
unsafe impl Scalar for i64 {
416+
const OID: pg_sys::Oid = pg_sys::INT8OID;
417+
}

pgrx/src/datum/array.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
1010
#![allow(clippy::question_mark)]
1111
use super::{UnboxDatum, unbox};
12-
use crate::array::RawArray;
12+
use crate::array::{RawArray, Scalar};
1313
use crate::nullable::{
1414
BitSliceNulls, IntoNullableIterator, MaybeStrictNulls, NullLayout, Nullable, NullableContainer,
1515
};
@@ -412,26 +412,6 @@ pub enum ArraySliceError {
412412
ContainsNulls,
413413
}
414414

415-
/// Marker for "simple scalars" in arrays
416-
///
417-
/// A Scalar must have:
418-
/// - A fixed size
419-
/// - No padding bits
420-
/// - All bitpatterns are valid
421-
/// - Postgres runtime handling which respects these properties
422-
///
423-
/// This allows for it to be copied from a Rust slice to a Postgres array,
424-
/// and obtain a Rust slice from a Postgres array if it contains no nulls.
425-
pub unsafe trait Scalar: Sized + Copy {}
426-
427-
unsafe impl Scalar for f32 {}
428-
#[cfg(target_pointer_width = "64")]
429-
unsafe impl Scalar for f64 {}
430-
unsafe impl Scalar for i8 {}
431-
unsafe impl Scalar for i16 {}
432-
unsafe impl Scalar for i32 {}
433-
unsafe impl Scalar for i64 {}
434-
435415
impl<T> Array<'_, T>
436416
where
437417
T: Scalar,

0 commit comments

Comments
 (0)