Skip to content

Commit 7711d0d

Browse files
committed
Move AsArg, AsVariant: object -> object/as_arg (internal-only)
1 parent e1ecbc3 commit 7711d0d

File tree

2 files changed

+129
-108
lines changed

2 files changed

+129
-108
lines changed

gdnative-core/src/object/as_arg.rs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
use crate::object::ownership::{Ownership, Shared, Unique};
2+
use crate::object::{GodotObject, Null, Ref, SubClass, TRef};
3+
4+
/// Trait for safe conversion from Godot object references into API method arguments. This is
5+
/// a sealed trait with no public interface.
6+
///
7+
/// In order to enforce thread safety statically, the ability to be passed to the engine is only
8+
/// given to some reference types. Specifically, they are:
9+
///
10+
/// - All *owned* `Ref<T, Unique>` references. The `Unique` access is lost if passed into a
11+
/// method.
12+
/// - Owned and borrowed `Shared` references, including temporary ones (`TRef`).
13+
///
14+
/// It's unsound to pass `ThreadLocal` references to the engine because there is no guarantee
15+
/// that the reference will stay on the same thread.
16+
///
17+
/// To explicitly pass a null reference to the engine, use `Null::null` or `GodotObject::null`.
18+
pub trait AsArg<T>: private::Sealed {
19+
#[doc(hidden)]
20+
fn as_arg_ptr(&self) -> *mut sys::godot_object;
21+
22+
#[doc(hidden)]
23+
#[inline]
24+
unsafe fn to_arg_variant(&self) -> crate::core_types::Variant {
25+
crate::core_types::Variant::from_object_ptr(self.as_arg_ptr())
26+
}
27+
}
28+
29+
/// Trait for safe conversion from Godot object references into Variant. This is
30+
/// a sealed trait with no public interface.
31+
///
32+
/// Used for `Variant` methods and implementations as a trait bound to improve type inference.
33+
pub trait AsVariant: AsArg<<Self as AsVariant>::Target> {
34+
type Target;
35+
}
36+
37+
// ----------------------------------------------------------------------------------------------------------------------------------------------
38+
// Sealed
39+
40+
mod private {
41+
pub trait Sealed {}
42+
}
43+
44+
impl<'a, T> private::Sealed for Null<T> {}
45+
impl<'a, T: GodotObject> private::Sealed for TRef<'a, T, Shared> {}
46+
impl<T: GodotObject, Own: Ownership> private::Sealed for Ref<T, Own> {}
47+
impl<'a, T: GodotObject> private::Sealed for &'a Ref<T, Shared> {}
48+
49+
// ----------------------------------------------------------------------------------------------------------------------------------------------
50+
// Null
51+
52+
impl<'a, T: GodotObject> AsArg<T> for Null<T> {
53+
#[inline]
54+
fn as_arg_ptr(&self) -> *mut sys::godot_object {
55+
std::ptr::null_mut()
56+
}
57+
}
58+
59+
impl<'a, T: GodotObject> AsVariant for Null<T> {
60+
type Target = T;
61+
}
62+
63+
// ----------------------------------------------------------------------------------------------------------------------------------------------
64+
// TRef
65+
66+
impl<'a, T, U> AsArg<U> for TRef<'a, T, Shared>
67+
where
68+
T: GodotObject + SubClass<U>,
69+
U: GodotObject,
70+
{
71+
#[inline]
72+
fn as_arg_ptr(&self) -> *mut sys::godot_object {
73+
self.as_ptr()
74+
}
75+
}
76+
77+
impl<'a, T: GodotObject> AsVariant for TRef<'a, T, Shared> {
78+
type Target = T;
79+
}
80+
81+
// ----------------------------------------------------------------------------------------------------------------------------------------------
82+
// Ref
83+
84+
impl<T, U> AsArg<U> for Ref<T, Shared>
85+
where
86+
T: GodotObject + SubClass<U>,
87+
U: GodotObject,
88+
{
89+
#[inline]
90+
fn as_arg_ptr(&self) -> *mut sys::godot_object {
91+
self.as_ptr()
92+
}
93+
}
94+
95+
impl<T, U> AsArg<U> for Ref<T, Unique>
96+
where
97+
T: GodotObject + SubClass<U>,
98+
U: GodotObject,
99+
{
100+
#[inline]
101+
fn as_arg_ptr(&self) -> *mut sys::godot_object {
102+
self.as_ptr()
103+
}
104+
}
105+
106+
impl<T: GodotObject> AsVariant for Ref<T, Unique> {
107+
type Target = T;
108+
}
109+
110+
impl<'a, T, U> AsArg<U> for &'a Ref<T, Shared>
111+
where
112+
T: GodotObject + SubClass<U>,
113+
U: GodotObject,
114+
{
115+
#[inline]
116+
fn as_arg_ptr(&self) -> *mut sys::godot_object {
117+
self.as_ptr()
118+
}
119+
}
120+
121+
impl<T: GodotObject> AsVariant for Ref<T, Shared> {
122+
type Target = T;
123+
}
124+
125+
impl<'a, T: GodotObject> AsVariant for &'a Ref<T, Shared> {
126+
type Target = T;
127+
}

gdnative-core/src/object/mod.rs

Lines changed: 2 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use crate::export::NativeClass;
2525
use crate::private::{get_api, ManuallyManagedClassPlaceholder, ReferenceCountedClassPlaceholder};
2626
use crate::sys;
2727

28+
pub use as_arg::*;
2829
pub use instance::*;
2930
pub use new_ref::NewRef;
3031
pub use raw::RawObject;
@@ -33,6 +34,7 @@ pub mod bounds;
3334
pub mod memory;
3435
pub mod ownership;
3536

