tract_data/
opaque.rs

1#![allow(clippy::derived_hash_with_manual_eq)]
2use crate::datum::DatumType;
3use crate::dim::TDim;
4use crate::internal::{TVec, Tensor, TractResult};
5use std::fmt::{Debug, Display};
6use std::hash::Hash;
7use std::ops::Deref;
8use std::sync::Arc;
9
10use downcast_rs::{impl_downcast, Downcast};
11use dyn_hash::DynHash;
12
13pub trait OpaquePayload: DynHash + Send + Sync + Debug + Display + Downcast {
14    fn clarify_to_tensor(&self) -> TractResult<Option<Arc<Tensor>>> {
15        Ok(None)
16    }
17
18    fn same_as(&self, other: &dyn OpaquePayload) -> bool;
19}
20impl_downcast!(OpaquePayload);
21dyn_hash::hash_trait_object!(OpaquePayload);
22
23pub trait OpaqueFact: DynHash + Send + Sync + Debug + dyn_clone::DynClone + Downcast {
24    fn same_as(&self, other: &dyn OpaqueFact) -> bool;
25
26    /// Whether or not it is acceptable for a Patch to substitute `self` by `other`.
27    ///
28    /// In other terms, all operators consuming `self` MUST accept also accept `other` without being altered.
29    fn compatible_with(&self, other: &dyn OpaqueFact) -> bool {
30        self.same_as(other)
31    }
32
33    fn clarify_dt_shape(&self) -> Option<(DatumType, &[usize])> {
34        None
35    }
36
37    fn mem_size(&self) -> TDim;
38}
39
40impl_downcast!(OpaqueFact);
41dyn_hash::hash_trait_object!(OpaqueFact);
42dyn_clone::clone_trait_object!(OpaqueFact);
43
44impl<T: OpaqueFact> From<T> for Box<dyn OpaqueFact> {
45    fn from(v: T) -> Self {
46        Box::new(v)
47    }
48}
49
50impl PartialEq for Box<dyn OpaqueFact> {
51    fn eq(&self, other: &Self) -> bool {
52        self.as_ref().same_as(other.as_ref())
53    }
54}
55
56impl Eq for Box<dyn OpaqueFact> {}
57
58impl OpaqueFact for TVec<Box<dyn OpaqueFact>> {
59    fn mem_size(&self) -> TDim {
60        self.iter().map(|it| it.mem_size()).sum()
61    }
62
63    fn same_as(&self, other: &dyn OpaqueFact) -> bool {
64        other.downcast_ref::<Self>().is_some_and(|o| self == o)
65    }
66}
67impl OpaqueFact for TVec<Option<Box<dyn OpaqueFact>>> {
68    fn mem_size(&self) -> TDim {
69        self.iter().flatten().map(|it| it.mem_size()).sum()
70    }
71
72    fn same_as(&self, other: &dyn OpaqueFact) -> bool {
73        other.downcast_ref::<Self>().is_some_and(|o| self == o)
74    }
75}
76
77#[derive(Debug, Hash, PartialEq, Eq)]
78pub struct DummyPayload;
79
80impl OpaquePayload for DummyPayload {
81    fn same_as(&self, other: &dyn OpaquePayload) -> bool {
82        other.downcast_ref::<Self>().is_some()
83    }
84}
85
86impl Display for DummyPayload {
87    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88        write!(f, "DummyPayload")
89    }
90}
91
92#[derive(Clone, Debug, Hash)]
93pub struct Opaque(pub Arc<dyn OpaquePayload>);
94
95impl Opaque {
96    pub fn downcast_ref<T: OpaquePayload>(&self) -> Option<&T> {
97        (*self.0).downcast_ref::<T>()
98    }
99
100    pub fn downcast_mut<T: OpaquePayload>(&mut self) -> Option<&mut T> {
101        Arc::get_mut(&mut self.0).and_then(|it| it.downcast_mut::<T>())
102    }
103}
104
105impl Deref for Opaque {
106    type Target = dyn OpaquePayload;
107    fn deref(&self) -> &Self::Target {
108        &*self.0
109    }
110}
111
112impl Display for Opaque {
113    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
114        write!(f, "{}", self.0)
115    }
116}
117
118impl Default for Opaque {
119    fn default() -> Self {
120        Opaque(Arc::new(DummyPayload))
121    }
122}
123
124impl PartialEq for Opaque {
125    fn eq(&self, other: &Self) -> bool {
126        Arc::ptr_eq(&self.0, &other.0) && self.0.same_as(&*other.0)
127    }
128}