-
-
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.
Use
composite_type!
lifetimes in pgrx-sql-entity-graph (#1463)
I was working on fixing the auto-staticizing lifetime issues and noticed this likely would be landable on its own. This moves out `struct CompositeTypeMacro` into its own file and starts to actually modularize the code around it a little, to start actually preserving the lifetimes in their usage during macro expansion. Because we need to also generate wrapping types, it doesn't count as much of a cleanup on its own, but that's why I'm landing it separately.
- Loading branch information
1 parent
684b4c3
commit 7df371d
Showing
3 changed files
with
56 additions
and
29 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,38 @@ | ||
/// Innards of a `composite_type!` | ||
/// | ||
/// For SQL generation and further expansion. | ||
/// Use this so you don't drop the span on the floor. | ||
#[derive(Debug, Clone)] | ||
pub struct CompositeTypeMacro { | ||
pub(crate) lifetime: Option<syn::Lifetime>, | ||
pub(crate) expr: syn::Expr, | ||
pub(crate) span: proc_macro2::Span, | ||
} | ||
|
||
impl syn::parse::Parse for CompositeTypeMacro { | ||
fn parse(input: syn::parse::ParseStream) -> Result<Self, syn::Error> { | ||
let span = input.span(); | ||
let lifetime: Option<syn::Lifetime> = input.parse().ok(); | ||
let _comma: Option<syn::Token![,]> = input.parse().ok(); | ||
let expr = input.parse()?; | ||
Ok(Self { lifetime, expr, span }) | ||
} | ||
} | ||
|
||
/// Take a `composite_type!` from a macro | ||
pub fn handle_composite_type_macro(mac: &syn::Macro) -> syn::Result<CompositeTypeMacro> { | ||
let out: CompositeTypeMacro = mac.parse_body()?; | ||
Ok(out) | ||
} | ||
|
||
impl CompositeTypeMacro { | ||
/// Expands into the implementing type, explicitly eliding the lifetime | ||
/// if none is actually given. | ||
pub fn expand_with_lifetime(&self) -> syn::Type { | ||
let CompositeTypeMacro { lifetime, span, .. } = self.clone(); | ||
let lifetime = lifetime.unwrap_or_else(|| syn::Lifetime::new("'_", span)); | ||
syn::parse_quote! { | ||
::pgrx::heap_tuple::PgHeapTuple<#lifetime, ::pgrx::pgbox::AllocatedByRust> | ||
} | ||
} | ||
} |
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