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

glib: add Array wrapper #444

Merged
merged 2 commits into from Apr 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions glib/src/array.rs
@@ -0,0 +1,43 @@
// Take a look at the license at the top of the repository in the LICENSE file.

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

wrapper! {
elmarco marked this conversation as resolved.
Show resolved Hide resolved
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Array(Shared<ffi::GArray>);

match fn {
ref => |ptr| ffi::g_array_ref(ptr),
unref => |ptr| ffi::g_array_unref(ptr),
sdroege marked this conversation as resolved.
Show resolved Hide resolved
get_type => || ffi::g_array_get_type(),
}
}

impl Array {
pub fn len(&self) -> usize {
elmarco marked this conversation as resolved.
Show resolved Hide resolved
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()
}
}
2 changes: 2 additions & 0 deletions glib/src/lib.rs
Expand Up @@ -90,6 +90,7 @@ pub use glib_macros::{
clone, gflags, object_interface, object_subclass, Downgrade, GBoxed, GEnum, GErrorDomain,
};

pub use self::array::Array;
pub use self::byte_array::ByteArray;
pub use self::bytes::Bytes;
pub use self::closure::Closure;
Expand Down Expand Up @@ -140,6 +141,7 @@ mod auto;
pub use self::gobject::*;
mod gobject;

mod array;
mod byte_array;
mod bytes;
pub mod char;
Expand Down