Is there a way to store a texture inside a struct using rust-sdl2? [duplicate]

流过昼夜 提交于 2019-12-02 20:06:27

问题


I'm using the rust-sdl2 crate to paint a buffer on a window screen, so I decided to abstract the SDL calls. I tried to follow this example from the rust-sdl2 repo and my first thought was to create something like this:

pub struct SDLDisplay {
    sdl_context: Sdl,
    video_subsystem: VideoSubsystem,
    canvas: WindowCanvas,
    texture_creator: TextureCreator<sdl2::video::WindowContext>,
    texture: Texture,
}

impl SDLDisplay {
    pub fn new() -> Result<SDLDisplay, String> {
        let sdl_context = sdl2::init()?;
        let video_subsystem = sdl_context.video()?;

        let window = video_subsystem
            .window("Test App", 256, 240)
            .position_centered()
            .build()
            .map_err(|e| e.to_string())?;

        let canvas = window.into_canvas().build().map_err(|e| e.to_string())?;

        let texture_creator = canvas.texture_creator();
        let texture = texture_creator
            .create_texture_streaming(Some(ARGB8888), 256, 240)
            .map_err(|e| e.to_string())?;

        Ok(SDLDisplay {
            sdl_context,
            video_subsystem,
            canvas,
            texture_creator,
            texture,
        })
    }
}

The first issue was that Texture needs a lifetime parameter; I solved that by spreading an <'a> across the code base... It stopped complaining about that, but now I have the following:

error[E0515]: cannot return value referencing local variable `texture_creator`
   |
  --> src/sdl_display.rs:44:9
40 |           let texture = texture_creator
   |                         --------------- `texture_creator` is borrowed here
   |
...
40 |           let texture = texture_creator
44 | /         Ok(SDLDisplay {
   |                         --------------- `texture_creator` is borrowed here
45 | |             sdl_context,
...
46 | |             video_subsystem,
44 | /         Ok(SDLDisplay {
47 | |             canvas,
45 | |             sdl_context,
48 | |             texture_creator,
46 | |             video_subsystem,
49 | |             texture,
47 | |             canvas,
48 | |             texture_creator,
49 | |             texture,
50 | |         })
   | |__________^ returns a value referencing data owned by the current function

50 | |         })
   | |__________^ returns a value referencing data owned by the current function

It's telling me I can't move texture_creator to the new struct because it was borrowed for creating the texture, but if I don't have the TextureCreator instance inside the struct, it complains that Texture must not outlive TextureCreator.

I'm completely lost here. Is there a way to actually store a texture inside a struct using rust-sdl2?

来源:https://stackoverflow.com/questions/56051593/is-there-a-way-to-store-a-texture-inside-a-struct-using-rust-sdl2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!