Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Conversation

@fanatid
Copy link
Contributor

@fanatid fanatid commented Aug 14, 2021

Ref solana-foundation/anchor#608

Problem

In project-serum/anchor we want to optimize code generation with something like:

pub trait KeyRef<'info> {
    fn key(&self) -> &'info Pubkey;
}

impl<'info, T> KeyRef<'info> for T
where
    T: AsRef<AccountInfo<'info>>,
{
    fn key(&self) -> &'info Pubkey {
        self.as_ref().key
    }
}

of course this produce error:

upstream crates may add a new impl of trait `std::convert::AsRef<solana_program::account_info::AccountInfo<'_>>` for type `solana_program::account_info::AccountInfo<'_>` in future versions

Summary of Changes

Implement AsRef<AccountInfo<'a>> for AccountInfo<'a>.

@mergify mergify bot added the community Community contribution label Aug 14, 2021
@mergify mergify bot requested a review from a team August 14, 2021 21:18
@fanatid fanatid force-pushed the account-info-asref branch from 1c883d2 to fef974c Compare August 14, 2021 21:20
@mvines
Copy link
Contributor

mvines commented Aug 14, 2021

lgtm but would you mind also adding a quick test for this, one that'll yell if somebody removes this in the future

@fanatid fanatid force-pushed the account-info-asref branch from 97045bf to 169da11 Compare August 15, 2021 05:49
@fanatid
Copy link
Contributor Author

fanatid commented Aug 15, 2021

Good idea 👍 Test added.

@codecov
Copy link

codecov bot commented Aug 15, 2021

Codecov Report

Merging #19235 (169da11) into master (a0e8b86) will increase coverage by 0.0%.
The diff coverage is 100.0%.

@@           Coverage Diff           @@
##           master   #19235   +/-   ##
=======================================
  Coverage    82.8%    82.8%           
=======================================
  Files         455      455           
  Lines      130032   130041    +9     
=======================================
+ Hits       107738   107763   +25     
+ Misses      22294    22278   -16     

@mvines mvines added the v1.7 label Aug 15, 2021
@mvines
Copy link
Contributor

mvines commented Aug 15, 2021

Thanks! This'll ship in Solana v1.7.11

@mvines mvines merged commit d10e37a into solana-labs:master Aug 15, 2021
mergify bot added a commit that referenced this pull request Aug 15, 2021
* Add AsRef<AccountInfo> for AccountInfo

(cherry picked from commit 930465e)

* add test for AsRef

(cherry picked from commit d10e37a)

Co-authored-by: Kirill Fomichev <[email protected]>
@fanatid fanatid deleted the account-info-asref branch August 15, 2021 17:20
@brooksprumo brooksprumo mentioned this pull request Aug 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

community Community contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants