morphix 0.18.5

Observing and serializing mutations
Documentation
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
use std::borrow::Cow;
use std::collections::TryReserveError;
use std::fmt::{Debug, Display};
use std::marker::PhantomData;
use std::ops::{AddAssign, Bound, Deref, DerefMut, Range, RangeBounds};
use std::string::Drain;

use crate::helper::macros::{default_impl_ref_observe, delegate_methods};
use crate::helper::{AsDeref, AsDerefMut, ObserverState, Pointer, QuasiObserver, Succ, Unsigned, Zero};
use crate::observe::{DefaultSpec, Observer, SerializeObserver};
use crate::{MutationKind, Mutations, Observe};

struct StringObserverState {
    pub append_index: usize, // byte index
    pub truncate_len: usize, // char count
}

impl StringObserverState {
    fn mark_truncate(&mut self, value: &str, new_len: usize) {
        let count = value[new_len..].chars().count();
        self.truncate_len += count;
        self.append_index = new_len;
    }

    fn mark_replace(&mut self, value: &str) {
        self.mark_truncate(value, 0);
    }
}

impl ObserverState for StringObserverState {
    type Target = String;

    fn invalidate(this: &mut Self, value: &String) {
        this.mark_replace(value.as_str());
    }
}

/// Observer implementation for [`String`].
pub struct StringObserver<'ob, S: ?Sized, D = Zero> {
    ptr: Pointer<S>,
    mutation: StringObserverState,
    phantom: PhantomData<&'ob mut D>,
}

impl<'ob, S: ?Sized, D> Deref for StringObserver<'ob, S, D> {
    type Target = Pointer<S>;

    fn deref(&self) -> &Self::Target {
        &self.ptr
    }
}

impl<'ob, S: ?Sized, D> DerefMut for StringObserver<'ob, S, D> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        std::ptr::from_mut(self).expose_provenance();
        Pointer::invalidate(&mut self.ptr);
        &mut self.ptr
    }
}

impl<'ob, S: ?Sized, D> QuasiObserver for StringObserver<'ob, S, D>
where
    D: Unsigned,
    S: AsDeref<D, Target = String>,
{
    type Head = S;
    type OuterDepth = Succ<Zero>;
    type InnerDepth = D;

    fn invalidate(this: &mut Self) {
        ObserverState::invalidate(&mut this.mutation, (*this.ptr).as_deref());
    }
}

impl<'ob, S: ?Sized, D> Observer for StringObserver<'ob, S, D>
where
    D: Unsigned,
    S: AsDerefMut<D, Target = String>,
{
    fn observe(head: &mut Self::Head) -> Self {
        let this = Self {
            mutation: StringObserverState {
                append_index: head.as_deref_mut().len(),
                truncate_len: 0,
            },
            ptr: Pointer::new(head),
            phantom: PhantomData,
        };
        Pointer::register_state::<_, D>(&this.ptr, &this.mutation);
        this
    }

    unsafe fn relocate(this: &mut Self, head: &mut Self::Head) {
        Pointer::set(this, head);
    }
}

impl<'ob, S: ?Sized, D> SerializeObserver for StringObserver<'ob, S, D>
where
    D: Unsigned,
    S: AsDeref<D, Target = String>,
{
    unsafe fn flush(this: &mut Self) -> Mutations {
        let value = (*this.ptr).as_deref();
        let len = value.len();
        let append_index = std::mem::replace(&mut this.mutation.append_index, len);
        let truncate_len = std::mem::replace(&mut this.mutation.truncate_len, 0);
        // After invalidate (mark_replace), append_index = 0 and truncate_len > 0,
        // meaning all tracked content was replaced.
        if append_index == 0 && truncate_len > 0 {
            return Mutations::replace(value);
        }
        let mut mutations = Mutations::new();
        #[cfg(feature = "truncate")]
        if truncate_len > 0 {
            mutations.extend(MutationKind::Truncate(truncate_len));
        }
        #[cfg(feature = "append")]
        if len > append_index {
            mutations.extend(Mutations::append(&value[append_index..]));
        }
        mutations
    }
}

impl<'ob, S: ?Sized, D> StringObserver<'ob, S, D>
where
    D: Unsigned,
    S: AsDerefMut<D, Target = String>,
{
    delegate_methods! { untracked_mut() as String =>
        pub fn reserve(&mut self, additional: usize);
        pub fn reserve_exact(&mut self, additional: usize);
        pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>;
        pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError>;
        pub fn shrink_to_fit(&mut self);
        pub fn shrink_to(&mut self, min_capacity: usize);
    }
}

