Skip to content

Commit

Permalink
Add impl AsObjectArg<T> for &Option<Gd<T>> (outer ref)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bromeon committed Nov 9, 2024
1 parent d2c61db commit 5512a13
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
21 changes: 21 additions & 0 deletions godot-core/src/obj/object_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,27 @@ where
}
}

// Exists to not require `.as_ref()` calls when passing options.
impl<T, U> AsObjectArg<T> for &Option<U>
where
T: GodotClass + Bounds<Declarer = bounds::DeclEngine>,
for<'a> &'a U: AsObjectArg<T>,
{
fn as_object_arg(&self) -> ObjectArg<T> {
match self {
Some(obj) => obj.as_object_arg(),
None => ObjectArg::null(),
}
}

fn consume_arg(self) -> ObjectCow<T> {
match self {
Some(obj) => obj.consume_arg(),
None => Gd::null_arg().consume_arg(),
}
}
}

impl<T> AsObjectArg<T> for ObjectNullArg<T>
where
T: GodotClass + Bounds<Declarer = bounds::DeclEngine>,
Expand Down
10 changes: 10 additions & 0 deletions itest/rust/src/object_tests/object_arg_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ fn object_arg_option_borrowed() {
});
}

#[itest]
fn object_arg_option_borrowed_outer() {
with_objects(|manual, refc| {
let db = ClassDb::singleton();
let a = db.class_set_property(&Some(manual), "name", &Variant::from("hello"));
let b = db.class_set_property(&Some(refc), "value", &Variant::from(-123));
(a, b)
});
}

#[itest]
fn object_arg_option_borrowed_mut() {
with_objects(|mut manual, mut refc| {
Expand Down

0 comments on commit 5512a13

Please sign in to comment.