将新构造的数组传递给函数时出现意外的生命周期问题
Getting unexpected lifetime issue when passing newly constructed array to function
我一直在开发一个计划,旨在 运行 在 Solana 运行 时间。我最近 运行 遇到一个问题,即传递给 'test2' 的数组中的第二项出现某种生命周期错误。当显式生命周期注解被添加到 test1 时,这个错误消失了,但我不明白为什么它首先出现。
pub fn test1(
test_account_1: &AccountInfo,
test_account_2: &AccountInfo,
mint_account: &AccountInfo,
) -> ProgramResult {
test2(&[test_account_1.clone(), test_account_2.clone()]);
return Ok(());
}
pub fn test2(arr: &[AccountInfo]) {
for a in arr.iter() {
println!("{}", a.key)
}
}
错误:
lifetime mismatch
...but data from test_account_1
flows into test_account_2
here rustc (E0623)
lib.rs(207, 22): these two types are declared with
different lifetimes...
lib.rs(208, 22):
lib.rs(211, 37): ...but data
from test_account_1
flows into test_account_2
here
AccountInfo 的定义:
/// Account information
#[derive(Clone)]
pub struct AccountInfo<'a> {
/// Public key of the account
pub key: &'a Pubkey,
/// Was the transaction signed by this account's public key?
pub is_signer: bool,
/// Is the account writable?
pub is_writable: bool,
/// The lamports in the account. Modifiable by programs.
pub lamports: Rc<RefCell<&'a mut u64>>,
/// The data held in this account. Modifiable by programs.
pub data: Rc<RefCell<&'a mut [u8]>>,
/// Program that owns this account
pub owner: &'a Pubkey,
/// This account's data contains a loaded program (and is now read-only)
pub executable: bool,
/// The epoch at which this account will next owe rent
pub rent_epoch: Epoch,
}
为了让您的数组保存对那些 AccountsInfo
的引用,它们需要至少存在相同的生命周期。因为它们有内部边界,所以它们也需要匹配:
pub fn test1<'a>(
test_account_1: &AccountInfo<'a>,
test_account_2: &AccountInfo<'a>,
mint_account: &AccountInfo,
) -> Result<(), ()> {
test2(&[test_account_1.clone(), test_account_2.clone()]);
Ok(())
}
另外,根据您的实际需要,您可能根本不需要克隆:
pub fn test1<'a>(
test_account_1: &AccountInfo<'a>,
test_account_2: &AccountInfo<'a>,
mint_account: &AccountInfo,
) -> Result<(), ()> {
test2(&[test_account_1, test_account_2]);
Ok(())
}
pub fn test2(arr: &[&AccountInfo]) {
for a in arr.iter() {
println!("{}", a.key)
}
}
我一直在开发一个计划,旨在 运行 在 Solana 运行 时间。我最近 运行 遇到一个问题,即传递给 'test2' 的数组中的第二项出现某种生命周期错误。当显式生命周期注解被添加到 test1 时,这个错误消失了,但我不明白为什么它首先出现。
pub fn test1(
test_account_1: &AccountInfo,
test_account_2: &AccountInfo,
mint_account: &AccountInfo,
) -> ProgramResult {
test2(&[test_account_1.clone(), test_account_2.clone()]);
return Ok(());
}
pub fn test2(arr: &[AccountInfo]) {
for a in arr.iter() {
println!("{}", a.key)
}
}
错误:
lifetime mismatch
...but data from
test_account_1
flows intotest_account_2
here rustc (E0623)
lib.rs(207, 22): these two types are declared with different lifetimes...
lib.rs(208, 22):
lib.rs(211, 37): ...but data from
test_account_1
flows intotest_account_2
here
AccountInfo 的定义:
/// Account information
#[derive(Clone)]
pub struct AccountInfo<'a> {
/// Public key of the account
pub key: &'a Pubkey,
/// Was the transaction signed by this account's public key?
pub is_signer: bool,
/// Is the account writable?
pub is_writable: bool,
/// The lamports in the account. Modifiable by programs.
pub lamports: Rc<RefCell<&'a mut u64>>,
/// The data held in this account. Modifiable by programs.
pub data: Rc<RefCell<&'a mut [u8]>>,
/// Program that owns this account
pub owner: &'a Pubkey,
/// This account's data contains a loaded program (and is now read-only)
pub executable: bool,
/// The epoch at which this account will next owe rent
pub rent_epoch: Epoch,
}
为了让您的数组保存对那些 AccountsInfo
的引用,它们需要至少存在相同的生命周期。因为它们有内部边界,所以它们也需要匹配:
pub fn test1<'a>(
test_account_1: &AccountInfo<'a>,
test_account_2: &AccountInfo<'a>,
mint_account: &AccountInfo,
) -> Result<(), ()> {
test2(&[test_account_1.clone(), test_account_2.clone()]);
Ok(())
}
另外,根据您的实际需要,您可能根本不需要克隆:
pub fn test1<'a>(
test_account_1: &AccountInfo<'a>,
test_account_2: &AccountInfo<'a>,
mint_account: &AccountInfo,
) -> Result<(), ()> {
test2(&[test_account_1, test_account_2]);
Ok(())
}
pub fn test2(arr: &[&AccountInfo]) {
for a in arr.iter() {
println!("{}", a.key)
}
}