Skip to content

Commit e7db2c1

Browse files
committed
Add ParamType::arg_into_owned() for generic arg->value conversion
1 parent 2bc0872 commit e7db2c1

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

godot-core/src/builtin/collections/array.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,10 @@ impl<T: ArrayElement> ParamType for Array<T> {
11371137
fn arg_to_ref<'r>(arg: &'r Self::Arg<'_>) -> &'r Self {
11381138
arg.cow_as_ref()
11391139
}
1140+
1141+
fn arg_into_owned(arg: Self::Arg<'_>) -> Self {
1142+
arg.cow_into_owned()
1143+
}
11401144
}
11411145

11421146
impl<T: ArrayElement> GodotConvert for Array<T> {

godot-core/src/meta/args/as_arg.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,17 @@ macro_rules! arg_into_ref {
9090
#[macro_export]
9191
macro_rules! arg_into_owned {
9292
($arg_variable:ident) => {
93+
// Non-generic version allows type inference. Only applicable for CowArg types.
9394
let $arg_variable = $arg_variable.into_arg();
9495
let $arg_variable = $arg_variable.cow_into_owned();
95-
// cow_into_owned() is not yet used generically; could be abstracted in ParamType::arg_to_owned() as well.
96+
};
97+
($arg_variable:ident: $T:ty) => {
98+
let $arg_variable = $arg_variable.into_arg();
99+
let $arg_variable: $T = $crate::meta::ParamType::arg_into_owned($arg_variable);
100+
};
101+
(infer $arg_variable:ident) => {
102+
let $arg_variable = $arg_variable.into_arg();
103+
let $arg_variable = $crate::meta::ParamType::arg_into_owned($arg_variable);
96104
};
97105
}
98106

@@ -116,6 +124,10 @@ macro_rules! impl_asarg_by_value {
116124
fn arg_to_ref<'r>(arg: &'r Self::Arg<'_>) -> &'r Self {
117125
arg
118126
}
127+
128+
fn arg_into_owned(arg: Self::Arg<'_>) -> Self {
129+
arg
130+
}
119131
}
120132
};
121133
}
@@ -149,6 +161,10 @@ macro_rules! impl_asarg_by_ref {
149161
fn arg_to_ref<'r>(arg: &'r Self::Arg<'_>) -> &'r Self {
150162
arg.cow_as_ref()
151163
}
164+
165+
fn arg_into_owned(arg: Self::Arg<'_>) -> Self {
166+
arg.cow_into_owned()
167+
}
152168
}
153169
};
154170
}
@@ -280,4 +296,10 @@ pub trait ParamType: sealed::Sealed + Sized + 'static
280296
/// Useful in generic contexts where you need to extract a reference of an argument, independently of how it is passed.
281297
#[doc(hidden)] // for now, users are encouraged to use only call-site of impl AsArg; declaration-site may still develop.
282298
fn arg_to_ref<'r>(arg: &'r Self::Arg<'_>) -> &'r Self;
299+
300+
/// Clones an argument into an owned value.
301+
///
302+
/// Useful in generic contexts where you need to extract a value of an argument, independently of how it is passed.
303+
#[doc(hidden)] // for now, users are encouraged to use only call-site of impl AsArg; declaration-site may still develop.
304+
fn arg_into_owned(arg: Self::Arg<'_>) -> Self;
283305
}

godot-core/src/obj/dyn_gd.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,10 @@ where
557557
fn arg_to_ref<'r>(arg: &'r Self::Arg<'_>) -> &'r Self {
558558
arg.cow_as_ref()
559559
}
560+
561+
fn arg_into_owned(arg: Self::Arg<'_>) -> Self {
562+
arg.cow_into_owned()
563+
}
560564
}
561565

562566
impl<T, D> meta::ArrayElement for DynGd<T, D>

godot-core/src/obj/gd.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,10 @@ impl<T: GodotClass> ParamType for Gd<T> {
862862
fn arg_to_ref<'r>(arg: &'r Self::Arg<'_>) -> &'r Self {
863863
arg.cow_as_ref()
864864
}
865+
866+
fn arg_into_owned(arg: Self::Arg<'_>) -> Self {
867+
arg.cow_into_owned()
868+
}
865869
}
866870

867871
impl<T: GodotClass> AsArg<Option<Gd<T>>> for Option<&Gd<T>> {
@@ -884,6 +888,10 @@ impl<T: GodotClass> ParamType for Option<Gd<T>> {
884888
fn arg_to_ref<'r>(arg: &'r Self::Arg<'_>) -> &'r Self {
885889
arg.cow_as_ref()
886890
}
891+
892+
fn arg_into_owned(arg: Self::Arg<'_>) -> Self {
893+
arg.cow_into_owned()
894+
}
887895
}
888896

889897
impl<T> Default for Gd<T>

0 commit comments

Comments
 (0)