'How to unwrap a tuple-like enum variant in Rust?

currently I am working on Image crate. This crate has a enum type called DynamicImage, As shown below:

#[non_exhaustive]
pub enum DynamicImage {
    ImageLuma8(GrayImage),
    ImageLumaA8(GrayAlphaImage),
    ImageRgb8(RgbImage),
    ImageRgba8(RgbaImage),
    ImageLuma16(ImageBuffer<Luma<u16>, Vec<u16>>),
    ImageLumaA16(ImageBuffer<LumaA<u16>, Vec<u16>>),
    ImageRgb16(ImageBuffer<Rgb<u16>, Vec<u16>>),
    ImageRgba16(ImageBuffer<Rgba<u16>, Vec<u16>>),
    ImageRgb32F(Rgb32FImage),
    ImageRgba32F(Rgba32FImage),
}

I want to implement a unwrap trait for it, so that I can use a unwrap function to access the field of enum variant, whose type is ImageBuffer.Just look like this:

let buffer = dynamic_image.unwrap()

Is there any way to achieve that? Any help would be appreciated.



Sources

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

Source: Stack Overflow

Solution Source