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

Commit

Permalink
glib: Merge pull request #259 from sdroege/strv
Browse files Browse the repository at this point in the history
Add support for storing/retrieving (NULL terminated) String arrays in…
  • Loading branch information
GuillaumeGomez committed Nov 20, 2017
2 parents 7ff5a3d + b22571e commit 5764c68
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/types.rs
Expand Up @@ -115,6 +115,22 @@ builtin!(f64, F64);
builtin!(str, String);
builtin!(String, String);

impl<'a> StaticType for [&'a str] {
fn static_type() -> Type {
unsafe {
from_glib(glib_ffi::g_strv_get_type())
}
}
}

impl StaticType for Vec<String> {
fn static_type() -> Type {
unsafe {
from_glib(glib_ffi::g_strv_get_type())
}
}
}

pub trait InstanceType {
fn instance_type(&self) -> Type;
}
Expand Down
50 changes: 49 additions & 1 deletion src/value.rs
Expand Up @@ -87,7 +87,7 @@ use std::ffi::CStr;
use std::ptr;
use std::any::Any;
use std::sync::Arc;
use libc::c_void;
use libc::{c_char, c_void};

use translate::*;
use types::{StaticType, Type};
Expand Down Expand Up @@ -768,6 +768,45 @@ impl SetValueOptional for str {
}
}

impl<'a> FromValueOptional<'a> for Vec<String> {
unsafe fn from_value_optional(value: &'a Value) -> Option<Self> {
let ptr = gobject_ffi::g_value_get_boxed(value.to_glib_none().0) as *const *const c_char;
if ptr.is_null() {
None
} else {
Some(FromGlibPtrContainer::from_glib_none(ptr))
}
}
}

impl<'a> SetValue for [&'a str] {
unsafe fn set_value(value: &mut Value, this: &Self) {
let ptr: *mut *mut c_char = this.to_glib_full();
gobject_ffi::g_value_take_boxed(value.to_glib_none_mut().0, ptr as *const c_void)
}
}

impl<'a> SetValueOptional for [&'a str] {
unsafe fn set_value_optional(value: &mut Value, this: Option<&Self>) {
let ptr: *mut *mut c_char = this.to_glib_full();
gobject_ffi::g_value_take_boxed(value.to_glib_none_mut().0, ptr as *const c_void)
}
}

impl SetValue for Vec<String> {
unsafe fn set_value(value: &mut Value, this: &Self) {
let ptr: *mut *mut c_char = this.to_glib_full();
gobject_ffi::g_value_take_boxed(value.to_glib_none_mut().0, ptr as *const c_void)
}
}

impl SetValueOptional for Vec<String> {
unsafe fn set_value_optional(value: &mut Value, this: Option<&Self>) {
let ptr: *mut *mut c_char = this.map(|v| v.to_glib_full()).unwrap_or(ptr::null_mut());
gobject_ffi::g_value_take_boxed(value.to_glib_none_mut().0, ptr as *const c_void)
}
}

impl<'a, T: ?Sized + SetValue> SetValue for &'a T {
unsafe fn set_value(value: &mut Value, this: &Self) {
SetValue::set_value(value, *this)
Expand Down Expand Up @@ -1123,4 +1162,13 @@ mod tests {
use std::thread;
thread::spawn(move || drop(any_v)).join().unwrap();
}

#[test]
fn test_strv() {
let v = vec!["123", "456"].to_value();
assert_eq!(v.get::<Vec<String>>(), Some(vec!["123".into(), "456".into()]));

let v = vec![String::from("123"), String::from("456")].to_value();
assert_eq!(v.get::<Vec<String>>(), Some(vec!["123".into(), "456".into()]));
}
}

0 comments on commit 5764c68

Please sign in to comment.