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
//! Example of restricting statically-checked mutability for types,
//! that are not originally designed for this (e.g. FFI wrappers etc)
//! or which uses interior mutability, but still need verbose
//! API interface.
//!
//! # The problem
//!
//! Let's start from the [monkey_ffi] module.
//!
//! This is an example of extern C API, that looks like object-oriented,
//! but is not safe and not guarantees the existence of objects, as well
//! as their relations.
//!
//! It looks like tree-structure:
//!
//! ```ignore
//! Root
//! ----Window
//! ----Frame
//! ----|----FrameButton
//! ----WindowButton
//! ```
//!
//! Ideally, only one object at time should be mutable,
//! and we have to guarantee, that parent lives at least as long as child.
//!
//! But the problem is, that with Rc, or just pointers, on every
//! fold level parent mutability state is lost. We can not make easily
//! two versions of [Window]: one, that keeps `&'a mut Root` and other,
//! that keeps `&'a Root`, just because we will need to type whole the
//! implementation block twice.
//!
//! But the fact, that `Self`, `&Self` and `&mut Self` are complete different types and this
//! [discussion](https://users.rust-lang.org/t/generic-mutability-parameters/16837/23)
//! pointed me the solution.
//!
//! Actually, mutability in `rust` is tri-state: immutable, mutable, and that, which
//! we don't care about. Which we want to be «generic». So, let's start from declaring
//! types, that will represent all the three states: two concrete structs and one trait.
//!
//! ```
//! trait ProbablyMutable {
//! fn is_mutable(&self) -> bool;
//! }
//! struct Mutable;
//! impl ProbablyMutable for Mutable {
//!     fn is_mutable(&self) -> bool {
//!         true
//!     }
//! }
//! struct Immutable;
//! impl ProbablyMutable for Immutable {
//!     fn is_mutable(&self) -> bool {
//!         false
//!     }
//! }
//! ```
//!
//! Then we will use these types as markers for future parametrization.
//!
//! Now let's make a skeleton of object structure and ensure that no child will
//! outlive their parents.
//!
//! ```
//! # trait ProbablyMutable;
//! struct Root;
//! ```
//!
//! Here we use [PhantomData] to keep «generic» part
//! outside of any concrete parent object.
//! Later, it will help not to have in a deep children structure
//! scary constructions, like:
//! `SecondChild<&mut Parent, &mut FirstChild<&mut Parent>>`
//!
//! ```
//! # trait ProbablyMutable;
//! # struct Root;
//! struct Window<'a, T: ProbablyMutable> {
//!     id: usize,
//!     name: String,
//!     frames_amount: usize,
//!     buttons_amount: usize,
//!     root: &'a Root,
//!     mutability: PhantomData<T>,
//! }
//!
//! struct Frame<'a, T: ProbablyMutable> {
//!     window: &'a Window<'a, T>,
//!     id: usize,
//!     width_px: Option<u16>,
//!     buttons_amount: usize,
//! }
//!
//! struct WindowButton<'a, T: ProbablyMutable> {
//!     id: usize,
//!     text: String,
//!     parent: &'a Window<'a, T>,
//! }
//!
//! struct FrameButton<'a, T: ProbablyMutable> {
//!     id: usize,
//!     text: String,
//!     parent: &'a Frame<'a, T>,
//! }
//! ```
//!
//! since there are two different functions sets for buttons
//! I decided to consider them as different classes with
//! the single interface (trait) [Button].
//!
//! Theoretically, it should be possible to make a single struct,
//! that keeps differs of types in enum. But for the moment, it
//! seemed to me like an overhead.
//!
//! Considering the generic mutation, I implement it like implementation
//! of three different types:
//! - `struct<T: ProbablyMutable>` for functions, that should be generic
//! in their mutability.
//! - `struct<Mutable>` for functions, that require object to be mutable.
//! - `struct<Immutable>` for functions, that require object to be immutable.
//!
//! ```
//! # trait ProbablyMutable;
//! # struct Root;
//! # struct Frame<T: ProbablyMutable>;
//! # struct Window<T: ProbablyMutable>;
//! impl<'a, T: ProbablyMutable> Window<'a, T> {
//!     fn new(root: &'a Root, id: usize) -> Option<Self> {todo!()}
//!     fn get_id(&self) -> usize {todo!()}
//!     fn get_name(&self) -> &String {todo!()}
//!     fn get_width(&self) -> u16 {todo!()}
//! }
//! impl<'a> Window<'a, Immutable> {
//!     fn get_frame(&self, id: usize) -> Option<Frame<Immutable>> {todo!()}
//!     fn get_button(&self, id: usize) -> Option<WindowButton<Immutable>> {todo!()}
//! }
//! impl<'a> Window<'a, Mutable> {
//!     fn set_name(&mut self, name: impl Into<String>) {todo!()}
//!     fn make_frame(&mut self) -> Frame<Mutable> {todo!()}
//!     fn make_button(&mut self) -> WindowButton<Mutable> {todo!()}
//! }
//! ```
use std::marker::PhantomData;

