1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//! Decorators that react to links which are still referenced by other links.
//!
//! Ports of C# `LinksUsagesValidator` and `LinksCascadeUsagesResolver`.
use std::{
fmt::{self, Debug, Formatter},
marker::PhantomData,
};
use data::{Flow, LinkReference, LinksConstants};
use super::{
helpers,
macros::{decorator_struct, forward},
};
use crate::{
data::{ReadHandler, WriteHandler},
Doublets, Error, Fuse, Link, Links,
};
decorator_struct! {
/// Rejects updates and deletions of a link that other links still reference.
///
/// Port of C# `LinksUsagesValidator`. The operation fails with [`Error::HasUsages`],
/// the analogue of C#'s `ArgumentLinkHasDependenciesException`.
UsagesValidator
}
impl<T: LinkReference, L: Doublets<T>> Links<T> for UsagesValidator<T, L> {
forward!(count_links, each_links, create_links);
#[inline]
fn update_links(
&mut self,
query: &[T],
change: &[T],
handler: WriteHandler<'_, T>,
) -> Result<Flow, Error<T>> {
let index = helpers::part(query, self.links.constants().index_part);
helpers::ensure_no_usages(&self.links, index)?;
self.links.update_links(query, change, handler)
}
#[inline]
fn delete_links(
&mut self,
query: &[T],
handler: WriteHandler<'_, T>,
) -> Result<Flow, Error<T>> {
let index = helpers::part(query, self.links.constants().index_part);
helpers::ensure_no_usages(&self.links, index)?;
self.links.delete_links(query, handler)
}
}
decorator_struct! {
/// Deletes everything that references a link before deleting the link itself.
///
/// Port of C# `LinksCascadeUsagesResolver` together with
/// `ILinksExtensions.DeleteAllUsages`.
///
/// # Deviation from C\#
///
/// C# recurses through the facade and keeps no record of what it has already visited,
/// so a reference cycle (`a` → `b` → `a`) makes it recurse until the stack overflows.
/// This port threads a visited set through the cascade, so cyclic graphs terminate.
/// The price is that the nested deletions run from this layer inwards rather than from
/// the top of the stack. Each usage is explicitly reset to `(null, null)` before
/// recursing, preserving the reset callbacks of the automatic resolution stack.
/// Place this decorator above any other layers whose behaviour the cascade must observe.
/// The initial reset of the requested link is left to
/// [`NonNullContentsLinkDeletionResolver`](super::NonNullContentsLinkDeletionResolver).
CascadeUsagesResolver
}
impl<T: LinkReference, L: Doublets<T>> CascadeUsagesResolver<T, L> {
/// Port of C# `ILinksExtensions.DeleteAllUsages` followed by the inner deletion.
fn cascade(
&mut self,
index: T,
visited: &mut Vec<T>,
handler: &mut dyn FnMut(Link<T>, Link<T>) -> Flow,
) -> Result<Flow, Error<T>> {
if visited.contains(&index) {
return Ok(Flow::Continue);
}
visited.push(index);
for usage in helpers::usages_of(self, index) {
if usage.index == index || !self.links.exist(usage.index) {
continue;
}
// C# re-enters the facade's non-null contents deletion resolver here.
// Static recursion bypasses that outer layer, so perform its reset explicitly.
helpers::enforce_reset_values(&mut self.links, usage.index, handler)?;
self.cascade(usage.index, visited, handler)?;
}
self.links.delete_links(&[index], handler)
}
}
impl<T: LinkReference, L: Doublets<T>> Links<T> for CascadeUsagesResolver<T, L> {
forward!(count_links, each_links, create_links, update_links);
#[inline]
fn delete_links(
&mut self,
query: &[T],
handler: WriteHandler<'_, T>,
) -> Result<Flow, Error<T>> {
let index = helpers::part(query, self.links.constants().index_part);
let mut fuse = Fuse::new(&mut *handler);
let mut visited = Vec::new();
self.cascade(index, &mut visited, &mut |before, after| {
fuse.call(before, after)
})
}
}