Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
glib: add Array len(), data() and element_size()
Browse files Browse the repository at this point in the history
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
  • Loading branch information
elmarco committed Apr 11, 2021
1 parent d0f78b8 commit 78b46a2
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion glib/src/array.rs
@@ -1,7 +1,10 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::translate::*;
use std::fmt;

wrapper! {
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Array(Shared<ffi::GArray>);

match fn {
Expand All @@ -10,3 +13,31 @@ wrapper! {
get_type => || ffi::g_array_get_type(),
}
}

impl Array {
pub fn len(&self) -> usize {
unsafe { (*self.to_glib_none().0).len as usize }
}

pub fn is_empty(&self) -> bool {
self.len() == 0
}

pub fn data(&self) -> *mut libc::c_void {
unsafe { (*self.to_glib_none().0).data as _ }
}

pub fn element_size(&self) -> usize {
unsafe { ffi::g_array_get_element_size(self.to_glib_none().0) as usize }
}
}

impl fmt::Debug for Array {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Array")
.field("len", &self.len())
.field("data", &self.data())
.field("element_size", &self.element_size())
.finish()
}
}

0 comments on commit 78b46a2

Please sign in to comment.