-
-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow specifying type alignment through #[pgrx(alignment = ...)]
Postgres allows for types to specify their alignment in the `CREATE TYPE` statement. This change adds the ability for the alignment of a type to be declared directly on the type, as follows: ``` #[derive(PostgresType)] #[pgrx(alignment = "double")] struct AlignedTo8Bytes { v1: u64, v2: [u64; 3] } ```
- Loading branch information
1 parent
7b68a18
commit 84e2e70
Showing
6 changed files
with
151 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//LICENSE Portions Copyright 2019-2021 ZomboDB, LLC. | ||
//LICENSE | ||
//LICENSE Portions Copyright 2021-2023 Technology Concepts & Design, Inc. | ||
//LICENSE | ||
//LICENSE Portions Copyright 2023-2023 PgCentral Foundation, Inc. <[email protected]> | ||
//LICENSE | ||
//LICENSE All rights reserved. | ||
//LICENSE | ||
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file. | ||
use pgrx::prelude::*; | ||
use serde::*; | ||
|
||
#[derive(PostgresType, Serialize, Deserialize)] | ||
pub struct AlignedTo4Bytes { | ||
v1: u64, | ||
v2: [u64; 3], | ||
} | ||
|
||
#[derive(PostgresType, Serialize, Deserialize)] | ||
#[pgrx(alignment = "double")] | ||
pub struct AlignedTo8Bytes { | ||
v1: u64, | ||
v2: [u64; 3], | ||
} | ||
|
||
#[cfg(any(test, feature = "pg_test"))] | ||
#[pg_schema] | ||
mod tests { | ||
use pgrx::prelude::*; | ||
|
||
#[cfg(not(feature = "no-schema-generation"))] | ||
#[pg_test] | ||
fn test_alignment_is_correct() { | ||
let val = Spi::get_one::<bool>(r#"SELECT typalign = 'i' FROM pg_type WHERE typname = 'alignedto4bytes'"#).unwrap().unwrap(); | ||
|
||
assert!(val == true); | ||
|
||
let val = Spi::get_one::<bool>(r#"SELECT typalign = 'd' FROM pg_type WHERE typname = 'alignedto8bytes'"#).unwrap().unwrap(); | ||
|
||
assert!(val == true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters