hedl-core 2.0.0

Core parser and data model for HEDL (Hierarchical Entity Data Language)
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// Dweve HEDL - Hierarchical Entity Data Language
//
// Copyright (c) 2025 Dweve IP B.V. and individual contributors.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE file at the
// root of this repository or at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Arena allocator requires unsafe for performance-critical pointer operations.
// Safety invariants are documented at each unsafe block.
#![allow(unsafe_code)]

//! Arena-backed vector for small, fixed-size collections.
//!
//! ArenaVec stores its contents directly in an arena, avoiding heap
//! allocation for small collections like node fields (typically 3-7 values).
//!
//! Unlike `Vec<T>` where the struct is arena-allocated but the buffer is
//! heap-allocated, ArenaVec stores the actual data in the arena.

use bumpalo::Bump;
use std::marker::PhantomData;
use std::ops::{Deref, Index};
use std::ptr::NonNull;
use std::slice;

/// Arena-backed vector (read-only after creation).
///
/// Stores its contents directly in an arena. This is efficient for small,
/// fixed-size collections that don't need to grow after creation.
///
/// # Performance Characteristics
///
/// - **Creation**: O(n) to copy n elements into arena
/// - **Access**: O(1) index access
/// - **Memory**: No heap allocation (data in arena)
/// - **Cache**: Better locality than heap-scattered Vec
///
/// # Limitations
///
/// - **Read-only**: Cannot push/pop after creation (use Vec for that)
/// - **Fixed-size**: Cannot grow (arena allocation is bump-only)
/// - **Lifetime-bounded**: Tied to arena lifetime
///
/// # Examples
///
/// ```ignore
/// use bumpalo::Bump;
/// use hedl_core::lex::arena::ArenaVec;
///
/// let arena = Bump::new();
/// let vec = ArenaVec::from_slice(&arena, &[1, 2, 3, 4, 5]);
///
/// assert_eq!(vec.len(), 5);
/// assert_eq!(vec[0], 1);
/// assert_eq!(vec[4], 5);
///
/// // Iterate
/// for (i, &val) in vec.iter().enumerate() {
///     assert_eq!(val, i as i32 + 1);
/// }
/// ```
pub struct ArenaVec<'arena, T> {
    /// Pointer to the first element
    ptr: NonNull<T>,
    /// Number of elements
    len: usize,
    /// Phantom data to track lifetime and ownership
    _marker: PhantomData<&'arena [T]>,
}

impl<'arena, T> ArenaVec<'arena, T> {
    /// Create an empty ArenaVec.
    ///
    /// This doesn't allocate anything in the arena.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use hedl_core::lex::arena::ArenaVec;
    ///
    /// let vec: ArenaVec<i32> = ArenaVec::empty();
    /// assert_eq!(vec.len(), 0);
    /// assert!(vec.is_empty());
    /// ```
    pub fn empty() -> Self {
        Self {
            ptr: NonNull::dangling(),
            len: 0,
            _marker: PhantomData,
        }
    }

