make AutoCanvasRestore usable by directly imitating a canvas
@@ -896,32 +896,45 @@ impl QuickReject<Path> for Canvas {
}
}
-pub struct AutoCanvasRestore<'a>(SkAutoCanvasRestore, PhantomData<&'a ()>);
+/// A reference to a Canvas that restores the Canvas's state when
+/// it's being dropped.
+pub struct AutoRestoredCanvas<'a> {
+ canvas: &'a mut Canvas,
+ restore: SkAutoCanvasRestore,
+}
+
+impl<'a> Deref for AutoRestoredCanvas<'a> {
+ type Target = Canvas;
+ fn deref(&self) -> &Self::Target {
+ self.canvas
+ }
+}
-impl<'a> NativeAccess<SkAutoCanvasRestore> for AutoCanvasRestore<'a> {
+impl<'a> DerefMut for AutoRestoredCanvas<'a> {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ self.canvas
+ }
+}
+
+impl<'a> NativeAccess<SkAutoCanvasRestore> for AutoRestoredCanvas<'a> {
fn native(&self) -> &SkAutoCanvasRestore {
- &self.0
+ &self.restore
}
+
fn native_mut(&mut self) -> &mut SkAutoCanvasRestore {
- &mut self.0
+ &mut self.restore
}
}
-impl<'a> Drop for AutoCanvasRestore<'a> {
+impl<'a> Drop for AutoRestoredCanvas<'a> {
fn drop(&mut self) {
unsafe {
- C_SkAutoCanvasRestore_destruct(&self.0)
+ C_SkAutoCanvasRestore_destruct(self.native_mut())
}
}
}
-impl<'a> AutoCanvasRestore<'a> {
- // TODO: test, scary looking lifetime requirements.
- pub fn guard(canvas: &mut Canvas, do_save: bool) -> AutoCanvasRestore<'_> {
- AutoCanvasRestore(unsafe {
- SkAutoCanvasRestore::new(canvas.native_mut(), do_save)
- }, PhantomData)
- }
+impl<'a> AutoRestoredCanvas<'a> {
pub fn restore(&mut self) {
unsafe {
@@ -932,6 +945,22 @@ impl<'a> AutoCanvasRestore<'a> {
}
}
+pub enum AutoCanvasRestore {}
+
+impl AutoCanvasRestore {
+
+ pub fn guard(canvas: &mut Canvas, do_save: bool) -> AutoRestoredCanvas {
+ let restore = unsafe {
+ SkAutoCanvasRestore::new(canvas.native_mut(), do_save)
+ };
+
+ AutoRestoredCanvas {
+ canvas,
+ restore
+ }
+ }
+}
+
#[cfg(test)]
mod tests {
use crate::skia::*;
