facet-value 0.46.0

Memory-efficient dynamic value type for facet, supporting JSON-like data plus bytes
Documentation
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! Extensible "Other" value types using tag 7 with a secondary discriminant.
//!
//! This module provides types that share tag 7 but are distinguished by a
//! secondary `OtherKind` discriminant stored on the heap. This allows for
//! unlimited future extensibility without consuming additional tag bits.
//!
//! Current types:
//! - `VQName`: Qualified name (namespace + local name) for XML namespace support
//! - `VUuid`: 128-bit UUID for preserving semantic identity

#[cfg(feature = "alloc")]
use alloc::alloc::{Layout, alloc, dealloc};
use core::fmt::{self, Debug, Formatter};
use core::hash::{Hash, Hasher};

use crate::value::{TypeTag, Value};

/// Secondary discriminant for "Other" types (tag 7).
///
/// This allows 256 subtypes to share a single tag value.
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum OtherKind {
    /// Qualified name (namespace + local name)
    QName = 0,
    /// UUID (128-bit universally unique identifier)
    Uuid = 1,
}

// ============================================================================
// VQName - Qualified Name
// ============================================================================

/// Header for VQName values.
///
/// Layout: [kind: u8][_pad: 7 bytes][namespace: Value][local_name: Value]
#[repr(C, align(8))]
struct QNameHeader {
    /// The OtherKind discriminant (always QName = 0)
    kind: OtherKind,
    /// Padding for alignment
    _pad: [u8; 7],
    /// Optional namespace (Value::NULL if none)
    namespace: Value,
    /// Local name (always a VString)
    local_name: Value,
}

/// A qualified name consisting of an optional namespace and a local name.
///
/// `VQName` is used for XML namespace support, where elements and attributes
/// can have qualified names like `{http://example.com}element`.
///
/// Both the namespace and local name are stored as `Value`s, allowing them
/// to benefit from inline string optimization for short names.
#[repr(transparent)]
pub struct VQName(pub(crate) Value);

impl VQName {
    const fn layout() -> Layout {
        Layout::new::<QNameHeader>()
    }

    #[cfg(feature = "alloc")]
    fn alloc() -> *mut QNameHeader {
        unsafe { alloc(Self::layout()).cast::<QNameHeader>() }
    }

    #[cfg(feature = "alloc")]
    fn dealloc(ptr: *mut QNameHeader) {
        unsafe {
            dealloc(ptr.cast::<u8>(), Self::layout());
        }
    }

    fn header(&self) -> &QNameHeader {
        unsafe { &*(self.0.heap_ptr() as *const QNameHeader) }
    }

    /// Creates a new qualified name with a namespace and local name.
    #[cfg(feature = "alloc")]
    #[must_use]
    pub fn new(namespace: impl Into<Value>, local_name: impl Into<Value>) -> Self {
        unsafe {
            let ptr = Self::alloc();
            // Use ptr::write to avoid dropping uninitialized memory
            core::ptr::write(&raw mut (*ptr).kind, OtherKind::QName);
            core::ptr::write(&raw mut (*ptr)._pad, [0; 7]);
            core::ptr::write(&raw mut (*ptr).namespace, namespace.into());
            core::ptr::write(&raw mut (*ptr).local_name, local_name.into());
            VQName(Value::new_ptr(ptr.cast(), TypeTag::Other))
        }
    }

    /// Creates a new qualified name without a namespace.
    #[cfg(feature = "alloc")]
    #[must_use]
    pub fn new_local(local_name: impl Into<Value>) -> Self {
        Self::new(Value::NULL, local_name)
    }

    /// Returns the namespace, or `None` if there is no namespace.
    #[must_use]
    pub fn namespace(&self) -> Option<&Value> {
        let ns = &self.header().namespace;
        if ns.is_null() { None } else { Some(ns) }
    }

    /// Returns the local name.
    #[must_use]
    pub fn local_name(&self) -> &Value {
        &self.header().local_name
    }

    /// Returns `true` if this qualified name has a namespace.
    #[must_use]
    pub fn has_namespace(&self) -> bool {
        !self.header().namespace.is_null()
    }

    // === Internal ===

    pub(crate) fn clone_impl(&self) -> Value {
        #[cfg(feature = "alloc")]
        {
            let h = self.header();
            Self::new(h.namespace.clone(), h.local_name.clone()).0
        }
        #[cfg(not(feature = "alloc"))]
        {
            panic!("cannot clone VQName without alloc feature")
        }
    }

    pub(crate) fn drop_impl(&mut self) {
        #[cfg(feature = "alloc")]
        unsafe {
            let ptr = self.0.heap_ptr_mut() as *mut QNameHeader;
            // Drop the contained Values
            core::ptr::drop_in_place(&mut (*ptr).namespace);
            core::ptr::drop_in_place(&mut (*ptr).local_name);
            Self::dealloc(ptr);
        }
    }
}

