add new macro: `pubkey!` (#21245)

* add new macro: `pubkey!`

* fmt
This commit is contained in:
Kirill Fomichev 2021-11-15 22:22:51 +03:00 committed by GitHub
parent f331ae95ad
commit d8a392c20b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 0 deletions

View File

@ -93,6 +93,36 @@ fn deprecated_id_to_tokens(
});
}
struct SdkPubkey(proc_macro2::TokenStream);
impl Parse for SdkPubkey {
fn parse(input: ParseStream) -> Result<Self> {
parse_id(input, quote! { ::solana_sdk::pubkey::Pubkey }).map(Self)
}
}
impl ToTokens for SdkPubkey {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let id = &self.0;
tokens.extend(quote! {#id})
}
}
struct ProgramSdkPubkey(proc_macro2::TokenStream);
impl Parse for ProgramSdkPubkey {
fn parse(input: ParseStream) -> Result<Self> {
parse_id(input, quote! { ::solana_program::pubkey::Pubkey }).map(Self)
}
}
impl ToTokens for ProgramSdkPubkey {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let id = &self.0;
tokens.extend(quote! {#id})
}
}
struct Id(proc_macro2::TokenStream);
impl Parse for Id {
@ -213,6 +243,18 @@ pub fn respan(input: TokenStream) -> TokenStream {
TokenStream::from(to_respan)
}
#[proc_macro]
pub fn pubkey(input: TokenStream) -> TokenStream {
let id = parse_macro_input!(input as SdkPubkey);
TokenStream::from(quote! {#id})
}
#[proc_macro]
pub fn program_pubkey(input: TokenStream) -> TokenStream {
let id = parse_macro_input!(input as ProgramSdkPubkey);
TokenStream::from(quote! {#id})
}
#[proc_macro]
pub fn declare_id(input: TokenStream) -> TokenStream {
let id = parse_macro_input!(input as Id);

View File

@ -87,6 +87,22 @@ pub use solana_sdk_macro::program_declare_deprecated_id as declare_deprecated_id
/// assert_eq!(id(), my_id);
/// ```
pub use solana_sdk_macro::program_declare_id as declare_id;
/// Convenience macro to define a static public key
///
/// Input: a single literal base58 string representation of a Pubkey
///
/// # Example
///
/// ```
/// use std::str::FromStr;
/// use solana_program::{pubkey, pubkey::Pubkey};
///
/// static ID: Pubkey = pubkey!("My11111111111111111111111111111111111111111");
///
/// let my_id = Pubkey::from_str("My11111111111111111111111111111111111111111").unwrap();
/// assert_eq!(ID, my_id);
/// ```
pub use solana_sdk_macro::program_pubkey as pubkey;
#[macro_use]
extern crate serde_derive;

View File

@ -73,6 +73,22 @@ pub use solana_sdk_macro::declare_deprecated_id;
/// assert_eq!(id(), my_id);
/// ```
pub use solana_sdk_macro::declare_id;
/// Convenience macro to define a static public key
///
/// Input: a single literal base58 string representation of a Pubkey
///
/// # Example
///
/// ```
/// use std::str::FromStr;
/// use solana_program::{pubkey, pubkey::Pubkey};
///
/// static ID: Pubkey = pubkey!("My11111111111111111111111111111111111111111");
///
/// let my_id = Pubkey::from_str("My11111111111111111111111111111111111111111").unwrap();
/// assert_eq!(ID, my_id);
/// ```
pub use solana_sdk_macro::pubkey;
pub use solana_sdk_macro::pubkeys;
#[rustversion::since(1.46.0)]
pub use solana_sdk_macro::respan;