Skip to content

Commit

Permalink
Merge pull request #847 from godot-rust/qol/object-arg-mut-refs
Browse files Browse the repository at this point in the history
`AsObjectArg<T>` is now implemented for `&mut Gd<T>`
  • Loading branch information
Bromeon authored Aug 10, 2024
2 parents b294625 + 4a1b88a commit 712e166
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
16 changes: 16 additions & 0 deletions godot-core/src/obj/object_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ where
}
}

impl<T, U> AsObjectArg<T> for &mut Gd<U>
where
T: GodotClass + Bounds<Declarer = bounds::DeclEngine>,
U: Inherits<T>,
{
// Delegate to &Gd impl.

fn as_object_arg(&self) -> ObjectArg<T> {
<&Gd<U>>::as_object_arg(&&**self)
}

fn consume_object(self) -> ObjectCow<T> {
<&Gd<U>>::consume_object(&*self)
}
}

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

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

#[itest]
fn object_arg_option_owned() {
with_objects(|manual, refc| {
Expand All @@ -53,6 +63,16 @@ fn object_arg_option_borrowed() {
});
}

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

#[itest]
fn object_arg_option_none() {
let manual: Option<Gd<Node>> = None;
Expand Down

0 comments on commit 712e166

Please sign in to comment.