From 6fa4f5ce9226dbfa71c80dbc69ab67ba4265d59b Mon Sep 17 00:00:00 2001 From: Adrien Burgun Date: Mon, 4 Jul 2022 11:13:23 +0200 Subject: [PATCH] :bug: Re-introduce UpdateContext::keep --- stackline/src/context.rs | 114 +++++++++++++++++++++------------------ stackline/src/pane.rs | 3 ++ stackline/tiles/wire.rs | 2 +- 3 files changed, 67 insertions(+), 52 deletions(-) diff --git a/stackline/src/context.rs b/stackline/src/context.rs index 649bee9..1630c08 100644 --- a/stackline/src/context.rs +++ b/stackline/src/context.rs @@ -1,5 +1,4 @@ use super::*; -use std::ptr::NonNull; use veccell::{VecRef, VecRefMut}; // TODO: write VecCell, to make miri happy @@ -85,11 +84,14 @@ impl<'a> UpdateContext<'a> { ret.is_some() -> ret.as_ref().unwrap().0.commit.updates.iter().find(|&&x| x == position).is_some(), "Should add an entry in self.commit.updates if result is Some" )] - pub(crate) fn new( - pane: &'a Pane, + pub(crate) fn new<'b>( + pane: &'b Pane, position: (usize, usize), commit: &'a mut UpdateCommit, - ) -> Option<(UpdateContext<'a>, VecRefMut<'a, FullTile>)> { + ) -> Option<(UpdateContext<'a>, VecRefMut<'b, FullTile>)> + where + 'b: 'a, // 'b ⊇ 'a + { let mut tile = pane.borrow_mut(position)?; if tile.updated { return None; @@ -105,8 +107,6 @@ impl<'a> UpdateContext<'a> { commit, }; - // SAFETY: ptr is a valid pointer - // SAFETY: aliasing is prevented by the invariants of UpdateContext Some((res, tile)) } @@ -324,6 +324,7 @@ impl<'a> UpdateContext<'a> { ret.is_ok() -> self.commit.signals.iter().find(|(x, y, _)| position == (*x, *y)).is_some(), "Should add an entry in self.commit.signals if result is Some" )] + #[allow(unused_mut)] pub fn force_send(&mut self, position: (usize, usize), mut signal: Signal) -> Result<(), SendError> { if !self.in_bounds(position) { return Err(SendError(signal)); @@ -362,51 +363,43 @@ impl<'a> UpdateContext<'a> { } } - // TODO: re-implement through UpdateCommit - - // /// Stores the current signal back in the current tile, guaranteeing that it will stay there for - // /// this update cycle. See [`take_signal`](UpdateContext::take_signal) for more information. - // /// - // /// This method differs from [`send`](UpdateContext::send), as it takes action immediately. - // /// The signal may also not be modified, as it would otherwise break the guarantees of [`Pane::step`]. - // /// - // /// This function will [`std::mem::take`] the signal stored in `UpdateContext`, similar to [`take_signal`](UpdateContext::take_signal). - // /// If you wish to modify or send copies of the signal, then you will need to call [`signal`](UpdateContext::signal) beforehand and make - // /// clones of the signal before calling `keep`. - // /// - // /// If `take_signal` or `keep` are called before this functions, then it will do nothing. - // /// - // /// # Example - // /// - // /// ``` - // /// # use stackline::prelude::*; - // /// #[derive(Clone, Debug)] - // /// pub struct StorageTile {}; - // /// - // /// impl Tile for StorageTile { - // /// fn update<'b>(&'b mut self, mut ctx: UpdateContext<'b>) { - // /// if ctx.signal().is_some() { - // /// ctx.keep(); - // /// } - // /// // If we weren't to do this, then the signal would get dropped here - // /// } - // /// } - // /// ``` - // #[ensures(self.signal.is_none())] - // pub fn keep(&mut self) { - // if self.signal.is_none() { - // return; - // } - - // unsafe { - // // SAFETY: we only access self.pane[self.position].signal, not self.pane[self.position].cell - // self.pane - // .as_mut() - // .get_mut(self.position) - // .unwrap_or_else(|| unreachable!()) - // .set_signal(std::mem::take(&mut self.signal)); - // } - // } + /// Stores the current signal back in the current tile, guaranteeing that it will stay there for + /// this update cycle. See [`take_signal`](UpdateContext::take_signal) for more information. + /// + /// This method differs from [`send`](UpdateContext::send), as it takes action immediately. + /// The signal may also not be modified, as it would otherwise break the guarantees of [`Pane::step`]. + /// + /// This function will [`std::mem::take`] the signal stored in `UpdateContext`, similar to [`take_signal`](UpdateContext::take_signal). + /// If you wish to modify or send copies of the signal, then you will need to call [`signal`](UpdateContext::signal) beforehand and make + /// clones of the signal before calling `keep`. + /// + /// If `take_signal` or `keep` are called before this functions, then it will do nothing. + /// + /// # Example + /// + /// ``` + /// # use stackline::prelude::*; + /// #[derive(Clone, Debug)] + /// pub struct StorageTile {}; + /// + /// impl Tile for StorageTile { + /// fn update<'b>(&'b mut self, mut ctx: UpdateContext<'b>) { + /// if ctx.signal().is_some() { + /// ctx.keep(); + /// } + /// // If we weren't to do this, then the signal would get dropped here + /// } + /// } + /// ``` + #[ensures(self.signal.is_none())] + pub fn keep(&mut self) { + match std::mem::take(&mut self.signal) { + Some(signal) => { + self.commit.set_self_signal(Some(signal)); + }, + _ => {} + } + } } pub struct SendError(pub Signal); @@ -431,6 +424,8 @@ pub(crate) struct UpdateCommit { states: Vec<(usize, usize, State)>, signals: Vec<(usize, usize, Option)>, updates: Vec<(usize, usize)>, + + self_signal: Option, } impl UpdateCommit { @@ -439,6 +434,8 @@ impl UpdateCommit { states: Vec::new(), signals: Vec::new(), updates: Vec::new(), + + self_signal: None, } } @@ -450,6 +447,10 @@ impl UpdateCommit { self.states.push((pos.0, pos.1, state)); } + fn set_self_signal(&mut self, signal: Option) { + self.self_signal = signal; + } + pub(crate) fn apply(self, pane: &mut Pane) { for (x, y) in self.updates { if let Some(tile) = pane.get_mut((x, y)) { @@ -478,4 +479,15 @@ impl UpdateCommit { } } } + + /// Applies transformations on a FullTile before the end of the update phase + #[inline] + pub(crate) fn apply_immediate(&mut self, tile: &mut FullTile) { + match std::mem::take(&mut self.self_signal) { + Some(signal) => { + tile.set_signal(Some(signal)); + } + None => {} + } + } } diff --git a/stackline/src/pane.rs b/stackline/src/pane.rs index 8549f91..ca04b0b 100644 --- a/stackline/src/pane.rs +++ b/stackline/src/pane.rs @@ -257,6 +257,7 @@ impl Pane { #[inline] #[ensures(ret.is_some() -> self.in_bounds(position) && (*self.get(position).unwrap()).get().is_some())] #[ensures(!self.in_bounds(position) -> ret.is_none())] + #[allow(unused_mut)] pub fn set_signal(&mut self, position: (usize, usize), mut signal: Signal) -> Option<()> { signal.set_position(position); if let Some(tile) = self.get_mut(position) { @@ -296,6 +297,8 @@ impl Pane { (*tile).get_mut()?.update(ctx); + commit.apply_immediate(&mut *tile); + Some(()) } diff --git a/stackline/tiles/wire.rs b/stackline/tiles/wire.rs index 57db725..faaa86a 100644 --- a/stackline/tiles/wire.rs +++ b/stackline/tiles/wire.rs @@ -1,7 +1,7 @@ //! Wires and diodes use crate::prelude::*; -use crate::tile::prelude::*; +// use crate::tile::prelude::*; #[derive(Clone, Debug)] pub struct Wire(Orientation);