Add macros for deprecacted ids (#18907)

This commit is contained in:
Jack May 2021-07-26 20:54:46 -07:00 committed by GitHub
parent a4024168de
commit eaeeffa5a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 101 additions and 1 deletions

View File

@ -63,6 +63,36 @@ fn id_to_tokens(
});
}
fn deprecated_id_to_tokens(
id: &proc_macro2::TokenStream,
pubkey_type: proc_macro2::TokenStream,
tokens: &mut proc_macro2::TokenStream,
) {
tokens.extend(quote! {
/// The static program ID
pub static ID: #pubkey_type = #id;
/// Confirms that a given pubkey is equivalent to the program ID
#[deprecated()]
pub fn check_id(id: &#pubkey_type) -> bool {
id == &ID
}
/// Returns the program ID
#[deprecated()]
pub fn id() -> #pubkey_type {
ID
}
#[cfg(test)]
#[test]
fn test_id() {
#[allow(deprecated)]
assert!(check_id(&id()));
}
});
}
struct Id(proc_macro2::TokenStream);
impl Parse for Id {
@ -77,8 +107,21 @@ impl ToTokens for Id {
}
}
struct ProgramSdkId(proc_macro2::TokenStream);
struct IdDeprecated(proc_macro2::TokenStream);
impl Parse for IdDeprecated {
fn parse(input: ParseStream) -> Result<Self> {
parse_id(input, quote! { ::solana_sdk::pubkey::Pubkey }).map(Self)
}
}
impl ToTokens for IdDeprecated {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
deprecated_id_to_tokens(&self.0, quote! { ::solana_sdk::pubkey::Pubkey }, tokens)
}
}
struct ProgramSdkId(proc_macro2::TokenStream);
impl Parse for ProgramSdkId {
fn parse(input: ParseStream) -> Result<Self> {
parse_id(input, quote! { ::solana_program::pubkey::Pubkey }).map(Self)
@ -91,6 +134,19 @@ impl ToTokens for ProgramSdkId {
}
}
struct ProgramSdkIdDeprecated(proc_macro2::TokenStream);
impl Parse for ProgramSdkIdDeprecated {
fn parse(input: ParseStream) -> Result<Self> {
parse_id(input, quote! { ::solana_program::pubkey::Pubkey }).map(Self)
}
}
impl ToTokens for ProgramSdkIdDeprecated {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
deprecated_id_to_tokens(&self.0, quote! { ::solana_program::pubkey::Pubkey }, tokens)
}
}
#[allow(dead_code)] // `respan` may be compiled out
struct RespanInput {
to_respan: Path,
@ -163,12 +219,24 @@ pub fn declare_id(input: TokenStream) -> TokenStream {
TokenStream::from(quote! {#id})
}
#[proc_macro]
pub fn declare_deprecated_id(input: TokenStream) -> TokenStream {
let id = parse_macro_input!(input as IdDeprecated);
TokenStream::from(quote! {#id})
}
#[proc_macro]
pub fn program_declare_id(input: TokenStream) -> TokenStream {
let id = parse_macro_input!(input as ProgramSdkId);
TokenStream::from(quote! {#id})
}
#[proc_macro]
pub fn program_declare_deprecated_id(input: TokenStream) -> TokenStream {
let id = parse_macro_input!(input as ProgramSdkIdDeprecated);
TokenStream::from(quote! {#id})
}
fn parse_pubkey(
id_literal: &LitStr,
pubkey_type: &proc_macro2::TokenStream,

View File

@ -62,6 +62,8 @@ pub mod vote {
}
}
/// Same as `declare_id` except report that this id has been deprecated
pub use solana_sdk_macro::program_declare_deprecated_id as declare_deprecated_id;
/// Convenience macro to declare a static public key and functions to interact with it
///
/// Input: a single literal base58 string representation of a program's id

View File

@ -51,6 +51,34 @@ macro_rules! declare_sysvar_id(
)
);
#[macro_export]
macro_rules! declare_deprecated_sysvar_id(
($name:expr, $type:ty) => (
$crate::declare_deprecated_id!($name);
impl $crate::sysvar::SysvarId for $type {
fn id() -> $crate::pubkey::Pubkey {
#[allow(deprecated)]
id()
}
fn check_id(pubkey: &$crate::pubkey::Pubkey) -> bool {
#[allow(deprecated)]
check_id(pubkey)
}
}
#[cfg(test)]
#[test]
fn test_sysvar_id() {
#[allow(deprecated)]
if !$crate::sysvar::is_sysvar_id(&id()) {
panic!("sysvar::is_sysvar_id() doesn't know about {}", $name);
}
}
)
);
// Owner pubkey for sysvar accounts
crate::declare_id!("Sysvar1111111111111111111111111111111111111");

View File

@ -50,6 +50,8 @@ pub mod timing;
pub mod transaction;
pub mod transport;
/// Same as `declare_id` except report that this id has been deprecated
pub use solana_sdk_macro::declare_deprecated_id;
/// Convenience macro to declare a static public key and functions to interact with it
///
/// Input: a single literal base58 string representation of a program's id