我想将预编译的 json 模式传递给 actix web,但编译器抱怨Value
用于创建的借用的JSONSchema
寿命不够长。有没有办法解决这个问题?
例子:
use jsonschema::JSONSchema;
use serde_json::from_str;
use actix_web::{web, get, App, HttpServer, HttpResponse, Responder};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
let schema_str = include_str!("schema.json");
let schema_value = from_str(schema_str).unwrap();
let schema_compiled = JSONSchema::compile(&schema_value).unwrap();
App::new()
.data(schema_compiled) // fixme: compiles if commented out
.service(index)
})
.bind("0.0.0.0:8080")?
.run()
.await
}
#[get("/")]
async fn index<'a>(_schema: web::Data<JSONSchema<'a>>) -> impl Responder {
HttpResponse::Ok().finish() // todo: use schema for something
}
来自 rustc 的错误:
error[E0597]: `schema_value` does not live long enough
--> src/main.rs:10:51
|
10 | let schema_compiled = JSONSchema::compile(&schema_value).unwrap();
| --------------------^^^^^^^^^^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `schema_value` is borrowed for `'static`
...
14 | })
| - `schema_value` dropped here while still borrowed
我是生锈的新手,所以如果这是一个伪装的通用生锈问题,我深表歉意(一旦我的理解得到改善,我很乐意用更小的可重复性修改这个问题)。