use crate::runtime::{ Variable, acquire, release, array_length, array_get, array_set }; use super::{Type, array}; pub struct Array { managed:bool, addr:Variable, } impl Array { pub fn new(size:usize, class:usize) -> Self { Self { managed:true, addr:unsafe {acquire(array(size, class))}, } } pub fn from(addr:Variable) -> Self { Self { managed:false, addr:addr } } pub fn length(&self) -> usize { unsafe {array_length(self.addr)} } pub fn set(&mut self, index:usize, source:Variable) { unsafe { array_set(self.addr, index, source); } } pub fn get(&self, index:usize) -> Type { Type::from(unsafe {array_get(self.addr, index)}) } } impl std::ops::Deref for Array { type Target = Variable; fn deref(&self) -> &Self::Target { return &self.addr; } } impl Drop for Array { fn drop(&mut self) { if self.managed { unsafe {release(self.addr)}; } } }