From f81bc3c95a0ee388fe91f1e97cb2efd0cc186840 Mon Sep 17 00:00:00 2001 From: Edhebi Date: Sat, 8 Jun 2019 17:21:01 +0200 Subject: [PATCH] added conversion from PixelFormatEnum to PixelFormat --- src/sdl2/pixels.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/sdl2/pixels.rs b/src/sdl2/pixels.rs index cc72869f..be1c3b34 100644 --- a/src/sdl2/pixels.rs +++ b/src/sdl2/pixels.rs @@ -5,6 +5,7 @@ use self::rand::distributions::{Distribution, Standard}; use libc::uint32_t; use num::FromPrimitive; use std::mem::transmute; +use std::convert::TryFrom; use crate::sys; use crate::get_error; @@ -460,6 +461,21 @@ impl FromPrimitive for PixelFormatEnum { fn from_u64(n: u64) -> Option { FromPrimitive::from_i64(n as i64) } } +impl TryFrom for PixelFormat { + type Error = String; + + fn try_from(pfe: PixelFormatEnum) -> Result { + unsafe { + let pf_ptr = sys::SDL_AllocFormat(pfe as u32); + if pf_ptr.is_null() { + Err(get_error()) + } else { + Ok(PixelFormat::from_ll(pf_ptr)) + } + } + } +} + // Just test a round-trip conversion from PixelFormat to // PixelFormatEnum and back.