From 934e4808590213dfe8d88b651c40c9e1c1352d57 Mon Sep 17 00:00:00 2001 From: Jeff Davis Date: Tue, 24 May 2016 23:34:25 -0700 Subject: [PATCH 1/3] Fix build. --- macros/src/lib.rs | 2 +- src/lib.rs | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/macros/src/lib.rs b/macros/src/lib.rs index f09b034..e63fcbd 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -25,7 +25,7 @@ pub fn plugin_registrar(reg: &mut Registry) { reg.register_syntax_extension(intern("pg_export"), MultiModifier(box expand_pg_export)); } -pub fn expand_pg_export(cx: &mut ExtCtxt, span: Span, _: &MetaItem, item: Annotatable) -> Annotatable { +pub fn expand_pg_export(_cx: &mut ExtCtxt, _span: Span, _: &MetaItem, item: Annotatable) -> Annotatable { //TODO: enforce func type check // if !func.is_fn_like() { diff --git a/src/lib.rs b/src/lib.rs index 12eddc7..2b9675f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +#![allow(non_camel_case_types)] + extern crate libc; extern crate core; @@ -12,7 +14,7 @@ type fmNodePtr = *mut c_void; type fmAggrefPtr = *mut c_void; /// A trait that is implemented for all Postgres-compatible data types. -trait PgType {} +pub trait PgType {} #[allow(dead_code)] extern { @@ -458,6 +460,7 @@ pub struct FunctionCallInfoData { } pub struct FunctionCallInfo<'a> { + #[allow(dead_code)] ptr: *mut FunctionCallInfoData, marker: PhantomData<&'a FunctionCallInfoData> } @@ -466,11 +469,12 @@ pub struct FunctionCallInfo<'a> { /// a pointer-sized unsigned integer that acts like /// a pointer. pub struct Datum { + #[allow(dead_code)] val: usize } impl Datum { - pub fn new_str(value: &str) -> Datum { + pub fn new_str(_value: &str) -> Datum { // We need to allocate our string onto the heap // and with the custom `palloc` allocator. `palloc` // allocates memory into contexts such that they From 8c5a5f1ea0598c4dd2c6aa211b04689b157ef1bf Mon Sep 17 00:00:00 2001 From: Jeff Davis Date: Tue, 24 May 2016 23:46:17 -0700 Subject: [PATCH 2/3] Break macros crate into two. This is to avoid mixing a plugin with normal macros. --- Cargo.toml | 5 +++-- macros/Cargo.toml | 5 +++-- macros/src/lib.rs | 54 ----------------------------------------------- plugin/Cargo.toml | 11 ++++++++++ plugin/src/lib.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 58 deletions(-) create mode 100644 plugin/Cargo.toml create mode 100644 plugin/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index cf7907d..e7172a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,11 +2,12 @@ name = "postgres_extension" version = "0.0.1" -authors = ["Daniel Fagnan "] +authors = ["Daniel Fagnan ", + "Jeff Davis "] [lib] name = "postgres_extension" -crate-type = ["dylib"] +crate-type = ["rlib"] [dependencies] libc = "*" diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 621bebc..0ce1d34 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -2,8 +2,9 @@ name = "postgres_extension_macros" version = "0.0.1" -authors = ["Daniel Fagnan "] +authors = ["Daniel Fagnan ", + "Jeff Davis "] [lib] name = "postgres_extension_macros" -crate-type = ["dylib", "rlib"] +crate-type = ["rlib"] diff --git a/macros/src/lib.rs b/macros/src/lib.rs index e63fcbd..1262aad 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -1,57 +1,3 @@ -#![feature(plugin_registrar, quote, box_syntax, rustc_private)] -#![allow(unused_imports)] - -extern crate syntax; -extern crate syntax_ext; -extern crate rustc; -extern crate rustc_plugin; - -use rustc_plugin::Registry; -use syntax::ext::base::{MultiDecorator, MultiModifier}; -use syntax::parse::token::intern; -use rustc::hir::map::blocks::MaybeFnLike; - -use syntax::ext::base::{ExtCtxt, Annotatable}; -use syntax::codemap::Span; -use syntax::ptr::P; - -use syntax::ast::{Item, MetaItem}; -use syntax::attr; -use syntax_ext::deriving::generic::{combine_substructure, EnumMatching, FieldInfo, MethodDef, Struct, Substructure, TraitDef, ty}; -use syntax::parse::token::InternedString; - -#[plugin_registrar] -pub fn plugin_registrar(reg: &mut Registry) { - reg.register_syntax_extension(intern("pg_export"), MultiModifier(box expand_pg_export)); -} - -pub fn expand_pg_export(_cx: &mut ExtCtxt, _span: Span, _: &MetaItem, item: Annotatable) -> Annotatable { - - //TODO: enforce func type check - // if !func.is_fn_like() { - // cx.span_err(span, "you can only export a function to PostgreSQL."); - // } - - match item { - Annotatable::Item(it) => { - let mut new_it = (*it).clone(); - new_it.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("no_mangle")))); - Annotatable::Item(P(new_it)) - } - Annotatable::ImplItem(it) => { - let mut new_it = (*it).clone(); - new_it.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("no_mangle")))); - Annotatable::ImplItem(P(new_it)) - } - Annotatable::TraitItem(tt) => { - let mut new_it = (*tt).clone(); - new_it.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("no_mangle")))); - Annotatable::TraitItem(P(new_it)) - } - } - -} - /// Postgres has a macro called `PG_MODULE_MAGIC` that is supposed /// to be called within extensions. This generates a bunch /// of metadata structures that Postgres reads to determine diff --git a/plugin/Cargo.toml b/plugin/Cargo.toml new file mode 100644 index 0000000..f92d00b --- /dev/null +++ b/plugin/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "postgres_extension_plugin" +version = "0.0.1" +authors = ["Daniel Fagnan ", + "Jeff Davis "] + +[dependencies] + +[lib] +crate-type = ["dylib"] + diff --git a/plugin/src/lib.rs b/plugin/src/lib.rs new file mode 100644 index 0000000..e7c0f7a --- /dev/null +++ b/plugin/src/lib.rs @@ -0,0 +1,54 @@ +#![feature(plugin_registrar, quote, box_syntax, rustc_private)] +#![allow(unused_imports)] + +extern crate syntax; +extern crate syntax_ext; +extern crate rustc; +extern crate rustc_plugin; + +use rustc_plugin::Registry; +use syntax::ext::base::{MultiDecorator, MultiModifier}; +use syntax::parse::token::intern; +use rustc::hir::map::blocks::MaybeFnLike; + +use syntax::ext::base::{ExtCtxt, Annotatable}; +use syntax::codemap::Span; +use syntax::ptr::P; + +use syntax::ast::{Item, MetaItem}; +use syntax::attr; +use syntax_ext::deriving::generic::{combine_substructure, EnumMatching, FieldInfo, MethodDef, Struct, Substructure, TraitDef, ty}; +use syntax::parse::token::InternedString; + +#[plugin_registrar] +pub fn plugin_registrar(reg: &mut Registry) { + reg.register_syntax_extension(intern("pg_export"), MultiModifier(box expand_pg_export)); +} + +pub fn expand_pg_export(_cx: &mut ExtCtxt, _span: Span, _: &MetaItem, item: Annotatable) -> Annotatable { + + //TODO: enforce func type check + // if !func.is_fn_like() { + // cx.span_err(span, "you can only export a function to PostgreSQL."); + // } + + match item { + Annotatable::Item(it) => { + let mut new_it = (*it).clone(); + new_it.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("no_mangle")))); + Annotatable::Item(P(new_it)) + } + Annotatable::ImplItem(it) => { + let mut new_it = (*it).clone(); + new_it.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("no_mangle")))); + Annotatable::ImplItem(P(new_it)) + } + Annotatable::TraitItem(tt) => { + let mut new_it = (*tt).clone(); + new_it.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("no_mangle")))); + Annotatable::TraitItem(P(new_it)) + } + } + +} + From d3e75ad79adcf4a9e7bc1def3842d9a5172a632e Mon Sep 17 00:00:00 2001 From: phgrey Date: Mon, 3 Apr 2017 16:11:00 +0300 Subject: [PATCH 3/3] rust 1.17-nightly ready --- .gitignore | 8 ++++++-- Cargo.toml | 2 +- Readme.md | 22 ++++++++++------------ examples/is_zero/.gitignore | 2 -- examples/is_zero/Cargo.toml | 5 ++++- examples/is_zero/Readme.md | 5 +++-- examples/is_zero/src/lib.rs | 11 +++++------ macros/.gitignore | 2 -- macros/Cargo.toml | 2 +- macros/src/lib.rs | 6 +++--- plugin/Cargo.toml | 4 ++-- plugin/src/lib.rs | 25 +++++++++---------------- src/lib.rs | 1 - 13 files changed, 44 insertions(+), 51 deletions(-) delete mode 100644 examples/is_zero/.gitignore delete mode 100644 macros/.gitignore diff --git a/.gitignore b/.gitignore index 2261fa0..7719d7b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ -/target +target logfile -/Cargo.lock +Cargo.lock + + +*.iml +.idea \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index e7172a8..30694b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ authors = ["Daniel Fagnan ", [lib] name = "postgres_extension" -crate-type = ["rlib"] +crate-type = ["dylib"] [dependencies] libc = "*" diff --git a/Readme.md b/Readme.md index 1c950a1..f75a5cc 100644 --- a/Readme.md +++ b/Readme.md @@ -26,6 +26,9 @@ path = "https://github.com/thehydroimpulse/postgres-extension.rs" [dependencies.postgres_extension_macros] path = "https://github.com/thehydroimpulse/postgres-extension.rs" + +[dependencies.postgres_extension_plugin] +path = "https://github.com/thehydroimpulse/postgres-extension.rs" ``` ## Hello World! @@ -36,19 +39,11 @@ The first task is to link in the appropriate crates that we need: ```rust // lib.rs -#![feature(phase)] +#![feature(libc, custom_attribute, plugin)] +#![plugin(postgres_extension_plugin)] -extern crate postgres_extension; - -#[phase(plugin)] -extern crate postgres_extension_macros; ``` -The `phase` feature allows us to specify when to link the specified crate (compile-time? run-time?). - -The reason we need two crates is because syntax extensions need to link against `rustc` and `libsyntax`, the Rust compiler and parser (among other things), respectively. These are both fairly big crates and we only have a *compile-time* requirement on them. Meaning, when we run our program (or whatever final output we have) we never ever need access to those compiler crates. - -As a result, we'll use the `phase` feature to selectively choose to only link the macro crate during compilation and *not* during runtime. **Compatibility Checks:** @@ -60,7 +55,10 @@ So, continuing from the previous code we wrote: // lib.rs // ... -pg_module!(version: 90500) +extern crate postgres_extension; +#[macro_use] +extern crate postgres_extension_macros; +pg_module!(version: 90500); ``` We're just specifying that this extension is compatible with Postgres 9.5, that's it! @@ -88,7 +86,7 @@ pub fn is_zero(a: i32) -> i32 { Simply run `psql` (with whatever options you need/want). ```sql -CREATE FUNCTION is_zero(int4) RETURNS Boolean AS '/path/to/target/libis_zero-*.dylib' LANGUAGE c; +CREATE FUNCTION is_zero(int4) RETURNS Boolean AS '/path/to/target/libis_zero.so' LANGUAGE c; ``` Replacing the path with the real location to the `dylib`, of course. diff --git a/examples/is_zero/.gitignore b/examples/is_zero/.gitignore deleted file mode 100644 index 4fffb2f..0000000 --- a/examples/is_zero/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/target -/Cargo.lock diff --git a/examples/is_zero/Cargo.toml b/examples/is_zero/Cargo.toml index e8cce02..453d4ac 100644 --- a/examples/is_zero/Cargo.toml +++ b/examples/is_zero/Cargo.toml @@ -13,4 +13,7 @@ crate-type = ["dylib"] path = "../../" [dependencies.postgres_extension_macros] -path = "../../macros" +path = "../../macros/" + +[dependencies.postgres_extension_plugin] +path = "../../plugin/" \ No newline at end of file diff --git a/examples/is_zero/Readme.md b/examples/is_zero/Readme.md index f139c30..b86dec3 100644 --- a/examples/is_zero/Readme.md +++ b/examples/is_zero/Readme.md @@ -15,10 +15,11 @@ Simply build the library with Cargo: cargo build ``` -This will generate a new `libis_zero*.dylib` library ready to use in Postgres. +This will generate a new `libis_zero.so` library ready to use in Postgres. Now you can run `psql` to link against the library. ```sql -CREATE FUNCTION is_zero(int4) RETURNS Boolean AS '/path/to/target/libis_zero-*.dylib' LANGUAGE c; +CREATE FUNCTION is_zero(int4) RETURNS Boolean AS '/path/to/target/libis_zero.so' LANGUAGE c; ``` +refer \ No newline at end of file diff --git a/examples/is_zero/src/lib.rs b/examples/is_zero/src/lib.rs index 403cf0b..6939434 100644 --- a/examples/is_zero/src/lib.rs +++ b/examples/is_zero/src/lib.rs @@ -1,16 +1,15 @@ #![feature(libc, custom_attribute, plugin)] -#![plugin(postgres_extension_macros)] +#![plugin(postgres_extension_plugin)] extern crate postgres_extension; - #[macro_use] extern crate postgres_extension_macros; +pg_module!(version: 90500); -extern crate libc; -use libc::{c_int}; +extern crate libc; +use libc::{ c_int }; -pg_module!(version: 90500); #[pg_export] pub fn is_zero(a: c_int) -> c_int { @@ -22,6 +21,6 @@ pub fn is_zero(a: c_int) -> c_int { } #[pg_export] -pub fn echo_num(a: c_int) -> c_int { +pub fn return_num(a: c_int) -> c_int { return a } diff --git a/macros/.gitignore b/macros/.gitignore deleted file mode 100644 index 4fffb2f..0000000 --- a/macros/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/target -/Cargo.lock diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 0ce1d34..1dbaf05 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -7,4 +7,4 @@ authors = ["Daniel Fagnan ", [lib] name = "postgres_extension_macros" -crate-type = ["rlib"] +crate-type = ["dylib"] diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 1262aad..f2dbb0d 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -15,7 +15,7 @@ #[macro_export] macro_rules! pg_module { (version: $vers:expr) => { - static mut Pg_magic_data: postgres_extension::Pg_magic_struct = + static mut PG_MAGIC_DATA: postgres_extension::Pg_magic_struct = postgres_extension::Pg_magic_struct { len: 0 as c_int, version: $vers, @@ -34,7 +34,7 @@ macro_rules! pg_module { use libc::{c_int}; unsafe { - Pg_magic_data = postgres_extension::Pg_magic_struct { + PG_MAGIC_DATA = postgres_extension::Pg_magic_struct { len: size_of::() as c_int, version: $vers / 100, funcmaxargs: 100, @@ -44,7 +44,7 @@ macro_rules! pg_module { float8byval: 1 }; - &Pg_magic_data + &PG_MAGIC_DATA } } } diff --git a/plugin/Cargo.toml b/plugin/Cargo.toml index f92d00b..c0b8754 100644 --- a/plugin/Cargo.toml +++ b/plugin/Cargo.toml @@ -7,5 +7,5 @@ authors = ["Daniel Fagnan ", [dependencies] [lib] -crate-type = ["dylib"] - +plugin = true +crate-type = ["dylib"] \ No newline at end of file diff --git a/plugin/src/lib.rs b/plugin/src/lib.rs index e7c0f7a..7efd879 100644 --- a/plugin/src/lib.rs +++ b/plugin/src/lib.rs @@ -1,4 +1,4 @@ -#![feature(plugin_registrar, quote, box_syntax, rustc_private)] +#![feature(plugin_registrar, box_syntax, rustc_private)] #![allow(unused_imports)] extern crate syntax; @@ -8,47 +8,40 @@ extern crate rustc_plugin; use rustc_plugin::Registry; use syntax::ext::base::{MultiDecorator, MultiModifier}; -use syntax::parse::token::intern; +use syntax::symbol::Symbol; use rustc::hir::map::blocks::MaybeFnLike; use syntax::ext::base::{ExtCtxt, Annotatable}; use syntax::codemap::Span; use syntax::ptr::P; -use syntax::ast::{Item, MetaItem}; +use syntax::ast::{Item, MetaItem, TraitItem, ImplItem}; use syntax::attr; use syntax_ext::deriving::generic::{combine_substructure, EnumMatching, FieldInfo, MethodDef, Struct, Substructure, TraitDef, ty}; -use syntax::parse::token::InternedString; #[plugin_registrar] pub fn plugin_registrar(reg: &mut Registry) { - reg.register_syntax_extension(intern("pg_export"), MultiModifier(box expand_pg_export)); + reg.register_syntax_extension(Symbol::intern("pg_export"), MultiModifier(box expand_pg_export)); } -pub fn expand_pg_export(_cx: &mut ExtCtxt, _span: Span, _: &MetaItem, item: Annotatable) -> Annotatable { - - //TODO: enforce func type check - // if !func.is_fn_like() { - // cx.span_err(span, "you can only export a function to PostgreSQL."); - // } +pub fn expand_pg_export(_cx: &mut ExtCtxt, span: Span, _: &MetaItem, item: Annotatable) -> Annotatable { match item { Annotatable::Item(it) => { let mut new_it = (*it).clone(); - new_it.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("no_mangle")))); + new_it.attrs.push(attr::mk_attr_outer(span, attr::mk_attr_id(), attr::mk_word_item(Symbol::intern("no_mangle")))); Annotatable::Item(P(new_it)) } Annotatable::ImplItem(it) => { let mut new_it = (*it).clone(); - new_it.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("no_mangle")))); + new_it.attrs.push(attr::mk_attr_outer(span, attr::mk_attr_id(), attr::mk_word_item(Symbol::intern("no_mangle")))); Annotatable::ImplItem(P(new_it)) } Annotatable::TraitItem(tt) => { let mut new_it = (*tt).clone(); - new_it.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("no_mangle")))); + new_it.attrs.push(attr::mk_attr_outer(span, attr::mk_attr_id(), attr::mk_word_item(Symbol::intern("no_mangle")))); Annotatable::TraitItem(P(new_it)) } } -} - +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 2b9675f..d69d5e2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,6 @@ pub const FUNC_MAX_ARGS: c_int = 100; type PGFunction = extern fn(FunctionCallInfo) -> Datum; type fmNodePtr = *mut c_void; -type fmAggrefPtr = *mut c_void; /// A trait that is implemented for all Postgres-compatible data types. pub trait PgType {}