代码之家  ›  专栏  ›  技术社区  ›  d33tah

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

  •  0
  • d33tah  · 技术社区  · 7 年前

    我想写一个服务器,调整一个巨大的图像大小。因为在每个请求上加载它会花费很多时间,所以我决定预加载它。不幸的是,我得到了以下错误:

       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();
    }
    

    要编译它,您需要 rocket image

    有可能创建这样一个全局变量吗?如果没有,我还有其他选择吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Boiethios    7 年前

    如果你想在火箭里分享一些东西,你必须使用 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`.
      }
      
    推荐文章