对Actix服务器中捕获的变量的引用导致“参数要求它必须比'静态'更有效”

时间:2019-02-10 15:51:29

标签: rust borrow-checker

在下面的示例中,我有由命令行参数设置的参数TextInputCREATE TABLE `sensor_history` ( `measurement_time_utc` int(11) NOT NULL, `sensor_id` int(11) NOT NULL, `sensor_measurement_x` double NOT NULL, `sensor_measurement_y` double NOT NULL, `sensor_measurement_z` double NOT NULL, `sensor_quality` int(11) NOT NULL ); CREATE TABLE `process_history` ( `log_time_utc` int(11) NOT NULL, `process_id` int(11) NOT NULL, `process_speed` double NOT NULL, `process_load` int(11) NOT NULL ); CREATE VIEW `rollup` AS SELECT `sensor_history`.`measurement_time_utc`, `sensor_history`.`sensor_id`, `sensor_history`.`sensor_measurement_x`, `sensor_history`.`sensor_measurement_y`, `sensor_history`.`sensor_measurement_z`, `sensor_history`.`sensor_quality`, (SELECT `process_history`.`process_id` FROM `process_history` WHERE `sensor_history`.`measurement_time_utc`>=`process_history`.`log_time_utc` ORDER BY `process_history`.`log_time_utc` DESC LIMIT 1) AS `process_id`, (SELECT `process_history`.`process_speed` FROM `process_history` WHERE `sensor_history`.`measurement_time_utc`>=`process_history`.`log_time_utc` ORDER BY `process_history`.`log_time_utc` DESC LIMIT 1) AS `process_speed`, (SELECT `process_history`.`process_load` FROM `process_history` WHERE `sensor_history`.`measurement_time_utc`>=`process_history`.`log_time_utc` ORDER BY `process_history`.`log_time_utc` DESC LIMIT 1) AS `process_load` FROM `sensor_history`; verbose是一个布尔型标志,但是data_source用于从一组可用函数中选择一个缺省值,这些函数可以根据应用程序的数据源使用。

Actix使用闭包来设置服务器,因此我需要将这些参数放入闭包中。我为布尔添加了verbose,但是在传递要用于索引的函数时遇到了麻烦,并且遇到了生命周期错误。我尝试将函数装箱,但这似乎无济于事。

如果我正确地理解了错误消息,则实际上是闭包本身未能超过data_source

我应该怎么做才能解决这个问题?

move
'static

1 个答案:

答案 0 :(得分:1)

我不明白编译器在说什么,但是很明显,在处理程序闭包中您需要move

if verbose {
    App::new()
        .middleware(middleware::Logger::default())
        .resource("/", move |r| r.method(Method::GET).f(index))
} else {
    App::new().resource("/", move |r| r.method(Method::GET).f(index))
}

这是因为您需要将index从外部封盖中移出。