use log::debug;

mod monkey_ffi {
    //! module that imitates some FFI functions set.
    //!
    //! ```ignore
    //! fn make_window() -> usize;
    //! fn get_window(window_id: usize) -> usize;
    //! fn make_frame(_window_id: usize) -> usize;
    //! fn make_window_button(_window_id: usize) -> usize;
    //! fn make_frame_button(_window_id: usize) -> usize;
    //! fn window_button_is_clicked(_window_id: usize, _button_id: usize) -> bool;
    //! fn window_button_click(window_id: usize, button_id: usize);
    //! fn window_button_set_text(window_id: usize, button_id: usize, text: &String);
    //! fn frame_button_is_clicked(_frame_id: usize, _button_id: usize) -> bool;
    //! fn frame_button_click(frame_id: usize, button_id: usize);
    //! fn frame_button_set_text(frame_id: usize, button_id: usize, text: &String);
    //! ```

    use log::info;

    static mut MONKEY_WINDOWS_AMOUNT: usize = 0;
    static mut MONKEY_FRAMES_AMOUNT: usize = 0;
    static mut MONKEY_WINDOW_BUTTONS_AMOUNT: usize = 0;
    static mut MONKEY_FRAME_BUTTONS_AMOUNT: usize = 0;

    pub fn make_window() -> usize {
        unsafe {
            MONKEY_WINDOWS_AMOUNT += 1;
            let id = MONKEY_WINDOWS_AMOUNT;
            info!("Created Window with id: {}", id);
            id
        }
    }
    pub fn get_window(window_id: usize) -> usize {
        unsafe {
            let id = MONKEY_WINDOWS_AMOUNT;
            if window_id > id {
                return 0;
            }
            info!("Returned Window with id: {}", id);
            id
        }
    }
    pub fn make_frame(_window_id: usize) -> usize {
        unsafe {
            MONKEY_FRAMES_AMOUNT += 1;
            let id = MONKEY_FRAMES_AMOUNT;
            info!("Created Frame with id: {}", id);
            id
        }
    }
    pub fn make_window_button(_window_id: usize) -> usize {
        unsafe {
            MONKEY_WINDOW_BUTTONS_AMOUNT += 1;
            let id = MONKEY_WINDOW_BUTTONS_AMOUNT;
            info!("Created WindowButton with id: {}", id);
            id
        }
    }
    pub fn make_frame_button(_window_id: usize) -> usize {
        unsafe {
            MONKEY_FRAME_BUTTONS_AMOUNT += 1;
            let id = MONKEY_FRAME_BUTTONS_AMOUNT;
            info!("Created FrameButton with id: {}", id);
            id
        }
    }
    pub fn window_button_is_clicked(_window_id: usize, _button_id: usize) -> bool {
        false
    }

    pub fn window_button_click(window_id: usize, button_id: usize) {
        info!(
            "WindowButton clicked! window={}, button={}",
            window_id, button_id
        );
    }
    pub fn window_button_set_text(window_id: usize, button_id: usize, text: &String) {
        info!(
            "WindowButton text set to: {}! window={}, button={}",
            text, window_id, button_id
        );
    }
    pub fn frame_button_is_clicked(_frame_id: usize, _button_id: usize) -> bool {
        false
    }

    pub fn frame_button_click(frame_id: usize, button_id: usize) {
        info!(
            "FrameButton clicked! window={}, button={}",
            frame_id, button_id
        );
    }
    pub fn frame_button_set_text(frame_id: usize, button_id: usize, text: &String) {
        info!(
            "FrameButton text set to: {}! window={}, button={}",
            text, frame_id, button_id
        );
    }
}