impl Clone for VQName {
    fn clone(&self) -> Self {
        VQName(self.clone_impl())
    }
}

impl PartialEq for VQName {
    fn eq(&self, other: &Self) -> bool {
        let (h1, h2) = (self.header(), other.header());
        h1.namespace == h2.namespace && h1.local_name == h2.local_name
    }
}

impl Eq for VQName {}

impl Hash for VQName {
    fn hash<H: Hasher>(&self, state: &mut H) {
        let h = self.header();
        h.namespace.hash(state);
        h.local_name.hash(state);
    }
}

impl Debug for VQName {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let h = self.header();
        if h.namespace.is_null() {
            write!(f, "{:?}", h.local_name)
        } else {
            write!(f, "{{{:?}}}{:?}", h.namespace, h.local_name)
        }
    }
}

#[cfg(feature = "alloc")]
impl From<VQName> for Value {
    fn from(qname: VQName) -> Self {
        qname.0
    }
}

// ============================================================================
// VUuid - UUID
// ============================================================================

/// Header for VUuid values.
///
/// Layout: [kind: u8][_pad: 7 bytes][uuid_bytes: 16 bytes]
#[repr(C, align(8))]
struct UuidHeader {
    /// The OtherKind discriminant (always Uuid = 1)
    kind: OtherKind,
    /// Padding for alignment
    _pad: [u8; 7],
    /// The 128-bit UUID in big-endian byte order
    bytes: [u8; 16],
}

/// A 128-bit universally unique identifier (UUID).
///
/// `VUuid` stores UUIDs in their native 128-bit form rather than as
/// 36-character strings, preserving semantic identity while being more
/// memory-efficient.
#[repr(transparent)]
pub struct VUuid(pub(crate) Value);

impl VUuid {
    const fn layout() -> Layout {
        Layout::new::<UuidHeader>()
    }

    #[cfg(feature = "alloc")]
    fn alloc() -> *mut UuidHeader {
        unsafe { alloc(Self::layout()).cast::<UuidHeader>() }
    }

    #[cfg(feature = "alloc")]
    fn dealloc(ptr: *mut UuidHeader) {
        unsafe {
            dealloc(ptr.cast::<u8>(), Self::layout());
        }
    }

    fn header(&self) -> &UuidHeader {
        unsafe { &*(self.0.heap_ptr() as *const UuidHeader) }
    }

    /// Creates a new UUID from 16 bytes (big-endian).
    #[cfg(feature = "alloc")]
    #[must_use]
    pub fn new(bytes: [u8; 16]) -> Self {
        unsafe {
            let ptr = Self::alloc();
            // Use ptr::write to avoid dropping uninitialized memory
            core::ptr::write(&raw mut (*ptr).kind, OtherKind::Uuid);
            core::ptr::write(&raw mut (*ptr)._pad, [0; 7]);
            core::ptr::write(&raw mut (*ptr).bytes, bytes);
            VUuid(Value::new_ptr(ptr.cast(), TypeTag::Other))
        }
    }

    /// Creates a new UUID from two 64-bit integers (high and low parts).
    #[cfg(feature = "alloc")]
    #[must_use]
    pub fn from_u64_pair(high: u64, low: u64) -> Self {
        let mut bytes = [0u8; 16];
        bytes[..8].copy_from_slice(&high.to_be_bytes());
        bytes[8..].copy_from_slice(&low.to_be_bytes());
        Self::new(bytes)
    }

    /// Creates a new UUID from a u128.
    #[cfg(feature = "alloc")]
    #[must_use]
    pub fn from_u128(value: u128) -> Self {
        Self::new(value.to_be_bytes())
    }

    /// Returns the UUID as 16 bytes (big-endian).
    #[must_use]
    pub fn as_bytes(&self) -> &[u8; 16] {
        &self.header().bytes
    }

    /// Returns the UUID as a u128.
    #[must_use]
    pub fn as_u128(&self) -> u128 {
        u128::from_be_bytes(self.header().bytes)
    }

    /// Returns the high 64 bits of the UUID.
    #[must_use]
    pub fn high(&self) -> u64 {
        let bytes = &self.header().bytes;
        u64::from_be_bytes([
            bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
        ])
    }

