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
use crate::serde::Datum;
use crate::types::PVShared;
use derive_more::{Deref, From};
use owning_ref::OwningRef;
use std::cmp::{Ord, PartialOrd};
use std::ops::Deref;
use std::sync::Arc;

/// A sub-portion of a [Value](crate::types::Value)
#[derive(From, Deref, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct SubValue(pub Datum);

#[derive(Clone, Debug)]
pub enum SVShared {
    Own(Arc<SubValue>),
    Ref(OwningRef<PVShared, Datum>),
}

impl Deref for SVShared {
    type Target = SubValue;
    fn deref(&self) -> &SubValue {
        match self {
            Self::Own(own) => own,
            Self::Ref(ownref) => {
                let ptr = ownref as &Datum as *const Datum as *const SubValue;
                unsafe { &*ptr }
            }
        }
    }
}

/* SVShared is comparable against the same type. */
impl PartialEq for SVShared {
    fn eq(&self, other: &SVShared) -> bool {
        (self as &SubValue).eq(other as &SubValue)
    }
}
impl Eq for SVShared {}