trait ProbablyMutable {
    fn is_mutable(&self) -> bool;
}
struct Mutable;
impl ProbablyMutable for Mutable {
    fn is_mutable(&self) -> bool {
        true
    }
}
struct Immutable;
impl ProbablyMutable for Immutable {
    fn is_mutable(&self) -> bool {
        false
    }
}

struct Root;
impl Root {
    fn new() -> Self {
        Self {}
    }
    fn make_child(&mut self) -> Window<Mutable> {
        let id = monkey_ffi::make_window();
        let child = Window::new(self, id).expect("Should be initialized.");
        child
    }
    fn get_child(&self, id: usize) -> Option<Window<Immutable>> {
        Window::new(self, id)
    }
    fn get_child_mut(&mut self, id: usize) -> Option<Window<Mutable>> {
        Window::new(self, id)
    }
}

struct Window<'a, T: ProbablyMutable> {
    id: usize,
    name: String,
    frames_amount: usize,
    buttons_amount: usize,
    root: &'a Root,
    mutability: PhantomData<T>,
}
impl<'a, T: ProbablyMutable> Window<'a, T> {
    fn new(root: &'a Root, id: usize) -> Option<Self> {
        match monkey_ffi::get_window(id) {
            0 => None,
            x => Self {
                id,
                name: String::default(),
                frames_amount: 0,
                buttons_amount: 0,
                root,
                mutability: PhantomData,
            }
            .into(),
        }
    }
    fn get_id(&self) -> usize {
        self.id
    }
    fn get_name(&self) -> &String {
        &self.name
    }
    fn get_width(&self) -> u16 {
        debug!("Some FFI call to het width");
        400
    }
}
impl<'a> Window<'a, Immutable> {
    fn get_frame(&self, id: usize) -> Option<Frame<Immutable>> {
        Frame::new(self, id)
    }
    fn get_button(&self, id: usize) -> Option<WindowButton<Immutable>> {
        Button::new(self, id)
    }
}
impl<'a> Window<'a, Mutable> {
    fn set_name(&mut self, name: impl Into<String>) {
        self.name = name.into();
    }
    fn make_frame(&mut self) -> Frame<Mutable> {
        debug!("Some FFI magic");
        let id = monkey_ffi::make_frame(self.get_id());
        self.frames_amount += 1;
        let sub_child = Frame::new(self, id).expect("Should be created and valid.");
        sub_child
    }
    fn make_button(&mut self) -> WindowButton<Mutable> {
        debug!("Some FFI magic");
        let id = self.buttons_amount;
        self.buttons_amount += 1;
        let button = WindowButton::new(&*self, id).expect("Should be created and valid.");
        button
    }
}

struct Frame<'a, T: ProbablyMutable> {
    window: &'a Window<'a, T>,
    id: usize,
    width_px: Option<u16>,
    buttons_amount: usize,
}
impl<'a, T: ProbablyMutable> Frame<'a, T> {
    fn new(window: &'a Window<T>, id: usize) -> Option<Self> {
        let id = monkey_ffi::make_frame(id);
        match id {
            0 => None,
            id => Self {
                window,
                id,
                width_px: None,
                buttons_amount: 0,
            }
            .into(),
        }
    }
    fn get_id(&self) -> usize {
        self.id
    }
    fn get_width(&self) -> u16 {
        match self.width_px {
            None => self.window.get_width(),
            Some(width) => width,
        }
    }
}
impl<'a> Frame<'a, Immutable> {
    fn get_button(&self, id: usize) -> Option<FrameButton<Immutable>> {
        FrameButton::new(self, id)
    }
}
impl<'a> Frame<'a, Mutable> {
    fn set_width(&mut self, width_px: impl Into<u16>) {
        self.width_px = Some(width_px.into())
    }
    fn make_button(&mut self) -> FrameButton<Mutable> {
        debug!("Some FFI magic");
        let id = self.buttons_amount;
        self.buttons_amount += 1;
        let button = FrameButton::new(&*self, id).expect("Should be created and valid.");
        button
    }
}

trait Button<T: ProbablyMutable>
where
    Self: Sized,
{
    type Parent;
    fn new(parent: Self::Parent, id: usize) -> Option<Self>;
    fn get_id(&self) -> usize;
    fn is_clicked(&self) -> bool;
    fn get_text(&self) -> &String;
}
trait ButtonMut
where
    Self: Sized,
{
    type Parent;
    fn click(&mut self);
    fn set_text(&mut self, text: impl Into<String>);
}