#[cfg(feature = "append")]
impl<'ob, S: ?Sized, D> StringObserver<'ob, S, D>
where
    D: Unsigned,
    S: AsDerefMut<D, Target = String>,
{
    fn __append_index(&mut self) -> usize {
        self.mutation.append_index
    }

    delegate_methods! { untracked_mut() as String =>
        pub fn push(&mut self, c: char);
        pub fn push_str(&mut self, s: &str);
        pub fn extend_from_within<R>(&mut self, src: R)
        where R: RangeBounds<usize>;
    }

    /// See [`String::insert`].
    pub fn insert(&mut self, idx: usize, ch: char) {
        if idx >= self.__append_index() {
            self.untracked_mut().insert(idx, ch)
        } else {
            self.tracked_mut().insert(idx, ch)
        }
    }

    /// See [`String::insert_str`].
    pub fn insert_str(&mut self, idx: usize, string: &str) {
        if idx >= self.__append_index() {
            self.untracked_mut().insert_str(idx, string)
        } else {
            self.tracked_mut().insert_str(idx, string)
        }
    }
}

#[cfg(any(feature = "truncate", feature = "append"))]
impl<'ob, S: ?Sized, D> StringObserver<'ob, S, D>
where
    D: Unsigned,
    S: AsDerefMut<D, Target = String>,
{
    fn __mark_truncate(&mut self, range: Range<usize>) {
        let count = (*self).untracked_ref()[range.clone()].chars().count();
        self.mutation.truncate_len += count;
        self.mutation.append_index = range.start;
    }

    /// See [`String::clear`].
    pub fn clear(&mut self) {
        if self.__append_index() == 0 {
            self.untracked_mut().clear()
        } else {
            self.tracked_mut().clear()
        }
    }

    /// See [`String::remove`].
    pub fn remove(&mut self, idx: usize) -> char {
        let char = self.untracked_mut().remove(idx);
        let append_index = self.__append_index();
        if idx >= append_index {
            // no-op
        } else if cfg!(feature = "truncate") && idx + char.len_utf8() == append_index {
            self.mutation.truncate_len += 1;
            self.mutation.append_index = idx;
        } else {
            let value = (*self.ptr).as_deref();
            self.mutation.mark_replace(value);
        }
        char
    }

    /// See [`String::pop`].
    pub fn pop(&mut self) -> Option<char> {
        let char = self.untracked_mut().pop()?;
        let append_index = self.__append_index();
        let len = (*self).untracked_ref().len();
        if len >= append_index {
            // no-op
        } else if cfg!(feature = "truncate") && len + char.len_utf8() == append_index {
            self.mutation.truncate_len += 1;
            self.mutation.append_index = len;
        } else {
            let value = (*self.ptr).as_deref();
            self.mutation.mark_replace(value);
        }
        Some(char)
    }

    /// See [`String::truncate`].
    pub fn truncate(&mut self, len: usize) {
        let append_index = self.__append_index();
        if len >= append_index {
            return self.untracked_mut().truncate(len);
        }
        if cfg!(not(feature = "truncate")) || len == 0 {
            return self.tracked_mut().truncate(len);
        }
        self.__mark_truncate(len..append_index);
        self.untracked_mut().truncate(len)
    }

    /// See [`String::split_off`].
    pub fn split_off(&mut self, at: usize) -> String {
        let append_index = self.__append_index();
        if at >= append_index {
            return self.untracked_mut().split_off(at);
        }
        if cfg!(not(feature = "truncate")) || at == 0 {
            return self.tracked_mut().split_off(at);
        }
        self.__mark_truncate(at..append_index);
        self.untracked_mut().split_off(at)
    }

    /// See [`String::drain`].
    pub fn drain<R>(&mut self, range: R) -> Drain<'_>
    where
        R: RangeBounds<usize>,
    {
        let append_index = self.__append_index();
        let start_index = match range.start_bound() {
            Bound::Included(&n) => n,
            Bound::Excluded(&n) => n + 1,
            Bound::Unbounded => 0,
        };
        if start_index >= append_index {
            return self.untracked_mut().drain(range);
        }
        if cfg!(not(feature = "truncate")) || start_index == 0 {
            return self.tracked_mut().drain(range);
        }
        let end_index = match range.end_bound() {
            Bound::Included(&n) => n + 1,
            Bound::Excluded(&n) => n,
            Bound::Unbounded => (*self).untracked_ref().len(),
        };
        if end_index < append_index {
            return self.tracked_mut().drain(range);
        }
        self.__mark_truncate(start_index..append_index);
        self.tracked_mut().drain(range)
    }

    /// See [`String::replace_range`].
    pub fn replace_range<R>(&mut self, range: R, replace_with: &str)
    where
        R: RangeBounds<usize>,
    {
        let append_index = self.__append_index();
        let start_index = match range.start_bound() {
            Bound::Included(&n) => n,
            Bound::Excluded(&n) => n + 1,
            Bound::Unbounded => 0,
        };
        if start_index >= append_index {
            return self.untracked_mut().replace_range(range, replace_with);
        }
        if cfg!(not(feature = "truncate")) || start_index == 0 {
            return self.tracked_mut().replace_range(range, replace_with);
        }
        let end_index = match range.end_bound() {
            Bound::Included(&n) => n + 1,
            Bound::Excluded(&n) => n,
            Bound::Unbounded => (*self).untracked_ref().len(),
        };
        if end_index < append_index {
            return self.tracked_mut().replace_range(range, replace_with);
        }
        self.__mark_truncate(start_index..append_index);
        self.untracked_mut().replace_range(range, replace_with);
    }
}

