Skip to content

Commit

Permalink
Support more variant types (bool), and fixing cyndis#16, too
Browse files Browse the repository at this point in the history
  • Loading branch information
liamsi committed Jun 13, 2015
1 parent 78cc247 commit 19a340f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub enum QVariantList {}
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
pub enum QrsVariantType {
Invalid = 0,
Bool,
Int64,
String
}
Expand All @@ -32,10 +33,12 @@ extern "C" {
pub fn qmlrs_variant_create() -> *mut QVariant;
pub fn qmlrs_variant_destroy(v: *mut QVariant);
pub fn qmlrs_variant_set_int64(var: *mut QVariant, x: i64);
pub fn qmlrs_variant_set_bool(var: *mut QVariant, x: bool);
pub fn qmlrs_variant_set_invalid(var: *mut QVariant);
pub fn qmlrs_variant_set_string(var: *mut QVariant, len: c_uint, data: *const c_char);
pub fn qmlrs_variant_get_type(var: *const QVariant) -> QrsVariantType;
pub fn qmlrs_variant_get_int64(var: *const QVariant, x: *mut i64);
pub fn qmlrs_variant_get_bool(var: *const QVariant, x: *mut bool);
pub fn qmlrs_variant_get_string_length(var: *const QVariant, out: *mut c_uint);
pub fn qmlrs_variant_get_string_data(var: *const QVariant, out: *mut c_char);

Expand Down
26 changes: 26 additions & 0 deletions src/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ffi::{QVariant, QrsVariantType};

pub enum Variant {
I64(i64),
Bool(bool),
String(String),
}

Expand All @@ -25,6 +26,20 @@ impl FromQVariant for i64 {
}
}

impl FromQVariant for bool {
fn from_qvariant(var: *const QVariant) -> Option<bool> {
unsafe {
if ffi::qmlrs_variant_get_type(var) == QrsVariantType::Bool {
let mut x: bool = false;
ffi::qmlrs_variant_get_bool(var, &mut x);
Some(x)
} else {
None
}
}
}
}

impl FromQVariant for String {
fn from_qvariant(var: *const QVariant) -> Option<String> {
unsafe {
Expand All @@ -51,6 +66,8 @@ impl FromQVariant for Variant {
match ffi::qmlrs_variant_get_type(var) {
Int64 =>
Some(Variant::I64(FromQVariant::from_qvariant(var).unwrap())),
Bool =>
Some(Variant::Bool(FromQVariant::from_qvariant(var).unwrap())),
String =>
Some(Variant::String(FromQVariant::from_qvariant(var).unwrap())),
_ => None
Expand Down Expand Up @@ -79,6 +96,14 @@ impl ToQVariant for i64 {
}
}

impl ToQVariant for bool {
fn to_qvariant(&self, var: *mut QVariant) {
unsafe {
ffi::qmlrs_variant_set_bool(var, *self);
}
}
}

macro_rules! int_toqvar {
($($t:ty)*) => (
$(
Expand Down Expand Up @@ -106,6 +131,7 @@ impl ToQVariant for Variant {
fn to_qvariant(&self, var: *mut QVariant) {
match *self {
Variant::I64(ref x) => x.to_qvariant(var),
Variant::Bool(ref x) => x.to_qvariant(var),
Variant::String(ref s) => s.to_qvariant(var),
}
}
Expand Down

0 comments on commit 19a340f

Please sign in to comment.