From 1cc5253adfa8dc2453ab542567c9063846110878 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Fri, 8 Mar 2019 11:15:43 +0100 Subject: [PATCH] Remove Rc usage from the crate Callers should use Rc wherever they want, so that adapters such as ErrorCheckingGl can do their job without multiple layers of virtual calls. --- src/gl.rs | 44 +++++++++++++++++++++++++++++--------------- src/gl_fns.rs | 6 +++--- src/gles_fns.rs | 6 +++--- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/gl.rs b/src/gl.rs index 247d9dc..d929dbc 100644 --- a/src/gl.rs +++ b/src/gl.rs @@ -13,7 +13,6 @@ use std::mem; use std::mem::size_of; use std::os::raw::{c_char, c_int, c_void}; use std::ptr; -use std::rc::Rc; use std::str; pub use ffi::types::*; @@ -80,7 +79,10 @@ macro_rules! declare_gl_apis { $($(unsafe $($garbo)*)* fn $name(&self $(, $arg:$t)*) $(-> $retty)* ;)+ } - impl Gl for ErrorCheckingGl { + impl Gl for ErrorCheckingGl + where + T: Gl, + { $($(unsafe $($garbo)*)* fn $name(&self $(, $arg:$t)*) $(-> $retty)* { let rv = self.gl.$name($($arg,)*); assert_eq!(self.gl.get_error(), 0); @@ -88,12 +90,16 @@ macro_rules! declare_gl_apis { })+ } - impl Gl for ErrorReactingGl { + impl Gl for ErrorReactingGl + where + T: Gl, + F: Fn(&Gl, &str, GLenum), + { $($(unsafe $($garbo)*)* fn $name(&self $(, $arg:$t)*) $(-> $retty)* { let rv = self.gl.$name($($arg,)*); let error = self.gl.get_error(); if error != 0 { - (self.callback)(&*self.gl, stringify!($name), error); + (self.callback)(&self.gl, stringify!($name), error); } rv })+ @@ -561,26 +567,34 @@ declare_gl_apis! { fn get_debug_messages(&self) -> Vec; } -//#[deprecated(since = "0.6.11", note = "use ErrorReactingGl instead")] -pub struct ErrorCheckingGl { - gl: Rc, +pub struct ErrorCheckingGl { + gl: T, } -impl ErrorCheckingGl { - pub fn wrap(fns: Rc) -> Rc { - Rc::new(ErrorCheckingGl { gl: fns }) as Rc +impl ErrorCheckingGl +where + T: Gl, +{ + #[inline] + pub fn wrap(gl: T) -> Self { + Self { gl } } } /// A wrapper around GL context that calls a specified callback on each GL error. -pub struct ErrorReactingGl { - gl: Rc, +pub struct ErrorReactingGl { + gl: T, callback: F, } -impl ErrorReactingGl { - pub fn wrap(fns: Rc, callback: F) -> Rc { - Rc::new(ErrorReactingGl { gl: fns, callback }) as Rc +impl ErrorReactingGl +where + T: Gl, + F: 'static + Fn(&Gl, &str, GLenum), +{ + #[inline] + pub fn wrap(gl: T, callback: F) -> Self { + ErrorReactingGl { gl, callback } } } diff --git a/src/gl_fns.rs b/src/gl_fns.rs index 4d2b4cb..b7aa01e 100644 --- a/src/gl_fns.rs +++ b/src/gl_fns.rs @@ -12,12 +12,12 @@ pub struct GlFns { } impl GlFns { - pub unsafe fn load_with<'a, F>(loadfn: F) -> Rc + #[inline] + pub unsafe fn load_with<'a, F>(loadfn: F) -> Self where F: FnMut(&str) -> *const c_void, { - let ffi_gl_ = GlFfi::load_with(loadfn); - Rc::new(GlFns { ffi_gl_: ffi_gl_ }) as Rc + Self { ffi_gl_: GlFfi::load_with(loadfn) } } } diff --git a/src/gles_fns.rs b/src/gles_fns.rs index 135aa5b..e3cbf55 100644 --- a/src/gles_fns.rs +++ b/src/gles_fns.rs @@ -12,12 +12,12 @@ pub struct GlesFns { } impl GlesFns { - pub unsafe fn load_with<'a, F>(loadfn: F) -> Rc + #[inline] + pub unsafe fn load_with<'a, F>(loadfn: F) -> Self where F: FnMut(&str) -> *const c_void, { - let ffi_gl_ = GlesFfi::load_with(loadfn); - Rc::new(GlesFns { ffi_gl_: ffi_gl_ }) as Rc + Self { ffi_gl_: GlesFfi::load_with(loadfn) } } }