This code is extremely erroneous

extern crate glium;

use glium::{Surface, glutin::{self, NotCurrent}, implement_vertex, uniform};
use std::vec;

#[derive(Clone, Copy)]
struct vertex{
    position: [f32; 2]
}

const fragment_shader: &str = r#"
#version 140

out vec4 color;

void main(){
    color = vec4(1.0, 0.0, 0.0, 0.0);
}
"#;

const vertex_shader: &str = r#"
#version 140

in vec2 position;

void main(){
    gl_Position = vec4(position.x, position.y, 0.0, 1.0);
}
"#;
pub struct Graphics<'a, 'b>{
    event_loop: glutin::event_loop::EventLoop<()>,
    wb: glutin::window::WindowBuilder,
    cb: glutin::ContextBuilder<'b, NotCurrent>,
    display: glium::Display,
    indices: glium::index::NoIndices,
    program: glium::Program,
    shape_holder: Vec<glium::VertexBuffer<vertex>>
}


impl<'a, 'b> Graphics<'a, 'b> {
    pub fn new() -> Self{
        implement_vertex!(vertex, position);
       
        let event_loop = glutin::event_loop::EventLoop::new();
        let wb = glutin::window::WindowBuilder::new();
        let cb = glutin::ContextBuilder::new();
        let display = glium::Display::new(wb, cb, &event_loop).unwrap();
        let indices = glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList);
        let program = glium::Program::from_source(&display, vertex_shader, fragment_shader, None).unwrap();

        Self{
            event_loop,
            wb, 
            cb,
            display, 
            indices,
            program,
            shape_holder: Vec::new()
        }


    }
    pub fn render(&mut self){
        self.event_loop.run(move |ev, _, control_flow| {

            let mut target = self.display.draw();
            target.clear_color(0.0, 0.0, 1.0, 1.0);
            //target.draw(&vertex_buffer,&indices, &program, &glium::uniforms::EmptyUniforms, &Default::default()).unwrap();
            target.finish().unwrap();
    
            let next_frame_time = std::time::Instant::now() +
                std::time::Duration::from_nanos(16_666_667);
    
            *control_flow = glutin::event_loop::ControlFlow::WaitUntil(next_frame_time);
            match ev {
                glutin::event::Event::WindowEvent { event, .. } => match event {
                    glutin::event::WindowEvent::CloseRequested => {
                        *control_flow = glutin::event_loop::ControlFlow::Exit;
                        return;
                    },
                    _ => return,
                },
                _ => (),
            }
        });
    }

}


I am getting multiple problems due to lifetimes. Whenever i fix a problem, another (or more) problems will come out of nowhere.

i tried looking at multiple different solutions but none of them worked and caused more problems.

Also is concurrency and threading a good idea for my graphics script?

Can you guys help me fix all the bugs?

If you want help, please make it easier to help you by editing your post to follow our code formatting guidelines:

[Edited to add formatting]

The formatting is still pretty terrible with every second line being empty..

I don't really understand the lifetimes here, but you might be able to replace the 'a lifetime in Graphics with 'static.

right sorry. I was going to edit it but Akismet, has temporarily hidden my topic.

hi sorry for the late response but when i changed the thing to static in the impl, it said something about static being a reserved term.

here is an error message

warning: unused import: `uniform`
 --> src\graphics.rs:3:68
  |
3 | use glium::{Surface, glutin::{self, NotCurrent}, implement_vertex, uniform};
  |                                                                    ^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default


warning: type `vertex` should have an upper camel case name
 --> src\graphics.rs:7:8
  |
