Skip to content

Commit 7426b7f

Browse files
committed
Godot conversion traits: separate public/private API, slightly improve docs
Rename files: godot_compatible -> godot_convert
1 parent 66df8f4 commit 7426b7f

File tree

7 files changed

+43
-26
lines changed

7 files changed

+43
-26
lines changed

godot-core/src/builtin/meta/godot_compatible/mod.rs renamed to godot-core/src/builtin/meta/godot_convert/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ use crate::builtin::{Variant, VariantConversionError};
1010

1111
use super::{GodotFfiVariant, GodotType};
1212

13-
/// Indicates that a type has some canonical Godot type that can represent it.
13+
/// Indicates that a type can be passed to/from Godot, either directly or through an intermediate "via" type.
1414
///
15-
/// The type specified here is what will be used to pass this type across to ffi-boundary to/from Godot.
16-
/// Generally [`ToGodot`] needs to be implemented to pass a type to Godot, and [`FromGodot`] to receive this
17-
/// type from Godot.
15+
/// The associated type `Via` specifies _how_ this type is passed across the FFI boundary to/from Godot.
16+
/// Generally [`ToGodot`] needs to be implemented to pass a type to Godot, and [`FromGodot`] to receive this type from Godot.
17+
///
18+
/// [`GodotType`] is a stronger bound than [`GodotConvert`], since it expresses that a type is _directly_ representable
19+
/// in Godot (without intermediate "via"). Every `GodotType` also implements `GodotConvert` with `Via = Self`.
1820
pub trait GodotConvert {
19-
/// The type used for ffi-passing.
21+
/// The type through which `Self` is represented in Godot.
2022
type Via: GodotType;
2123
}
2224