struct WindowButton<'a, T: ProbablyMutable> {
    id: usize,
    text: String,
    parent: &'a Window<'a, T>,
}
impl<'a, T: ProbablyMutable> Button<T> for WindowButton<'a, T> {
    type Parent = &'a Window<'a, T>;

    fn new(parent: Self::Parent, id: usize) -> Option<Self> {
        let id = monkey_ffi::make_window_button(id);
        match id {
            0 => None,
            id => Self {
                id,
                text: String::default(),
                parent,
            }
            .into(),
        }
    }
    fn get_id(&self) -> usize {
        self.id
    }

    fn is_clicked(&self) -> bool {
        monkey_ffi::window_button_is_clicked(self.parent.get_id(), self.id)
    }

    fn get_text(&self) -> &String {
        &self.text
    }
}
impl<'a> ButtonMut for WindowButton<'a, Mutable> {
    type Parent = Window<'a, Mutable>;

    fn click(&mut self) {
        monkey_ffi::window_button_click(self.parent.get_id(), self.id)
    }

    fn set_text(&mut self, text: impl Into<String>) {
        self.text = text.into();
        monkey_ffi::window_button_set_text(self.parent.get_id(), self.id, &self.text);
    }
}

struct FrameButton<'a, T: ProbablyMutable> {
    id: usize,
    text: String,
    parent: &'a Frame<'a, T>,
}
impl<'a, T: ProbablyMutable> Button<T> for FrameButton<'a, T> {
    type Parent = &'a Frame<'a, T>;

    fn new(parent: Self::Parent, id: usize) -> Option<Self> {
        let id = monkey_ffi::make_frame_button(id);
        match id {
            0 => None,
            id => Self {
                id,
                text: String::default(),
                parent,
            }
            .into(),
        }
    }
    fn get_id(&self) -> usize {
        self.id
    }

    fn is_clicked(&self) -> bool {
        monkey_ffi::frame_button_is_clicked(self.parent.get_id(), self.id)
    }

    fn get_text(&self) -> &String {
        &self.text
    }
}
impl<'a> ButtonMut for FrameButton<'a, Mutable> {
    type Parent = Frame<'a, Mutable>;

    fn click(&mut self) {
        monkey_ffi::frame_button_click(self.parent.get_id(), self.id)
    }

    fn set_text(&mut self, text: impl Into<String>) {
        self.text = text.into();
        monkey_ffi::frame_button_set_text(self.parent.get_id(), self.id, &self.text);
    }
}

fn main() {
    env_logger::init();
    let mut root = Root::new();
    let window1 = root.make_child();
    // Err: cannot borrow `root` as mutable more than once at a time
    // let window2 = root.make_child(); // try to uncomment

    let w1_id = window1.get_id(); // Drop window1 and finish &mut borrow of Root
    debug!("{}", w1_id);
    let id2 = root.make_child().get_id();
    let _window1 = root.get_child(w1_id).unwrap();
    let _window2 = root.get_child(id2).unwrap(); // OK!

    // Err: no method named `make_button` found for struct `Window<'_,
    // test::Immutable>` in the current scope. The method was found for
    // `Window<'a, test::Mutable>`
    // _window1.make_button()

    let mut window1 = root.get_child_mut(w1_id).unwrap();
    let button = window1.make_button();
    let b_id = button.get_id();
    let mut frame = window1.make_frame();
    let fr_b_id = frame.make_button().get_id();
    let f_id = frame.get_id();
    // Err: cannot borrow `window1` as mutable more than once at a time
    // debug!("button text: {}", button.get_text());

    // Err: no method named `get_button` found for struct
    // `Window<'_, test::Mutable>` in the current scope
    // the method was found for - `Window<'a, test::Immutable>`
    // let button = window1.get_button(b_id);
    let window1 = root.get_child(w1_id).unwrap();
    let frame = window1.get_frame(f_id).unwrap();
    let w_b = window1.get_button(b_id).unwrap();
    let fr_b = frame.get_button(fr_b_id).unwrap();

    debug!("is window button clicked: {}", w_b.is_clicked());
    debug!("is frame button clicked: {}", fr_b.is_clicked());
}