Add macros for deprecacted ids (#18907)
This commit is contained in:
@ -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,
|
||||
|
Reference in New Issue
Block a user