借用的值在结果 - 映射的通用结构中存在的时间不够长
impl<'a, T> ClientDBMain<T>
where
T: FromRow<'a, PgRow>,
{
async fn query_one(&self, pool: &PgPool, query_raw: &str) -> Result<T, MyError> {
let res = sqlx::query(query_raw)
.fetch_one(pool)
.await
.map(|r: PgRow| {
// error here
T::from_row(&r).unwrap()
})
.map_err(|err| {
// to save for brevity
})?;
Ok(res)
}
}
map()
中的 r
存活时间不够长。
r
的寿命是多少?
如何解决这个问题呢?通过强制延长使用寿命,至少作为a
?有什么办法可以管理吗?
impl<'a, T> ClientDBMain<T>
where
T: FromRow<'a, PgRow>,
{
async fn query_one(&self, pool: &PgPool, query_raw: &str) -> Result<T, MyError> {
let res = sqlx::query(query_raw)
.fetch_one(pool)
.await
.map(|r: PgRow| {
// error here
T::from_row(&r).unwrap()
})
.map_err(|err| {
// to save for brevity
})?;
Ok(res)
}
}
r
in map()
does not live long enough.
What is the life span of r
?
How to solve this problem? By enforcing longer life time, at least as a
? Is there any way to manage that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您几乎肯定想要使用排名更高的特征界限:
您的原始代码传达的是,如果该行具有特定的生命周期,则
T
只能从PgRow
生成。外部生命周期不可能适合仅存在于该闭包内的r
。相反,for<'a>
引入了通用生命周期,本质上是说T
为任何生命周期实现了FromRow
。You almost definitely want to use a higher-ranked trait bound:
Your original code was communicating that
T
could only be made from aPgRow
if the row had a specific lifetime. There is no way that an external lifetime would be appropriate forr
which only lives within that closure. Instead, thefor<'a>
introduces a generic lifetime, essentially saying thatT
implementsFromRow
for any lifetime.r
似乎有一个'static
生命周期,但是由于它不会被移动,因此它会在map.问题是
FromRow
可能会生成引用r
的值,这意味着它不能比r
更长寿。由于它的执行方式可能是通过'a
实现的,因此您可以通过要求T: FromRow<'static, PgRow>
来解决此问题。如果结果类型具有'static
生命周期,那么它将不依赖于r
的生命周期。r
appears to have a'static
lifetime, however since it does not get moved it will be dropped upon falling out of scope at the end of themap
. The issue is thatFromRow
may produce a value referencingr
meaning it can not outliver
. Since it appear that the way it does this is probably via'a
, you can probably fix this by requiringT: FromRow<'static, PgRow>
. If the resulting type has a'static
lifetime then it would not be reliant of the lifetime ofr
.