perspective_viewer/model/
structural.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
5// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13//! A simple "structurally-typed" method extension implementation.  This
14//! collection of `trait`s allows methods to be automatically defined for
15//! `struct`s only if the define accessors for the necessary applications state
16//! objects (which are conviently derivable with the `derive_model!` macro).
17
18use crate::dragdrop::*;
19use crate::presentation::*;
20use crate::renderer::*;
21use crate::session::*;
22
23pub trait HasSession {
24    fn session(&self) -> &'_ Session;
25}
26
27pub trait HasRenderer {
28    fn renderer(&self) -> &'_ Renderer;
29}
30
31pub trait HasPresentation {
32    fn presentation(&self) -> &'_ Presentation;
33}
34
35pub trait HasDragDrop {
36    fn dragdrop(&self) -> &'_ DragDrop;
37}
38
39#[macro_export]
40macro_rules! derive_model {
41    (DragDrop for $key:ty) => {
42        impl $crate::model::HasDragDrop for $key {
43            fn dragdrop(&self) -> &'_ DragDrop {
44                &self.dragdrop
45            }
46        }
47    };
48    (Renderer for $key:ty) => {
49        impl $crate::model::HasRenderer for $key {
50            fn renderer(&self) -> &'_ Renderer {
51                &self.renderer
52            }
53        }
54    };
55    (Session for $key:ty) => {
56        impl $crate::model::HasSession for $key {
57            fn session(&self) -> &'_ Session {
58                &self.session
59            }
60        }
61    };
62    (Presentation for $key:ty) => {
63        impl $crate::model::HasPresentation for $key {
64            fn presentation(&self) -> &'_ Presentation {
65                &self.presentation
66            }
67        }
68    };
69    ($i:ident, $($is:ident),+ for $key:ty) => {
70        derive_model!($i for $key);
71        derive_model!($($is),+ for $key);
72    };
73}