|
|
use std::{ |
|
|
fmt::Debug, |
|
|
mem::take, |
|
|
ops::{Deref, DerefMut}, |
|
|
}; |
|
|
|
|
|
use auto_hash_map::AutoSet; |
|
|
use parking_lot::{Mutex, MutexGuard}; |
|
|
use serde::{Deserialize, Serialize}; |
|
|
|
|
|
use crate::{ |
|
|
Invalidator, OperationValue, SerializationInvalidator, get_invalidator, mark_session_dependent, |
|
|
mark_stateful, trace::TraceRawVcs, |
|
|
}; |
|
|
|
|
|
#[derive(Serialize, Deserialize)] |
|
|
struct StateInner<T> { |
|
|
value: T, |
|
|
invalidators: AutoSet<Invalidator>, |
|
|
} |
|
|
|
|
|
impl<T> StateInner<T> { |
|
|
pub fn new(value: T) -> Self { |
|
|
Self { |
|
|
value, |
|
|
invalidators: AutoSet::new(), |
|
|
} |
|
|
} |
|
|
|
|
|
pub fn add_invalidator(&mut self, invalidator: Invalidator) { |
|
|
self.invalidators.insert(invalidator); |
|
|
} |
|
|
|
|
|
pub fn set_unconditionally(&mut self, value: T) { |
|
|
self.value = value; |
|
|
for invalidator in take(&mut self.invalidators) { |
|
|
invalidator.invalidate(); |
|
|
} |
|
|
} |
|
|
|
|
|
pub fn update_conditionally(&mut self, update: impl FnOnce(&mut T) -> bool) -> bool { |
|
|
if !update(&mut self.value) { |
|
|
return false; |
|
|
} |
|
|
for invalidator in take(&mut self.invalidators) { |
|
|
invalidator.invalidate(); |
|
|
} |
|
|
true |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T: PartialEq> StateInner<T> { |
|
|
pub fn set(&mut self, value: T) -> bool { |
|
|
if self.value == value { |
|
|
return false; |
|
|
} |
|
|
self.value = value; |
|
|
for invalidator in take(&mut self.invalidators) { |
|
|
invalidator.invalidate(); |
|
|
} |
|
|
true |
|
|
} |
|
|
} |
|
|
|
|
|
pub struct StateRef<'a, T> { |
|
|
serialization_invalidator: Option<&'a SerializationInvalidator>, |
|
|
inner: MutexGuard<'a, StateInner<T>>, |
|
|
mutated: bool, |
|
|
} |
|
|
|
|
|
impl<T> Deref for StateRef<'_, T> { |
|
|
type Target = T; |
|
|
|
|
|
fn deref(&self) -> &Self::Target { |
|
|
&self.inner.value |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T> DerefMut for StateRef<'_, T> { |
|
|
fn deref_mut(&mut self) -> &mut Self::Target { |
|
|
self.mutated = true; |
|
|
&mut self.inner.value |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T> Drop for StateRef<'_, T> { |
|
|
fn drop(&mut self) { |
|
|
if self.mutated { |
|
|
for invalidator in take(&mut self.inner.invalidators) { |
|
|
invalidator.invalidate(); |
|
|
} |
|
|
if let Some(serialization_invalidator) = self.serialization_invalidator { |
|
|
serialization_invalidator.invalidate(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)] |
|
|
pub struct State<T> { |
|
|
serialization_invalidator: SerializationInvalidator, |
|
|
inner: Mutex<StateInner<T>>, |
|
|
} |
|
|
|
|
|
impl<T: Debug> Debug for State<T> { |
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
|
|
f.debug_struct("State") |
|
|
.field("value", &self.inner.lock().value) |
|
|
.finish() |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T: TraceRawVcs> TraceRawVcs for State<T> { |
|
|
fn trace_raw_vcs(&self, trace_context: &mut crate::trace::TraceRawVcsContext) { |
|
|
self.inner.lock().value.trace_raw_vcs(trace_context); |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T: Default + OperationValue> Default for State<T> { |
|
|
fn default() -> Self { |
|
|
|
|
|
Self::new(Default::default()) |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T> PartialEq for State<T> { |
|
|
fn eq(&self, _other: &Self) -> bool { |
|
|
false |
|
|
} |
|
|
} |
|
|
impl<T> Eq for State<T> {} |
|
|
|
|
|
impl<T> State<T> { |
|
|
pub fn new(value: T) -> Self |
|
|
where |
|
|
T: OperationValue, |
|
|
{ |
|
|
Self { |
|
|
serialization_invalidator: mark_stateful(), |
|
|
inner: Mutex::new(StateInner::new(value)), |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get(&self) -> StateRef<'_, T> { |
|
|
let invalidator = get_invalidator(); |
|
|
let mut inner = self.inner.lock(); |
|
|
inner.add_invalidator(invalidator); |
|
|
StateRef { |
|
|
serialization_invalidator: Some(&self.serialization_invalidator), |
|
|
inner, |
|
|
mutated: false, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub fn get_untracked(&self) -> StateRef<'_, T> { |
|
|
let inner = self.inner.lock(); |
|
|
StateRef { |
|
|
serialization_invalidator: Some(&self.serialization_invalidator), |
|
|
inner, |
|
|
mutated: false, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn set_unconditionally(&self, value: T) { |
|
|
{ |
|
|
let mut inner = self.inner.lock(); |
|
|
inner.set_unconditionally(value); |
|
|
} |
|
|
self.serialization_invalidator.invalidate(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn update_conditionally(&self, update: impl FnOnce(&mut T) -> bool) { |
|
|
{ |
|
|
let mut inner = self.inner.lock(); |
|
|
if !inner.update_conditionally(update) { |
|
|
return; |
|
|
} |
|
|
} |
|
|
self.serialization_invalidator.invalidate(); |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T: PartialEq> State<T> { |
|
|
|
|
|
|
|
|
pub fn set(&self, value: T) { |
|
|
{ |
|
|
let mut inner = self.inner.lock(); |
|
|
if !inner.set(value) { |
|
|
return; |
|
|
} |
|
|
} |
|
|
self.serialization_invalidator.invalidate(); |
|
|
} |
|
|
} |
|
|
|
|
|
pub struct TransientState<T> { |
|
|
inner: Mutex<StateInner<Option<T>>>, |
|
|
} |
|
|
|
|
|
impl<T> Serialize for TransientState<T> { |
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
|
|
where |
|
|
S: serde::Serializer, |
|
|
{ |
|
|
Serialize::serialize(&(), serializer) |
|
|
} |
|
|
} |
|
|
|
|
|
impl<'de, T> Deserialize<'de> for TransientState<T> { |
|
|
fn deserialize<D>(deserializer: D) -> Result<TransientState<T>, D::Error> |
|
|
where |
|
|
D: serde::Deserializer<'de>, |
|
|
{ |
|
|
let () = Deserialize::deserialize(deserializer)?; |
|
|
Ok(TransientState { |
|
|
inner: Mutex::new(StateInner::new(Default::default())), |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T: Debug> Debug for TransientState<T> { |
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
|
|
f.debug_struct("TransientState") |
|
|
.field("value", &self.inner.lock().value) |
|
|
.finish() |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T: TraceRawVcs> TraceRawVcs for TransientState<T> { |
|
|
fn trace_raw_vcs(&self, trace_context: &mut crate::trace::TraceRawVcsContext) { |
|
|
self.inner.lock().value.trace_raw_vcs(trace_context); |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T> Default for TransientState<T> { |
|
|
fn default() -> Self { |
|
|
|
|
|
Self::new() |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T> PartialEq for TransientState<T> { |
|
|
fn eq(&self, _other: &Self) -> bool { |
|
|
false |
|
|
} |
|
|
} |
|
|
impl<T> Eq for TransientState<T> {} |
|
|
|
|
|
impl<T> TransientState<T> { |
|
|
pub fn new() -> Self { |
|
|
mark_stateful(); |
|
|
Self { |
|
|
inner: Mutex::new(StateInner::new(None)), |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get(&self) -> StateRef<'_, Option<T>> { |
|
|
mark_session_dependent(); |
|
|
let invalidator = get_invalidator(); |
|
|
let mut inner = self.inner.lock(); |
|
|
inner.add_invalidator(invalidator); |
|
|
StateRef { |
|
|
serialization_invalidator: None, |
|
|
inner, |
|
|
mutated: false, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub fn get_untracked(&self) -> StateRef<'_, Option<T>> { |
|
|
let inner = self.inner.lock(); |
|
|
StateRef { |
|
|
serialization_invalidator: None, |
|
|
inner, |
|
|
mutated: false, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn set_unconditionally(&self, value: T) { |
|
|
let mut inner = self.inner.lock(); |
|
|
inner.set_unconditionally(Some(value)); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn unset_unconditionally(&self) { |
|
|
let mut inner = self.inner.lock(); |
|
|
inner.set_unconditionally(None); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn update_conditionally(&self, update: impl FnOnce(&mut Option<T>) -> bool) { |
|
|
let mut inner = self.inner.lock(); |
|
|
inner.update_conditionally(update); |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T: PartialEq> TransientState<T> { |
|
|
|
|
|
|
|
|
pub fn set(&self, value: T) { |
|
|
let mut inner = self.inner.lock(); |
|
|
inner.set(Some(value)); |
|
|
} |
|
|
|
|
|
|
|
|
pub fn unset(&self) { |
|
|
let mut inner = self.inner.lock(); |
|
|
inner.set(None); |
|
|
} |
|
|
} |
|
|
|