added array bindings

This commit is contained in:
neo
2026-08-24 19:30:32 +02:00
parent 7dc656d705
commit ae0fe2aa44
+46
View File
@@ -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<wl_array>,
}
impl Array {
pub fn new() -> Result<Self> {
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()) };
}
}