diff --git a/src/array.rs b/src/array.rs new file mode 100644 index 0000000..fa6fa6d --- /dev/null +++ b/src/array.rs @@ -0,0 +1,46 @@ +use std::io::{Error, ErrorKind, Result}; +use std::ptr::NonNull; +use crate::ffi::wl_array; +use crate::ffi::{wl_array_add, wl_array_copy, wl_array_init, wl_array_release}; + +pub struct Array { + raw: NonNull, +} +impl Array { + pub fn new() -> Result { + let raw: *mut wl_array = std::ptr::null_mut(); + unsafe { wl_array_init(raw) }; + + Ok(Self { raw: NonNull::new(raw).unwrap() }) + } + + /// Increases the size of the array by size bytes + /// + /// * `size` - Number of bytes to increase the size of the array by + pub fn add(&self, size: usize) -> Result<()> { + let ptr = unsafe { wl_array_add(self.raw.as_ptr(), size) }; + + if ptr.is_null() { + return Err(Error::new(ErrorKind::OutOfMemory, "unable to resize array")); + } + Ok(()) + } + + /// Copies the contents of `source` to this `array` + /// + /// * `source` - Source array to copy from + pub fn copy(&self, source: &mut Array) -> Result<()> { + let ret = unsafe { wl_array_copy(self.raw.as_ptr(), source.raw.as_ptr()) }; + + if ret != 0 { + return Err(Error::new(ErrorKind::Other, "unable to copy array")); + } + Ok(()) + } + + /// Release the array data + /// Leaves the array in an invalid state + pub fn release(&self) { + unsafe { wl_array_release(self.raw.as_ptr()) }; + } +}