@@ -33,8 +35,7 @@ pub trait ToGodot: Sized + GodotConvert {
3335

3436
/// Converts this type to the Godot type.
3537
///
36-
/// This can in some cases enable some optimizations, such as avoiding reference counting for
37-
/// reference-counted values.
38+
/// This can in some cases enable minor optimizations, such as avoiding reference counting operations.
3839
fn into_godot(self) -> Self::Via {
3940
self.to_godot()
4041
}

godot-core/src/builtin/meta/mod.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
pub mod registration;
88

99
mod class_name;
10-
mod godot_compatible;
10+
mod godot_convert;
1111
mod return_marshal;
1212
mod signature;
1313

1414
pub use class_name::*;
15-
pub use godot_compatible::*;
15+
pub use godot_convert::*;
1616
#[doc(hidden)]
1717
pub use return_marshal::*;
1818
#[doc(hidden)]
@@ -25,7 +25,8 @@ use crate::builtin::*;
2525
use crate::engine::global;
2626
use registration::method::MethodParamOrReturnInfo;
2727

28-
/// Conversion of GodotFfi-types into/from [`Variant`].
28+
/// Conversion of [`GodotFfi`] types to/from [`Variant`].
29+
#[doc(hidden)]
2930
pub trait GodotFfiVariant: Sized + GodotFfi {
3031
fn ffi_to_variant(&self) -> Variant;
3132
fn ffi_from_variant(variant: &Variant) -> Result<Self, VariantConversionError>;
@@ -98,38 +99,48 @@ mod sealed {
9899
}
99100
}
100101

101-
/// Types that can represent some Godot type.
102-
///
103-
/// This trait cannot be implemented for custom user types, for that you should see [`GodotConvert`]
104-
/// instead. A type implements `GodotType` when it can directly represent some primitive type exposed by
105-
/// Godot. For instance, [`i64`] implements `GodotType`, since it can be directly represented by Godot's
106-
/// `int` type. But [`VariantType`] does not implement `GodotType`. Since while it is an enum Godot uses, we
107-
/// have no native way to indicate to Godot that a value should be one of the variants of `VariantType`.
102+
/// Type that is directly representable in the engine.
108103
///
109-
/// Unlike [`GodotFfi`], types implementing this trait don't need to fully represent its corresponding Godot
110-
/// type. For instance [`i32`] does not implement [`GodotFfi`] because it cannot represent all values of
111-
/// Godot's `int` type, however it does implement `GodotType` because we can set the metadata of values with
112-
/// this type to indicate that they are 32 bits large.
104+
/// This trait cannot be implemented for custom user types; for those, [`GodotConvert`] exists instead.
105+
/// A type implements `GodotType` when Godot has a direct, native representation for it. For instance:
106+
/// - [`i64`] implements `GodotType`, since it can be directly represented by Godot's `int` type.
107+
/// - But [`VariantType`] does not implement `GodotType`. While it is an enum Godot uses, we have no native way to indicate
108+
/// to Godot that a value should be one of the variants of `VariantType`.
109+
//
110+
// Unlike `GodotFfi`, types implementing this trait don't need to fully represent its corresponding Godot
111+
// type. For instance [`i32`] does not implement `GodotFfi` because it cannot represent all values of
112+
// Godot's `int` type, however it does implement `GodotType` because we can set the metadata of values with
113+
// this type to indicate that they are 32 bits large.
113114
pub trait GodotType: GodotConvert<Via = Self> + ToGodot + FromGodot + sealed::Sealed {
115+
#[doc(hidden)]
114116
type Ffi: GodotFfiVariant;
115117

118+
#[doc(hidden)]
116119
fn to_ffi(&self) -> Self::Ffi;
120+
121+
#[doc(hidden)]
117122
fn into_ffi(self) -> Self::Ffi;
123+
124+
#[doc(hidden)]
118125
fn try_from_ffi(ffi: Self::Ffi) -> Option<Self>;
119126

127+
#[doc(hidden)]
120128
fn from_ffi(ffi: Self::Ffi) -> Self {
121129
Self::try_from_ffi(ffi).unwrap()
122130
}
123131

132+
#[doc(hidden)]
124133
fn param_metadata() -> sys::GDExtensionClassMethodArgumentMetadata {
125134
Self::Ffi::default_param_metadata()
126135
}
127136

137+
#[doc(hidden)]
128138
fn class_name() -> ClassName {
129139
// If we use `ClassName::of::<()>()` then this type shows up as `(no base)` in documentation.
130140
ClassName::none()
131141
}
132142

143+
#[doc(hidden)]
133144
fn property_info(property_name: &str) -> PropertyInfo {
134145
PropertyInfo {
135146
variant_type: Self::Ffi::variant_type(),
@@ -141,10 +152,12 @@ pub trait GodotType: GodotConvert<Via = Self> + ToGodot + FromGodot + sealed::Se
141152
}
142153
}
143154

155+
#[doc(hidden)]
144156
fn argument_info(property_name: &str) -> MethodParamOrReturnInfo {
145157
MethodParamOrReturnInfo::new(Self::property_info(property_name), Self::param_metadata())
146158
}
147159

160+
#[doc(hidden)]
148161
fn return_info() -> Option<MethodParamOrReturnInfo> {
149162
Some(MethodParamOrReturnInfo::new(
150163
Self::property_info(""),

godot-core/src/builtin/meta/return_marshal.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub trait PtrcallReturn {
1919

2020
// ----------------------------------------------------------------------------------------------------------------------------------------------
2121

22+
#[doc(hidden)]
2223
pub struct PtrcallReturnOptionGdT<R> {
2324
_marker: std::marker::PhantomData<R>,
2425
}
@@ -33,6 +34,7 @@ impl<T: GodotClass> PtrcallReturn for PtrcallReturnOptionGdT<Gd<T>> {
3334

3435
// ----------------------------------------------------------------------------------------------------------------------------------------------
3536

37+
#[doc(hidden)]
3638
pub struct PtrcallReturnT<R> {
3739
_marker: std::marker::PhantomData<R>,
3840
}
@@ -53,6 +55,7 @@ impl<T: FromGodot> PtrcallReturn for PtrcallReturnT<T> {
5355

5456
// ----------------------------------------------------------------------------------------------------------------------------------------------
5557

58+
#[doc(hidden)]
5659
pub enum PtrcallReturnUnit {}
5760

5861
impl PtrcallReturn for PtrcallReturnUnit {

godot-macros/src/derive/derive_godot_compatible.rs renamed to godot-macros/src/derive/derive_godot_convert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use venial::Declaration;
1111
use crate::util::{decl_get_info, DeclInfo};
1212
use crate::ParseResult;
1313

14-
pub fn derive_godot_compatible(decl: Declaration) -> ParseResult<TokenStream> {
14+
pub fn derive_godot_convert(decl: Declaration) -> ParseResult<TokenStream> {
1515
let DeclInfo {
1616
where_,
1717
generic_params,

godot-macros/src/derive/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
99
mod derive_export;
1010
mod derive_from_variant;
11-
mod derive_godot_compatible;
11+
mod derive_godot_convert;
1212
mod derive_property;
1313
mod derive_to_variant;
1414

1515
pub(crate) use derive_export::*;
1616
pub(crate) use derive_from_variant::*;
17-
pub(crate) use derive_godot_compatible::*;
17+
pub(crate) use derive_godot_convert::*;
1818
pub(crate) use derive_property::*;
1919
pub(crate) use derive_to_variant::*;

godot-macros/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,8 @@ pub fn godot_api(_meta: TokenStream, input: TokenStream) -> TokenStream {
443443
}
444444

445445
#[proc_macro_derive(GodotConvert)]
446-
pub fn derive_godot_compatible(input: TokenStream) -> TokenStream {
447-
translate(input, derive::derive_godot_compatible)
446+
pub fn derive_godot_convert(input: TokenStream) -> TokenStream {
447+
translate(input, derive::derive_godot_convert)
448448
}
449449

450450
/// Derive macro for [ToGodot](../builtin/meta/trait.ToGodot.html) on structs or enums.

0 commit comments

Comments
 (0)