    /// Create an ArenaVec from a slice.
    ///
    /// Copies the slice elements into the arena.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use bumpalo::Bump;
    /// use hedl_core::lex::arena::ArenaVec;
    ///
    /// let arena = Bump::new();
    /// let data = [1, 2, 3, 4, 5];
    /// let vec = ArenaVec::from_slice(&arena, &data);
    ///
    /// assert_eq!(vec.len(), 5);
    /// assert_eq!(&vec[..], &data);
    /// ```
    pub fn from_slice(arena: &'arena Bump, slice: &[T]) -> Self
    where
        T: Copy,
    {
        if slice.is_empty() {
            return Self::empty();
        }

        // Allocate storage in arena and copy elements
        let arena_storage = arena.alloc_slice_copy(slice);

        Self {
            ptr: NonNull::new(arena_storage.as_mut_ptr()).expect("arena allocation is non-null"),
            len: slice.len(),
            _marker: PhantomData,
        }
    }

    /// Create an ArenaVec from an iterator.
    ///
    /// Collects the iterator into a temporary Vec, then copies to arena.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use bumpalo::Bump;
    /// use hedl_core::lex::arena::ArenaVec;
    ///
    /// let arena = Bump::new();
    /// let vec = ArenaVec::from_iter(&arena, [1, 2, 3, 4, 5].iter().copied());
    ///
    /// assert_eq!(vec.len(), 5);
    /// ```
    pub fn from_iter(arena: &'arena Bump, iter: impl IntoIterator<Item = T>) -> Self
    where
        T: Copy,
    {
        let items: Vec<T> = iter.into_iter().collect();
        Self::from_slice(arena, &items)
    }

    /// Get the number of elements.
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns true if the vector is empty.
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Get a slice of all elements.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use bumpalo::Bump;
    /// use hedl_core::lex::arena::ArenaVec;
    ///
    /// let arena = Bump::new();
    /// let vec = ArenaVec::from_slice(&arena, &[1, 2, 3]);
    ///
    /// let slice: &[i32] = vec.as_slice();
    /// assert_eq!(slice, &[1, 2, 3]);
    /// ```
    pub fn as_slice(&self) -> &[T] {
        if self.len == 0 {
            &[]
        } else {
            // SAFETY: ptr and len were created from valid slice
            unsafe { slice::from_raw_parts(self.ptr.as_ptr(), self.len) }
        }
    }

    /// Get an iterator over the elements.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use bumpalo::Bump;
    /// use hedl_core::lex::arena::ArenaVec;
    ///
    /// let arena = Bump::new();
    /// let vec = ArenaVec::from_slice(&arena, &[1, 2, 3]);
    ///
    /// let sum: i32 = vec.iter().sum();
    /// assert_eq!(sum, 6);
    /// ```
    pub fn iter(&self) -> slice::Iter<'_, T> {
        self.as_slice().iter()
    }

    /// Get a reference to an element by index.
    ///
    /// Returns None if index is out of bounds.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use bumpalo::Bump;
    /// use hedl_core::lex::arena::ArenaVec;
    ///
    /// let arena = Bump::new();
    /// let vec = ArenaVec::from_slice(&arena, &[10, 20, 30]);
    ///
    /// assert_eq!(vec.get(0), Some(&10));
    /// assert_eq!(vec.get(2), Some(&30));
    /// assert_eq!(vec.get(3), None);
    /// ```
    pub fn get(&self, index: usize) -> Option<&T> {
        self.as_slice().get(index)
    }

    /// Get the first element.
    ///
    /// Returns None if the vector is empty.
    pub fn first(&self) -> Option<&T> {
        self.as_slice().first()
    }

    /// Get the last element.
    ///
    /// Returns None if the vector is empty.
    pub fn last(&self) -> Option<&T> {
        self.as_slice().last()
    }

    /// Convert to an owned Vec by cloning all elements.
    ///
    /// This is used when converting arena-backed temporary data to
    /// heap-allocated final document.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use bumpalo::Bump;
    /// use hedl_core::lex::arena::ArenaVec;
    ///
    /// let arena = Bump::new();
    /// let vec = ArenaVec::from_slice(&arena, &[1, 2, 3]);
    ///
    /// let owned: Vec<i32> = vec.to_vec();
    /// assert_eq!(owned, vec![1, 2, 3]);
    /// ```
    pub fn to_vec(&self) -> Vec<T>
    where
        T: Clone,
    {
        self.as_slice().to_vec()
    }
}

// Deref to slice for convenient access
impl<'arena, T> Deref for ArenaVec<'arena, T> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        self.as_slice()
    }
}

// Index access
impl<'arena, T> Index<usize> for ArenaVec<'arena, T> {
    type Output = T;

    fn index(&self, index: usize) -> &Self::Output {
        &self.as_slice()[index]
    }
}

// Clone (shallow - just copies via Copy since this type is Copy)
impl<'arena, T> Clone for ArenaVec<'arena, T> {
    fn clone(&self) -> Self {
        *self
    }
}

// Copy (for cheap copying of the vec reference)
impl<'arena, T> Copy for ArenaVec<'arena, T> {}

// Debug
impl<'arena, T: std::fmt::Debug> std::fmt::Debug for ArenaVec<'arena, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.as_slice().fmt(f)
    }
}

