diff --git a/src/sdl2/render.rs b/src/sdl2/render.rs index 6090dc2c..e88c90cf 100644 --- a/src/sdl2/render.rs +++ b/src/sdl2/render.rs @@ -1095,10 +1095,9 @@ impl Canvas { /// Sets the drawing area for rendering on the current target. #[doc(alias = "SDL_RenderSetViewport")] pub fn set_viewport>>(&mut self, rect: R) { - let ptr = match rect.into() { - Some(ref rect) => rect.raw(), - None => ptr::null(), - }; + let rect = rect.into(); + // as_ref is important because we need rect to live until the end of the FFI call, but map_or consumes an Option + let ptr = rect.as_ref().map_or(ptr::null(), |rect| rect.raw()); let ret = unsafe { sys::SDL_RenderSetViewport(self.context.raw, ptr) }; if ret != 0 { panic!("Could not set viewport: {}", get_error()) @@ -1119,15 +1118,10 @@ impl Canvas { /// If the rectangle is `None`, clipping will be disabled. #[doc(alias = "SDL_RenderSetClipRect")] pub fn set_clip_rect>>(&mut self, rect: R) { - let ret = unsafe { - sys::SDL_RenderSetClipRect( - self.context.raw, - match rect.into() { - Some(ref rect) => rect.raw(), - None => ptr::null(), - }, - ) - }; + let rect = rect.into(); + // as_ref is important because we need rect to live until the end of the FFI call, but map_or consumes an Option + let ptr = rect.as_ref().map_or(ptr::null(), |rect| rect.raw()); + let ret = unsafe { sys::SDL_RenderSetClipRect(self.context.raw, ptr) }; if ret != 0 { panic!("Could not set clip rect: {}", get_error()) }