0

egui::Image::new can take a User(u64) as the first parameter. I have a glow::Framebuffer that I want to pass to the function like

egui::Image::new(User(frame_buffer), egui::Vec2::new(1280.0, 720.0));

It looks like glow::Frambuffer is a NativeFramebuffer(NonZeroU32). How do I convert a glow::Framebuffer to a u64?

Chandan
  • 11,465
  • 1
  • 6
  • 25
Grayden Hormes
  • 855
  • 1
  • 15
  • 34

2 Answers2

1

NonZeroU64 has implemented From<NonZeroU32> and u64 has implemented From<NonZeroU64> so you can use from method of these two types to safely convert the type. Here is a brief example.

use std::num::*;
#[derive(Debug)]
struct NativeFrameBuffer(NonZeroU32);

#[derive(Debug)]
struct User(u64);

fn test(u:User) {
    println!("{:?}", u);
}

fn main() {
    let u32num = NonZeroU32::new(10u32).unwrap();
    let fb = NativeFrameBuffer(u32num);
    test(User(u64::from(NonZeroU64::from(fb.0))));
}
whilrun
  • 1,701
  • 11
  • 21
0

I solved it like this

    pub  fn unwrap_color_attachment(&self) -> u32 {
        #[cfg(not(target_os = "wasm"))]
        unsafe { mem::transmute(self.color_attachment) }
    }

egui::Image::new(User(my_framebuffer_wrapper.unwrap_color_attachment() as u64), egui::Vec2::new(1280.0, 720.0));

unsafe is required

Grayden Hormes
  • 855
  • 1
  • 15
  • 34
  • Very bad idea. According to [the docs for `transmute`](https://doc.rust-lang.org/1.54.0/std/mem/fn.transmute.html): _Both types must have the same size_ so you can't transmute a 32-bit value into a 64-bit value. – Jmb Dec 01 '21 at 07:35
  • sorry i actually transmuted to u32 then safe casted to u64. I have updated my answer – Grayden Hormes Dec 06 '21 at 03:10