Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Update texture in Rust  (Read 12714 times)

0 Members and 1 Guest are viewing this topic.

weremsoft

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Update texture in Rust
« on: December 13, 2018, 08:35:03 pm »
Anyone knows how to update the texture in rust? I have the following:


Quote
let mut image = Image::from_color(WIDTH, HEIGHT, &Color::GREEN).unwrap();
let mut texture = Texture::from_image(&image).unwrap();
let sprite = Sprite::with_texture(&texture); // -------- immutable borrow occurs here
texture.update_from_image(&image, 0, 0); // ERROR!!! --- cannot borrow `texture` as mutable because it is also borrowed as immutable


Thanks

cptchuckles

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Re: Update texture in Rust
« Reply #1 on: March 13, 2019, 05:22:16 pm »
This looks like a scenario where RefCell<> would come in handy. You're trying to mutate something that already has an immutable borrow out there, so maybe you could do:
Code: [Select]
let mut image = Image::from_color(WIDTH, HEIGHT, &Color::GREEN).unwrap();
let texture = RefCell<Texture>::new(Texture::from_image(&image).unwrap());
let sprite = Sprite::with_texture(texture.borrow());
texture.borrow_mut().update_from_image(&image, 0, 0);

i have NOT tested this, nor have i actually used RefCell before :D I just remember this from my learnings.
https://doc.rust-lang.org/book/ch15-05-interior-mutability.html

Edit: yeah no that didn't work at all lol it just panics, back to the drawing board
« Last Edit: March 13, 2019, 05:46:39 pm by cptchuckles »

 

anything