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
use std::hash::Hash;

use re_log_types::RowId;

use crate::{InstancePath, InstancePathHash};

// ----------------------------------------------------------------------------

/// A versioned path (i.e. pinned to a specific [`RowId`]) to either a specific instance of an entity,
/// or the whole entity.
///
/// The easiest way to construct this type is via [`crate::InstancePath::versioned`].
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct VersionedInstancePath {
    pub instance_path: InstancePath,
    pub row_id: RowId,
}

impl VersionedInstancePath {
    /// Do we refer to the whole entity (all instances of it)?
    ///
    /// For example: the whole point cloud, rather than a specific point.
    #[inline]
    pub fn is_all(&self) -> bool {
        self.instance_path.is_all()
    }

    #[inline]
    pub fn hash(&self) -> VersionedInstancePathHash {
        VersionedInstancePathHash {
            instance_path_hash: self.instance_path.hash(),
            row_id: self.row_id,
        }
    }
}

impl std::fmt::Display for VersionedInstancePath {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        format!("{} @ {}", self.instance_path, self.row_id).fmt(f)
    }
}

// ----------------------------------------------------------------------------

/// Hashes of the components of a [`VersionedInstancePath`].
///
/// The easiest way to construct this type is to use either [`crate::InstancePathHash::versioned`]
/// or [`crate::VersionedInstancePath::hash`].
#[derive(Clone, Copy, Eq)]
pub struct VersionedInstancePathHash {
    pub instance_path_hash: InstancePathHash,
    pub row_id: RowId,
}

impl std::fmt::Debug for VersionedInstancePathHash {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let Self {
            instance_path_hash,
            row_id,
        } = self;
        write!(
            f,
            "VersionedInstancePathHash({instance_path_hash:?}, {row_id})"
        )
    }
}

impl std::hash::Hash for VersionedInstancePathHash {
    #[inline]
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        let Self {
            instance_path_hash,
            row_id,
        } = self;
        let InstancePathHash {
            entity_path_hash,
            instance,
        } = instance_path_hash;

        state.write_u64(entity_path_hash.hash64());
        state.write_u64(instance.get());
        state.write_u128(row_id.as_u128());
    }
}

impl std::cmp::PartialEq for VersionedInstancePathHash {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        let Self {
            instance_path_hash,
            row_id,
        } = self;

        instance_path_hash == &other.instance_path_hash && row_id == &other.row_id
    }
}

impl VersionedInstancePathHash {
    pub const NONE: Self = Self {
        instance_path_hash: InstancePathHash::NONE,
        row_id: RowId::ZERO,
    };

    #[inline]
    pub fn is_some(&self) -> bool {
        self.instance_path_hash.is_some()
    }

    #[inline]
    pub fn is_none(&self) -> bool {
        self.instance_path_hash.is_none()
    }
}

impl Default for VersionedInstancePathHash {
    fn default() -> Self {
        Self::NONE
    }
}