-1

I have error on line 87:
missing lifetime specifier
expected named lifetime parameterrustcClick for full compiler diagnostic

main.rs(87, 11): consider introducing a named lifetime parameter: `<'a>`, `<'a>`

I tried different ways to fix it, but nothing helps me


use std::sync::Arc;

use pixels::{SurfaceTexture,Pixels};

use winit::application::ApplicationHandler;
use winit::dpi::PhysicalSize;
use winit::event::{DeviceEvent, ElementState, MouseButton, WindowEvent};
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
use winit::keyboard::KeyCode;
use winit::window::{Window, WindowId};
use winit::event::MouseScrollDelta;
use winit::keyboard::PhysicalKey;


const WIDTH:u32 = 320;
const HEIGHT:u32 = 240;
const BOX_SIZE:u16 = 120;


struct World {
    box_x:i16,
    box_y:i16,
    velocity_x:i16,
    velocity_y:i16
}

impl World{
    fn new() -> Self {
        Self{
            box_x:24,
            box_y:16,
            velocity_x:1,
            velocity_y:1,
        }
    }
    fn update(&mut self) {
        if self.box_x <= 0 || self.box_x + BOX_SIZE as i16 > WIDTH as i16 {
            self.velocity_x *= -1
        }
        if self.box_y <= 0 || self.box_y + BOX_SIZE as i16 > HEIGHT as i16{
            self.velocity_y *= -1
        }
        self.box_x += self.velocity_x;
        self.box_y += self.velocity_y;
    }

    fn draw<>(&self, frame:&mut [u8]){
        for (i,pixel) in frame.chunks_exact_mut(4).enumerate() {
            let x = (i % WIDTH as usize) as i16;
            let y = (i / WIDTH as usize) as i16;

            let inside_the_box:bool = x >= self.box_x
                && x < self.box_x + BOX_SIZE as i16
                && y >= self.box_y
                && y < self.box_y + BOX_SIZE as i16;

            

            let rgba = if inside_the_box{
                [0x5e, 0x48, 0xe8, 0xff]
            }else{
                [0x48, 0xb2, 0xe8, 0xff]
            };

            pixel.copy_from_slice(&rgba);
        }
    }
}

fn main() {
    let event_loop = EventLoop::new().unwrap();
    event_loop.set_control_flow(ControlFlow::Poll);
    


    let mut app = App{
        window:None,
        pixels:None,
        world:World::new(),
    };
    event_loop.run_app(&mut app).unwrap();
    
}

//#[derive(Default)]
struct App {
    window: Option<Arc<Window>>,
    pixels: Option<Pixels>,
    world: World,
    
}


impl ApplicationHandler for App {
    fn resumed(&mut self,event_loop: &ActiveEventLoop){
        if self.window.is_none() {
            let window = Arc::new(event_loop
                .create_window(Window::default_attributes()
                    .with_title("title")
                    .with_inner_size(PhysicalSize::new(WIDTH,HEIGHT))
                    .with_min_inner_size(PhysicalSize::new(WIDTH,HEIGHT))

                ).unwrap());

            self.window = Some(window.clone());
            let window_size = window.inner_size();

            if let Some(window) = &self.window {
                let window_size = window.inner_size();
                let surface_texture = SurfaceTexture::new(
                    window_size.width,
                    window_size.height,
                    window.as_ref(),
                );
                let pixels = Pixels::new(WIDTH, HEIGHT, surface_texture).unwrap();
                self.pixels = Some(pixels);

            }
        }
    }
    
    fn window_event(
        &mut self,
        event_loop: &ActiveEventLoop,
        _id: WindowId,
        event: WindowEvent,
    ) {
        match event {
            WindowEvent::CloseRequested => event_loop.exit(),

            WindowEvent::KeyboardInput {  event,..} => {
                if event.physical_key == PhysicalKey::Code(KeyCode::Escape){
                    event_loop.exit();
                }
            }
            
            WindowEvent::Resized(new_size) => {
                if let Some(pixels) = &mut self.pixels {
                    if let Err(err) = pixels.resize_surface(new_size.width, new_size.height) {
                        eprintln!("pixels.resize_surface() failed: {err}");
                        event_loop.exit();
                    }
                }
                
            }
            
            WindowEvent::RedrawRequested => {
              
                if let (Some(window),Some(pixels)) = (&self.window,&mut self.pixels) {
                    self.world.update();
                    self.world.draw(pixels.frame_mut());
                    if let Err(err) = pixels.render(){
                        eprintln!("pixels.render() failed: {err}");
                        event_loop.exit();
                        return;
                    }
                    window.request_redraw();
                }
            }
            WindowEvent::MouseInput { button ,state,..} => {
                match state {
                    ElementState::Pressed => {
                        match button {
                            MouseButton::Left => {
                                println!("left mouse button is pressed")
                            }
                            _ => {
                                println!("Hello!");
                                ()
                            }
                

                        }
                    }
                    _ => ()
                    
                }
                
            }
            _ => ()
        }
    }
    fn device_event(
        &mut self,
        _event_loop: &ActiveEventLoop,
        _device_id: winit::event::DeviceId,
        event: winit::event::DeviceEvent,
    ) {
        match event {
            DeviceEvent::MouseWheel { delta,.. } => {
                match delta {
                    MouseScrollDelta::PixelDelta(pos) => {
                            println!("{}, {}",pos.x,pos.y)
                        }
                        
                        MouseScrollDelta::LineDelta(x, y) => {
                            println!("{}, {}",x,y)
                        }
                    
                }
                    
            }
                _ => ()
        }
        
    }
}

my dependencies and package:


[package]
name = "app"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1.0.100"
chrono = "0.4.42"
dirs = "6.0.0"
pixels = "0.15.0"
pollster = "0.4.0"
rdev = { version = "0.5", features = ["unstable_grab"] }
screenshots = "0.8.10"
softbuffer = "0.4.6"
wgpu = "27.0.1"
winit = "0.30.12"


maybe i need to rewrite my code

1
  • 5
    Please edit your question to include the full error message by the compiler (i.e. cargo check) not the IDE (which "helpfully" hides a lot of information) as formatted text. Commented Nov 30 at 15:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.