ruchy 4.2.0

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
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
//! Safe arena allocator without unsafe code
//!
//! Provides bounded memory allocation using safe Rust abstractions.
use anyhow::{anyhow, Result};
use std::cell::RefCell;
use std::rc::Rc;
// ============================================================================
// Safe Arena Allocator
// ============================================================================
/// Safe arena allocator using Rc for memory management
#[derive(Debug)]
pub struct SafeArena {
    /// Storage for allocated values
    storage: RefCell<Vec<Box<dyn std::any::Any>>>,
    /// Current memory usage estimate
    used: RefCell<usize>,
    /// Maximum allowed memory
    max_size: usize,
}
impl SafeArena {
    /// Create a new arena with the given size limit
    /// # Examples
    ///
    /// ```
    /// use ruchy::runtime::safe_arena::SafeArena;
    ///
    /// let instance = SafeArena::new();
    /// // Verify behavior
    /// ```
    /// # Examples
    ///
    /// ```
    /// use ruchy::runtime::safe_arena::SafeArena;
    ///
    /// let instance = SafeArena::new();
    /// // Verify behavior
    /// ```
    pub fn new(max_size: usize) -> Self {
        Self {
            storage: RefCell::new(Vec::new()),
            used: RefCell::new(0),
            max_size,
        }
    }
    /// Allocate a value in the arena
    pub fn alloc<T: 'static>(&self, value: T) -> Result<ArenaRef<'_, T>> {
        let size = std::mem::size_of::<T>();
        // Check memory limit
        if *self.used.borrow() + size > self.max_size {
            return Err(anyhow!("Arena memory limit exceeded"));
        }
        // Store value in Rc
        let rc_value = Rc::new(value);
        self.storage
            .borrow_mut()
            .push(Box::new(rc_value.clone()) as Box<dyn std::any::Any>);
        *self.used.borrow_mut() += size;
        Ok(ArenaRef {
            value: rc_value,
            _arena: self,
        })
    }
    /// Reset the arena, clearing all allocations
    /// # Examples
    ///
    /// ```
    /// use ruchy::runtime::safe_arena::SafeArena;
    ///
    /// let mut instance = SafeArena::new();
    /// let result = instance.reset();
    /// // Verify behavior
    /// ```
    /// # Examples
    ///
    /// ```ignore
    /// use ruchy::runtime::safe_arena::reset;
    ///
    /// let result = reset(());
    /// assert_eq!(result, Ok(()));
    /// ```
    pub fn reset(&self) {
        self.storage.borrow_mut().clear();
        *self.used.borrow_mut() = 0;
    }
    /// Get current memory usage
    /// # Examples
    ///
    /// ```ignore
    /// use ruchy::runtime::safe_arena::used;
    ///
    /// let result = used(());
    /// assert_eq!(result, Ok(()));
    /// ```
    pub fn used(&self) -> usize {
        *self.used.borrow()
    }
}
/// Reference to a value in the arena
#[derive(Debug)]
pub struct ArenaRef<'a, T> {
    value: Rc<T>,
    _arena: &'a SafeArena,
}
impl<T> std::ops::Deref for ArenaRef<'_, T> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        &self.value
    }
}
// ============================================================================
// Transactional Arena
// ============================================================================
/// Arena with checkpoint/rollback support
#[derive(Debug)]
pub struct TransactionalArena {
    /// Current values
    current: Rc<SafeArena>,
    /// Checkpoints
    checkpoints: Vec<ArenaCheckpoint>,
}
#[derive(Clone, Debug)]
struct ArenaCheckpoint {
    storage_size: usize,
    used: usize,
}
impl TransactionalArena {
    pub fn new(max_size: usize) -> Self {
        Self {
            current: Rc::new(SafeArena::new(max_size)),
            checkpoints: Vec::new(),
        }
    }
    /// # Examples
    ///
    /// ```ignore
    /// use ruchy::runtime::safe_arena::checkpoint;
    ///
    /// let result = checkpoint(());
    /// assert_eq!(result, Ok(()));
    /// ```
    pub fn checkpoint(&mut self) -> usize {
        let checkpoint = ArenaCheckpoint {
            storage_size: self.current.storage.borrow().len(),
            used: self.current.used(),
        };
        self.checkpoints.push(checkpoint);
        self.checkpoints.len() - 1
    }
    /// # Examples
    ///
    /// ```ignore
    /// use ruchy::runtime::safe_arena::rollback;
    ///
    /// let result = rollback(());
    /// assert_eq!(result, Ok(()));
    /// ```
    pub fn rollback(&mut self, checkpoint_id: usize) -> Result<()> {
        if checkpoint_id >= self.checkpoints.len() {
            return Err(anyhow!("Invalid checkpoint"));
        }
        let checkpoint = &self.checkpoints[checkpoint_id];
        // Truncate storage to checkpoint size
        self.current
            .storage
            .borrow_mut()
            .truncate(checkpoint.storage_size);
        *self.current.used.borrow_mut() = checkpoint.used;
        // Remove later checkpoints
        self.checkpoints.truncate(checkpoint_id + 1);
        Ok(())
    }
    /// # Examples
    ///
    /// ```ignore
    /// use ruchy::runtime::safe_arena::commit;
    ///
    /// let result = commit(());
    /// assert_eq!(result, Ok(()));
    /// ```
    pub fn commit(&mut self) -> Result<()> {
        if self.checkpoints.is_empty() {
            return Err(anyhow!("No checkpoint to commit"));
        }
        self.checkpoints.pop();
        Ok(())
    }
    /// # Examples
    ///
    /// ```ignore
    /// use ruchy::runtime::safe_arena::arena;
    ///
    /// let result = arena(());
    /// assert_eq!(result, Ok(()));
    /// ```
    pub fn arena(&self) -> &SafeArena {
        &self.current
    }
    pub fn reset(&mut self) {
        self.current.reset();
        self.checkpoints.clear();
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_safe_arena() {
        let arena = SafeArena::new(1024);
        let v1 = arena.alloc(42).expect("operation should succeed in test");
        let v2 = arena
            .alloc("hello".to_string())
            .expect("operation should succeed in test");
        assert_eq!(*v1, 42);
        assert_eq!(*v2, "hello");
        arena.reset();
        assert_eq!(arena.used(), 0);
    }
    #[test]
    fn test_transactional() {
        let mut arena = TransactionalArena::new(1024);
        arena
            .arena()
            .alloc(1)
            .expect("operation should succeed in test");
        let checkpoint = arena.checkpoint();
        arena
            .arena()
            .alloc(2)
            .expect("operation should succeed in test");
        let used_before = arena.arena().used();
        arena
            .rollback(checkpoint)
            .expect("operation should succeed in test");
        let used_after = arena.arena().used();
        assert!(used_after < used_before);
    }
    #[test]
    fn test_arena_memory_limit() {
        let arena = SafeArena::new(16); // Very small limit
                                        // First allocation should succeed
        let _val1 = arena
            .alloc([0u8; 8])
            .expect("operation should succeed in test");
        // Second allocation should fail due to memory limit
        let result = arena.alloc([0u8; 16]);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("memory limit exceeded"));
    }
    #[test]
    fn test_arena_used_tracking() {
        let arena = SafeArena::new(1024);
        assert_eq!(arena.used(), 0);
        let _val1 = arena
            .alloc(42i32)
            .expect("operation should succeed in test");
        let used_after_int = arena.used();
        assert!(used_after_int >= 4); // At least size of i32
        let _val2 = arena
            .alloc("test".to_string())
            .expect("operation should succeed in test");
        let used_after_string = arena.used();
        assert!(used_after_string > used_after_int);
    }
    #[test]
    fn test_arena_ref_deref() {
        let arena = SafeArena::new(1024);
        let val = arena
            .alloc(vec![1, 2, 3, 4])
            .expect("operation should succeed in test");
        // Test Deref trait
        assert_eq!(val.len(), 4);
        assert_eq!(val[0], 1);
        assert_eq!(val[3], 4);
    }
    #[test]
    fn test_transactional_arena_new() {
        let arena = TransactionalArena::new(2048);
        assert_eq!(arena.arena().used(), 0);
        assert!(arena.checkpoints.is_empty());
    }
    #[test]
    fn test_transactional_arena_multiple_checkpoints() {
        let mut arena = TransactionalArena::new(1024);
        // Initial allocation
        arena
            .arena()
            .alloc(100)
            .expect("operation should succeed in test");
        // First checkpoint
        let cp1 = arena.checkpoint();
        arena
            .arena()
            .alloc(200)
            .expect("operation should succeed in test");
        // Second checkpoint
        let _cp2 = arena.checkpoint();
        arena
            .arena()
            .alloc(300)
            .expect("operation should succeed in test");
        // Rollback to first checkpoint
        arena
            .rollback(cp1)
            .expect("operation should succeed in test");
        // Should only have allocations up to first checkpoint
        let used = arena.arena().used();
        assert!(used >= 4); // At least the first allocation
    }
    #[test]
    fn test_transactional_arena_invalid_checkpoint() {
        let mut arena = TransactionalArena::new(1024);
        // Try to rollback to invalid checkpoint
        let result = arena.rollback(999);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Invalid checkpoint"));
    }
    #[test]
    fn test_transactional_arena_commit() {
        let mut arena = TransactionalArena::new(1024);
        // Create checkpoint
        let _cp = arena.checkpoint();
        assert!(!arena.checkpoints.is_empty());
        // Commit should remove the checkpoint
        arena.commit().expect("operation should succeed in test");
        assert!(arena.checkpoints.is_empty());
    }
    #[test]
    fn test_transactional_arena_commit_without_checkpoint() {
        let mut arena = TransactionalArena::new(1024);
        // Try to commit without checkpoint
        let result = arena.commit();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("No checkpoint to commit"));
    }
    #[test]
    fn test_transactional_arena_reset() {
        let mut arena = TransactionalArena::new(1024);
        // Add some data and checkpoints
        arena
            .arena()
            .alloc(42)
            .expect("operation should succeed in test");
        arena.checkpoint();
        arena
            .arena()
            .alloc(84)
            .expect("operation should succeed in test");
        assert!(arena.arena().used() > 0);
        assert!(!arena.checkpoints.is_empty());
        // Reset should clear everything
        arena.reset();
        assert_eq!(arena.arena().used(), 0);
        assert!(arena.checkpoints.is_empty());
    }
    #[test]
    fn test_checkpoint_clone() {
        let checkpoint1 = ArenaCheckpoint {
            storage_size: 10,
            used: 100,
        };
        let checkpoint2 = checkpoint1.clone();
        assert_eq!(checkpoint1.storage_size, checkpoint2.storage_size);
        assert_eq!(checkpoint1.used, checkpoint2.used);
    }
    #[test]
    fn test_arena_with_different_types() {
        let arena = SafeArena::new(1024);
        // Allocate different types
        let int_val = arena
            .alloc(42i32)
            .expect("operation should succeed in test");
        let string_val = arena
            .alloc("hello".to_string())
            .expect("operation should succeed in test");
        let vec_val = arena
            .alloc(vec![1, 2, 3])
            .expect("operation should succeed in test");
        let bool_val = arena.alloc(true).expect("operation should succeed in test");
        // Verify all values
        assert_eq!(*int_val, 42);
        assert_eq!(*string_val, "hello");
        assert_eq!(*vec_val, vec![1, 2, 3]);
        assert!(*bool_val);
    }
    #[test]
    fn test_transactional_arena_nested_operations() {
        let mut arena = TransactionalArena::new(1024);
        // Initial state
        arena
            .arena()
            .alloc(1)
            .expect("operation should succeed in test");
        let initial_used = arena.arena().used();
        // Start transaction
        let cp = arena.checkpoint();
        arena
            .arena()
            .alloc(2)
            .expect("operation should succeed in test");
        arena
            .arena()
            .alloc(3)
            .expect("operation should succeed in test");
        let mid_used = arena.arena().used();
        assert!(mid_used > initial_used);
        // Rollback
        arena
            .rollback(cp)
            .expect("operation should succeed in test");
        let final_used = arena.arena().used();
        assert_eq!(final_used, initial_used);
    }
    #[test]
    fn test_arena_large_allocation() {
        let arena = SafeArena::new(1024);
        // Try to allocate something larger than limit
        let result = arena.alloc([0u8; 2048]);
        assert!(result.is_err());
    }
    #[test]
    fn test_transactional_checkpoint_return_value() {
        let mut arena = TransactionalArena::new(1024);
        let cp1 = arena.checkpoint();
        assert_eq!(cp1, 0);
        let cp2 = arena.checkpoint();
        assert_eq!(cp2, 1);
        let cp3 = arena.checkpoint();
        assert_eq!(cp3, 2);
    }
}
#[cfg(test)]
mod property_tests_safe_arena {
    use proptest::proptest;

    proptest! {
        /// Property: Function never panics on any input
        #[test]
        fn test_new_never_panics(input: String) {
            // Limit input size to avoid timeout
            let _input = if input.len() > 100 { &input[..100] } else { &input[..] };
            // Function should not panic on any input
            let _ = std::panic::catch_unwind(|| {
                // Call function with various inputs
                // This is a template - adjust based on actual function signature
            });
        }
    }
}

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

    #[test]
    fn test_alloc_memory_limit_comparison_operator() {
        // MISSED: replace > with >= in SafeArena::alloc (line 49)
        let arena = SafeArena::new(100);

        // Allocate items up to but not exceeding limit (> not >=)
        let result1 = arena.alloc(0u64); // 8 bytes
        assert!(result1.is_ok(), "First allocation should succeed");

        let result2 = arena.alloc([0u64; 11]); // 88 bytes (8+88=96 < 100)
        assert!(
            result2.is_ok(),
            "Should allow allocation when total < limit (>)"
        );

        // This should fail (96+8=104 > 100)
        let result3 = arena.alloc(0u64);
        assert!(result3.is_err(), "Should fail when total exceeds limit");
    }
}