luaur_ast/methods/parser_copy_parser.rs
1use crate::records::ast_array::AstArray;
2use crate::records::parser::Parser;
3
4impl Parser {
5 pub fn copy_t_usize<T: Clone>(&mut self, data: *const T, size: usize) -> AstArray<T> {
6 let mut result = AstArray {
7 data: core::ptr::null_mut(),
8 size,
9 };
10
11 if size == 0 {
12 return result;
13 }
14
15 unsafe {
16 // allocator is a *mut Allocator in the Parser struct
17 let storage = crate::records::allocator::Allocator::allocate(
18 &mut *self.allocator,
19 core::mem::size_of::<T>() * size,
20 ) as *mut T;
21
22 result.data = storage;
23
24 // The C++ implementation uses placement new with the copy constructor.
25 // In Rust, for types that are Clone, we can use ptr::write with clone().
26 // Since Luau AST nodes/types in this context are POD-like or managed by the arena
27 // and don't have destructors (as noted in the C++ comment), this is a safe mapping.
28 for i in 0..size {
29 let src = &*data.add(i);
30 core::ptr::write(result.data.add(i), src.clone());
31 }
32 }
33
34 result
35 }
36}