打开的图像作为全局变量?

An opened image as a global variable?

我想写一个服务器来调整一个巨大的图像。由于在每次请求时都加载它会花费很多时间,因此我决定预加载它。不幸的是我收到以下错误:

   Compiling hello_world v0.0.0 (/tmp/Rocket/examples/hello_world)                                                                                                                                                                                                                                                                                                         
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants                                                                                                                                                                                                                                                                         
 --> examples/hello_world/src/main.rs:9:35                                                                                                                                                                                                                                                                                                                                 
  |                                                                                                                                                                                                                                                                                                                                                                        
9 | static img: image::DynamicImage = image::open("/home/d33tah/tmp/combined.png").unwrap();                                                                                                                                                                                                                                                                               
  |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                                                                                                                                         

error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants                                                                                                                                                                                                                                                                         
 --> examples/hello_world/src/main.rs:9:35                                                                                                                                                                                                                                                                                                                                 
  |                                                                                                                                                                                                                                                                                                                                                                        
9 | static img: image::DynamicImage = image::open("/home/d33tah/tmp/combined.png").unwrap();                                                                                                                                                                                                                                                                               
  |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                                                                                                                                

error: aborting due to 2 previous errors                                                                                                                                                                                                                                                                                                                                   

For more information about this error, try `rustc --explain E0015`.                                                                                                                                                                                                                                                                                                        
error: Could not compile `hello_world`.                                                                                                                                                                                                                                                                                                                                    

To learn more, run the command again with --verbose.

代码如下:

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;

#[cfg(test)] mod tests;

extern crate image;

static img: image::DynamicImage = image::open("/home/d33tah/tmp/combined.png").unwrap();

#[get("/")]
fn hello() -> Vec<u8> {
    "".into()
}

fn main() {
    rocket::ignite().mount("/", routes![hello]).launch();
}

要编译它,您需要根据其最新的 Github 签出(当前:831d8dfbe30dd69f0367a75361e027127b2100e1)和 image crate 安装 rocket

是否可以创建这样的全局变量?如果没有,我还有其他选择吗?

编辑:这被标记为这个问题的重复,但正如 Boiethios 所展示的,这个具体案例最好由 Rocket 的 API.

解决

如果你想在 Rocket 中分享一些东西,你将不得不使用 managed state:

  1. 对 Rocket 说你想管理资源:

    let image = image::open("/home/d33tah/tmp/combined.png").unwrap();
    rocket::ignite().manage(image);
    
  2. 在你需要的路径中获取:

    #[get("/foo")]
    fn foo(image: State<DynamicImage>) {
        // Can use `image`.
    }