🐛 Re-introduce UpdateContext::keep

main
Shad Amethyst 2 years ago
parent a7a4aa7d4c
commit 6fa4f5ce92
Signed by: amethyst
GPG Key ID: D970C8DD1D6DEE36

@ -1,5 +1,4 @@
use super::*; use super::*;
use std::ptr::NonNull;
use veccell::{VecRef, VecRefMut}; use veccell::{VecRef, VecRefMut};
// TODO: write VecCell, to make miri happy // 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(), 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" "Should add an entry in self.commit.updates if result is Some"
)] )]
pub(crate) fn new( pub(crate) fn new<'b>(
pane: &'a Pane, pane: &'b Pane,
position: (usize, usize), position: (usize, usize),
commit: &'a mut UpdateCommit, 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)?; let mut tile = pane.borrow_mut(position)?;
if tile.updated { if tile.updated {
return None; return None;
@ -105,8 +107,6 @@ impl<'a> UpdateContext<'a> {
commit, commit,
}; };
// SAFETY: ptr is a valid pointer
// SAFETY: aliasing is prevented by the invariants of UpdateContext
Some((res, tile)) 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(), 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" "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> { pub fn force_send(&mut self, position: (usize, usize), mut signal: Signal) -> Result<(), SendError> {
if !self.in_bounds(position) { if !self.in_bounds(position) {
return Err(SendError(signal)); 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.
// /// 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 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
// /// This function will [`std::mem::take`] the signal stored in `UpdateContext`, similar to [`take_signal`](UpdateContext::take_signal). /// clones of the signal before calling `keep`.
// /// 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.
// /// ///
// /// If `take_signal` or `keep` are called before this functions, then it will do nothing. /// # Example
// /// ///
// /// # Example /// ```
// /// /// # use stackline::prelude::*;
// /// ``` /// #[derive(Clone, Debug)]
// /// # use stackline::prelude::*; /// pub struct StorageTile {};
// /// #[derive(Clone, Debug)] ///
// /// pub struct StorageTile {}; /// impl Tile for StorageTile {
// /// /// fn update<'b>(&'b mut self, mut ctx: UpdateContext<'b>) {
// /// impl Tile for StorageTile { /// if ctx.signal().is_some() {
// /// fn update<'b>(&'b mut self, mut ctx: UpdateContext<'b>) { /// ctx.keep();
// /// if ctx.signal().is_some() { /// }
// /// ctx.keep(); /// // If we weren't to do this, then the signal would get dropped here
// /// } /// }
// /// // If we weren't to do this, then the signal would get dropped here /// }
// /// } /// ```
// /// } #[ensures(self.signal.is_none())]
// /// ``` pub fn keep(&mut self) {
// #[ensures(self.signal.is_none())] match std::mem::take(&mut self.signal) {
// pub fn keep(&mut self) { Some(signal) => {
// if self.signal.is_none() { self.commit.set_self_signal(Some(signal));
// 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));
// }
// }
} }
pub struct SendError(pub Signal); pub struct SendError(pub Signal);
@ -431,6 +424,8 @@ pub(crate) struct UpdateCommit {
states: Vec<(usize, usize, State)>, states: Vec<(usize, usize, State)>,
signals: Vec<(usize, usize, Option<Signal>)>, signals: Vec<(usize, usize, Option<Signal>)>,
updates: Vec<(usize, usize)>, updates: Vec<(usize, usize)>,
self_signal: Option<Signal>,
} }
impl UpdateCommit { impl UpdateCommit {
@ -439,6 +434,8 @@ impl UpdateCommit {
states: Vec::new(), states: Vec::new(),
signals: Vec::new(), signals: Vec::new(),
updates: Vec::new(), updates: Vec::new(),
self_signal: None,
} }
} }
@ -450,6 +447,10 @@ impl UpdateCommit {
self.states.push((pos.0, pos.1, state)); self.states.push((pos.0, pos.1, state));
} }
fn set_self_signal(&mut self, signal: Option<Signal>) {
self.self_signal = signal;
}
pub(crate) fn apply(self, pane: &mut Pane) { pub(crate) fn apply(self, pane: &mut Pane) {
for (x, y) in self.updates { for (x, y) in self.updates {
if let Some(tile) = pane.get_mut((x, y)) { 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 => {}
}
}
} }

@ -257,6 +257,7 @@ impl Pane {
#[inline] #[inline]
#[ensures(ret.is_some() -> self.in_bounds(position) && (*self.get(position).unwrap()).get().is_some())] #[ensures(ret.is_some() -> self.in_bounds(position) && (*self.get(position).unwrap()).get().is_some())]
#[ensures(!self.in_bounds(position) -> ret.is_none())] #[ensures(!self.in_bounds(position) -> ret.is_none())]
#[allow(unused_mut)]
pub fn set_signal(&mut self, position: (usize, usize), mut signal: Signal) -> Option<()> { pub fn set_signal(&mut self, position: (usize, usize), mut signal: Signal) -> Option<()> {
signal.set_position(position); signal.set_position(position);
if let Some(tile) = self.get_mut(position) { if let Some(tile) = self.get_mut(position) {
@ -296,6 +297,8 @@ impl Pane {
(*tile).get_mut()?.update(ctx); (*tile).get_mut()?.update(ctx);
commit.apply_immediate(&mut *tile);
Some(()) Some(())
} }

@ -1,7 +1,7 @@
//! Wires and diodes //! Wires and diodes
use crate::prelude::*; use crate::prelude::*;
use crate::tile::prelude::*; // use crate::tile::prelude::*;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Wire(Orientation); pub struct Wire(Orientation);

Loading…
Cancel
Save