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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use std::fmt;

use kstring::KStringCow;

use super::DisplayCow;
use super::State;
use super::Value;
use super::{ValueView, ValueViewCmp};
use crate::array::{Array, ArrayView};
use crate::object::{Object, ObjectView};
use crate::scalar::{Scalar, ScalarCow};

/// Abstract the lifetime of a `Value`.
#[derive(Clone, Debug)]
pub enum ValueCow<'s> {
    /// A boxed `Value`
    Owned(Value),
    /// A borrowed `Value`
    Borrowed(&'s dyn ValueView),
}

impl<'s> ValueCow<'s> {
    /// Extracts the owned data.
    ///
    /// Clones the data if it is not already owned.
    pub fn into_owned(self) -> Value {
        match self {
            ValueCow::Owned(x) => x,
            ValueCow::Borrowed(x) => x.to_value(),
        }
    }

    /// Performs the conversion.
    pub fn as_view(&self) -> &dyn ValueView {
        match self {
            ValueCow::Owned(o) => o.as_view(),
            ValueCow::Borrowed(b) => *b,
        }
    }
}

impl<'s> ValueView for ValueCow<'s> {
    fn as_debug(&self) -> &dyn fmt::Debug {
        self
    }

    fn render(&self) -> DisplayCow<'_> {
        self.as_view().render()
    }
    fn source(&self) -> DisplayCow<'_> {
        self.as_view().source()
    }
    fn type_name(&self) -> &'static str {
        self.as_view().type_name()
    }
    fn query_state(&self, state: State) -> bool {
        self.as_view().query_state(state)
    }

    fn to_kstr(&self) -> KStringCow<'_> {
        self.as_view().to_kstr()
    }
    fn to_value(&self) -> Value {
        self.as_view().to_value()
    }

    fn as_scalar(&self) -> Option<ScalarCow<'_>> {
        self.as_view().as_scalar()
    }

    fn as_array(&self) -> Option<&dyn ArrayView> {
        self.as_view().as_array()
    }

    fn as_object(&self) -> Option<&dyn ObjectView> {
        self.as_view().as_object()
    }

    fn as_state(&self) -> Option<State> {
        self.as_view().as_state()
    }

    fn is_nil(&self) -> bool {
        self.as_view().is_nil()
    }
}

impl From<Value> for ValueCow<'static> {
    fn from(other: Value) -> Self {
        ValueCow::Owned(other)
    }
}

impl<'s> From<&'s Value> for ValueCow<'s> {
    fn from(other: &'s Value) -> Self {
        ValueCow::Borrowed(other.as_view())
    }
}

impl From<Scalar> for ValueCow<'static> {
    fn from(other: Scalar) -> Self {
        ValueCow::Owned(Value::Scalar(other))
    }
}

impl From<Array> for ValueCow<'static> {
    fn from(other: Array) -> Self {
        ValueCow::Owned(Value::Array(other))
    }
}

impl From<Object> for ValueCow<'static> {
    fn from(other: Object) -> Self {
        ValueCow::Owned(Value::Object(other))
    }
}

impl From<State> for ValueCow<'static> {
    fn from(other: State) -> Self {
        ValueCow::Owned(Value::State(other))
    }
}

impl<'v> Default for ValueCow<'v> {
    fn default() -> Self {
        ValueCow::Owned(Value::default())
    }
}

impl<'v> PartialEq<ValueCow<'v>> for ValueCow<'v> {
    fn eq(&self, other: &Self) -> bool {
        crate::value_eq(self.as_view(), other.as_view())
    }
}

impl<'v> PartialEq<ValueViewCmp<'v>> for ValueCow<'v> {
    fn eq(&self, other: &ValueViewCmp<'v>) -> bool {
        ValueViewCmp::new(self.as_view()) == *other
    }
}

impl<'v> PartialEq<Value> for ValueCow<'v> {
    fn eq(&self, other: &Value) -> bool {
        crate::value_eq(self.as_view(), other.as_view())
    }
}

impl<'v> PartialEq<i32> for ValueCow<'v> {
    fn eq(&self, other: &i32) -> bool {
        crate::value_eq(self.as_view(), other)
    }
}

impl<'v> PartialEq<f64> for ValueCow<'v> {
    fn eq(&self, other: &f64) -> bool {
        crate::value_eq(self.as_view(), other)
    }
}

impl<'v> PartialEq<bool> for ValueCow<'v> {
    fn eq(&self, other: &bool) -> bool {
        crate::value_eq(self.as_view(), other)
    }
}

impl<'v> PartialEq<crate::scalar::DateTime> for ValueCow<'v> {
    fn eq(&self, other: &crate::scalar::DateTime) -> bool {
        crate::value_eq(self.as_view(), other)
    }
}

impl<'v> PartialEq<crate::scalar::Date> for ValueCow<'v> {
    fn eq(&self, other: &crate::scalar::Date) -> bool {
        crate::value_eq(self.as_view(), other)
    }
}

impl<'v> PartialEq<str> for ValueCow<'v> {
    fn eq(&self, other: &str) -> bool {
        let other = KStringCow::from_ref(other);
        crate::value_eq(self.as_view(), &other)
    }
}

impl<'v> PartialEq<&'v str> for ValueCow<'v> {
    fn eq(&self, other: &&str) -> bool {
        crate::value_eq(self.as_view(), other)
    }
}

impl<'v> PartialEq<String> for ValueCow<'v> {
    fn eq(&self, other: &String) -> bool {
        crate::value_eq(self.as_view(), other)
    }
}

impl<'v> PartialEq<kstring::KString> for ValueCow<'v> {
    fn eq(&self, other: &kstring::KString) -> bool {
        crate::value_eq(self.as_view(), &other.as_ref())
    }
}

impl<'v> PartialEq<kstring::KStringRef<'v>> for ValueCow<'v> {
    fn eq(&self, other: &kstring::KStringRef<'v>) -> bool {
        crate::value_eq(self.as_view(), other)
    }
}

impl<'v> PartialEq<kstring::KStringCow<'v>> for ValueCow<'v> {
    fn eq(&self, other: &kstring::KStringCow<'v>) -> bool {
        crate::value_eq(self.as_view(), other)
    }
}