    /// Returns the low 64 bits of the UUID.
    #[must_use]
    pub fn low(&self) -> u64 {
        let bytes = &self.header().bytes;
        u64::from_be_bytes([
            bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15],
        ])
    }

    // === Internal ===

    pub(crate) fn clone_impl(&self) -> Value {
        #[cfg(feature = "alloc")]
        {
            Self::new(self.header().bytes).0
        }
        #[cfg(not(feature = "alloc"))]
        {
            panic!("cannot clone VUuid without alloc feature")
        }
    }

    pub(crate) fn drop_impl(&mut self) {
        #[cfg(feature = "alloc")]
        unsafe {
            Self::dealloc(self.0.heap_ptr_mut().cast());
        }
    }
}

impl Clone for VUuid {
    fn clone(&self) -> Self {
        VUuid(self.clone_impl())
    }
}

impl PartialEq for VUuid {
    fn eq(&self, other: &Self) -> bool {
        self.header().bytes == other.header().bytes
    }
}

impl Eq for VUuid {}

impl Hash for VUuid {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.header().bytes.hash(state);
    }
}

impl Debug for VUuid {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let bytes = &self.header().bytes;
        // Format as standard UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
        write!(
            f,
            "{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
            bytes[0],
            bytes[1],
            bytes[2],
            bytes[3],
            bytes[4],
            bytes[5],
            bytes[6],
            bytes[7],
            bytes[8],
            bytes[9],
            bytes[10],
            bytes[11],
            bytes[12],
            bytes[13],
            bytes[14],
            bytes[15]
        )
    }
}

#[cfg(feature = "alloc")]
impl From<VUuid> for Value {
    fn from(uuid: VUuid) -> Self {
        uuid.0
    }
}

#[cfg(feature = "alloc")]
impl From<[u8; 16]> for VUuid {
    fn from(bytes: [u8; 16]) -> Self {
        Self::new(bytes)
    }
}

#[cfg(feature = "alloc")]
impl From<u128> for VUuid {
    fn from(value: u128) -> Self {
        Self::from_u128(value)
    }
}

// ============================================================================
// Helper to get OtherKind from a Value with tag 7
// ============================================================================

/// Returns the OtherKind for a Value that has TypeTag::Other.
///
/// # Safety
/// The value must have TypeTag::Other (tag 7) and point to valid heap memory.
pub(crate) unsafe fn get_other_kind(value: &Value) -> OtherKind {
    // The first byte of any Other header is the OtherKind discriminant
    let ptr = value.heap_ptr();
    unsafe { *(ptr as *const OtherKind) }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::VString;

    #[test]
    fn test_qname_with_namespace() {
        let qname = VQName::new(VString::new("http://example.com"), VString::new("element"));
        assert!(qname.has_namespace());
        assert_eq!(
            qname.namespace().unwrap().as_string().unwrap().as_str(),
            "http://example.com"
        );
        assert_eq!(qname.local_name().as_string().unwrap().as_str(), "element");
    }

    #[test]
    fn test_qname_local_only() {
        let qname = VQName::new_local(VString::new("element"));
        assert!(!qname.has_namespace());
        assert!(qname.namespace().is_none());
        assert_eq!(qname.local_name().as_string().unwrap().as_str(), "element");
    }

    #[test]
    fn test_qname_clone() {
        let qname = VQName::new(VString::new("ns"), VString::new("local"));
        let cloned = qname.clone();
        assert_eq!(qname, cloned);
    }

    #[test]
    fn test_qname_debug() {
        let qname = VQName::new(VString::new("ns"), VString::new("local"));
        let debug = format!("{qname:?}");
        assert!(debug.contains("ns"));
        assert!(debug.contains("local"));
    }

    #[test]
    fn test_uuid_new() {
        let bytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
        let uuid = VUuid::new(bytes);
        assert_eq!(uuid.as_bytes(), &bytes);
    }

    #[test]
    fn test_uuid_from_u128() {
        let value: u128 = 0x0102030405060708090a0b0c0d0e0f10;
        let uuid = VUuid::from_u128(value);
        assert_eq!(uuid.as_u128(), value);
    }

    #[test]
    fn test_uuid_high_low() {
        let uuid = VUuid::from_u64_pair(0x0102030405060708, 0x090a0b0c0d0e0f10);
        assert_eq!(uuid.high(), 0x0102030405060708);
        assert_eq!(uuid.low(), 0x090a0b0c0d0e0f10);
    }

    #[test]
    fn test_uuid_clone() {
        let uuid = VUuid::from_u128(0x12345678_9abc_def0_1234_56789abcdef0);
        let cloned = uuid.clone();
        assert_eq!(uuid, cloned);
    }

    #[test]
    fn test_uuid_debug_format() {
        let uuid = VUuid::from_u128(0x12345678_9abc_def0_1234_56789abcdef0);
        let debug = format!("{uuid:?}");
        assert_eq!(debug, "12345678-9abc-def0-1234-56789abcdef0");
    }
}