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
use core::{any::Any, cmp::Ordering, fmt};
use std::rc::Rc;
/// Strong identity lease for one retained semantic object.
///
/// The erased owner keeps its allocation alive while the identity is present in
/// a renderer map. That makes pointer identity collision-free even when a
/// collection removes one retained node and allocates its replacement during
/// the same refresh.
#[derive(Clone)]
pub(crate) struct RetainedIdentity {
_owner: Rc<dyn Any>,
address: usize,
}
impl RetainedIdentity {
pub(crate) fn for_rc<T: 'static>(owner: &Rc<T>) -> Self {
let address = Rc::as_ptr(owner) as usize;
let owner: Rc<dyn Any> = owner.clone();
Self {
_owner: owner,
address,
}
}
pub(crate) const fn address(&self) -> usize {
self.address
}
/// Whether the retained object is still owned by something other than this
/// lease.
///
/// A renderer map that keys its entries by identity can use this to keep an
/// entry alive for exactly as long as the retained tree holds the object,
/// rather than for as long as it happens to be rendered. A container that
/// renders one child at a time — tabs, a collapsed split column — retains
/// every child but flushes only the visible one, so "not flushed this
/// frame" does not mean "gone".
///
/// This requires that the renderer hold no strong reference to the object
/// beyond the single lease stored in the map.
pub(crate) fn is_retained_elsewhere(&self) -> bool {
Rc::strong_count(&self._owner) > 1
}
}
impl fmt::Debug for RetainedIdentity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("RetainedIdentity")
.field(&self.address)
.finish()
}
}
impl PartialEq for RetainedIdentity {
fn eq(&self, other: &Self) -> bool {
self.address == other.address
}
}
impl Eq for RetainedIdentity {}
impl PartialOrd for RetainedIdentity {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for RetainedIdentity {
fn cmp(&self, other: &Self) -> Ordering {
self.address.cmp(&other.address)
}
}