Skip to content

Commit

Permalink
don't deprecate the pubkey macro, but make it a declarative macro
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinheavey committed Oct 11, 2024
1 parent 86fea9f commit c129050
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
1 change: 1 addition & 0 deletions sdk/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ pub fn pubkey(input: TokenStream) -> TokenStream {
TokenStream::from(quote! {#id})
}

#[deprecated(since = "2.1.0", note = "Use `solana_pubkey::pubkey!` instead")]
#[proc_macro]
pub fn program_pubkey(input: TokenStream) -> TokenStream {
let id = parse_macro_input!(input as ProgramSdkPubkey);
Expand Down
22 changes: 1 addition & 21 deletions sdk/program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,27 +597,7 @@ pub mod sdk_ids {

#[deprecated(since = "2.1.0", note = "Use `solana-decode-error` crate instead")]
pub use solana_decode_error as decode_error;
pub use solana_pubkey::{declare_deprecated_id, 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);
/// ```
#[deprecated(
since = "2.1.0",
note = "Use `solana_pubkey::Pubkey::from_str_const` instead"
)]
pub use solana_sdk_macro::program_pubkey as pubkey;
pub use solana_pubkey::{declare_deprecated_id, declare_id, pubkey};

#[macro_use]
extern crate serde_derive;
Expand Down
15 changes: 15 additions & 0 deletions sdk/program/tests/test_pubkey_export.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use {
solana_program::{pubkey, pubkey::Pubkey},
std::str::FromStr,
};

// solana_program::pubkey refers to both a module and a macro.
// This test demonstrates that both imports are working
#[test]
fn test_pubkey_import() {
let pk = pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
assert_eq!(
pk,
Pubkey::from_str("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL").unwrap()
);
}
32 changes: 32 additions & 0 deletions sdk/pubkey/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,28 @@ macro_rules! declare_deprecated_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_pubkey::{pubkey, Pubkey};
///
/// static ID: Pubkey = pubkey!("My11111111111111111111111111111111111111111");
///
/// let my_id = Pubkey::from_str("My11111111111111111111111111111111111111111").unwrap();
/// assert_eq!(ID, my_id);
/// ```
#[macro_export]
macro_rules! pubkey {
($input:literal) => {
$crate::Pubkey::from_str_const($input)
};
}

#[cfg(test)]
mod tests {
use {super::*, strum::IntoEnumIterator};
Expand Down Expand Up @@ -1361,4 +1383,14 @@ mod tests {
);
}
}

#[test]
fn test_pubkey_macro() {
const PK: Pubkey = Pubkey::from_str_const("9h1HyLCW5dZnBVap8C5egQ9Z6pHyjsh5MNy83iPqqRuq");
assert_eq!(pubkey!("9h1HyLCW5dZnBVap8C5egQ9Z6pHyjsh5MNy83iPqqRuq"), PK);
assert_eq!(
Pubkey::from_str("9h1HyLCW5dZnBVap8C5egQ9Z6pHyjsh5MNy83iPqqRuq").unwrap(),
PK
);
}
}

0 comments on commit c129050

Please sign in to comment.