Try to not borrow a static function

Hello everyone! I am trying to build non-static function over the static one:

type VertexShader = Fn(AppVertex) -> GeometryVertex;
type VertexShaderSource = fn(AppVertex, transoform: na::Matrix4<f32>) -> GeometryVertex;

impl VertexPipe {
...
    fn build_shader(&self, source: &'static VertexShaderSource) -> Box<VertexShader> {
        Box::new(|app_vertex| source(app_vertex, na::Matrix4::<f32>::identity()))
    }
... 
}

And compiler says that I am borrowing source because that function owns it:

error[E0373]: closure may outlive the current function, but it borrows `source`, which is owned by the current function
  --> src/main.rs:47:18
   |
47 |         Box::new(|app_vertex| source(app_vertex, na::Matrix4::<f32>::identity()))
   |                  ^^^^^^^^^^^^ ------ `source` is borrowed here
   |                  |
   |                  may outlive borrowed value `source`

How I can copy reference to the closure? Why rust doesn't automatically?

If you specify move, it will be moved into the closure.

2 Likes

I forgot to add that I want to use a static function after.

What do you mean?

function build_shader takes VertexShaderSources(this is a ref to a static function). And I may use this static function again. I want be able to call build_shader twice with the same args.

So what is the problem?

Thank you very much. Now I got you. Also I fell in love with Rust community!

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.