Skip to content

Commit dc6f3e2

Browse files
author
toasteater
committed
Implement derive macro for ToVariant and FromVariant
The macro does the following mapping between Rust structures and Godot types: - `Newtype(inner)` are mapped to `inner` - `Tuple(a, b, c)` are mapped to `[a, b, c]` - `Struct { a, b, c }` are mapped to `{ "a": a, "b": b, "c": c }` - `Unit` are mapped to `{}` - `Enum::Variant(a, b, c)` are mapped to `{ "Variant": [a, b, c] }`
1 parent 060c8c1 commit dc6f3e2

File tree

5 files changed

+569
-0
lines changed

5 files changed

+569
-0
lines changed

gdnative-core/src/variant.rs

+19
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,25 @@ godot_test!(
688688
assert!(v_m1.try_to_f64().is_none());
689689
assert!(v_m1.try_to_array().is_none());
690690
}
691+
692+
test_variant_bool {
693+
let v_true = Variant::from_bool(true);
694+
assert_eq!(v_true.get_type(), VariantType::Bool);
695+
696+
assert!(!v_true.is_nil());
697+
assert_eq!(v_true.try_to_bool(), Some(true));
698+
assert!(v_true.try_to_f64().is_none());
699+
assert!(v_true.try_to_array().is_none());
700+
701+
let v_false = Variant::from_bool(false);
702+
assert_eq!(v_false.get_type(), VariantType::Bool);
703+
704+
assert!(!v_false.is_nil());
705+
assert_eq!(v_false.try_to_bool(), Some(false));
706+
assert!(v_false.try_to_f64().is_none());
707+
assert!(v_false.try_to_array().is_none());
708+
709+
}
691710
);
692711

693712
/// Types that can be converted to a `Variant`.

gdnative-derive/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ proc-macro = true
1515
[dependencies]
1616
syn = { version = "0.15.29", features = ["full", "extra-traits"] }
1717
quote = "0.6.11"
18+
proc-macro2 = "^0.4.4"

0 commit comments

Comments
 (0)