I'm using rust-sfml
(rsfml::graphics
) to, at the moment, draw pixels to the screen. (I'm just starting with both Rust and the project.) I'm storing the data in an Image
, then copying to a Texture
.
This Texture
is used to create a Sprite<'s>
; herein lies my problem. I need to be able to mutate the Texture
, but the type of Sprite<'s>
seems to guarantee that I can't do what I want. Since I need to be able to call window.draw(&sprite)
every time the window is redrawn, I'm just creating a new Sprite
each time.
The preferable alternative would keep the Sprite<'s>
in my struct Render
along with the Texture
. Since 'Sprite' has a lifetime parameter, it becomes struct Render<'s>
:
struct Render<'s> {
texture: Texture,
sprite: Sprite<'s>,
}
I have a method on Render
:
fn blit(&'s mut self) -> ()
which mutates the Render
(by editing the Texture
). Now, as soon as I try to call blit
more than once, I run into this problem:
render.blit();
render.blit(); // error: cannot borrow `render` as mutable more than once at a time
which, I think, happens because the lifetime parameter forces the the Render
's lifetime-as-borrowed-by-the-first-blit
-call to be equal to the lifetime of the Render
instance (the whole main function).
How can I keep my original Sprite
and continue to be able to mutate the container? Is it possible?
Here is a stupid-looking and fairly minimal example:
extern crate rsfml;
use rsfml::graphics::Sprite;
fn main() -> () {
let mut render = Render::new();
render.blit();
render.blit(); // error: cannot borrow `render` as mutable more than once at a time
}
struct Render<'s> {
sprite: Option<Sprite<'s>>,
}
impl<'s> Render<'s> {
fn new() -> Render { Render { sprite: None } }
fn blit(&'s mut self) -> () { }
}
(Apologies if the question is not clear. It's difficult to express when I'm not very familiar with the concepts.)