我正在尝试使用 SQLX 来开发 Rust 应用程序服务器作为后端存储。它适用于具有一些特殊默认行为的原始 CMS,即:如果找不到页面,则自动生成带有一些空白内容的页面。我希望能够重用调用 SQL 的函数,因此我希望它们采用实现该sqlx::Executor特征的类型,包括&mut sqlx::Connection、&mut sqlx::pool::PoolConnection和&mut sqlx::Transaction。
每个只需调用数据库一次的函数就可以很好地工作!
但是,我似乎无法重复使用执行器。在我确定页面不存在后调用此函数;我创建一个事务并使用 an&mut Transaction作为e参数调用它:
async fn create_page_for_title(e: impl Executor<'_, Database = Sqlite>, title: &str) -> SqlResult<RawPage> {
let newcontent_id = insert_new_root_content(e).await?;
let newpage = insert_new_page(e, title, newcontent_id).await?;
Ok(newpage)
}
Run Code Online (Sandbox Code Playgroud)
Rust (1.46) 是这样说的:
Error[E0382]: use of moved value: `e`
--> src/store.rs:239:30
|
230 | async fn create_page_for_title(e: impl Executor<'_, Database = Sqlite>, title: &str) -> SqlResult<RawPage>
| - move occurs because `e` has type `impl Executor<'_, Database = Sqlite>`, which does not implement the `Copy` trait
...
238 | let newcontent_id = insert_new_root_content(e).await?;
| - value moved here
239 | let newpage = insert_new_page(e, title, newcontent_id).await?;
| ^ value used here after move
Run Code Online (Sandbox Code Playgroud)
我知道e是参考,参考是Copy。但我似乎无法让 Rust 相信这一点,我也不知道为什么。impl Executor这是我在同一范围内使用两次的唯一函数,但我确信随着时间的推移我会发现更多。请帮忙?