gpt4 book ai didi

rust - 将引用作为参数更正 Into 的生命周期

转载 作者:行者123 更新时间:2023-11-29 08:27:21 26 4
gpt4 key购买 nike

我不确定在这里问什么是合适的问题,但我对在通用方法中设置正确的生命周期有疑问。完整代码是 here ,但基本问题是我有一个类似于下面的方法,但我遇到了终身错误。 &params 引用中的任何内容都不会嵌入到从 .into() 返回的结构中,因此这应该是安全的。我需要什么才能让它工作?

pub fn index<'a, T, R, P>(repository: T, query: &'a str) -> Result<Vec<<R as ToJson>::Attrs>, QueryStringParseError>
where
T: JsonApiRepository<'a, T = R>,
P: 'a,
R: ToJson,
R: QueryString<'a, Params = P>,
<R as ToJson>::Attrs: From<(R, &'a P)>,
QueryStringParseError: From<<T as JsonApiRepository<'a>>::Error>
{
let params = <R as QueryString>::from_str(query)?;
let list = repository.find_all(&params)?;

// What's the correct lifetime incantation for this to work?
// No references will be embedded in `R::Attrs`, so this
// should be safe
let data: Vec<<R as ToJson>::Attrs> = list.into_iter().map(|e| (e, &params).into()).collect();
Ok(data)
}

(修改后的问题,下面有错误)

rustc 1.16.0 (30cf806ef 2017-03-10)
error[E0373]: closure may outlive the current function, but it borrows `params`, which is owned by the current function
--> <anon>:16:64
|
16 | let data: Vec<<R as ToJson>::Attrs> = list.into_iter().map(|e| (e, &params).into()).collect();
| ^^^ ------ `params` is borrowed here
| |
| may outlive borrowed value `params`
|
help: to force the closure to take ownership of `params` (and any other referenced variables), use the `move` keyword, as shown:
| let data: Vec<<R as ToJson>::Attrs> = list.into_iter().map(move |e| (e, &params).into()).collect();

最佳答案

你得到的错误(我不知道你为什么不分享):

error[E0373]: closure may outlive the current function, but it borrows `params`, which is owned by the current function
--> src/main.rs:22:64
|
22 | let data: Vec<<R as ToJson>::Attrs> = list.into_iter().map(|e| (e, &params).into()).collect();
| ^^^ ------ `params` is borrowed here
| |
| may outlive borrowed value `params`
|
help: to force the closure to take ownership of `params` (and any other referenced variables), use the `move` keyword, as shown:
| let data: Vec<<R as ToJson>::Attrs> = list.into_iter().map(move |e| (e, &params).into()).collect();

您在函数内部创建了 params,但是所有特征边界都要求 params 的生命周期必须与从外部传入的字符串的生命周期相匹配。不可能满足该界限,并且看起来对 params 的引用必须通过闭包比函数存在的时间更长。因此,马马虎虎的错误信息。

您不应该将这些生命周期捆绑在一起。使用 higher-ranked trait bounds相反:

pub fn index<T, R, P, S, F>
(repository: T,
query: &str)
-> Result<JsonApiArray<<R as ToJson>::Attrs>, QueryStringParseError>
where T: JsonApiRepository<T = R>,
P: Params,
P: Default,
P: TypedParams<SortField = S, FilterField = F>,
P: for<'b> TryFrom<(&'b str, SortOrder, P), Err = QueryStringParseError>,
P: for<'b> TryFrom<(&'b str, Vec<&'b str>, P), Err = QueryStringParseError>,
R: ToJson,
R: for<'b> QueryString<'b, Params = P, SortField = S, FilterField = F>,
R: JsonApiResource<Params = P>,
<R as ToJson>::Attrs: for<'b> From<(R, &'b P)>,
QueryStringParseError: From<<T as JsonApiRepository>::Error>
{

关于rust - 将引用作为参数更正 Into<Foo> 的生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43072710/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com