Skip to main content

perspective_viewer/utils/
ptr_eq_rc.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
13use std::ops::Deref;
14use std::rc::Rc;
15
16use yew::html::{ImplicitClone, IntoPropValue};
17
18/// A thin wrapper around `Rc<T>` whose `PartialEq` uses pointer identity
19/// (`Rc::ptr_eq`) instead of deep structural comparison.  This makes it
20/// suitable for Yew `Properties` fields that hold large, cheaply-shared
21/// snapshots (e.g. `ViewConfig`, `SessionMetadata`, `Vec<String>`).
22pub struct PtrEqRc<T>(Rc<T>);
23
24impl<T> PtrEqRc<T> {
25    pub fn new(val: T) -> Self {
26        Self(Rc::new(val))
27    }
28}
29
30impl<T> Clone for PtrEqRc<T> {
31    fn clone(&self) -> Self {
32        Self(Rc::clone(&self.0))
33    }
34}
35
36impl<T> PartialEq for PtrEqRc<T> {
37    fn eq(&self, other: &Self) -> bool {
38        Rc::ptr_eq(&self.0, &other.0)
39    }
40}
41
42impl<T> Deref for PtrEqRc<T> {
43    type Target = T;
44
45    fn deref(&self) -> &T {
46        &self.0
47    }
48}
49
50impl<T> From<T> for PtrEqRc<T> {
51    fn from(rc: T) -> Self {
52        Self(Rc::new(rc))
53    }
54}
55
56impl<T: Default> Default for PtrEqRc<T> {
57    fn default() -> Self {
58        Self(Rc::default())
59    }
60}
61
62impl<T: std::fmt::Debug> std::fmt::Debug for PtrEqRc<T> {
63    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64        self.0.fmt(f)
65    }
66}
67
68impl<T> ImplicitClone for PtrEqRc<T> {}
69
70impl<T> IntoPropValue<PtrEqRc<T>> for Rc<T> {
71    fn into_prop_value(self) -> PtrEqRc<T> {
72        PtrEqRc(self)
73    }
74}