rhai/src/restore.rs

109 lines
3.5 KiB
Rust
Raw Normal View History

2022-11-08 04:52:46 +01:00
//! Facility to run state restoration logic at the end of scope.
use std::ops::{Deref, DerefMut};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
2022-12-04 07:06:54 +01:00
/// Automatically restore state at the end of the scope.
macro_rules! auto_restore {
(let $temp:ident = $var:ident . $prop:ident; $code:stmt) => {
auto_restore!(let $temp = $var.$prop; $code => move |v| v.$prop = $temp);
};
(let $temp:ident = $var:ident . $prop:ident; $code:stmt => $restore:expr) => {
let $temp = $var.$prop;
$code
auto_restore!($var => $restore);
};
2022-12-04 15:47:10 +01:00
($var:ident => $restore:ident; let $temp:ident = $save:expr;) => {
auto_restore!($var => $restore; let $temp = $save; {});
};
($var:ident if $guard:expr => $restore:ident; let $temp:ident = $save:expr;) => {
auto_restore!($var if $guard => $restore; let $temp = $save; {});
};
($var:ident => $restore:ident; let $temp:ident = $save:expr; $code:stmt) => {
let $temp = $save;
$code
auto_restore!($var => move |v| { v.$restore($temp); });
};
($var:ident if $guard:expr => $restore:ident; let $temp:ident = $save:expr; $code:stmt) => {
let $temp = $save;
$code
auto_restore!($var if $guard => move |v| { v.$restore($temp); });
};
($var:ident => $restore:expr) => {
2022-12-04 07:06:54 +01:00
auto_restore!($var = $var => $restore);
};
2022-12-04 15:47:10 +01:00
($var:ident = $value:expr => $restore:expr) => {
2022-12-11 14:50:47 +01:00
let $var = &mut *crate::RestoreOnDrop::lock($value, $restore);
2022-12-04 07:06:54 +01:00
};
2022-12-10 15:37:13 +01:00
($var:ident if Some($guard:ident) => $restore:expr) => {
auto_restore!($var = ($var) if Some($guard) => $restore);
};
($var:ident = ( $value:expr ) if Some($guard:ident) => $restore:expr) => {
let mut __rx__;
let $var = if let Some($guard) = $guard {
2022-12-11 14:50:47 +01:00
__rx__ = crate::RestoreOnDrop::lock($value, $restore);
2022-12-10 15:37:13 +01:00
&mut *__rx__
} else {
&mut *$value
};
};
2022-12-04 15:47:10 +01:00
($var:ident if $guard:expr => $restore:expr) => {
auto_restore!($var = ($var) if $guard => $restore);
2022-12-04 07:06:54 +01:00
};
2022-12-04 15:47:10 +01:00
($var:ident = ( $value:expr ) if $guard:expr => $restore:expr) => {
2022-12-04 07:06:54 +01:00
let mut __rx__;
let $var = if $guard {
2022-12-11 14:50:47 +01:00
__rx__ = crate::RestoreOnDrop::lock($value, $restore);
2022-12-04 07:06:54 +01:00
&mut *__rx__
} else {
&mut *$value
};
};
}
2022-11-08 04:52:46 +01:00
/// Run custom restoration logic upon the end of scope.
#[must_use]
2022-11-23 06:24:14 +01:00
pub struct RestoreOnDrop<'a, T: ?Sized, R: FnOnce(&mut T)> {
2022-11-08 04:52:46 +01:00
value: &'a mut T,
restore: Option<R>,
}
2022-11-23 06:24:14 +01:00
impl<'a, T: ?Sized, R: FnOnce(&mut T)> RestoreOnDrop<'a, T, R> {
2022-11-08 08:01:40 +01:00
/// Create a new [`RestoreOnDrop`] that locks a mutable reference and runs restoration logic at
/// the end of scope.
///
/// Beware that the end of scope means the end of its lifetime, not necessarily waiting until
/// the current block scope is exited.
2022-11-08 04:52:46 +01:00
#[inline(always)]
2022-11-08 08:01:40 +01:00
pub fn lock(value: &'a mut T, restore: R) -> Self {
2022-11-08 04:52:46 +01:00
Self {
value,
restore: Some(restore),
}
}
}
2022-11-23 06:24:14 +01:00
impl<'a, T: ?Sized, R: FnOnce(&mut T)> Drop for RestoreOnDrop<'a, T, R> {
2022-11-08 04:52:46 +01:00
#[inline(always)]
fn drop(&mut self) {
2022-12-04 07:06:54 +01:00
self.restore.take().unwrap()(self.value);
2022-11-08 04:52:46 +01:00
}
}
2022-11-23 06:24:14 +01:00
impl<'a, T: ?Sized, R: FnOnce(&mut T)> Deref for RestoreOnDrop<'a, T, R> {
2022-11-08 04:52:46 +01:00
type Target = T;
#[inline(always)]
fn deref(&self) -> &Self::Target {
self.value
}
}
2022-11-23 06:24:14 +01:00
impl<'a, T: ?Sized, R: FnOnce(&mut T)> DerefMut for RestoreOnDrop<'a, T, R> {
2022-11-08 04:52:46 +01:00
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
self.value
}
}