Compare commits
2
Commits
7dc656d705
...
0cb4086729
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0cb4086729 | ||
|
|
ae0fe2aa44 |
@@ -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()) };
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
#![feature(c_variadic)]
|
#![feature(c_variadic)]
|
||||||
|
|
||||||
pub(crate) mod ffi;
|
pub(crate) mod ffi;
|
||||||
|
|
||||||
|
// public api
|
||||||
|
pub mod array;
|
||||||
|
|||||||
Reference in New Issue
Block a user