#[cfg(not(windows))]
use std::os::unix::prelude::*;
use std::{
borrow::Cow,
char,
cmp::Ordering,
collections::HashMap,
error::Error,
ffi::{CStr, CString, OsStr, OsString},
fmt,
marker::PhantomData,
mem,
path::{Path, PathBuf},
ptr,
};
pub use crate::collections::{ptr_slice::IntoPtrSlice, strv::IntoStrV};
use crate::ffi;
pub use crate::gstring::{IntoGStr, IntoOptionalGStr};
use libc::{c_char, size_t};
pub trait Ptr: Copy + 'static {
fn is_null(&self) -> bool;
fn from<X>(ptr: *mut X) -> Self;
fn to<X>(self) -> *mut X;
}
impl<T: 'static> Ptr for *const T {
#[inline]
fn is_null(&self) -> bool {
(*self).is_null()
}
#[inline]
fn from<X>(ptr: *mut X) -> *const T {
ptr as *const T
}
#[inline]
fn to<X>(self) -> *mut X {
self as *mut X
}
}
impl<T: 'static> Ptr for *mut T {
#[inline]
fn is_null(&self) -> bool {
(*self).is_null()
}
#[inline]
fn from<X>(ptr: *mut X) -> *mut T {
ptr as *mut T
}
#[inline]
fn to<X>(self) -> *mut X {
self as *mut X
}
}
#[inline]
pub fn mut_override<T>(ptr: *const T) -> *mut T {
ptr as *mut T
}
#[inline]
pub fn const_override<T>(ptr: *mut T) -> *const T {
ptr as *const T
}
pub trait Uninitialized {
unsafe fn uninitialized() -> Self;
}
#[inline]
pub unsafe fn uninitialized<T: Uninitialized>() -> T {
T::uninitialized()
}
pub struct Stash<'a, P: Copy, T: ?Sized + ToGlibPtr<'a, P>>(
pub P,
pub <T as ToGlibPtr<'a, P>>::Storage,
);
pub struct StashMut<'a, P: Copy, T: ?Sized>(pub P, pub <T as ToGlibPtrMut<'a, P>>::Storage)
where
T: ToGlibPtrMut<'a, P>;
#[derive(Debug)]
pub struct Borrowed<T>(mem::ManuallyDrop<T>);
impl<T> Borrowed<T> {
#[inline]
pub fn new(val: T) -> Self {
Self(mem::ManuallyDrop::new(val))
}
#[inline]
pub unsafe fn into_inner(self) -> T {
mem::ManuallyDrop::into_inner(self.0)
}
}
impl<T> AsRef<T> for Borrowed<T> {
#[inline]
fn as_ref(&self) -> &T {
&self.0
}
}
impl<T> std::ops::Deref for Borrowed<T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
&self.0
}
}
pub trait UnsafeFrom<T> {
unsafe fn unsafe_from(t: T) -> Self;
}
pub trait IntoGlib {
type GlibType: Copy;
fn into_glib(self) -> Self::GlibType;
}
impl IntoGlib for bool {
type GlibType = ffi::gboolean;
#[inline]
fn into_glib(self) -> ffi::gboolean {
if self {
ffi::GTRUE
} else {
ffi::GFALSE
}
}
}
impl IntoGlib for char {
type GlibType = u32;
#[inline]
fn into_glib(self) -> u32 {
self as u32
}
}
unsafe impl TransparentType for char {
type GlibType = u32;
}
impl IntoGlib for Option<char> {
type GlibType = u32;
#[inline]
fn into_glib(self) -> u32 {
self.as_ref().map(|&c| c as u32).unwrap_or(0)
}
}
impl IntoGlib for Ordering {
type GlibType = i32;
#[inline]
fn into_glib(self) -> i32 {
match self {
Ordering::Less => -1,
Ordering::Equal => 0,
Ordering::Greater => 1,
}
}
}
impl<O, E, G> IntoGlib for Result<O, E>
where
G: Copy,
O: IntoGlib<GlibType = G> + TryFromGlib<G, Error = E>,
E: IntoGlib<GlibType = G>,
{
type GlibType = G;
#[inline]
fn into_glib(self) -> Self::GlibType {
match self {
Ok(ok) => ok.into_glib(),
Err(err) => err.into_glib(),
}
}
}
pub trait OptionIntoGlib: IntoGlib {
const GLIB_NONE: Self::GlibType;
}
impl<T: OptionIntoGlib> IntoGlib for Option<T> {
type GlibType = T::GlibType;
#[inline]
fn into_glib(self) -> Self::GlibType {
match self {
Some(t) => t.into_glib(),
None => T::GLIB_NONE,
}
}
}
pub trait GlibPtrDefault {
type GlibType: Ptr;
}
impl<T: ?Sized + GlibPtrDefault> GlibPtrDefault for &T {
type GlibType = <T as GlibPtrDefault>::GlibType;
}
pub trait ToGlibPtr<'a, P: Copy> {
type Storage;
fn to_glib_none(&'a self) -> Stash<'a, P, Self>;
fn to_glib_container(&'a self) -> Stash<'a, P, Self> {
unimplemented!();
}
fn to_glib_full(&self) -> P {
unimplemented!();
}
}
pub trait ToGlibPtrMut<'a, P: Copy> {
type Storage;
#[allow(clippy::wrong_self_convention)]
fn to_glib_none_mut(&'a mut self) -> StashMut<'a, P, Self>;
}
impl<'a, P: Ptr, T: ToGlibPtr<'a, P>> ToGlibPtr<'a, P> for Option<T> {
type Storage = Option<<T as ToGlibPtr<'a, P>>::Storage>;
#[inline]
fn to_glib_none(&'a self) -> Stash<'a, P, Option<T>> {
self.as_ref()
.map_or(Stash(Ptr::from::<()>(ptr::null_mut()), None), |s| {
let s = s.to_glib_none();
Stash(s.0, Some(s.1))
})
}
#[inline]
fn to_glib_full(&self) -> P {
self.as_ref()
.map_or(Ptr::from::<()>(ptr::null_mut()), ToGlibPtr::to_glib_full)
}
}
impl<'a, 'opt: 'a, P: Ptr, T: ToGlibPtrMut<'a, P>> ToGlibPtrMut<'a, P> for Option<&'opt mut T> {
type Storage = Option<<T as ToGlibPtrMut<'a, P>>::Storage>;
#[inline]
fn to_glib_none_mut(&'a mut self) -> StashMut<'a, P, Option<&'opt mut T>> {
self.as_mut()
.map_or(StashMut(Ptr::from::<()>(ptr::null_mut()), None), |s| {
let s = s.to_glib_none_mut();
StashMut(s.0, Some(s.1))
})
}
}
impl<'a, P: Ptr, T: ?Sized + ToGlibPtr<'a, P>> ToGlibPtr<'a, P> for &'a T {
type Storage = <T as ToGlibPtr<'a, P>>::Storage;
#[inline]
fn to_glib_none(&'a self) -> Stash<'a, P, Self> {
let s = (*self).to_glib_none();
Stash(s.0, s.1)
}
#[inline]
fn to_glib_full(&self) -> P {
(*self).to_glib_full()
}
}
#[doc(hidden)]
#[derive(Debug)]
pub enum CowStash<B, O> {
Borrowed(B),
Owned(O),
}
impl<'a, P: Ptr, T> ToGlibPtr<'a, P> for Cow<'a, T>
where
T: ToOwned + ?Sized + ToGlibPtr<'a, P>,
T::Owned: ToGlibPtr<'a, P>,
{
type Storage = CowStash<T::Storage, <T::Owned as ToGlibPtr<'a, P>>::Storage>;
#[inline]
fn to_glib_none(&'a self) -> Stash<'a, P, Self> {
match self {
Cow::Borrowed(v) => {
let s = v.to_glib_none();
Stash(s.0, CowStash::Borrowed(s.1))
}
Cow::Owned(v) => {
let s = v.to_glib_none();
Stash(s.0, CowStash::Owned(s.1))
}
}
}
#[inline]
fn to_glib_full(&self) -> P {
match self {
Cow::Borrowed(v) => v.to_glib_full(),
Cow::Owned(v) => v.to_glib_full(),
}
}
}
impl<'a> ToGlibPtr<'a, *const c_char> for str {
type Storage = Cow<'static, [u8]>;
fn to_glib_none(&'a self) -> Stash<'a, *const c_char, Self> {
static EMPTY_STRING: &[u8] = &[0];
let bytes = if self.is_empty() {
Cow::Borrowed(EMPTY_STRING)
} else {
if cfg!(debug_assertions) {
crate::GStr::check_interior_nuls(self).unwrap();
}
let mut bytes = Vec::with_capacity(self.len() + 1);
unsafe {
ptr::copy_nonoverlapping(self.as_ptr(), bytes.as_mut_ptr(), self.len());
bytes.as_mut_ptr().add(self.len()).write(0);
bytes.set_len(self.len() + 1);
}
Cow::Owned(bytes)
};
Stash(bytes.as_ptr() as *const c_char, bytes)
}
#[inline]
fn to_glib_full(&self) -> *const c_char {
if cfg!(debug_assertions) {
crate::GStr::check_interior_nuls(self).unwrap();
}
unsafe {
ffi::g_strndup(self.as_ptr() as *const c_char, self.len() as size_t) as *const c_char
}
}
}
impl<'a> ToGlibPtr<'a, *mut c_char> for str {
type Storage = Cow<'static, [u8]>;
#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *mut c_char, Self> {
let s = ToGlibPtr::<*const c_char>::to_glib_none(self);
Stash(s.0 as *mut _, s.1)
}
#[inline]
fn to_glib_full(&self) -> *mut c_char {
ToGlibPtr::<*const c_char>::to_glib_full(self) as *mut _
}
}
impl<'a> ToGlibPtr<'a, *const c_char> for String {
type Storage = Cow<'static, [u8]>;
#[inline]
fn to_glib_none(&self) -> Stash<'a, *const c_char, String> {
let s = ToGlibPtr::to_glib_none(self.as_str());
Stash(s.0, s.1)
}
#[inline]
fn to_glib_full(&self) -> *const c_char {
ToGlibPtr::to_glib_full(self.as_str())
}
}
impl<'a> ToGlibPtr<'a, *mut c_char> for String {
type Storage = Cow<'static, [u8]>;
#[inline]
fn to_glib_none(&self) -> Stash<'a, *mut c_char, String> {
let s = ToGlibPtr::to_glib_none(self.as_str());
Stash(s.0, s.1)
}
#[inline]
fn to_glib_full(&self) -> *mut c_char {
ToGlibPtr::to_glib_full(self.as_str())
}
}
impl<'a> ToGlibPtr<'a, *const c_char> for CStr {
type Storage = PhantomData<&'a Self>;
#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *const c_char, Self> {
Stash(self.as_ptr(), PhantomData)
}
#[inline]
fn to_glib_full(&self) -> *const c_char {
unsafe {
ffi::g_strndup(
self.as_ptr() as *const c_char,
self.to_bytes().len() as size_t,
) as *const c_char
}
}
}
impl<'a> ToGlibPtr<'a, *mut c_char> for CStr {
type Storage = PhantomData<&'a Self>;
#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *mut c_char, Self> {
Stash(self.as_ptr() as *mut c_char, PhantomData)
}
#[inline]
fn to_glib_full(&self) -> *mut c_char {
unsafe {
ffi::g_strndup(
self.as_ptr() as *const c_char,
self.to_bytes().len() as size_t,
) as *mut c_char
}
}
}
impl<'a> ToGlibPtr<'a, *const c_char> for CString {
type Storage = PhantomData<&'a Self>;
#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *const c_char, Self> {
Stash(self.as_ptr(), PhantomData)
}
#[inline]
fn to_glib_full(&self) -> *const c_char {
unsafe {
ffi::g_strndup(
self.as_ptr() as *const c_char,
self.as_bytes().len() as size_t,
) as *const c_char
}
}
}
impl<'a> ToGlibPtr<'a, *mut c_char> for CString {
type Storage = PhantomData<&'a Self>;
#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *mut c_char, Self> {
Stash(self.as_ptr() as *mut c_char, PhantomData)
}
#[inline]
fn to_glib_full(&self) -> *mut c_char {
unsafe {
ffi::g_strndup(
self.as_ptr() as *const c_char,
self.as_bytes().len() as size_t,
) as *mut c_char
}
}
}
pub trait IntoGlibPtr<P: Ptr> {
#[allow(clippy::wrong_self_convention)]
unsafe fn into_glib_ptr(self) -> P;
}
impl<P: Ptr, T: IntoGlibPtr<P>> IntoGlibPtr<P> for Option<T> {
#[inline]
unsafe fn into_glib_ptr(self) -> P {
self.map_or(Ptr::from::<()>(ptr::null_mut()), |s| {
IntoGlibPtr::into_glib_ptr(s)
})
}
}
impl GlibPtrDefault for str {
type GlibType = *mut c_char;
}
impl GlibPtrDefault for String {
type GlibType = *mut c_char;
}
#[cfg(not(windows))]
pub(crate) fn path_to_c(path: &Path) -> CString {
CString::new(path.as_os_str().as_bytes()).expect("Invalid path with NUL bytes")
}
#[cfg(windows)]
pub(crate) fn path_to_c(path: &Path) -> CString {
let path_str = path
.to_str()
.expect("Path can't be represented as UTF-8")
.to_owned();
if path_str.starts_with("\\\\?\\") {
CString::new(path_str[4..].as_bytes())
} else {
CString::new(path_str.as_bytes())
}
.expect("Invalid path with NUL bytes")
}
#[cfg(not(windows))]
pub(crate) fn os_str_to_c(s: &OsStr) -> CString {
CString::new(s.as_bytes()).expect("Invalid OS String with NUL bytes")
}
#[cfg(windows)]
pub(crate) fn os_str_to_c(s: &OsStr) -> CString {
let os_str = s
.to_str()
.expect("OS String can't be represented as UTF-8")
.to_owned();
CString::new(os_str.as_bytes()).expect("Invalid OS string with NUL bytes")
}
impl<'a> ToGlibPtr<'a, *const c_char> for Path {
type Storage = CString;
#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *const c_char, Self> {
let tmp = path_to_c(self);
Stash(tmp.as_ptr(), tmp)
}
}
impl<'a> ToGlibPtr<'a, *mut c_char> for Path {
type Storage = CString;
#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *mut c_char, Self> {
let tmp = path_to_c(self);
Stash(tmp.as_ptr() as *mut c_char, tmp)
}
#[inline]
fn to_glib_full(&self) -> *mut c_char {
let tmp = path_to_c(self);
unsafe { ffi::g_strdup(tmp.as_ptr()) }
}
}
impl<'a> ToGlibPtr<'a, *const c_char> for PathBuf {
type Storage = CString;
#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *const c_char, Self> {
let tmp = path_to_c(self);
Stash(tmp.as_ptr(), tmp)
}
}
impl<'a> ToGlibPtr<'a, *mut c_char> for PathBuf {
type Storage = CString;
#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *mut c_char, Self> {
let tmp = path_to_c(self);
Stash(tmp.as_ptr() as *mut c_char, tmp)
}
#[inline]
fn to_glib_full(&self) -> *mut c_char {
let tmp = path_to_c(self);
unsafe { ffi::g_strdup(tmp.as_ptr()) }
}
}
impl GlibPtrDefault for Path {
type GlibType = *mut c_char;
}
impl GlibPtrDefault for PathBuf {
type GlibType = *mut c_char;
}
impl<'a> ToGlibPtr<'a, *const c_char> for OsStr {
type Storage = CString;
#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *const c_char, Self> {
let tmp = os_str_to_c(self);
Stash(tmp.as_ptr(), tmp)
}
}
impl<'a> ToGlibPtr<'a, *mut c_char> for OsStr {
type Storage = CString;
#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *mut c_char, Self> {
let tmp = os_str_to_c(self);
Stash(tmp.as_ptr() as *mut c_char, tmp)
}
#[inline]
fn to_glib_full(&self) -> *mut c_char {
let tmp = os_str_to_c(self);
unsafe { ffi::g_strdup(tmp.as_ptr()) }
}
}
impl<'a> ToGlibPtr<'a, *const c_char> for OsString {
type Storage = CString;
#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *const c_char, Self> {
let tmp = os_str_to_c(self);
Stash(tmp.as_ptr(), tmp)
}
}
impl<'a> ToGlibPtr<'a, *mut c_char> for OsString {
type Storage = CString;
#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *mut c_char, Self> {
let tmp = os_str_to_c(self);
Stash(tmp.as_ptr() as *mut c_char, tmp)
}
#[inline]
fn to_glib_full(&self) -> *mut c_char {
let tmp = os_str_to_c(self);
unsafe { ffi::g_strdup(tmp.as_ptr()) }
}
}
impl GlibPtrDefault for OsStr {
type GlibType = *mut c_char;
}
impl GlibPtrDefault for OsString {
type GlibType = *mut c_char;
}
pub trait ToGlibContainerFromSlice<'a, P>
where
Self: Sized,
{
type Storage;
#[allow(clippy::wrong_self_convention)]
fn to_glib_none_from_slice(t: &'a [Self]) -> (P, Self::Storage);
#[allow(clippy::wrong_self_convention)]
fn to_glib_container_from_slice(t: &'a [Self]) -> (P, Self::Storage);
#[allow(clippy::wrong_self_convention)]
fn to_glib_full_from_slice(t: &[Self]) -> P;
}
macro_rules! impl_to_glib_container_from_slice_fundamental {
($name:ty) => {
impl<'a> ToGlibContainerFromSlice<'a, *mut $name> for $name {
type Storage = std::marker::PhantomData<&'a [$name]>;
#[inline]
fn to_glib_none_from_slice(t: &'a [$name]) -> (*mut $name, Self::Storage) {
(t.as_ptr() as *mut $name, std::marker::PhantomData)
}
#[inline]
fn to_glib_container_from_slice(t: &'a [$name]) -> (*mut $name, Self::Storage) {
(
ToGlibContainerFromSlice::to_glib_full_from_slice(t),
std::marker::PhantomData,
)
}
#[inline]
fn to_glib_full_from_slice(t: &[$name]) -> *mut $name {
if t.len() == 0 {
return ptr::null_mut();
}
unsafe {
let res = ffi::g_malloc(mem::size_of_val(t)) as *mut $name;
ptr::copy_nonoverlapping(t.as_ptr(), res, t.len());
res
}
}
}
};
}
impl_to_glib_container_from_slice_fundamental!(u8);
impl_to_glib_container_from_slice_fundamental!(i8);
impl_to_glib_container_from_slice_fundamental!(u16);
impl_to_glib_container_from_slice_fundamental!(i16);
impl_to_glib_container_from_slice_fundamental!(u32);
impl_to_glib_container_from_slice_fundamental!(i32);
impl_to_glib_container_from_slice_fundamental!(u64);
impl_to_glib_container_from_slice_fundamental!(i64);
impl_to_glib_container_from_slice_fundamental!(f32);
impl_to_glib_container_from_slice_fundamental!(f64);
macro_rules! impl_to_glib_container_from_slice_string {
($name:ty, $ffi_name:ty) => {
impl<'a> ToGlibContainerFromSlice<'a, *mut $ffi_name> for $name {
type Storage = (Vec<Stash<'a, $ffi_name, $name>>, Option<Vec<$ffi_name>>);
fn to_glib_none_from_slice(t: &'a [$name]) -> (*mut $ffi_name, Self::Storage) {
let v: Vec<_> = t.iter().map(ToGlibPtr::to_glib_none).collect();
let mut v_ptr: Vec<_> = v.iter().map(|s| s.0).collect();
v_ptr.push(ptr::null_mut() as $ffi_name);
(v_ptr.as_ptr() as *mut $ffi_name, (v, Some(v_ptr)))
}
fn to_glib_container_from_slice(t: &'a [$name]) -> (*mut $ffi_name, Self::Storage) {
let v: Vec<_> = t.iter().map(ToGlibPtr::to_glib_none).collect();
let v_ptr = unsafe {
let v_ptr = ffi::g_malloc(mem::size_of::<$ffi_name>() * (t.len() + 1))
as *mut $ffi_name;
for (i, s) in v.iter().enumerate() {
ptr::write(v_ptr.add(i), s.0);
}
ptr::write(v_ptr.add(t.len()), ptr::null_mut());
v_ptr
};
(v_ptr, (v, None))
}
fn to_glib_full_from_slice(t: &[$name]) -> *mut $ffi_name {
unsafe {
let v_ptr = ffi::g_malloc(mem::size_of::<$ffi_name>() * (t.len() + 1))
as *mut $ffi_name;
for (i, s) in t.iter().enumerate() {
ptr::write(v_ptr.add(i), s.to_glib_full());
}
ptr::write(v_ptr.add(t.len()), ptr::null_mut());
v_ptr
}
}
}
impl<'a> ToGlibContainerFromSlice<'a, *const $ffi_name> for $name {
type Storage = (Vec<Stash<'a, $ffi_name, $name>>, Option<Vec<$ffi_name>>);
fn to_glib_none_from_slice(t: &'a [$name]) -> (*const $ffi_name, Self::Storage) {
let v: Vec<_> = t.iter().map(ToGlibPtr::to_glib_none).collect();
let mut v_ptr: Vec<_> = v.iter().map(|s| s.0).collect();
v_ptr.push(ptr::null_mut() as $ffi_name);
(v_ptr.as_ptr() as *const $ffi_name, (v, Some(v_ptr)))
}
fn to_glib_container_from_slice(t: &'a [$name]) -> (*const $ffi_name, Self::Storage) {
let v: Vec<_> = t.iter().map(ToGlibPtr::to_glib_none).collect();
let v_ptr = unsafe {
let v_ptr = ffi::g_malloc(mem::size_of::<$ffi_name>() * (t.len() + 1))
as *mut $ffi_name;
for (i, s) in v.iter().enumerate() {
ptr::write(v_ptr.add(i), s.0);
}
ptr::write(v_ptr.add(t.len()), ptr::null_mut());
v_ptr as *const $ffi_name
};
(v_ptr, (v, None))
}
fn to_glib_full_from_slice(t: &[$name]) -> *const $ffi_name {
unsafe {
let v_ptr = ffi::g_malloc(mem::size_of::<$ffi_name>() * (t.len() + 1))
as *mut $ffi_name;
for (i, s) in t.iter().enumerate() {
ptr::write(v_ptr.add(i), s.to_glib_full());
}
ptr::write(v_ptr.add(t.len()), ptr::null_mut());
v_ptr as *const $ffi_name
}
}
}
};
}
impl_to_glib_container_from_slice_string!(&'a str, *mut c_char);
impl_to_glib_container_from_slice_string!(&'a str, *const c_char);
impl_to_glib_container_from_slice_string!(String, *mut c_char);
impl_to_glib_container_from_slice_string!(String, *const c_char);
impl_to_glib_container_from_slice_string!(&'a Path, *mut c_char);
impl_to_glib_container_from_slice_string!(&'a Path, *const c_char);
impl_to_glib_container_from_slice_string!(PathBuf, *mut c_char);
impl_to_glib_container_from_slice_string!(PathBuf, *const c_char);
impl_to_glib_container_from_slice_string!(&'a OsStr, *mut c_char);
impl_to_glib_container_from_slice_string!(&'a OsStr, *const c_char);
impl_to_glib_container_from_slice_string!(OsString, *mut c_char);
impl_to_glib_container_from_slice_string!(OsString, *const c_char);
impl_to_glib_container_from_slice_string!(crate::GString, *mut c_char);
impl_to_glib_container_from_slice_string!(crate::GString, *const c_char);
impl<'a, T> ToGlibContainerFromSlice<'a, *mut ffi::GList> for T
where
T: GlibPtrDefault + ToGlibPtr<'a, <T as GlibPtrDefault>::GlibType>,
{
type Storage = (
Option<List>,
Vec<Stash<'a, <T as GlibPtrDefault>::GlibType, T>>,
);
#[inline]
fn to_glib_none_from_slice(t: &'a [T]) -> (*mut ffi::GList, Self::Storage) {
let stash_vec: Vec<_> = t.iter().rev().map(ToGlibPtr::to_glib_none).collect();
let mut list: *mut ffi::GList = ptr::null_mut();
unsafe {
for stash in &stash_vec {
list = ffi::g_list_prepend(list, Ptr::to(stash.0));
}
}
let stash = (ptr::NonNull::new(list).map(List), stash_vec);
(list, stash)
}
#[inline]
fn to_glib_container_from_slice(t: &'a [T]) -> (*mut ffi::GList, Self::Storage) {
let stash_vec: Vec<_> = t.iter().rev().map(ToGlibPtr::to_glib_none).collect();
let mut list: *mut ffi::GList = ptr::null_mut();
unsafe {
for stash in &stash_vec {
list = ffi::g_list_prepend(list, Ptr::to(stash.0));
}
}
(list, (None, stash_vec))
}
#[inline]
fn to_glib_full_from_slice(t: &[T]) -> *mut ffi::GList {
let mut list: *mut ffi::GList = ptr::null_mut();
unsafe {
for ptr in t.iter().rev().map(ToGlibPtr::to_glib_full) {
list = ffi::g_list_prepend(list, Ptr::to(ptr));
}
}
list
}
}
impl<'a, T> ToGlibContainerFromSlice<'a, *const ffi::GList> for T
where
T: GlibPtrDefault + ToGlibPtr<'a, <T as GlibPtrDefault>::GlibType>,
{
type Storage = (
Option<List>,
Vec<Stash<'a, <T as GlibPtrDefault>::GlibType, T>>,
);
#[inline]
fn to_glib_none_from_slice(t: &'a [T]) -> (*const ffi::GList, Self::Storage) {
let (list, stash) = ToGlibContainerFromSlice::<*mut ffi::GList>::to_glib_none_from_slice(t);
(list as *const ffi::GList, stash)
}
#[inline]
fn to_glib_container_from_slice(_t: &'a [T]) -> (*const ffi::GList, Self::Storage) {
unimplemented!()
}
#[inline]
fn to_glib_full_from_slice(_t: &[T]) -> *const ffi::GList {
unimplemented!()
}
}
#[doc(alias = "GList")]
pub struct List(ptr::NonNull<ffi::GList>);
impl Drop for List {
#[inline]
fn drop(&mut self) {
unsafe { ffi::g_list_free(self.0.as_ptr()) }
}
}
impl<'a, T> ToGlibContainerFromSlice<'a, *mut ffi::GSList> for &'a T
where
T: GlibPtrDefault + ToGlibPtr<'a, <T as GlibPtrDefault>::GlibType>,
{
type Storage = (
Option<SList>,
Vec<Stash<'a, <T as GlibPtrDefault>::GlibType, &'a T>>,
);
#[inline]
fn to_glib_none_from_slice(t: &'a [&'a T]) -> (*mut ffi::GSList, Self::Storage) {
let stash_vec: Vec<_> = t.iter().rev().map(ToGlibPtr::to_glib_none).collect();
let mut list: *mut ffi::GSList = ptr::null_mut();
unsafe {
for stash in &stash_vec {
list = ffi::g_slist_prepend(list, Ptr::to(stash.0));
}
}
let stash = (ptr::NonNull::new(list).map(SList), stash_vec);
(list, stash)
}
#[inline]
fn to_glib_container_from_slice(t: &'a [&'a T]) -> (*mut ffi::GSList, Self::Storage) {
let stash_vec: Vec<_> = t.iter().rev().map(ToGlibPtr::to_glib_none).collect();
let mut list: *mut ffi::GSList = ptr::null_mut();
unsafe {
for stash in &stash_vec {
list = ffi::g_slist_prepend(list, Ptr::to(stash.0));
}
}
(list, (None, stash_vec))
}
#[inline]
fn to_glib_full_from_slice(t: &[&'a T]) -> *mut ffi::GSList {
let mut list: *mut ffi::GSList = ptr::null_mut();
unsafe {
for ptr in t.iter().rev().map(ToGlibPtr::to_glib_full) {
list = ffi::g_slist_prepend(list, Ptr::to(ptr));
}
}
list
}
}
impl<'a, T> ToGlibContainerFromSlice<'a, *const ffi::GSList> for &'a T
where
T: GlibPtrDefault + ToGlibPtr<'a, <T as GlibPtrDefault>::GlibType>,
{
type Storage = (
Option<SList>,
Vec<Stash<'a, <T as GlibPtrDefault>::GlibType, &'a T>>,
);
#[inline]
fn to_glib_none_from_slice(t: &'a [&'a T]) -> (*const ffi::GSList, Self::Storage) {
let (list, stash) =
ToGlibContainerFromSlice::<*mut ffi::GSList>::to_glib_none_from_slice(t);
(list as *const ffi::GSList, stash)
}
#[inline]
fn to_glib_container_from_slice(_t: &'a [&'a T]) -> (*const ffi::GSList, Self::Storage) {
unimplemented!()
}
#[inline]
fn to_glib_full_from_slice(_t: &[&'a T]) -> *const ffi::GSList {
unimplemented!()
}
}
#[doc(alias = "GSList")]
pub struct SList(ptr::NonNull<ffi::GSList>);
impl Drop for SList {
#[inline]
fn drop(&mut self) {
unsafe { ffi::g_slist_free(self.0.as_ptr()) }
}
}
impl<'a, P: Ptr, T: ToGlibContainerFromSlice<'a, P>> ToGlibPtr<'a, P> for [T] {
type Storage = T::Storage;
#[inline]
fn to_glib_none(&'a self) -> Stash<'a, P, Self> {
let result = ToGlibContainerFromSlice::to_glib_none_from_slice(self);
Stash(result.0, result.1)
}
#[inline]
fn to_glib_container(&'a self) -> Stash<'a, P, Self> {
let result = ToGlibContainerFromSlice::to_glib_container_from_slice(self);
Stash(result.0, result.1)
}
#[inline]
fn to_glib_full(&self) -> P {
ToGlibContainerFromSlice::to_glib_full_from_slice(self)
}
}
#[allow(clippy::implicit_hasher)]
impl<'a> ToGlibPtr<'a, *mut ffi::GHashTable> for HashMap<String, String> {
type Storage = HashTable;
#[inline]
fn to_glib_none(&self) -> Stash<'a, *mut ffi::GHashTable, Self> {
let ptr = self.to_glib_full();
Stash(ptr, HashTable(unsafe { ptr::NonNull::new_unchecked(ptr) }))
}
#[inline]
fn to_glib_full(&self) -> *mut ffi::GHashTable {
unsafe {
let ptr = ffi::g_hash_table_new_full(
Some(ffi::g_str_hash),
Some(ffi::g_str_equal),
Some(ffi::g_free),
Some(ffi::g_free),
);
for (k, v) in self {
let k: *mut c_char = k.to_glib_full();
let v: *mut c_char = v.to_glib_full();
ffi::g_hash_table_insert(ptr, k as *mut _, v as *mut _);
}
ptr
}
}
}
#[doc(alias = "GHashTable")]
pub struct HashTable(ptr::NonNull<ffi::GHashTable>);
impl Drop for HashTable {
#[inline]
fn drop(&mut self) {
unsafe { ffi::g_hash_table_unref(self.0.as_ptr()) }
}
}
#[doc(alias = "GPtrArray")]
pub struct PtrArray(ptr::NonNull<ffi::GPtrArray>);
impl Drop for PtrArray {
#[inline]
fn drop(&mut self) {
unsafe {
ffi::g_ptr_array_unref(self.0.as_ptr());
}
}
}
impl<'a, T> ToGlibContainerFromSlice<'a, *mut ffi::GPtrArray> for T
where
T: GlibPtrDefault + ToGlibPtr<'a, <T as GlibPtrDefault>::GlibType>,
{
type Storage = (
Option<PtrArray>,
Vec<Stash<'a, <T as GlibPtrDefault>::GlibType, T>>,
);
#[inline]
fn to_glib_none_from_slice(t: &'a [T]) -> (*mut ffi::GPtrArray, Self::Storage) {
let stash_vec: Vec<_> = t.iter().map(ToGlibPtr::to_glib_none).collect();
let arr = unsafe { ffi::g_ptr_array_sized_new(t.len() as _) };
unsafe {
for stash in &stash_vec {
ffi::g_ptr_array_add(arr, Ptr::to(stash.0));
}
}
(
arr,
(
Some(PtrArray(unsafe { ptr::NonNull::new_unchecked(arr) })),
stash_vec,
),
)
}
#[inline]
fn to_glib_container_from_slice(t: &'a [T]) -> (*mut ffi::GPtrArray, Self::Storage) {
let stash_vec: Vec<_> = t.iter().map(ToGlibPtr::to_glib_none).collect();
let arr = unsafe { ffi::g_ptr_array_sized_new(t.len() as _) };
unsafe {
for stash in &stash_vec {
ffi::g_ptr_array_add(arr, Ptr::to(stash.0));
}
}
(arr, (None, stash_vec))
}
#[inline]
fn to_glib_full_from_slice(t: &[T]) -> *mut ffi::GPtrArray {
let arr = unsafe { ffi::g_ptr_array_sized_new(t.len() as _) };
unsafe {
for ptr in t.iter().map(ToGlibPtr::to_glib_full) {
ffi::g_ptr_array_add(arr, Ptr::to(ptr));
}
}
arr
}
}
impl<'a, T> ToGlibContainerFromSlice<'a, *const ffi::GPtrArray> for T
where
T: GlibPtrDefault + ToGlibPtr<'a, <T as GlibPtrDefault>::GlibType>,
{
type Storage = (
Option<PtrArray>,
Vec<Stash<'a, <T as GlibPtrDefault>::GlibType, T>>,
);
#[inline]
fn to_glib_none_from_slice(t: &'a [T]) -> (*const ffi::GPtrArray, Self::Storage) {
let (arr, stash) =
ToGlibContainerFromSlice::<*mut ffi::GPtrArray>::to_glib_none_from_slice(t);
(arr as *const ffi::GPtrArray, stash)
}
#[inline]
fn to_glib_container_from_slice(_t: &'a [T]) -> (*const ffi::GPtrArray, Self::Storage) {
unimplemented!()
}
#[inline]
fn to_glib_full_from_slice(_t: &[T]) -> *const ffi::GPtrArray {
unimplemented!()
}
}
pub trait FromGlib<G: Copy>: Sized {
unsafe fn from_glib(val: G) -> Self;
}
#[inline]
pub unsafe fn from_glib<G: Copy, T: FromGlib<G>>(val: G) -> T {
FromGlib::from_glib(val)
}
impl FromGlib<ffi::gboolean> for bool {
#[inline]
unsafe fn from_glib(val: ffi::gboolean) -> Self {
val != ffi::GFALSE
}
}
impl FromGlib<i32> for Ordering {
#[inline]
unsafe fn from_glib(val: i32) -> Self {
val.cmp(&0)
}
}
pub trait TryFromGlib<G: Copy>: Sized {
type Error;
unsafe fn try_from_glib(val: G) -> Result<Self, Self::Error>;
}
#[inline]
pub unsafe fn try_from_glib<G: Copy, T: TryFromGlib<G>>(
val: G,
) -> Result<T, <T as TryFromGlib<G>>::Error> {
TryFromGlib::try_from_glib(val)
}
#[derive(Debug, PartialEq, Eq)]
pub struct GlibNoneError;
impl fmt::Display for GlibNoneError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "glib value is None")
}
}
impl std::error::Error for GlibNoneError {}
impl<G: Copy, T: TryFromGlib<G, Error = GlibNoneError>> FromGlib<G> for Option<T> {
#[inline]
unsafe fn from_glib(val: G) -> Self {
T::try_from_glib(val).ok()
}
}
#[derive(Debug, Eq, PartialEq)]
pub enum GlibNoneOrInvalidError<I: Error> {
Invalid(I),
None,
}
impl<I: Error> GlibNoneOrInvalidError<I> {
#[inline]
pub fn none() -> Self {
Self::None
}
#[inline]
pub fn is_none(&self) -> bool {
matches!(self, Self::None)
}
#[inline]
pub fn is_invalid(&self) -> bool {
matches!(self, Self::Invalid(_))
}
}
impl<I: Error> From<I> for GlibNoneOrInvalidError<I> {
#[inline]
fn from(invalid: I) -> Self {
Self::Invalid(invalid)
}
}
impl<I: Error> fmt::Display for GlibNoneOrInvalidError<I> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Invalid(err) => {
write!(fmt, "glib value is invalid: ")?;
fmt::Display::fmt(err, fmt)
}
Self::None => write!(fmt, "glib value is None"),
}
}
}
impl<I: Error> Error for GlibNoneOrInvalidError<I> {}
impl<G: Copy, I: Error, T: TryFromGlib<G, Error = GlibNoneOrInvalidError<I>>> FromGlib<G>
for Result<Option<T>, I>
{
#[inline]
unsafe fn from_glib(val: G) -> Self {
match T::try_from_glib(val) {
Ok(value) => Ok(Some(value)),
Err(GlibNoneOrInvalidError::None) => Ok(None),
Err(GlibNoneOrInvalidError::Invalid(err)) => Err(err),
}
}
}
pub trait FromGlibPtrNone<P: Ptr>: Sized {
unsafe fn from_glib_none(ptr: P) -> Self;
}
pub trait FromGlibPtrFull<P: Ptr>: Sized {
unsafe fn from_glib_full(ptr: P) -> Self;
}
pub trait FromGlibPtrBorrow<P: Ptr>: Sized {
unsafe fn from_glib_borrow(_ptr: P) -> Borrowed<Self> {
unimplemented!();
}
}
#[inline]
pub unsafe fn from_glib_none<P: Ptr, T: FromGlibPtrNone<P>>(ptr: P) -> T {
FromGlibPtrNone::from_glib_none(ptr)
}
#[inline]
pub unsafe fn from_glib_full<P: Ptr, T: FromGlibPtrFull<P>>(ptr: P) -> T {
FromGlibPtrFull::from_glib_full(ptr)
}
#[inline]
pub unsafe fn from_glib_borrow<P: Ptr, T: FromGlibPtrBorrow<P>>(ptr: P) -> Borrowed<T> {
FromGlibPtrBorrow::from_glib_borrow(ptr)
}
impl<P: Ptr, T: FromGlibPtrNone<P>> FromGlibPtrNone<P> for Option<T> {
#[inline]
unsafe fn from_glib_none(ptr: P) -> Option<T> {
if ptr.is_null() {
None
} else {
Some(from_glib_none(ptr))
}
}
}
impl<P: Ptr, T: FromGlibPtrBorrow<P>> FromGlibPtrBorrow<P> for Option<T> {
#[inline]
unsafe fn from_glib_borrow(ptr: P) -> Borrowed<Option<T>> {
if ptr.is_null() {
Borrowed::new(None)
} else {
let val = T::from_glib_borrow(ptr);
Borrowed::new(Some(val.into_inner()))
}
}
}
impl<P: Ptr, T: FromGlibPtrFull<P>> FromGlibPtrFull<P> for Option<T> {
#[inline]
unsafe fn from_glib_full(ptr: P) -> Option<T> {
if ptr.is_null() {
None
} else {
Some(from_glib_full(ptr))
}
}
}
impl FromGlibPtrNone<*const c_char> for String {
#[inline]
unsafe fn from_glib_none(ptr: *const c_char) -> Self {
debug_assert!(!ptr.is_null());
Self::from_utf8_lossy(CStr::from_ptr(ptr).to_bytes()).into_owned()
}
}
impl FromGlibPtrFull<*const c_char> for String {
#[inline]
unsafe fn from_glib_full(ptr: *const c_char) -> Self {
let res = from_glib_none(ptr);
ffi::g_free(ptr as *mut _);
res
}
}
impl FromGlibPtrNone<*mut c_char> for String {
#[inline]
unsafe fn from_glib_none(ptr: *mut c_char) -> Self {
debug_assert!(!ptr.is_null());
Self::from_utf8_lossy(CStr::from_ptr(ptr).to_bytes()).into_owned()
}
}
impl FromGlibPtrFull<*mut c_char> for String {
#[inline]
unsafe fn from_glib_full(ptr: *mut c_char) -> Self {
let res = from_glib_none(ptr);
ffi::g_free(ptr as *mut _);
res
}
}
#[cfg(not(windows))]
pub(crate) unsafe fn c_to_path_buf(ptr: *const c_char) -> PathBuf {
debug_assert!(!ptr.is_null());
OsString::from_vec(CStr::from_ptr(ptr).to_bytes().to_vec()).into()
}
#[cfg(windows)]
pub(crate) unsafe fn c_to_path_buf(ptr: *const c_char) -> PathBuf {
debug_assert!(!ptr.is_null());
String::from_utf8(CStr::from_ptr(ptr).to_bytes().into())
.expect("Invalid, non-UTF8 path")
.into()
}
#[cfg(not(windows))]
pub(crate) unsafe fn c_to_os_string(ptr: *const c_char) -> OsString {
debug_assert!(!ptr.is_null());
OsString::from_vec(CStr::from_ptr(ptr).to_bytes().to_vec())
}
#[cfg(windows)]
pub(crate) unsafe fn c_to_os_string(ptr: *const c_char) -> OsString {
debug_assert!(!ptr.is_null());
String::from_utf8(CStr::from_ptr(ptr).to_bytes().into())
.expect("Invalid, non-UTF8 path")
.into()
}
impl FromGlibPtrNone<*const c_char> for PathBuf {
#[inline]
unsafe fn from_glib_none(ptr: *const c_char) -> Self {
debug_assert!(!ptr.is_null());
c_to_path_buf(ptr)
}
}
impl FromGlibPtrFull<*const c_char> for PathBuf {
#[inline]
unsafe fn from_glib_full(ptr: *const c_char) -> Self {
let res = from_glib_none(ptr);
ffi::g_free(ptr as *mut _);
res
}
}
impl FromGlibPtrNone<*mut c_char> for PathBuf {
#[inline]
unsafe fn from_glib_none(ptr: *mut c_char) -> Self {
debug_assert!(!ptr.is_null());
c_to_path_buf(ptr)
}
}
impl FromGlibPtrFull<*mut c_char> for PathBuf {
#[inline]
unsafe fn from_glib_full(ptr: *mut c_char) -> Self {
let res = from_glib_none(ptr);
ffi::g_free(ptr as *mut _);
res
}
}
#[cfg(not(windows))]
pub(crate) unsafe fn c_to_path_buf_num(ptr: *const c_char, num: usize) -> PathBuf {
debug_assert!(!ptr.is_null());
let slice = std::slice::from_raw_parts(ptr as *const u8, num);
OsString::from_vec(slice.to_vec()).into()
}
#[cfg(windows)]
pub(crate) unsafe fn c_to_path_buf_num(ptr: *const c_char, num: usize) -> PathBuf {
debug_assert!(!ptr.is_null());
let slice = std::slice::from_raw_parts(ptr as *const u8, num);
String::from_utf8(slice.into())
.expect("Invalid, non-UTF8 path")
.into()
}
#[doc(hidden)]
impl FromGlibContainer<*const c_char, *const i8> for PathBuf {
unsafe fn from_glib_none_num(ptr: *const i8, num: usize) -> Self {
c_to_path_buf_num(ptr as *const _, num)
}
unsafe fn from_glib_container_num(ptr: *const i8, num: usize) -> Self {
c_to_path_buf_num(ptr as *const _, num)
}
unsafe fn from_glib_full_num(ptr: *const i8, num: usize) -> Self {
let res = c_to_path_buf_num(ptr as *const _, num);
ffi::g_free(ptr as *mut _);
res
}
}
#[doc(hidden)]
impl FromGlibContainer<*const c_char, *mut i8> for PathBuf {
unsafe fn from_glib_none_num(ptr: *mut i8, num: usize) -> Self {
FromGlibContainer::from_glib_none_num(ptr as *const i8, num)
}
unsafe fn from_glib_container_num(ptr: *mut i8, num: usize) -> Self {
FromGlibContainer::from_glib_container_num(ptr as *const i8, num)
}
unsafe fn from_glib_full_num(ptr: *mut i8, num: usize) -> Self {
FromGlibContainer::from_glib_full_num(ptr as *const i8, num)
}
}
#[doc(hidden)]
impl FromGlibContainer<*const c_char, *const u8> for PathBuf {
unsafe fn from_glib_none_num(ptr: *const u8, num: usize) -> Self {
FromGlibContainer::from_glib_none_num(ptr as *const i8, num)
}
unsafe fn from_glib_container_num(ptr: *const u8, num: usize) -> Self {
FromGlibContainer::from_glib_container_num(ptr as *const i8, num)
}
unsafe fn from_glib_full_num(ptr: *const u8, num: usize) -> Self {
FromGlibContainer::from_glib_full_num(ptr as *const i8, num)
}
}
#[doc(hidden)]
impl FromGlibContainer<*const c_char, *mut u8> for PathBuf {
unsafe fn from_glib_none_num(ptr: *mut u8, num: usize) -> Self {
FromGlibContainer::from_glib_none_num(ptr as *const i8, num)
}
unsafe fn from_glib_container_num(ptr: *mut u8, num: usize) -> Self {
FromGlibContainer::from_glib_container_num(ptr as *const i8, num)
}
unsafe fn from_glib_full_num(ptr: *mut u8, num: usize) -> Self {
FromGlibContainer::from_glib_full_num(ptr as *const i8, num)
}
}
impl FromGlibPtrNone<*const c_char> for OsString {
#[inline]
unsafe fn from_glib_none(ptr: *const c_char) -> Self {
debug_assert!(!ptr.is_null());
c_to_os_string(ptr)
}
}
impl FromGlibPtrFull<*const c_char> for OsString {
#[inline]
unsafe fn from_glib_full(ptr: *const c_char) -> Self {
let res = from_glib_none(ptr);
ffi::g_free(ptr as *mut _);
res
}
}
impl FromGlibPtrNone<*mut c_char> for OsString {
#[inline]
unsafe fn from_glib_none(ptr: *mut c_char) -> Self {
debug_assert!(!ptr.is_null());
c_to_os_string(ptr)
}
}
impl FromGlibPtrFull<*mut c_char> for OsString {
#[inline]
unsafe fn from_glib_full(ptr: *mut c_char) -> Self {
let res = from_glib_none(ptr);
ffi::g_free(ptr as *mut _);
res
}
}
pub trait FromGlibContainer<T, P: Ptr>: Sized {
unsafe fn from_glib_none_num(ptr: P, num: usize) -> Self;
unsafe fn from_glib_container_num(ptr: P, num: usize) -> Self;
unsafe fn from_glib_full_num(ptr: P, num: usize) -> Self;
}
pub trait FromGlibPtrContainer<P: Ptr, PP: Ptr>: FromGlibContainer<P, PP> + Sized {
unsafe fn from_glib_none(ptr: PP) -> Self;
unsafe fn from_glib_container(ptr: PP) -> Self;
unsafe fn from_glib_full(ptr: PP) -> Self;
}
pub unsafe fn c_ptr_array_len<P: Ptr>(mut ptr: *const P) -> usize {
let mut len = 0;
if !ptr.is_null() {
while !(*ptr).is_null() {
len += 1;
ptr = ptr.offset(1);
}
}
len
}
pub trait FromGlibContainerAsVec<T, P: Ptr>
where
Self: Sized,
{
unsafe fn from_glib_none_num_as_vec(ptr: P, num: usize) -> Vec<Self>;
unsafe fn from_glib_container_num_as_vec(ptr: P, num: usize) -> Vec<Self>;
unsafe fn from_glib_full_num_as_vec(ptr: P, num: usize) -> Vec<Self>;
}
pub trait FromGlibPtrArrayContainerAsVec<P: Ptr, PP: Ptr>: FromGlibContainerAsVec<P, PP>
where
Self: Sized,
{
unsafe fn from_glib_none_as_vec(ptr: PP) -> Vec<Self>;
unsafe fn from_glib_container_as_vec(ptr: PP) -> Vec<Self>;
unsafe fn from_glib_full_as_vec(ptr: PP) -> Vec<Self>;
}
impl FromGlibContainerAsVec<bool, *const ffi::gboolean> for bool {
unsafe fn from_glib_none_num_as_vec(ptr: *const ffi::gboolean, num: usize) -> Vec<Self> {
if num == 0 || ptr.is_null() {
return Vec::new();
}
let mut res = Vec::<Self>::with_capacity(num);
let res_ptr = res.as_mut_ptr();
for i in 0..num {
*res_ptr.add(i) = from_glib(ptr::read(ptr.add(i)));
}
res.set_len(num);
res
}
unsafe fn from_glib_container_num_as_vec(_: *const ffi::gboolean, _: usize) -> Vec<Self> {
unimplemented!();
}
unsafe fn from_glib_full_num_as_vec(_: *const ffi::gboolean, _: usize) -> Vec<Self> {
unimplemented!();
}
}
impl FromGlibContainerAsVec<bool, *mut ffi::gboolean> for bool {
unsafe fn from_glib_none_num_as_vec(ptr: *mut ffi::gboolean, num: usize) -> Vec<Self> {
FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr as *const _, num)
}
unsafe fn from_glib_container_num_as_vec(ptr: *mut ffi::gboolean, num: usize) -> Vec<Self> {
let res = FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, num);
ffi::g_free(ptr as *mut _);
res
}
unsafe fn from_glib_full_num_as_vec(ptr: *mut ffi::gboolean, num: usize) -> Vec<Self> {
FromGlibContainerAsVec::from_glib_container_num_as_vec(ptr, num)
}
}
macro_rules! impl_from_glib_container_as_vec_fundamental {
($name:ty) => {
unsafe impl TransparentType for $name {
type GlibType = $name;
}
impl FromGlibContainerAsVec<$name, *const $name> for $name {
unsafe fn from_glib_none_num_as_vec(ptr: *const $name, num: usize) -> Vec<Self> {
if num == 0 || ptr.is_null() {
return Vec::new();
}
let mut res = Vec::with_capacity(num);
let res_ptr = res.as_mut_ptr();
std::ptr::copy_nonoverlapping(ptr, res_ptr, num);
res.set_len(num);
res
}
unsafe fn from_glib_container_num_as_vec(_: *const $name, _: usize) -> Vec<Self> {
unimplemented!();
}
unsafe fn from_glib_full_num_as_vec(_: *const $name, _: usize) -> Vec<Self> {
unimplemented!();
}
}
impl FromGlibContainerAsVec<$name, *mut $name> for $name {
unsafe fn from_glib_none_num_as_vec(ptr: *mut $name, num: usize) -> Vec<Self> {
FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr as *const _, num)
}
unsafe fn from_glib_container_num_as_vec(ptr: *mut $name, num: usize) -> Vec<Self> {
let res = FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, num);
ffi::g_free(ptr as *mut _);
res
}
unsafe fn from_glib_full_num_as_vec(ptr: *mut $name, num: usize) -> Vec<Self> {
FromGlibContainerAsVec::from_glib_container_num_as_vec(ptr, num)
}
}
};
}
impl_from_glib_container_as_vec_fundamental!(u8);
impl_from_glib_container_as_vec_fundamental!(i8);
impl_from_glib_container_as_vec_fundamental!(u16);
impl_from_glib_container_as_vec_fundamental!(i16);
impl_from_glib_container_as_vec_fundamental!(u32);
impl_from_glib_container_as_vec_fundamental!(i32);
impl_from_glib_container_as_vec_fundamental!(u64);
impl_from_glib_container_as_vec_fundamental!(i64);
impl_from_glib_container_as_vec_fundamental!(f32);
impl_from_glib_container_as_vec_fundamental!(f64);
macro_rules! impl_from_glib_container_as_vec_string {
($name:ty, $ffi_name:ty) => {
impl FromGlibContainerAsVec<$ffi_name, *const $ffi_name> for $name {
unsafe fn from_glib_none_num_as_vec(ptr: *const $ffi_name, num: usize) -> Vec<Self> {
if num == 0 || ptr.is_null() {
return Vec::new();
}
let mut res = Vec::<Self>::with_capacity(num);
let res_ptr = res.as_mut_ptr();
for i in 0..num {
std::ptr::write(
res_ptr.add(i),
from_glib_none(ptr::read(ptr.add(i)) as $ffi_name),
);
}
res.set_len(num);
res
}
unsafe fn from_glib_container_num_as_vec(_: *const $ffi_name, _: usize) -> Vec<Self> {
unimplemented!();
}
unsafe fn from_glib_full_num_as_vec(_: *const $ffi_name, _: usize) -> Vec<Self> {
unimplemented!();
}
}
impl FromGlibContainerAsVec<$ffi_name, *mut $ffi_name> for $name {
unsafe fn from_glib_none_num_as_vec(ptr: *mut $ffi_name, num: usize) -> Vec<Self> {
FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr as *const _, num)
}
unsafe fn from_glib_container_num_as_vec(ptr: *mut $ffi_name, num: usize) -> Vec<Self> {
let res = FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, num);
ffi::g_free(ptr as *mut _);
res
}
unsafe fn from_glib_full_num_as_vec(ptr: *mut $ffi_name, num: usize) -> Vec<Self> {
if num == 0 || ptr.is_null() {
ffi::g_free(ptr as *mut _);
return Vec::new();
}
let mut res = Vec::<Self>::with_capacity(num);
let res_ptr = res.as_mut_ptr();
for i in 0..num {
std::ptr::write(
res_ptr.add(i),
from_glib_full(ptr::read(ptr.add(i)) as $ffi_name),
);
}
res.set_len(num);
ffi::g_free(ptr as *mut _);
res
}
}
impl FromGlibPtrArrayContainerAsVec<$ffi_name, *mut $ffi_name> for $name {
unsafe fn from_glib_none_as_vec(ptr: *mut $ffi_name) -> Vec<Self> {
FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, c_ptr_array_len(ptr))
}
unsafe fn from_glib_container_as_vec(ptr: *mut $ffi_name) -> Vec<Self> {
FromGlibContainerAsVec::from_glib_container_num_as_vec(ptr, c_ptr_array_len(ptr))
}
unsafe fn from_glib_full_as_vec(ptr: *mut $ffi_name) -> Vec<Self> {
FromGlibContainerAsVec::from_glib_full_num_as_vec(ptr, c_ptr_array_len(ptr))
}
}
impl FromGlibPtrArrayContainerAsVec<$ffi_name, *const $ffi_name> for $name {
unsafe fn from_glib_none_as_vec(ptr: *const $ffi_name) -> Vec<Self> {
FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, c_ptr_array_len(ptr))
}
unsafe fn from_glib_container_as_vec(ptr: *const $ffi_name) -> Vec<Self> {
FromGlibContainerAsVec::from_glib_container_num_as_vec(ptr, c_ptr_array_len(ptr))
}
unsafe fn from_glib_full_as_vec(ptr: *const $ffi_name) -> Vec<Self> {
FromGlibContainerAsVec::from_glib_full_num_as_vec(ptr, c_ptr_array_len(ptr))
}
}
};
}
impl_from_glib_container_as_vec_string!(String, *const c_char);
impl_from_glib_container_as_vec_string!(String, *mut c_char);
impl_from_glib_container_as_vec_string!(PathBuf, *const c_char);
impl_from_glib_container_as_vec_string!(PathBuf, *mut c_char);
impl_from_glib_container_as_vec_string!(OsString, *const c_char);
impl_from_glib_container_as_vec_string!(OsString, *mut c_char);
impl<P, PP: Ptr, T: FromGlibContainerAsVec<P, PP>> FromGlibContainer<P, PP> for Vec<T> {
unsafe fn from_glib_none_num(ptr: PP, num: usize) -> Vec<T> {
FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, num)
}
unsafe fn from_glib_container_num(ptr: PP, num: usize) -> Vec<T> {
FromGlibContainerAsVec::from_glib_container_num_as_vec(ptr, num)
}
unsafe fn from_glib_full_num(ptr: PP, num: usize) -> Vec<T> {
FromGlibContainerAsVec::from_glib_full_num_as_vec(ptr, num)
}
}
impl<P: Ptr, PP: Ptr, T: FromGlibPtrArrayContainerAsVec<P, PP>> FromGlibPtrContainer<P, PP>
for Vec<T>
{
unsafe fn from_glib_none(ptr: PP) -> Vec<T> {
FromGlibPtrArrayContainerAsVec::from_glib_none_as_vec(ptr)
}
unsafe fn from_glib_container(ptr: PP) -> Vec<T> {
FromGlibPtrArrayContainerAsVec::from_glib_container_as_vec(ptr)
}
unsafe fn from_glib_full(ptr: PP) -> Vec<T> {
FromGlibPtrArrayContainerAsVec::from_glib_full_as_vec(ptr)
}
}
impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut ffi::GSList> for T
where
T: GlibPtrDefault
+ FromGlibPtrNone<<T as GlibPtrDefault>::GlibType>
+ FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
{
unsafe fn from_glib_none_num_as_vec(mut ptr: *mut ffi::GSList, num: usize) -> Vec<T> {
if num == 0 || ptr.is_null() {
return Vec::new();
}
let mut res = Vec::with_capacity(num);
for _ in 0..num {
if ptr.is_null() {
break;
}
let item_ptr: <T as GlibPtrDefault>::GlibType = Ptr::from((*ptr).data);
if !item_ptr.is_null() {
res.push(from_glib_none(item_ptr));
}
ptr = (*ptr).next;
}
res
}
unsafe fn from_glib_container_num_as_vec(ptr: *mut ffi::GSList, num: usize) -> Vec<T> {
let res = FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, num);
ffi::g_slist_free(ptr);
res
}
unsafe fn from_glib_full_num_as_vec(mut ptr: *mut ffi::GSList, num: usize) -> Vec<T> {
if num == 0 || ptr.is_null() {
return Vec::new();
}
let orig_ptr = ptr;
let mut res = Vec::with_capacity(num);
for _ in 0..num {
if ptr.is_null() {
break;
}
let item_ptr: <T as GlibPtrDefault>::GlibType = Ptr::from((*ptr).data);
if !item_ptr.is_null() {
res.push(from_glib_full(item_ptr));
}
ptr = (*ptr).next;
}
ffi::g_slist_free(orig_ptr);
res
}
}
impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut ffi::GSList> for T
where
T: GlibPtrDefault
+ FromGlibPtrNone<<T as GlibPtrDefault>::GlibType>
+ FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
{
unsafe fn from_glib_none_as_vec(mut ptr: *mut ffi::GSList) -> Vec<T> {
let mut res = Vec::new();
while !ptr.is_null() {
let item_ptr: <T as GlibPtrDefault>::GlibType = Ptr::from((*ptr).data);
if !item_ptr.is_null() {
res.push(from_glib_none(item_ptr));
}
ptr = (*ptr).next;
}
res
}
unsafe fn from_glib_container_as_vec(ptr: *mut ffi::GSList) -> Vec<T> {
let res = FromGlibPtrArrayContainerAsVec::from_glib_none_as_vec(ptr);
ffi::g_slist_free(ptr);
res
}
unsafe fn from_glib_full_as_vec(mut ptr: *mut ffi::GSList) -> Vec<T> {
let orig_ptr = ptr;
let mut res = Vec::new();
while !ptr.is_null() {
let item_ptr: <T as GlibPtrDefault>::GlibType = Ptr::from((*ptr).data);
if !item_ptr.is_null() {
res.push(from_glib_full(item_ptr));
}
ptr = (*ptr).next;
}
ffi::g_slist_free(orig_ptr);
res
}
}
impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut ffi::GList> for T
where
T: GlibPtrDefault
+ FromGlibPtrNone<<T as GlibPtrDefault>::GlibType>
+ FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
{
unsafe fn from_glib_none_num_as_vec(mut ptr: *mut ffi::GList, num: usize) -> Vec<T> {
if num == 0 || ptr.is_null() {
return Vec::new();
}
let mut res = Vec::with_capacity(num);
for _ in 0..num {
if ptr.is_null() {
break;
}
let item_ptr: <T as GlibPtrDefault>::GlibType = Ptr::from((*ptr).data);
if !item_ptr.is_null() {
res.push(from_glib_none(item_ptr));
}
ptr = (*ptr).next;
}
res
}
unsafe fn from_glib_container_num_as_vec(ptr: *mut ffi::GList, num: usize) -> Vec<T> {
let res = FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, num);
ffi::g_list_free(ptr);
res
}
unsafe fn from_glib_full_num_as_vec(mut ptr: *mut ffi::GList, num: usize) -> Vec<T> {
if num == 0 || ptr.is_null() {
return Vec::new();
}
let orig_ptr = ptr;
let mut res = Vec::with_capacity(num);
for _ in 0..num {
if ptr.is_null() {
break;
}
let item_ptr: <T as GlibPtrDefault>::GlibType = Ptr::from((*ptr).data);
if !item_ptr.is_null() {
res.push(from_glib_full(item_ptr));
}
ptr = (*ptr).next;
}
ffi::g_list_free(orig_ptr);
res
}
}
impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut ffi::GList> for T
where
T: GlibPtrDefault
+ FromGlibPtrNone<<T as GlibPtrDefault>::GlibType>
+ FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
{
unsafe fn from_glib_none_as_vec(mut ptr: *mut ffi::GList) -> Vec<T> {
let mut res = Vec::new();
while !ptr.is_null() {
let item_ptr: <T as GlibPtrDefault>::GlibType = Ptr::from((*ptr).data);
if !item_ptr.is_null() {
res.push(from_glib_none(item_ptr));
}
ptr = (*ptr).next;
}
res
}
unsafe fn from_glib_container_as_vec(ptr: *mut ffi::GList) -> Vec<T> {
let res = FromGlibPtrArrayContainerAsVec::from_glib_none_as_vec(ptr);
ffi::g_list_free(ptr);
res
}
unsafe fn from_glib_full_as_vec(mut ptr: *mut ffi::GList) -> Vec<T> {
let orig_ptr = ptr;
let mut res = Vec::new();
while !ptr.is_null() {
let item_ptr: <T as GlibPtrDefault>::GlibType = Ptr::from((*ptr).data);
if !item_ptr.is_null() {
res.push(from_glib_full(item_ptr));
}
ptr = (*ptr).next;
}
ffi::g_list_free(orig_ptr);
res
}
}
impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const ffi::GList> for T
where
T: GlibPtrDefault
+ FromGlibPtrNone<<T as GlibPtrDefault>::GlibType>
+ FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
{
unsafe fn from_glib_none_num_as_vec(ptr: *const ffi::GList, num: usize) -> Vec<T> {
FromGlibContainerAsVec::from_glib_none_num_as_vec(mut_override(ptr), num)
}
unsafe fn from_glib_container_num_as_vec(_: *const ffi::GList, _: usize) -> Vec<T> {
unimplemented!()
}
unsafe fn from_glib_full_num_as_vec(_: *const ffi::GList, _: usize) -> Vec<T> {
unimplemented!()
}
}
impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const ffi::GList> for T
where
T: GlibPtrDefault
+ FromGlibPtrNone<<T as GlibPtrDefault>::GlibType>
+ FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
{
unsafe fn from_glib_none_as_vec(ptr: *const ffi::GList) -> Vec<T> {
FromGlibPtrArrayContainerAsVec::from_glib_none_as_vec(mut_override(ptr))
}
unsafe fn from_glib_container_as_vec(_: *const ffi::GList) -> Vec<T> {
unimplemented!()
}
unsafe fn from_glib_full_as_vec(_: *const ffi::GList) -> Vec<T> {
unimplemented!()
}
}
impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const ffi::GSList> for T
where
T: GlibPtrDefault
+ FromGlibPtrNone<<T as GlibPtrDefault>::GlibType>
+ FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
{
unsafe fn from_glib_none_num_as_vec(ptr: *const ffi::GSList, num: usize) -> Vec<T> {
FromGlibContainerAsVec::from_glib_none_num_as_vec(mut_override(ptr), num)
}
unsafe fn from_glib_container_num_as_vec(_: *const ffi::GSList, _: usize) -> Vec<T> {
unimplemented!()
}
unsafe fn from_glib_full_num_as_vec(_: *const ffi::GSList, _: usize) -> Vec<T> {
unimplemented!()
}
}
impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const ffi::GSList> for T
where
T: GlibPtrDefault
+ FromGlibPtrNone<<T as GlibPtrDefault>::GlibType>
+ FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
{
unsafe fn from_glib_none_as_vec(ptr: *const ffi::GSList) -> Vec<T> {
FromGlibPtrArrayContainerAsVec::from_glib_none_as_vec(mut_override(ptr))
}
unsafe fn from_glib_container_as_vec(_: *const ffi::GSList) -> Vec<T> {
unimplemented!()
}
unsafe fn from_glib_full_as_vec(_: *const ffi::GSList) -> Vec<T> {
unimplemented!()
}
}
#[allow(clippy::implicit_hasher)]
impl FromGlibContainer<*const c_char, *mut ffi::GHashTable> for HashMap<String, String> {
unsafe fn from_glib_none_num(ptr: *mut ffi::GHashTable, _: usize) -> Self {
FromGlibPtrContainer::from_glib_none(ptr)
}
unsafe fn from_glib_container_num(ptr: *mut ffi::GHashTable, _: usize) -> Self {
FromGlibPtrContainer::from_glib_full(ptr)
}
unsafe fn from_glib_full_num(ptr: *mut ffi::GHashTable, _: usize) -> Self {
FromGlibPtrContainer::from_glib_full(ptr)
}
}
#[allow(clippy::implicit_hasher)]
impl FromGlibPtrContainer<*const c_char, *mut ffi::GHashTable> for HashMap<String, String> {
unsafe fn from_glib_none(ptr: *mut ffi::GHashTable) -> Self {
unsafe extern "C" fn read_string_hash_table(
key: ffi::gpointer,
value: ffi::gpointer,
hash_map: ffi::gpointer,
) {
let key: String = from_glib_none(key as *const c_char);
let value: String = from_glib_none(value as *const c_char);
let hash_map: &mut HashMap<String, String> =
&mut *(hash_map as *mut HashMap<String, String>);
hash_map.insert(key, value);
}
let mut map = HashMap::with_capacity(ffi::g_hash_table_size(ptr) as usize);
ffi::g_hash_table_foreach(
ptr,
Some(read_string_hash_table),
&mut map as *mut HashMap<String, String> as *mut _,
);
map
}
unsafe fn from_glib_container(ptr: *mut ffi::GHashTable) -> Self {
FromGlibPtrContainer::from_glib_full(ptr)
}
unsafe fn from_glib_full(ptr: *mut ffi::GHashTable) -> Self {
let map = FromGlibPtrContainer::from_glib_none(ptr);
ffi::g_hash_table_unref(ptr);
map
}
}
impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut ffi::GPtrArray> for T
where
T: GlibPtrDefault
+ FromGlibPtrNone<<T as GlibPtrDefault>::GlibType>
+ FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
{
unsafe fn from_glib_none_num_as_vec(ptr: *mut ffi::GPtrArray, num: usize) -> Vec<T> {
if num == 0 || ptr.is_null() {
return Vec::new();
}
let pdata = (*ptr).pdata;
debug_assert!((*ptr).len as usize >= num);
let mut res = Vec::with_capacity(num);
for i in 0..num {
let item_ptr: <T as GlibPtrDefault>::GlibType = Ptr::from(ptr::read(pdata.add(i)));
if !item_ptr.is_null() {
res.push(from_glib_none(item_ptr));
}
}
res
}
unsafe fn from_glib_container_num_as_vec(ptr: *mut ffi::GPtrArray, num: usize) -> Vec<T> {
let res = FromGlibContainer::from_glib_none_num(ptr, num);
if !ptr.is_null() {
ffi::g_ptr_array_unref(ptr);
}
res
}
unsafe fn from_glib_full_num_as_vec(ptr: *mut ffi::GPtrArray, num: usize) -> Vec<T> {
if ptr.is_null() {
return Vec::new();
}
if num == 0 {
ffi::g_ptr_array_unref(ptr);
return Vec::new();
}
let pdata = (*ptr).pdata;
debug_assert!((*ptr).len as usize >= num);
let mut res = Vec::with_capacity(num);
for i in 0..num {
let item_ptr: <T as GlibPtrDefault>::GlibType = Ptr::from(ptr::read(pdata.add(i)));
if !item_ptr.is_null() {
res.push(from_glib_none(item_ptr));
}
}
ffi::g_ptr_array_unref(ptr);
res
}
}
impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut ffi::GPtrArray> for T
where
T: GlibPtrDefault
+ FromGlibPtrNone<<T as GlibPtrDefault>::GlibType>
+ FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
{
unsafe fn from_glib_none_as_vec(ptr: *mut ffi::GPtrArray) -> Vec<T> {
let num = (*ptr).len as usize;
FromGlibContainer::from_glib_none_num(ptr, num)
}
unsafe fn from_glib_container_as_vec(ptr: *mut ffi::GPtrArray) -> Vec<T> {
let num = (*ptr).len as usize;
FromGlibContainer::from_glib_container_num(ptr, num)
}
unsafe fn from_glib_full_as_vec(ptr: *mut ffi::GPtrArray) -> Vec<T> {
let num = (*ptr).len as usize;
FromGlibContainer::from_glib_full_num(ptr, num)
}
}
impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const ffi::GPtrArray> for T
where
T: GlibPtrDefault
+ FromGlibPtrNone<<T as GlibPtrDefault>::GlibType>
+ FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
{
unsafe fn from_glib_none_num_as_vec(ptr: *const ffi::GPtrArray, num: usize) -> Vec<T> {
FromGlibContainerAsVec::from_glib_none_num_as_vec(mut_override(ptr), num)
}
unsafe fn from_glib_container_num_as_vec(_: *const ffi::GPtrArray, _: usize) -> Vec<T> {
unimplemented!()
}
unsafe fn from_glib_full_num_as_vec(_: *const ffi::GPtrArray, _: usize) -> Vec<T> {
unimplemented!()
}
}
impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const ffi::GPtrArray> for T
where
T: GlibPtrDefault
+ FromGlibPtrNone<<T as GlibPtrDefault>::GlibType>
+ FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
{
unsafe fn from_glib_none_as_vec(ptr: *const ffi::GPtrArray) -> Vec<T> {
FromGlibPtrArrayContainerAsVec::from_glib_none_as_vec(mut_override(ptr))
}
unsafe fn from_glib_container_as_vec(_: *const ffi::GPtrArray) -> Vec<T> {
unimplemented!()
}
unsafe fn from_glib_full_as_vec(_: *const ffi::GPtrArray) -> Vec<T> {
unimplemented!()
}
}
pub unsafe trait TransparentPtrType: Clone + Sized + GlibPtrDefault {}
pub unsafe trait TransparentType: Clone + Sized {
type GlibType;
}
unsafe impl<T: TransparentPtrType> TransparentType for T {
type GlibType = <T as GlibPtrDefault>::GlibType;
}
#[cfg(test)]
mod tests {
use std::{collections::HashMap, fs};
use tempfile::tempdir;
use super::*;
use crate::{FileTest, GString};
#[test]
fn boolean() {
assert_eq!(true.into_glib(), ffi::GTRUE);
assert_eq!(false.into_glib(), ffi::GFALSE);
assert!(unsafe { bool::from_glib(ffi::GTRUE) });
assert!(!unsafe { bool::from_glib(ffi::GFALSE) });
assert!(unsafe { bool::from_glib(42) });
}
#[test]
fn ordering() {
assert_eq!(Ordering::Less.into_glib(), -1);
assert_eq!(Ordering::Equal.into_glib(), 0);
assert_eq!(Ordering::Greater.into_glib(), 1);
assert_eq!(Ordering::Less, unsafe { Ordering::from_glib(-42) });
assert_eq!(Ordering::Less, unsafe { Ordering::from_glib(-1) });
assert_eq!(Ordering::Equal, unsafe { Ordering::from_glib(0) });
assert_eq!(Ordering::Greater, unsafe { Ordering::from_glib(1) });
assert_eq!(Ordering::Greater, unsafe { Ordering::from_glib(42) });
}
#[test]
fn string() {
let s = "ABC";
let owned = "ABC".to_string();
let cstring = CString::new("ABC").unwrap();
let stash = s.to_glib_none();
assert_eq!(unsafe { CStr::from_ptr(stash.0) }, cstring.as_c_str());
let stash = owned.to_glib_none();
assert_eq!(unsafe { CStr::from_ptr(stash.0) }, cstring.as_c_str());
let ptr: *mut c_char = s.to_glib_full();
assert_eq!(unsafe { CStr::from_ptr(ptr) }, cstring.as_c_str());
unsafe {
ffi::g_free(ptr as *mut _);
}
let ptr: *mut c_char = owned.to_glib_full();
assert_eq!(unsafe { CStr::from_ptr(ptr) }, cstring.as_c_str());
assert_eq!(s, unsafe { String::from_glib_none(ptr) });
assert_eq!(owned, unsafe { String::from_glib_full(ptr) });
}
#[test]
fn string_hash_map() {
let mut map = HashMap::new();
map.insert("A".into(), "1".into());
map.insert("B".into(), "2".into());
map.insert("C".into(), "3".into());
let ptr: *mut ffi::GHashTable = map.to_glib_full();
let map = unsafe { HashMap::from_glib_full(ptr) };
assert_eq!(map.get("A"), Some(&"1".into()));
assert_eq!(map.get("B"), Some(&"2".into()));
assert_eq!(map.get("C"), Some(&"3".into()));
}
#[test]
fn string_array() {
let v = vec!["A".to_string(), "B".to_string(), "C".to_string()];
let stash = v.to_glib_none();
let ptr: *mut *mut c_char = stash.0;
let ptr_copy = unsafe { ffi::g_strdupv(ptr) };
let actual: Vec<String> = unsafe { FromGlibPtrContainer::from_glib_full(ptr_copy) };
assert_eq!(v, actual);
}
#[test]
fn gstring_array() {
let v = vec!["A".to_string(), "B".to_string(), "C".to_string()];
let stash = v.to_glib_none();
let ptr: *mut *mut c_char = stash.0;
let ptr_copy = unsafe { ffi::g_strdupv(ptr) };
let actual: Vec<GString> = unsafe { FromGlibPtrContainer::from_glib_full(ptr_copy) };
assert_eq!(v, actual);
}
#[test]
fn ptr_array() {
let strings = &["A", "B", "C"];
let (ptr, _stash) =
ToGlibContainerFromSlice::<*mut ffi::GPtrArray>::to_glib_none_from_slice(strings);
let v: Vec<GString> = unsafe { FromGlibPtrArrayContainerAsVec::from_glib_none_as_vec(ptr) };
assert_eq!(&v, strings);
}
#[test]
#[cfg(not(target_os = "macos"))]
fn test_paths() {
let tmp_dir = tempdir().unwrap();
let dir_1 = tmp_dir.path().join("abcd");
fs::create_dir(&dir_1).unwrap();
assert_eq!(crate::path_get_basename(&dir_1), Path::new("abcd"));
assert_eq!(
crate::path_get_basename(dir_1.canonicalize().unwrap()),
Path::new("abcd")
);
#[cfg(not(windows))]
assert_eq!(
crate::path_get_dirname(dir_1.canonicalize().unwrap()),
tmp_dir.path()
);
assert!(crate::file_test(
&dir_1,
FileTest::EXISTS | FileTest::IS_DIR
));
assert!(crate::file_test(
dir_1.canonicalize().unwrap(),
FileTest::EXISTS | FileTest::IS_DIR
));
let dir_2 = tmp_dir.as_ref().join("øäöü");
fs::create_dir(&dir_2).unwrap();
assert_eq!(crate::path_get_basename(&dir_2), Path::new("øäöü"));
assert_eq!(
crate::path_get_basename(dir_2.canonicalize().unwrap()),
Path::new("øäöü")
);
#[cfg(not(windows))]
assert_eq!(
crate::path_get_dirname(dir_2.canonicalize().unwrap()),
tmp_dir.path()
);
assert!(crate::file_test(
&dir_2,
FileTest::EXISTS | FileTest::IS_DIR
));
assert!(crate::file_test(
dir_2.canonicalize().unwrap(),
FileTest::EXISTS | FileTest::IS_DIR
));
}
#[test]
#[cfg(target_os = "macos")]
fn test_paths() {
let t_dir = tempdir().unwrap();
let tmp_dir = t_dir.path().canonicalize().unwrap();
let dir_1 = tmp_dir.join("abcd");
fs::create_dir(&dir_1).unwrap();
assert_eq!(crate::path_get_basename(&dir_1), Path::new("abcd"));
assert_eq!(
crate::path_get_basename(dir_1.canonicalize().unwrap()),
Path::new("abcd")
);
assert_eq!(
crate::path_get_dirname(dir_1.canonicalize().unwrap()),
tmp_dir
);
assert!(crate::file_test(
&dir_1,
FileTest::EXISTS | FileTest::IS_DIR
));
assert!(crate::file_test(
&dir_1.canonicalize().unwrap(),
FileTest::EXISTS | FileTest::IS_DIR
));
}
#[test]
fn none_value() {
const CLONG_NONE: libc::c_long = -1;
#[derive(Debug, PartialEq, Eq)]
struct SpecialU32(u32);
impl IntoGlib for SpecialU32 {
type GlibType = libc::c_uint;
fn into_glib(self) -> libc::c_uint {
self.0 as libc::c_uint
}
}
impl OptionIntoGlib for SpecialU32 {
const GLIB_NONE: Self::GlibType = CLONG_NONE as libc::c_uint;
}
assert_eq!(SpecialU32(0).into_glib(), 0);
assert_eq!(SpecialU32(42).into_glib(), 42);
assert_eq!(Some(SpecialU32(0)).into_glib(), 0);
assert_eq!(Some(SpecialU32(42)).into_glib(), 42);
assert_eq!(
Option::None::<SpecialU32>.into_glib(),
SpecialU32::GLIB_NONE
);
impl TryFromGlib<libc::c_uint> for SpecialU32 {
type Error = GlibNoneError;
#[allow(clippy::unnecessary_cast)]
unsafe fn try_from_glib(val: libc::c_uint) -> Result<Self, GlibNoneError> {
if val == SpecialU32::GLIB_NONE {
return Err(GlibNoneError);
}
Ok(SpecialU32(val as u32))
}
}
assert_eq!(unsafe { SpecialU32::try_from_glib(0) }, Ok(SpecialU32(0)));
assert_eq!(unsafe { SpecialU32::try_from_glib(42) }, Ok(SpecialU32(42)));
assert_eq!(
unsafe { SpecialU32::try_from_glib(SpecialU32::GLIB_NONE) },
Err(GlibNoneError)
);
assert_eq!(
unsafe { Option::<SpecialU32>::from_glib(0) },
Some(SpecialU32(0))
);
assert_eq!(
unsafe { Option::<SpecialU32>::from_glib(42) },
Some(SpecialU32(42))
);
assert!(unsafe { Option::<SpecialU32>::from_glib(SpecialU32::GLIB_NONE) }.is_none());
}
#[test]
fn invalid_value() {
use std::num::TryFromIntError;
#[derive(Debug, PartialEq, Eq)]
struct U32(u32);
impl TryFromGlib<libc::c_long> for U32 {
type Error = TryFromIntError;
unsafe fn try_from_glib(val: libc::c_long) -> Result<Self, TryFromIntError> {
Ok(U32(u32::try_from(val)?))
}
}
assert_eq!(unsafe { U32::try_from_glib(0) }, Ok(U32(0)));
assert_eq!(unsafe { U32::try_from_glib(42) }, Ok(U32(42)));
assert!(unsafe { U32::try_from_glib(-1) }.is_err());
assert!(unsafe { U32::try_from_glib(-42) }.is_err());
}
#[test]
fn none_or_invalid_value() {
use std::num::TryFromIntError;
#[derive(Debug, PartialEq, Eq)]
struct SpecialU32(u32);
impl IntoGlib for SpecialU32 {
type GlibType = libc::c_long;
fn into_glib(self) -> libc::c_long {
self.0 as libc::c_long
}
}
impl OptionIntoGlib for SpecialU32 {
const GLIB_NONE: Self::GlibType = -1;
}
assert_eq!(SpecialU32(0).into_glib(), 0);
assert_eq!(SpecialU32(42).into_glib(), 42);
assert_eq!(Some(SpecialU32(42)).into_glib(), 42);
assert_eq!(
Option::None::<SpecialU32>.into_glib(),
SpecialU32::GLIB_NONE
);
impl TryFromGlib<libc::c_long> for SpecialU32 {
type Error = GlibNoneOrInvalidError<TryFromIntError>;
unsafe fn try_from_glib(
val: libc::c_long,
) -> Result<Self, GlibNoneOrInvalidError<TryFromIntError>> {
if val == SpecialU32::GLIB_NONE {
return Err(GlibNoneOrInvalidError::None);
}
Ok(SpecialU32(u32::try_from(val)?))
}
}
assert_eq!(unsafe { SpecialU32::try_from_glib(0) }, Ok(SpecialU32(0)));
assert_eq!(unsafe { SpecialU32::try_from_glib(42) }, Ok(SpecialU32(42)));
assert!(unsafe { SpecialU32::try_from_glib(SpecialU32::GLIB_NONE) }
.unwrap_err()
.is_none());
assert!(unsafe { SpecialU32::try_from_glib(-42) }
.unwrap_err()
.is_invalid());
assert_eq!(
unsafe { Result::<Option<SpecialU32>, _>::from_glib(0) },
Ok(Some(SpecialU32(0)))
);
assert_eq!(
unsafe { Result::<Option<SpecialU32>, _>::from_glib(42) },
Ok(Some(SpecialU32(42)))
);
assert_eq!(
unsafe { Result::<Option<SpecialU32>, _>::from_glib(SpecialU32::GLIB_NONE) },
Ok(None)
);
assert!(unsafe { Result::<Option<SpecialU32>, _>::from_glib(-42) }.is_err());
}
}