impl<'ob, S: ?Sized, D> AddAssign<&str> for StringObserver<'ob, S, D>
where
    D: Unsigned,
    S: AsDerefMut<D, Target = String>,
{
    fn add_assign(&mut self, rhs: &str) {
        #[cfg(feature = "append")]
        self.push_str(rhs);
        #[cfg(not(feature = "append"))]
        self.tracked_mut().add_assign(rhs);
    }
}

#[cfg(feature = "append")]
impl<'ob, S: ?Sized, D, U> Extend<U> for StringObserver<'ob, S, D>
where
    D: Unsigned,
    S: AsDerefMut<D, Target = String>,
    String: Extend<U>,
{
    fn extend<I: IntoIterator<Item = U>>(&mut self, other: I) {
        self.untracked_mut().extend(other);
    }
}

impl<'ob, S: ?Sized, D> Debug for StringObserver<'ob, S, D>
where
    D: Unsigned,
    S: AsDeref<D, Target = String>,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("StringObserver").field(&self.untracked_ref()).finish()
    }
}

impl<'ob, S: ?Sized, D> Display for StringObserver<'ob, S, D>
where
    D: Unsigned,
    S: AsDeref<D, Target = String>,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(self.untracked_ref(), f)
    }
}

macro_rules! generic_impl_partial_eq {
    ($($(#[$meta:meta])* impl $([$($gen:tt)*])? PartialEq<$ty:ty> for String);* $(;)?) => {
        $(
            $(#[$meta])*
            impl<'ob, $($($gen)*,)? S, D> PartialEq<$ty> for StringObserver<'ob, S, D>
            where
                D: Unsigned,
                S: AsDeref<D, Target = String>,
                String: PartialEq<$ty>,
            {
                fn eq(&self, other: &$ty) -> bool {
                    self.untracked_ref().eq(other)
                }
            }
        )*
    };
}

generic_impl_partial_eq! {
    impl PartialEq<String> for String;
    impl ['a, U: ?Sized] PartialEq<&'a U> for String;
    impl ['a, U: ToOwned + ?Sized] PartialEq<Cow<'a, U>> for String;
    #[rustversion::since(1.91)]
    impl PartialEq<std::path::Path> for String;
    #[rustversion::since(1.91)]
    impl PartialEq<std::path::PathBuf> for String;
}

impl<'ob, S1, S2, D1, D2> PartialEq<StringObserver<'ob, S2, D2>> for StringObserver<'ob, S1, D1>
where
    D1: Unsigned,
    D2: Unsigned,
    S1: AsDeref<D1, Target = String>,
    S2: AsDeref<D2, Target = String>,
{
    fn eq(&self, other: &StringObserver<'ob, S2, D2>) -> bool {
        self.untracked_ref().eq(other.untracked_ref())
    }
}

impl<'ob, S, D> Eq for StringObserver<'ob, S, D>
where
    D: Unsigned,
    S: AsDeref<D, Target = String>,
{
}

impl<'ob, S, D> PartialOrd<String> for StringObserver<'ob, S, D>
where
    D: Unsigned,
    S: AsDeref<D, Target = String>,
{
    fn partial_cmp(&self, other: &String) -> Option<std::cmp::Ordering> {
        self.untracked_ref().partial_cmp(other)
    }
}

impl<'ob, S1, S2, D1, D2> PartialOrd<StringObserver<'ob, S2, D2>> for StringObserver<'ob, S1, D1>
where
    D1: Unsigned,
    D2: Unsigned,
    S1: AsDeref<D1, Target = String>,
    S2: AsDeref<D2, Target = String>,
{
    fn partial_cmp(&self, other: &StringObserver<'ob, S2, D2>) -> Option<std::cmp::Ordering> {
        self.untracked_ref().partial_cmp(other.untracked_ref())
    }
}

impl<'ob, S, D> Ord for StringObserver<'ob, S, D>
where
    D: Unsigned,
    S: AsDeref<D, Target = String>,
{
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.untracked_ref().cmp(other.untracked_ref())
    }
}

impl Observe for String {
    type Observer<'ob, S, D>
        = StringObserver<'ob, S, D>
    where
        Self: 'ob,
        D: Unsigned,
        S: AsDerefMut<D, Target = Self> + ?Sized + 'ob;

    type Spec = DefaultSpec;
}

default_impl_ref_observe! {
    impl RefObserve for String;
}

#[cfg(test)]
mod tests {
    use morphix_test_utils::*;
    use serde_json::json;

    use crate::adapter::Json;
    use crate::observe::{ObserveExt, SerializeObserverExt};

    #[test]
    fn no_mutation_returns_none() {
        let mut s = String::from("hello");
        let mut ob = s.__observe();
        let Json(mutation) = ob.flush().unwrap();
        assert_eq!(mutation, None);
    }

    #[test]
    fn replace_on_deref_mut() {
        let mut s = String::from("hello");
        let mut ob = s.__observe();
        ob.clear();
        ob.push_str("world"); // append after replace should have no effect
        let Json(mutation) = ob.flush().unwrap();
        assert_eq!(mutation, Some(replace!(_, json!("world"))));
    }

    #[test]
    fn append_with_push() {
        let mut s = String::from("a");
        let mut ob = s.__observe();
        ob.push('b');
        ob.push('c');
        let Json(mutation) = ob.flush().unwrap();
        assert_eq!(mutation, Some(append!(_, json!("bc"))));
    }

    #[test]
    fn append_with_push_str() {
        let mut s = String::from("foo");
        let mut ob = s.__observe();
        ob.push_str("bar");
        let Json(mutation) = ob.flush().unwrap();
        assert_eq!(mutation, Some(append!(_, json!("bar"))));
    }

    #[test]
    fn append_with_add_assign() {
        let mut s = String::from("foo");
        let mut ob = s.__observe();
        ob += "bar";
        let Json(mutation) = ob.flush().unwrap();
        assert_eq!(mutation, Some(append!(_, json!("bar"))));
    }

    #[test]
    fn append_empty_string() {
        let mut s = String::from("foo");
        let mut ob = s.__observe();
        ob.push_str("");
        ob += "";
        let Json(mutation) = ob.flush().unwrap();
        assert_eq!(mutation, None);
    }

    #[test]
    fn replace_after_append() {
        let mut s = String::from("abc");
        let mut ob = s.__observe();
        ob.push_str("def");
        **ob = String::from("xyz");
        let Json(mutation) = ob.flush().unwrap();
        assert_eq!(mutation, Some(replace!(_, json!("xyz"))));
    }

    #[test]
    fn truncate() {
        let mut s = String::from("你好,世界!");
        let mut ob = s.__observe();
        ob.truncate("你好".len());
        let Json(mutation) = ob.flush().unwrap();
        assert_eq!(mutation, Some(truncate!(_, 4)));
    }

    #[test]
    fn pop_as_truncate() {
        let mut s = String::from("你好,世界!");
        let mut ob = s.__observe();
        ob.pop();
        ob.pop();
        let Json(mutation) = ob.flush().unwrap();
        assert_eq!(mutation, Some(truncate!(_, 2)));
    }

    #[test]
    fn pop_after_append() {
        let mut s = String::from("你好!");
        let mut ob = s.__observe();
        ob.push_str("世界!");
        ob.pop();
        let Json(mutation) = ob.flush().unwrap();
        assert_eq!(mutation, Some(append!(_, json!("世界"))));
    }

    #[test]
    fn append_after_pop() {
        let mut s = String::from("你好,世界!");
        let mut ob = s.__observe();
        ob.pop();
        ob.push('~');
        let Json(mutation) = ob.flush().unwrap();
        assert_eq!(mutation, Some(batch!(_, truncate!(_, 1), append!(_, json!("~")))));
    }

    #[test]
    fn remove_before_append_index() {
        let mut s = String::from("你好,世界!");
        let mut ob = s.__observe();
        assert_eq!(ob.remove("你好".len()), '');
        let Json(mutation) = ob.flush().unwrap();
        assert_eq!(mutation, Some(replace!(_, json!("你好世界!"))));
    }

    #[test]
    fn remove_at_append_index() {
        let mut s = String::from("你好,世界!");
        let mut ob = s.__observe();
        assert_eq!(ob.remove("你好,世界".len()), '');
        assert_eq!(ob.remove("你好,世".len()), '');
        let Json(mutation) = ob.flush().unwrap();
        assert_eq!(mutation, Some(truncate!(_, 2)));
    }
}