'Convert glow::Framebuffer to u64

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?



Solution 1:[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))));
}

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 whilrun
Solution 2