From: Ian Jackson Date: Wed, 5 Aug 2020 01:51:13 +0000 (+0100) Subject: before Cell::from_mut X-Git-Tag: otter-0.2.0~1180 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=2523da3d0376b1bf8332bd2c6c1bf11a06eac7c8;p=otter.git before Cell::from_mut --- 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)]