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
#![no_std]
extern crate no_std_compat as std;
use node::NodePath;
use node::NodePathHash;
use std::collections::hash_map::DefaultHasher;
use std::sync::Mutex;
use std::sync::MutexGuard;
use core::cmp::Ord;
use core::cmp::Ordering;
use core::cmp::PartialOrd;
use core::hash::Hash;
use core::hash::Hasher;
pub mod app;
pub mod bitmap;
pub mod flexbox;
pub mod geometry;
pub mod node;
#[cfg(feature = "text")]
pub mod text;
#[cfg(feature = "xml")]
pub mod xml;
#[cfg(feature = "png")]
pub mod png;
#[cfg(feature = "railway")]
pub mod railway;
pub type Point = geometry::Point;
pub type Size = geometry::Size;
pub type Spot = geometry::Spot;
pub type Status = Result<(), ()>;
pub type PlatformLog = &'static dyn Fn(&str);
pub type PlatformBlit = &'static dyn Fn(Spot, BlitKey) -> Option<(&'static mut [u8], usize, bool)>;
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum BlitKey {
Node(usize, NodePathHash),
Background,
Overlay,
}
#[derive(Debug, Copy, Clone)]
pub enum BlitPath<'a> {
Node(&'a NodePath),
Background,
Overlay,
}
impl<'a> BlitPath<'a> {
pub fn to_key(&self) -> BlitKey {
match self {
BlitPath::Node(path) => BlitKey::Node(path.len() + 1, single_hash(path)),
BlitPath::Background => BlitKey::Background,
BlitPath::Overlay => BlitKey::Overlay,
}
}
}
pub fn single_hash(hashable: impl Hash) -> u64 {
let mut hasher = DefaultHasher::new();
hashable.hash(&mut hasher);
hasher.finish()
}
pub fn lock<T: ?Sized>(mutex: &Mutex<T>) -> Option<MutexGuard<T>> {
#[cfg(feature = "std")]
let result = mutex.lock().ok();
#[cfg(not(feature = "std"))]
let result = Some(mutex.lock());
result
}
pub fn status<T>(option: Option<T>) -> Result<T, ()> {
option.ok_or(())
}
#[macro_export]
macro_rules! format {
($($arg:tt)*) => {{
let mut string = String::new();
core::fmt::write(&mut string, core::format_args!($($arg)*)).unwrap();
string
}}
}
impl Ord for BlitKey {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(BlitKey::Node(d1, _), BlitKey::Node(d2, _)) => d2.cmp(d1),
(BlitKey::Background, BlitKey::Node(_, _)) => Ordering::Less,
(BlitKey::Node(_, _), BlitKey::Background) => Ordering::Greater,
(BlitKey::Overlay, BlitKey::Node(_, _)) => Ordering::Greater,
(BlitKey::Node(_, _), BlitKey::Overlay) => Ordering::Less,
(BlitKey::Background, BlitKey::Overlay) => Ordering::Less,
(BlitKey::Overlay, BlitKey::Background) => Ordering::Greater,
(BlitKey::Background, BlitKey::Background) => Ordering::Equal,
(BlitKey::Overlay, BlitKey::Overlay) => Ordering::Equal,
}
}
}
impl PartialOrd for BlitKey {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}