Эх сурвалжийг харах

lang: Add const of program ID to `declare_id!` and `declare_program!` (#3019)

cryptopapi997 1 жил өмнө
parent
commit
3c5483f0d0

+ 1 - 0
CHANGELOG.md

@@ -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)).
 - 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)).
 - 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)).
 - 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
 ### Fixes
 
 

+ 8 - 0
lang/attribute/account/src/id.rs

@@ -46,6 +46,9 @@ fn id_to_tokens(
         /// The static program ID
         /// The static program ID
         pub static ID: #pubkey_type = #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
         /// Confirms that a given pubkey is equivalent to the program ID
         pub fn check_id(id: &#pubkey_type) -> bool {
         pub fn check_id(id: &#pubkey_type) -> bool {
             id == &ID
             id == &ID
@@ -56,6 +59,11 @@ fn id_to_tokens(
             ID
             ID
         }
         }
 
 
+        /// Const version of `ID`
+        pub const fn id_const() -> #pubkey_type {
+            ID_CONST
+        }
+
         #[cfg(test)]
         #[cfg(test)]
         #[test]
         #[test]
         fn test_id() {
         fn test_id() {

+ 4 - 0
lang/attribute/program/src/declare_program/mod.rs

@@ -114,8 +114,12 @@ fn gen_id(idl: &Idl) -> proc_macro2::TokenStream {
         #[doc = #doc]
         #[doc = #doc]
         pub static ID: Pubkey = __ID;
         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
         /// The name is intentionally prefixed with `__` in order to reduce to possibility of name
         /// clashes with the crate's `ID`.
         /// clashes with the crate's `ID`.
         static __ID: Pubkey = Pubkey::new_from_array([#(#address_bytes,)*]);
         static __ID: Pubkey = Pubkey::new_from_array([#(#address_bytes,)*]);
+        const __ID_CONST : Pubkey = Pubkey::new_from_array([#(#address_bytes,)*]);
     }
     }
 }
 }