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

标签 rust rust-rocket

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

   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 checkout(当前: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`.
    }
    

关于rust - 打开的图像作为全局变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52895363/

相关文章:

multithreading - Rayon 中的每线程初始化

rust - "let x = ~10;"在 Rust 中已经过时了吗?

rust - 在迭代器中写入数组

rest - rust + 火箭 : How I do read POST body from request as string?

rust - 如何使用 Rocket 中的 Tera 模板在 HTML 中显示使用 Diesel 检索的值?

json - 如何使用 Rocket 响应包含 JSON 数据的 POST 请求?

rust - 如何使包含 Arc 的结构字段可写?

rust - 如何确定何时应将函数输入参数作为引用?