7 | struct vertex{
  |        ^^^^^^ help: convert the identifier to upper camel case (notice the capitalization): `Vertex`
  |
  = note: `#[warn(non_camel_case_types)]` on by default


error[E0477]: the type `[closure@src\graphics.rs:110:29: 131:10]` does not fulfill the required lifetime
   --> src\graphics.rs:110:25
    |
110 |         self.event_loop.run(move |ev, _, control_flow| {
    |                         ^^^
    |
    = note: type must satisfy the static lifetime


error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
   --> src\graphics.rs:110:29
    |
110 |           self.event_loop.run(move |ev, _, control_flow| {
    |  _____________________________^
111 | |
112 | |             let mut target = self.display.draw();
113 | |             target.clear_color(0.0, 0.0, 1.0, 1.0);
...   |
130 | |             }
131 | |         });
    | |_________^
    |
note: first, the lifetime cannot outlive the lifetime `'b` as defined on the impl at 86:6...
   --> src\graphics.rs:86:6
    |
86  | impl<'b> Graphics<'b> {
    |      ^^
note: ...so that the reference type `&mut Graphics<'b>` does not outlive the data it points at
   --> src\graphics.rs:110:9
    |
110 | /         self.event_loop.run(move |ev, _, control_flow| {
111 | |
112 | |             let mut target = self.display.draw();
113 | |             target.clear_color(0.0, 0.0, 1.0, 1.0);
...   |
130 | |             }
131 | |         });
    | |__________^
    = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `[closure@src\graphics.rs:110:29: 131:10]` will meet its required lifetime bounds
   --> src\graphics.rs:110:25
    |
110 |         self.event_loop.run(move |ev, _, control_flow| {
    |                         ^^^

'''

Please put the error message in a code block so it's readable.

fixed it.

What are you copying this from? Whatever it is, it must be inserting empty lines between every line.

I am copying this code from the vs code terminal. I do not know why there are empty lines.

Well, please look up how to fix that.

And please post the actual changes you made as well.

sure i will try to fix it

that's strange. I copied it from the terminal again and there were no extra spaces. The error message is slightly different since i tried changing the code but the problem is still the same

The fact that Graphics still has a lifetime annotated on it tells me that you misunderstood my suggestion. What I suggested is this:

pub struct Graphics {
    event_loop: glutin::event_loop::EventLoop<()>,
    wb: glutin::window::WindowBuilder,
    cb: glutin::ContextBuilder<'static, NotCurrent>,
    display: glium::Display,
    indices: glium::index::NoIndices,
    program: glium::Program,
    shape_holder: Vec<glium::VertexBuffer<vertex>>
}

Tried that but then I got this error

'''
error[E0759]: self has an anonymous lifetime '_ but it needs to satisfy a 'static lifetime requirement
--> src\graphics.rs:110:29
|
109 | pub fn render(&mut self){
| --------- this data with an anonymous lifetime '_...
110 | self.event_loop.run(move |ev, _, control_flow| {
| ____________________^
111 | |
112 | | let mut target = self.display.draw();
113 | | target.clear_color(0.0, 0.0, 1.0, 1.0);
... |
130 | | }
131 | | });
| |
^ ...is captured here...
|
note: ...and is required to live as long as 'static here
--> src\graphics.rs:110:25
|
110 | self.event_loop.run(move |ev, _, control_flow| {
| ^^^
'''

Please fix your code blocks :slight_smile: The website has a preview box where you can see how it looks before you post.

That error is unrelated to my suggestion. The signature of run is the following:

pub fn run<F>(self, event_handler: F) -> !
where
    F: 'static + FnMut(Event<'_, T>, &EventLoopWindowTarget<T>, &mut ControlFlow),

Because of the 'static in the above signature, you cannot use self inside the closure. You may be able to fix it by changing the method to take self instead of &mut self.

1 Like

I tried using just a self as an argument but I got multiple errors due to the move statement moving the self

Right, I see why. So the problem is:

  1. The run method only allows owned variables to be moved into it, so if the method takes &mut self, then that wont work as that isn't owned.
  2. Even if change it to take self, you still cannot move self into the closure as you need the event_loop field outside of the closure.

This would be a lot easier if these were local variables instead of fields in a struct :slight_smile:

One option is to construct the event_loop variable in the render method so that it isn't a field of the struct. Then if you also take self, you should be able to move self into the closure.

Another option is to move everything here into your main function with ordinary variables instead of trying to use struct fields.

You may also be able to make the event_loop field an Option and use Option::take to take the event_loop out of self before calling run on it.

1 Like

Thanks dude, your solution worked for me. I didn't ever think for putting the variables in the render method and not the struct. :smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.