forked from kaspanet/rusty-kaspa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
General purpose python macros (kaspanet#58)
* kaspa-python-macros * cleanup + rename 'inner' to 'client'
- Loading branch information
Showing
8 changed files
with
194 additions
and
73 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
[package] | ||
name = "kaspa-python-macros" | ||
rust-version.workspace = true | ||
authors.workspace = true | ||
edition.workspace = true | ||
license.workspace = true | ||
version.workspace = true | ||
repository.workspace = true | ||
keywords = ["kaspa","python"] | ||
categories = [] | ||
exclude = ["/.*", "/test"] | ||
description = """ | ||
Macros for the Kaspa Python bindings | ||
""" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
convert_case.workspace = true | ||
proc-macro-error = { version = "1.0.0", default-features = false } | ||
proc-macro2 = { version = "1.0.43" } | ||
quote = "1.0.21" | ||
regex.workspace = true | ||
syn = {version="1.0.99",features=["full","fold","extra-traits","parsing","proc-macro"]} # do not update! |
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,10 @@ | ||
use proc_macro::TokenStream; | ||
use proc_macro_error::proc_macro_error; | ||
|
||
mod py_async; | ||
|
||
#[proc_macro] | ||
#[proc_macro_error] | ||
pub fn py_async(input: TokenStream) -> TokenStream { | ||
py_async::py_async(input) | ||
} |
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,58 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::{quote, ToTokens}; | ||
use std::convert::Into; | ||
use syn::{ | ||
parse::{Parse, ParseStream}, | ||
parse_macro_input, | ||
punctuated::Punctuated, | ||
Error, Expr, ExprAsync, Result, Token, | ||
}; | ||
|
||
#[derive(Debug)] | ||
struct PyAsync { | ||
py: Expr, | ||
block: ExprAsync, | ||
} | ||
|
||
impl Parse for PyAsync { | ||
fn parse(input: ParseStream) -> Result<Self> { | ||
let parsed = Punctuated::<Expr, Token![,]>::parse_terminated(input).unwrap(); | ||
if parsed.len() != 2 { | ||
return Err(Error::new_spanned(parsed, "usage: py_async!{py, async move { Ok(()) }}".to_string())); | ||
} | ||
|
||
let mut iter = parsed.iter(); | ||
// python object (py: Python) | ||
let py = iter.next().unwrap().clone(); | ||
|
||
// async block to encapsulate | ||
let block = match iter.next().unwrap().clone() { | ||
Expr::Async(block) => block, | ||
statement => { | ||
return Err(Error::new_spanned(statement, "the argument must be an async block".to_string())); | ||
} | ||
}; | ||
|
||
Ok(PyAsync { py, block }) | ||
} | ||
} | ||
|
||
impl ToTokens for PyAsync { | ||
fn to_tokens(&self, tokens: &mut TokenStream) { | ||
let PyAsync { py, block } = self; | ||
|
||
quote! { | ||
let __fut__ = #block; | ||
let __py_fut__ = pyo3_asyncio_0_21::tokio::future_into_py(#py, __fut__)?; | ||
pyo3::prelude::Python::with_gil(|py| Ok(__py_fut__.into_py(#py))) | ||
} | ||
.to_tokens(tokens); | ||
} | ||
} | ||
|
||
pub fn py_async(input: proc_macro::TokenStream) -> proc_macro::TokenStream { | ||
let py_async = parse_macro_input!(input as PyAsync); | ||
let token_stream = py_async.to_token_stream(); | ||
// println!("MACRO: {}", token_stream.to_string()); | ||
token_stream.into() | ||
} |
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