37+
mod as_arg;
3638
mod instance;
3739
mod new_ref;
3840
mod raw;
@@ -305,8 +307,6 @@ unsafe impl<T: GodotObject, Own: Ownership + Send> Send for Ref<T, Own> {}
305307
/// `Ref` is `Sync` if the thread access is `Shared`.
306308
unsafe impl<T: GodotObject, Own: Ownership + Sync> Sync for Ref<T, Own> {}
307309

308-
impl<T: GodotObject, Own: Ownership> private::Sealed for Ref<T, Own> {}
309-
310310
/// `Ref` is `Copy` if the underlying object is manually-managed, and the access is not
311311
/// `Unique`.
312312
impl<T, Own> Copy for Ref<T, Own>
@@ -994,39 +994,6 @@ impl<'a, T: GodotObject> TRef<'a, T, Shared> {
994994
}
995995
}
996996

997-
/// Trait for safe conversion from Godot object references into API method arguments. This is
998-
/// a sealed trait with no public interface.
999-
///
1000-
/// In order to enforce thread safety statically, the ability to be passed to the engine is only
1001-
/// given to some reference types. Specifically, they are:
1002-
///
1003-
/// - All *owned* `Ref<T, Unique>` references. The `Unique` access is lost if passed into a
1004-
/// method.
1005-
/// - Owned and borrowed `Shared` references, including temporary ones (`TRef`).
1006-
///
1007-
/// It's unsound to pass `ThreadLocal` references to the engine because there is no guarantee
1008-
/// that the reference will stay on the same thread.
1009-
///
1010-
/// To explicitly pass a null reference to the engine, use `Null::null` or `GodotObject::null`.
1011-
pub trait AsArg<T>: private::Sealed {
1012-
#[doc(hidden)]
1013-
fn as_arg_ptr(&self) -> *mut sys::godot_object;
1014-
1015-
#[doc(hidden)]
1016-
#[inline]
1017-
unsafe fn to_arg_variant(&self) -> crate::core_types::Variant {
1018-
crate::core_types::Variant::from_object_ptr(self.as_arg_ptr())
1019-
}
1020-
}
1021-
1022-
/// Trait for safe conversion from Godot object references into Variant. This is
1023-
/// a sealed trait with no public interface.
1024-
///
1025-
/// Used for `Variant` methods and implementations as a trait bound to improve type inference.
1026-
pub trait AsVariant: AsArg<<Self as AsVariant>::Target> {
1027-
type Target;
1028-
}
1029-
1030997
/// Represents an explicit null reference in method arguments. This works around type inference
1031998
/// issues with `Option`. You may create `Null`s with `Null::null` or `GodotObject::null`.
1032999
pub struct Null<T>(PhantomData<T>);
@@ -1040,76 +1007,3 @@ impl<T: GodotObject> Null<T> {
10401007
Null(PhantomData)
10411008
}
10421009
}
1043-
1044-
impl<'a, T> private::Sealed for Null<T> {}
1045-
impl<'a, T: GodotObject> AsArg<T> for Null<T> {
1046-
#[inline]
1047-
fn as_arg_ptr(&self) -> *mut sys::godot_object {
1048-
std::ptr::null_mut()
1049-
}
1050-
}
1051-
impl<'a, T: GodotObject> AsVariant for Null<T> {
1052-
type Target = T;
1053-
}
1054-
1055-
impl<'a, T: GodotObject> private::Sealed for TRef<'a, T, Shared> {}
1056-
impl<'a, T, U> AsArg<U> for TRef<'a, T, Shared>
1057-
where
1058-
T: GodotObject + SubClass<U>,
1059-
U: GodotObject,
1060-
{
1061-
#[inline]
1062-
fn as_arg_ptr(&self) -> *mut sys::godot_object {
1063-
self.as_ptr()
1064-
}
1065-
}
1066-
impl<'a, T: GodotObject> AsVariant for TRef<'a, T, Shared> {
1067-
type Target = T;
1068-
}
1069-
1070-
impl<T, U> AsArg<U> for Ref<T, Shared>
1071-
where
1072-
T: GodotObject + SubClass<U>,
1073-
U: GodotObject,
1074-
{
1075-
#[inline]
1076-
fn as_arg_ptr(&self) -> *mut sys::godot_object {
1077-
self.as_ptr()
1078-
}
1079-
}
1080-
impl<T: GodotObject> AsVariant for Ref<T, Shared> {
1081-
type Target = T;
1082-
}
1083-
1084-
impl<T, U> AsArg<U> for Ref<T, Unique>
1085-
where
1086-
T: GodotObject + SubClass<U>,
1087-
U: GodotObject,
1088-
{
1089-
#[inline]
1090-
fn as_arg_ptr(&self) -> *mut sys::godot_object {
1091-
self.as_ptr()
1092-
}
1093-
}
1094-
impl<T: GodotObject> AsVariant for Ref<T, Unique> {
1095-
type Target = T;
1096-
}
1097-
1098-
impl<'a, T: GodotObject> private::Sealed for &'a Ref<T, Shared> {}
1099-
impl<'a, T, U> AsArg<U> for &'a Ref<T, Shared>
1100-
where
1101-
T: GodotObject + SubClass<U>,
1102-
U: GodotObject,
1103-
{
1104-
#[inline]
1105-
fn as_arg_ptr(&self) -> *mut sys::godot_object {
1106-
self.as_ptr()
1107-
}
1108-
}
1109-
impl<'a, T: GodotObject> AsVariant for &'a Ref<T, Shared> {
1110-
type Target = T;
1111-
}
1112-
1113-
mod private {
1114-
pub trait Sealed {}
1115-
}

0 commit comments

Comments
 (0)