-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
247: impl `GodotFfi` for `Option<T>` when `T` is pointer sized and nullable #240 r=Bromeon a=TitanNano `ToVariant` and `FromVariant` are implemented for `Option<Gd<T>>` as well as they are a requirement of `#[godot_api]`. Closes #240. Co-authored-by: Jovan Gerodetti <jovan.gerodetti@titannano.de>
- Loading branch information
Showing
6 changed files
with
250 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
use godot::prelude::{godot_api, Gd, GodotClass, Node, Object, RefCounted}; | ||
use godot::sys::GodotFfi; | ||
|
||
use crate::itest; | ||
|
||
#[itest] | ||
fn option_some_sys_conversion() { | ||
let v = Some(Object::new_alloc()); | ||
let ptr = v.sys(); | ||
|
||
let v2 = unsafe { Option::<Gd<Object>>::from_sys(ptr) }; | ||
assert_eq!(v2, v); | ||
|
||
v.unwrap().free(); | ||
} | ||
|
||
#[itest] | ||
fn option_none_sys_conversion() { | ||
let v = None; | ||
let ptr = v.sys(); | ||
|
||
let v2 = unsafe { Option::<Gd<Object>>::from_sys(ptr) }; | ||
assert_eq!(v2, v); | ||
} | ||
|
||
#[derive(GodotClass, Debug)] | ||
#[class(base = RefCounted, init)] | ||
struct OptionFfiTest; | ||
|
||
#[godot_api] | ||
impl OptionFfiTest { | ||
#[func] | ||
fn return_option_refcounted_none(&self) -> Option<Gd<RefCounted>> { | ||
None | ||
} | ||
|
||
#[func] | ||
fn accept_option_refcounted_none(&self, value: Option<Gd<RefCounted>>) -> bool { | ||
value.is_none() | ||
} | ||
|
||
#[func] | ||
fn return_option_refcounted_some(&self) -> Option<Gd<RefCounted>> { | ||
Some(RefCounted::new()) | ||
} | ||
|
||
#[func] | ||
fn accept_option_refcounted_some(&self, value: Option<Gd<RefCounted>>) -> bool { | ||
value.is_some() | ||
} | ||
|
||
#[func] | ||
fn mirror_option_refcounted(&self, value: Option<Gd<RefCounted>>) -> Option<Gd<RefCounted>> { | ||
value | ||
} | ||
|
||
#[func] | ||
fn return_option_node_none(&self) -> Option<Gd<Node>> { | ||
None | ||
} | ||
|
||
#[func] | ||
fn accept_option_node_none(&self, value: Option<Gd<Node>>) -> bool { | ||
value.is_none() | ||
} | ||
|
||
#[func] | ||
fn return_option_node_some(&self) -> Option<Gd<Node>> { | ||
Some(Node::new_alloc()) | ||
} | ||
|
||
#[func] | ||
fn accept_option_node_some(&self, value: Option<Gd<Node>>) -> bool { | ||
value.is_some() | ||
} | ||
|
||
#[func] | ||
fn mirror_option_node(&self, value: Option<Gd<Node>>) -> Option<Gd<Node>> { | ||
value | ||
} | ||
} |