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
use XY;
use vec::Vec2;
/// Cache around a one-dimensional layout result.
///
/// This is not a View, but something to help you if you create your own Views.
#[derive(PartialEq, Debug, Clone, Copy)]
pub struct SizeCache {
/// Cached value
pub value: usize,
/// `true` if the last size was constrained.
///
/// If unconstrained, any request larger than this value
/// would return the same size.
pub constrained: bool,
}
impl SizeCache {
/// Creates a new sized cache
pub fn new(value: usize, constrained: bool) -> Self {
SizeCache {
value: value,
constrained: constrained,
}
}
/// Returns `true` if `self` is still valid for the given `request`.
pub fn accept(self, request: usize) -> bool {
if request < self.value {
false
} else if request == self.value {
true
} else {
!self.constrained
}
}
/// Creates a new bi-dimensional cache.
///
/// It will stay valid for the same request, and compatible ones.
///
/// A compatible request is one where, for each axis, either:
///
/// * the request is equal to the cached size, or
/// * the request is larger than the cached size and the cache is unconstrained
///
/// Notes:
///
/// * `size` must fit inside `req`.
/// * for each dimension, `constrained = (size == req)`
pub fn build(size: Vec2, req: Vec2) -> XY<Self> {
XY::new(SizeCache::new(size.x, size.x >= req.x),
SizeCache::new(size.y, size.y >= req.y))
}
}