chiark / gitweb /
before Cell::from_mut
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 5 Aug 2020 01:51:13 +0000 (02:51 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 5 Aug 2020 01:51:13 +0000 (02:51 +0100)
src/bin/otter.rs

index 628dcd8c5255a24cbeefa4551a1f1c78df145f4d..a09caf41e0b7a04701a9840c7c3719fb28107650 100644 (file)
@@ -1,5 +1,42 @@
 //
 
+use game::imports::*;
+use std::cell::Cell;
+
+pub struct CellRef<'a, T> {
+  origin: &'a mut T,
+  current: Cell<T>,
+}
+
+impl<'a,T> CellRef<'a,T> {
+  pub fn cloning(origin: &mut T) -> CellRef<T> where T: Clone {
+    let current = Cell::new(origin.clone());
+    CellRef { origin, current }
+  }
+  pub fn copying(origin: &mut T) -> CellRef<T> where T: Copy {
+    let current = Cell::new(*origin);
+    CellRef { origin, current }
+  }
+  pub fn defaulting(origin: &mut T) -> CellRef<T> where T: Default {
+    let current = Cell::new(mem::take(origin));
+    CellRef { origin, current }
+  }
+  pub fn with_placeholder(origin: &mut T, placeholder: T) -> CellRef<T> {
+    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<T>;
+  fn deref(&self) -> &Cell<T> { &self.current }
+}
+
 use structopt::StructOpt;
 
 #[derive(Debug,StructOpt)]