// PartialEq
impl<'arena, T: PartialEq> PartialEq for ArenaVec<'arena, T> {
    fn eq(&self, other: &Self) -> bool {
        self.as_slice() == other.as_slice()
    }
}

impl<'arena, T: Eq> Eq for ArenaVec<'arena, T> {}

// PartialEq with slice
impl<'arena, T: PartialEq> PartialEq<[T]> for ArenaVec<'arena, T> {
    fn eq(&self, other: &[T]) -> bool {
        self.as_slice() == other
    }
}

impl<'arena, T: PartialEq> PartialEq<&[T]> for ArenaVec<'arena, T> {
    fn eq(&self, other: &&[T]) -> bool {
        self.as_slice() == *other
    }
}

impl<'arena, T: PartialEq> PartialEq<Vec<T>> for ArenaVec<'arena, T> {
    fn eq(&self, other: &Vec<T>) -> bool {
        self.as_slice() == other.as_slice()
    }
}

// IntoIterator
impl<'arena, T> IntoIterator for ArenaVec<'arena, T> {
    type Item = &'arena T;
    type IntoIter = slice::Iter<'arena, T>;

    fn into_iter(self) -> Self::IntoIter {
        // SAFETY: We're creating an iterator with the same lifetime as the arena
        unsafe { slice::from_raw_parts(self.ptr.as_ptr(), self.len).iter() }
    }
}

impl<'a, 'arena, T> IntoIterator for &'a ArenaVec<'arena, T> {
    type Item = &'a T;
    type IntoIter = slice::Iter<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

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

    #[test]
    fn test_empty() {
        let vec: ArenaVec<'_, i32> = ArenaVec::empty();
        assert_eq!(vec.len(), 0);
        assert!(vec.is_empty());
        assert_eq!(vec.as_slice(), &[]);
    }

    #[test]
    fn test_from_slice() {
        let arena = Bump::new();
        let data = [1, 2, 3, 4, 5];
        let vec = ArenaVec::from_slice(&arena, &data);

        assert_eq!(vec.len(), 5);
        assert!(!vec.is_empty());
        assert_eq!(vec.as_slice(), &data);
    }

    #[test]
    fn test_from_empty_slice() {
        let arena = Bump::new();
        let vec = ArenaVec::from_slice(&arena, &[] as &[i32]);

        assert_eq!(vec.len(), 0);
        assert!(vec.is_empty());
    }

    #[test]
    fn test_from_iter() {
        let arena = Bump::new();
        let vec = ArenaVec::from_iter(&arena, [1, 2, 3, 4, 5].iter().copied());

        assert_eq!(vec.len(), 5);
        assert_eq!(vec.as_slice(), &[1, 2, 3, 4, 5]);
    }

    #[test]
    fn test_indexing() {
        let arena = Bump::new();
        let vec = ArenaVec::from_slice(&arena, &[10, 20, 30, 40, 50]);

        assert_eq!(vec[0], 10);
        assert_eq!(vec[2], 30);
        assert_eq!(vec[4], 50);
    }

    #[test]
    fn test_get() {
        let arena = Bump::new();
        let vec = ArenaVec::from_slice(&arena, &[10, 20, 30]);

        assert_eq!(vec.get(0), Some(&10));
        assert_eq!(vec.get(2), Some(&30));
        assert_eq!(vec.get(3), None);
    }

    #[test]
    fn test_first_last() {
        let arena = Bump::new();
        let vec = ArenaVec::from_slice(&arena, &[10, 20, 30]);

        assert_eq!(vec.first(), Some(&10));
        assert_eq!(vec.last(), Some(&30));

        let empty: ArenaVec<'_, i32> = ArenaVec::empty();
        assert_eq!(empty.first(), None);
        assert_eq!(empty.last(), None);
    }

    #[test]
    fn test_iter() {
        let arena = Bump::new();
        let vec = ArenaVec::from_slice(&arena, &[1, 2, 3, 4, 5]);

        let sum: i32 = vec.iter().sum();
        assert_eq!(sum, 15);

        let doubled: Vec<i32> = vec.iter().map(|x| x * 2).collect();
        assert_eq!(doubled, vec![2, 4, 6, 8, 10]);
    }

