Skip to content

Commit 22d7e12

Browse files
authored
Allow exporting RsRef<T> (#71)
1 parent 68fd6a1 commit 22d7e12

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

rust-script/src/interface.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::{collections::HashMap, fmt::Debug};
1313

1414
use godot::meta::{FromGodot, GodotConvert, ToGodot};
1515
use godot::obj::Inherits;
16-
use godot::prelude::{Gd, Object, StringName, Variant};
16+
use godot::prelude::{ConvertError, Gd, Object, StringName, Variant};
1717

1818
pub use crate::runtime::Context;
1919

@@ -110,6 +110,30 @@ impl<T: GodotScript> Clone for RsRef<T> {
110110
}
111111
}
112112

113+
impl<T: GodotScript> GodotConvert for RsRef<T> {
114+
type Via = Gd<T::Base>;
115+
}
116+
117+
impl<T: GodotScript> FromGodot for RsRef<T>
118+
where
119+
T::Base: Inherits<T::Base>,
120+
{
121+
fn try_from_godot(via: Self::Via) -> Result<Self, godot::prelude::ConvertError> {
122+
via.try_to_script().map_err(ConvertError::with_error)
123+
}
124+
}
125+
126+
impl<T: GodotScript> ToGodot for RsRef<T> {
127+
type ToVia<'v>
128+
= Gd<T::Base>
129+
where
130+
Self: 'v;
131+
132+
fn to_godot(&self) -> Self::ToVia<'_> {
133+
self.deref().clone()
134+
}
135+
}
136+
113137
#[derive(thiserror::Error, Debug)]
114138
pub enum GodotScriptCastError {
115139
#[error("Object has no script attached!")]

rust-script/src/interface/export.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use godot::obj::{EngineEnum, Gd};
2020
use godot::prelude::GodotClass;
2121
use godot::sys::GodotFfi;
2222

23+
use super::{GodotScript, RsRef};
24+
2325
pub trait GodotScriptExport: GodotConvert + FromGodot + ToGodot {
2426
fn hint_string(custom_hint: Option<PropertyHint>, custom_string: Option<String>) -> String;
2527

@@ -50,6 +52,30 @@ impl<T: GodotClass> GodotScriptExport for Gd<T> {
5052
}
5153
}
5254

55+
impl<T: GodotScript> GodotScriptExport for RsRef<T> {
56+
fn hint_string(_custom_hint: Option<PropertyHint>, custom_string: Option<String>) -> String {
57+
if let Some(custom) = custom_string {
58+
return custom;
59+
}
60+
61+
T::CLASS_NAME.to_string()
62+
}
63+
64+
fn hint(custom: Option<PropertyHint>) -> PropertyHint {
65+
if let Some(custom) = custom {
66+
return custom;
67+
}
68+
69+
if T::Base::inherits::<Node>() {
70+
PropertyHint::NODE_TYPE
71+
} else if T::Base::inherits::<Resource>() {
72+
PropertyHint::RESOURCE_TYPE
73+
} else {
74+
PropertyHint::NONE
75+
}
76+
}
77+
}
78+
5379
impl<T: GodotScriptExport> GodotScriptExport for Option<T>
5480
where
5581
for<'v> T: 'v,

0 commit comments

Comments
 (0)