-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
lang: Remove 3 lifetime definitions from Context
#3340
base: master
Are you sure you want to change the base?
lang: Remove 3 lifetime definitions from Context
#3340
Conversation
@acheroncrypto is attempting to deploy a commit to the coral-xyz Team on Vercel. A member of the Team first needs to authorize it. |
It basically fixes the problem. It still requires a lifetime annotation, but we should be able to handle this automically in the
Yeah, but the alternative is worse (declaring another lifetime as explained in the "Details" section of #2770).
Does that even matter here? I don't think the Rust compiler would allow you to overwrite the data with a reference declared inside the instruction handler. |
With a normal struct, it is possible to change the lifetime to a smaller one by re-borrowing struct X<'a> {
s: &'a str,
}
fn cast<'a, 'b>(x: X<'a>) -> X<'b> where 'a: 'b {
X { s: &*x.s }
// or just `return x;`, rust can cast automatically
} but we can't do this with
|
Yeah, I'm aware of that, but I'm not sure how that answers my initial comment about the Rust compiler not allowing you to overwrite the data with a reference declared inside the instruction handler.
Since you're using "they" when referring to the Metaplex team, I'm assuming you're not from the Metaplex team, and you just want to interact with the If my assumptions are correct, then trying to fully upgrade that program is completely unnecessary for your use case, because you don't need the program's internal logic to be able to interact with a program (you only need implementation signatures). If you want to use the program's crate as a CPI client or an off-chain client, you can safely remove all its instruction handler logic, including the parts where it uses As a side note, the Furthermore, you don't even need to upgrade anything to interact with older programs if you're using the latest version (v0.30.1). In fact, you don't even need to add programs as a dependency. Here are some useful links: |
Problem
Context
struct definition includes 4 lifetimes:anchor/lang/src/context.rs
Line 24 in ebbad72
While this is technically correct, and it's also what Rust does by default, it results in a poor developer experience because the lifetimes leak to the Anchor users in various cases. For example, remaining accounts usage requires annotating the instruction handler with lifetimes, which is quite difficult to figure out for people who're less experienced with lifetimes (not to mention this is completely unnecessary).
Summary of changes
Remove 3 (out of 4) lifetimes from the
Context
struct. In other words, make all references have the same lifetime. This makes it so much easier for Anchor users to to handle places thatContext
is used. For example, remaining accounts usage:anchor/tests/misc/programs/remaining-accounts/src/lib.rs
Lines 25 to 27 in ebbad72
simply becomes: