lang: Add const of program ID to `declare_id!` and `declare_program!` (#3019)
This commit is contained in:
parent
64c52c66e3
commit
3c5483f0d0
|
@ -28,6 +28,7 @@ The minor version will be incremented upon a breaking change and the patch versi
|
|||
- lang: Add `anchor_lang::pubkey` macro for declaring `Pubkey` const values ([#3021](https://github.com/coral-xyz/anchor/pull/3021)).
|
||||
- cli: Sync program ids on the initial build ([#3023](https://github.com/coral-xyz/anchor/pull/3023)).
|
||||
- idl: Remove `anchor-syn` dependency ([#3030](https://github.com/coral-xyz/anchor/pull/3030)).
|
||||
- lang: Add `const` of program ID to `declare_id!` and `declare_program!` ([#3019](https://github.com/coral-xyz/anchor/pull/3019)).
|
||||
|
||||
### Fixes
|
||||
|
||||
|
|
|
@ -46,6 +46,9 @@ fn id_to_tokens(
|
|||
/// The static program ID
|
||||
pub static ID: #pubkey_type = #id;
|
||||
|
||||
/// Const version of `ID`
|
||||
pub const ID_CONST: #pubkey_type = #id;
|
||||
|
||||
/// Confirms that a given pubkey is equivalent to the program ID
|
||||
pub fn check_id(id: &#pubkey_type) -> bool {
|
||||
id == &ID
|
||||
|
@ -56,6 +59,11 @@ fn id_to_tokens(
|
|||
ID
|
||||
}
|
||||
|
||||
/// Const version of `ID`
|
||||
pub const fn id_const() -> #pubkey_type {
|
||||
ID_CONST
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[test]
|
||||
fn test_id() {
|
||||
|
|
|
@ -114,8 +114,12 @@ fn gen_id(idl: &Idl) -> proc_macro2::TokenStream {
|
|||
#[doc = #doc]
|
||||
pub static ID: Pubkey = __ID;
|
||||
|
||||
/// Const version of `ID`
|
||||
pub const ID_CONST: Pubkey = __ID_CONST;
|
||||
|
||||
/// The name is intentionally prefixed with `__` in order to reduce to possibility of name
|
||||
/// clashes with the crate's `ID`.
|
||||
static __ID: Pubkey = Pubkey::new_from_array([#(#address_bytes,)*]);
|
||||
const __ID_CONST : Pubkey = Pubkey::new_from_array([#(#address_bytes,)*]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue