1use crate::internal::*;
2use std::ops::Deref;
3
4use tract_ndarray::Array;
5
6#[derive(Clone, Eq)]
7pub struct TValue(Arc<Tensor>);
8
9impl std::fmt::Debug for TValue {
10 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
11 (**self).fmt(f)
12 }
13}
14
15impl PartialEq for TValue {
16 fn eq(&self, other: &Self) -> bool {
17 self.deref() == other.deref()
18 }
19}
20
21impl TValue {
22 pub fn is_exclusive(&self) -> bool {
23 Arc::strong_count(&self.0) == 1
24 }
25
26 pub fn from_const(t: Arc<Tensor>) -> Self {
27 TValue(t)
28 }
29
30 pub fn as_arc_tensor(&self) -> Option<&Arc<Tensor>> {
31 Some(&self.0)
32 }
33}
34
35impl From<Tensor> for TValue {
36 fn from(t: Tensor) -> Self {
37 TValue(Arc::new(t))
38 }
39}
40
41impl std::ops::Deref for TValue {
42 type Target = Tensor;
43 fn deref(&self) -> &Self::Target {
44 &self.0
45 }
46}
47
48impl std::borrow::Borrow<Tensor> for TValue {
49 fn borrow(&self) -> &Tensor {
50 self
51 }
52}
53
54impl IntoTensor for TValue {
55 fn into_tensor(self) -> Tensor {
56 self.0.into_tensor()
57 }
58}
59
60impl IntoArcTensor for TValue {
61 fn into_arc_tensor(self) -> Arc<Tensor> {
62 self.0
63 }
64}
65
66pub trait IntoTValue {
67 fn into_tvalue(self) -> TValue;
68}
69
70impl IntoTValue for Tensor {
71 fn into_tvalue(self) -> TValue {
72 self.into_tensor().into()
73 }
74}
75
76impl IntoTValue for Arc<Tensor> {
77 fn into_tvalue(self) -> TValue {
78 TValue(self)
79 }
80}
81
82impl<D: ::ndarray::Dimension, T: Datum> IntoTValue for Array<T, D> {
83 fn into_tvalue(self) -> TValue {
84 Tensor::from(self).into_tvalue()
85 }
86}