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

时间:2018-10-19 15:17:34

标签: 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结帐(当前:831d8dfbe30dd69f0367a75361e027127b2100e1)和rocket条板箱安装image

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

编辑:这被标记为该问题的重复,但是正如Boiethios所表明的那样,此特定情况可以通过Rocket的API更好地解决。

1 个答案:

答案 0 :(得分:2)

如果要在Rocket中共享内容,则必须使用managed state

  1. 对Rocket说,您要管理资源:

    Optional<Boolean> op = Optional.ofNullable(primaryDTO)
                .flatMap(x -> Optional.ofNullable(x.getSecondaryDTOs()))
                .map(x -> x.stream()
                        .filter(Objects::nonNull)
                        .flatMap(y -> y.getSecondaryCategoryDTOs().stream())
                        .map(SecondaryCategoryDTO::isStatus)
                        .anyMatch(z -> z == true)); 
    
    if(op.isPresent()){
        // do your work
    }
    
  2. 在您需要的路线中检索它:

    let image = image::open("/home/d33tah/tmp/combined.png").unwrap();
    rocket::ignite().manage(image);