From 2523da3d0376b1bf8332bd2c6c1bf11a06eac7c8 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 5 Aug 2020 02:51:13 +0100 Subject: [PATCH] before Cell::from_mut --- src/bin/otter.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/bin/otter.rs b/src/bin/otter.rs index 628dcd8c..a09caf41 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -1,5 +1,42 @@ // +use game::imports::*; +use std::cell::Cell; + +pub struct CellRef<'a, T> { + origin: &'a mut T, + current: Cell, +} + +impl<'a,T> CellRef<'a,T> { + pub fn cloning(origin: &mut T) -> CellRef where T: Clone { + let current = Cell::new(origin.clone()); + CellRef { origin, current } + } + pub fn copying(origin: &mut T) -> CellRef where T: Copy { + let current = Cell::new(*origin); + CellRef { origin, current } + } + pub fn defaulting(origin: &mut T) -> CellRef where T: Default { + let current = Cell::new(mem::take(origin)); + CellRef { origin, current } + } + pub fn with_placeholder(origin: &mut T, placeholder: T) -> CellRef { + let current = Cell::new(mem::replace(origin, placeholder)); + CellRef { origin, current } + } +} + +impl<'a,T> Drop for CellRef<'a,T> { + fn drop(&mut self) { + mem::swap(self.current.get_mut(), self.origin); + } +} +impl<'a,T> Deref for CellRef<'a,T> { + type Target = Cell; + fn deref(&self) -> &Cell { &self.current } +} + use structopt::StructOpt; #[derive(Debug,StructOpt)] -- 2.30.2