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
119
120
121
122
123
124
use crate::{CreatedDelta, Diffable, Patchable};
use std::borrow::{Borrow, Cow};
impl<'s, 'e, 'a, T> Diffable<'s, 'e, Cow<'a, T>> for Cow<'a, T>
where
T: ToOwned,
T: ?Sized,
T: Diffable<'s, 'e, T>,
{
type Delta = <T as Diffable<'s, 'e, T>>::Delta;
type DeltaOwned = <T as Diffable<'s, 'e, T>>::DeltaOwned;
fn create_delta_towards(&'s self, end_state: &'e Cow<'a, T>) -> CreatedDelta<Self::Delta> {
let inner_self: &T = self.borrow();
inner_self.create_delta_towards(end_state.borrow())
}
}
impl<'s, 'e, 'a, T> Patchable<<Self as Diffable<'s, 'e, Self>>::DeltaOwned> for Cow<'a, T>
where
T: ToOwned,
T: ?Sized,
T: Diffable<'s, 'e, T>,
<T as ToOwned>::Owned: Patchable<<T as Diffable<'s, 'e, T>>::DeltaOwned>,
{
fn apply_patch(&mut self, patch: <T as Diffable<'s, 'e, T>>::DeltaOwned) {
self.to_mut().apply_patch(patch)
}
}
#[cfg(test)]
mod tests {
use crate::sequence::SequenceModificationDelta;
use crate::DipaImplTester;
use std::borrow::Cow;
#[test]
fn cow_impl() {
let hello_static = "hello";
let empty_static = "";
DipaImplTester {
label: Some("Cow no change Borrowed -> Owned"),
start: &mut Cow::Borrowed(hello_static),
end: &Cow::Owned(hello_static.to_string()),
expected_delta: vec![],
expected_serialized_patch_size: 1,
expected_did_change: false,
}
.test();
DipaImplTester {
label: Some("Cow no change Owned -> Borrowed"),
start: &mut Cow::Owned(hello_static.to_string()),
end: &Cow::Borrowed(hello_static),
expected_delta: vec![],
expected_serialized_patch_size: 1,
expected_did_change: false,
}
.test();
DipaImplTester {
label: Some("Cow no change Borrowed -> Borrowed"),
start: &mut Cow::Borrowed(hello_static),
end: &Cow::Borrowed(hello_static),
expected_delta: vec![],
expected_serialized_patch_size: 1,
expected_did_change: false,
}
.test();
DipaImplTester {
label: Some("Cow no change Owned -> Owned"),
start: &mut Cow::Owned::<'static, str>(hello_static.to_string()),
end: &Cow::Owned(hello_static.to_string()),
expected_delta: vec![],
expected_serialized_patch_size: 1,
expected_did_change: false,
}
.test();
DipaImplTester {
label: Some("Cow change Borrowed -> Owned"),
start: &mut Cow::Borrowed(hello_static),
end: &Cow::Owned(empty_static.to_string()),
expected_delta: vec![SequenceModificationDelta::DeleteAll],
expected_serialized_patch_size: 2,
expected_did_change: true,
}
.test();
DipaImplTester {
label: Some("Cow change Borrowed -> Borrowed"),
start: &mut Cow::Borrowed(hello_static),
end: &Cow::Borrowed(empty_static),
expected_delta: vec![SequenceModificationDelta::DeleteAll],
expected_serialized_patch_size: 2,
expected_did_change: true,
}
.test();
DipaImplTester {
label: Some("Cow change Owned -> Borrowed"),
start: &mut Cow::Owned(hello_static.to_string()),
end: &Cow::Borrowed(empty_static),
expected_delta: vec![SequenceModificationDelta::DeleteAll],
expected_serialized_patch_size: 2,
expected_did_change: true,
}
.test();
DipaImplTester {
label: Some("Cow change Owned -> Owned"),
start: &mut Cow::Owned::<'static, str>(hello_static.to_string()),
end: &Cow::Owned(empty_static.to_string()),
expected_delta: vec![SequenceModificationDelta::DeleteAll],
expected_serialized_patch_size: 2,
expected_did_change: true,
}
.test();
}
}