Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more variant types (bool), and fixing #16, too #17

Merged
merged 1 commit into from
Jun 14, 2015
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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