    #[test]
    fn test_into_iter() {
        let arena = Bump::new();
        let vec = ArenaVec::from_slice(&arena, &[1, 2, 3]);

        let sum: i32 = vec.into_iter().sum();
        assert_eq!(sum, 6);
    }

    #[test]
    fn test_to_vec() {
        let arena = Bump::new();
        let vec = ArenaVec::from_slice(&arena, &[1, 2, 3]);

        let owned = vec.to_vec();
        assert_eq!(owned, vec![1, 2, 3]);
    }

    #[test]
    fn test_clone_copy() {
        let arena = Bump::new();
        let vec1 = ArenaVec::from_slice(&arena, &[1, 2, 3]);

        let vec2 = vec1;
        let vec3 = vec1; // Copy

        assert_eq!(vec1, vec2);
        assert_eq!(vec1, vec3);
        assert_eq!(vec2, vec3);
    }

    #[test]
    fn test_equality() {
        let arena = Bump::new();
        let vec1 = ArenaVec::from_slice(&arena, &[1, 2, 3]);
        let vec2 = ArenaVec::from_slice(&arena, &[1, 2, 3]);
        let vec3 = ArenaVec::from_slice(&arena, &[1, 2, 4]);

        assert_eq!(vec1, vec2);
        assert_ne!(vec1, vec3);

        // Compare with slice
        assert_eq!(vec1, &[1, 2, 3][..]);
        assert_eq!(vec1.as_slice(), &[1, 2, 3]);

        // Compare with Vec
        assert_eq!(vec1, vec![1, 2, 3]);
        assert_ne!(vec1, vec![1, 2, 4]);
    }

    #[test]
    fn test_debug() {
        let arena = Bump::new();
        let vec = ArenaVec::from_slice(&arena, &[1, 2, 3]);

        let debug = format!("{:?}", vec);
        assert_eq!(debug, "[1, 2, 3]");
    }

    #[test]
    fn test_deref() {
        let arena = Bump::new();
        let vec = ArenaVec::from_slice(&arena, &[1, 2, 3, 4, 5]);

        // Can use slice methods via Deref
        assert!(vec.contains(&3));
        assert!(!vec.contains(&10));
        assert_eq!(vec.binary_search(&3), Ok(2));
    }

    #[test]
    fn test_strings() {
        let arena = Bump::new();
        let strings = vec!["hello", "world", "test"];
        let vec = ArenaVec::from_slice(&arena, &strings);

        assert_eq!(vec.len(), 3);
        assert_eq!(vec[0], "hello");
        assert_eq!(vec[1], "world");
        assert_eq!(vec[2], "test");
    }

    #[test]
    fn test_large_vec() {
        let arena = Bump::new();
        let data: Vec<i32> = (0..1000).collect();
        let vec = ArenaVec::from_slice(&arena, &data);

        assert_eq!(vec.len(), 1000);
        assert_eq!(vec[0], 0);
        assert_eq!(vec[999], 999);

        let sum: i32 = vec.iter().sum();
        assert_eq!(sum, 999 * 1000 / 2); // Sum of 0..1000
    }

    #[test]
    fn test_multiple_vecs_same_arena() {
        let arena = Bump::new();

        let vec1 = ArenaVec::from_slice(&arena, &[1, 2, 3]);
        let vec2 = ArenaVec::from_slice(&arena, &[4, 5, 6]);
        let vec3 = ArenaVec::from_slice(&arena, &[7, 8, 9]);

        assert_eq!(vec1.as_slice(), &[1, 2, 3]);
        assert_eq!(vec2.as_slice(), &[4, 5, 6]);
        assert_eq!(vec3.as_slice(), &[7, 8, 9]);
    }

    #[test]
    fn test_slice_operations() {
        let arena = Bump::new();
        let vec = ArenaVec::from_slice(&arena, &[1, 2, 3, 4, 5]);

        // Can use slice syntax via Deref
        let slice = vec.as_slice();
        assert_eq!(&slice[1..4], &[2, 3, 4]);
        assert_eq!(&slice[..3], &[1, 2, 3]);
        assert_eq!(&slice[2..], &[3, 4, 5]);
    }
}