-
Notifications
You must be signed in to change notification settings - Fork 261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add const pubkey support and make declare_id a declarative macro #2348
Conversation
0553bfc
to
199760f
Compare
e799f9b
to
4461538
Compare
946d41c
to
f0ec89b
Compare
@@ -304,6 +304,12 @@ impl Pubkey { | |||
Self(pubkey_array) | |||
} | |||
|
|||
/// Decode a string into a Pubkey, usable in a const context | |||
pub const fn from_str_const(s: &str) -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello, I'm just passing by, I think this could use a &'static str
input and renamed to from_static
, similar pattern as HeaderValue::from_static
. Using &'static
will prevent people from accidentally using this function for faillible inputs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this will accomplish what you think it will
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think what you're really looking for is the thing a macro does which is to check that the input is a (string) literal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something like this, which was the previous solution
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it's not a perfect solution, but the function isn't intented to be called with a live 'a
, so there is no reason to allow it in the function signature. Using &' static
prevent common misuse, if someone leaks a string, they should already know what they are doing. I think, as a library design principle, function signature should clearly communicate what is it use case, using &str
tell users that it works with a live 'a
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the meantime I've undone the deprecation of the pubkey macro and converted it to a declarative macro in solana-pubkey, since this discussion revealed that Pubkey::from_str_const
does not make it completely obsolete
893b85a
to
c129050
Compare
sdk/src/lib.rs
Outdated
@@ -144,6 +144,10 @@ pub use solana_sdk_macro::declare_deprecated_id; | |||
/// assert_eq!(id(), my_id); | |||
/// ``` | |||
pub use solana_sdk_macro::declare_id; | |||
#[deprecated( | |||
since = "2.1.0", | |||
note = "Use `solana_program::pubkey::Pubkey::from_str_const` instead" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here it should say "Use solana_pubkey::pubkey
... ", right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good spot, I've also updated it to re-export solana_pubkey::pubkey instead of the one from solana_sdk_macro
87c3252
to
296f002
Compare
This reverts commit 60dbddd.
296f002
to
bfa2bf9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Thanks for your patience, I wanted to do due diligence and read over the five8_const code, which also required reading firedancer code. Everything is good to go on my side
@@ -204,12 +206,17 @@ pub fn declare_deprecated_id(input: TokenStream) -> TokenStream { | |||
TokenStream::from(quote! {#id}) | |||
} | |||
|
|||
#[deprecated(since = "2.1.0", note = "Use `solana_pubkey::declare_id` instead")] | |||
#[proc_macro] | |||
pub fn program_declare_id(input: TokenStream) -> TokenStream { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll be honest, until this PR, I hadn't realized there were different versions of these macros for solana_sdk and solana_program. This PR is good as is, but we should definitely consider removing a lot of the extra cruft in favor of solana_pubkey
's macros in future work.
good work |
…a-xyz#2348) * make declare_id a declarative macro * remove old comment * deprecate program_declare_id * deprecate pubkey macro * put deprecation on the re-export of the pubkey macro * replace pubkey! with from_str_const in this repo * fmt * remove unused import * Revert "remove unused rustc_version dep from wen-restart (wrong branch)" This reverts commit 60dbddd. * avoid wen-restart changes again * fmt * fix deprecation text * make declare_deprecated_id a declarative macro * put back the deprecation on the re-export of the pubkey macro * fmt * don't deprecate the pubkey macro, but make it a declarative macro * update deprecation note * re-export the new pubkey macro in solana-sdk (with deprecation) instead of the old one
Problem
solana_program
.five8_const
crate, which has more useful error messages than the bs58 crate (that repo has a PR to improve this but it's been open for months)Summary of Changes
from_str_const
associated function toPubkey
. I just lifted this from package-metadata: Add macro to define program id from Cargo.toml #1806 which is a draft PR at the time of writingdeclare_id
alias insolana_program
with a declarative macro of the same name that does the same thing insolana_pubkey
(re-exported insolana_program
)program_declare_id!
macro insolana_sdk_macro
pubkey!
macro to a declarative macro and deprecatesolana_sdk_macro::program_pubkey