我在借阅判断器上遇到了问题;以下是我正在try 的简化版本:
trait CommitmentScheme<In> {
type Com;
fn commit(input: In) -> Self::Com;
fn open(com1: &Self::Com, com2: &Self::Com);
}
fn prove<CS: for<'a> CommitmentScheme<&'a [u32]>>() {
let msg1 = vec![1u32];
let com1 = CS::commit(&msg1);
let msg2 = vec![2u32];
let com2 = CS::commit(&msg2);
CS::open(&com1, &com2);
}
这无法编译,并出现以下错误,
error[E0597]: `msg2` does not live long enough
--> src/lib.rs:13:27
|
12 | let msg2 = vec![2u32];
| ---- binding `msg2` declared here
13 | let com2 = CS::commit(&msg2);
| ^^^^^ borrowed value does not live long enough
14 | CS::open(&com1, &com2);
15 | }
| -
| |
| `msg2` dropped here while still borrowed
| borrow might be used here, when `com1` is dropped and runs the destructor for type `<CS as CommitmentScheme<&[u32]>>::Com`
|
= note: values in a scope are dropped in the opposite order they are defined
早些时候声明msg2
可以避免该错误,但在我的设置中,直到第一个commit
调用之后才能构建msg2
.
我对这个问题的理解是,返回的Com
有可能保留对提交消息的引用.然而,根据相反的顺序规则,似乎每个promise 都应该在它可能引用的消息之前丢弃.我首先try 了手动drop
个promise ,但没有成功.
我还试着声明type Com: 'static;
,我认为这基本上可以保证commit
返回一些免borrow 的东西(除了可能的静态borrow ,这看起来不是问题).我认为这将确保msg1
和msg2
只在commit
调用期间被borrow ,而不是在commit
调用之后被borrow ,但错误仍然存在.