Skip to content

Commit d4cc89a

Browse files
committed
move godot compat
1 parent 4b2c56d commit d4cc89a

File tree

2 files changed

+90
-79
lines changed

2 files changed

+90
-79
lines changed

godot-core/src/builtin/meta/godot_compat.rs renamed to godot-core/src/builtin/meta/godot_compat/impls.rs

Lines changed: 3 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
55
*/
66

7-
use godot_ffi as sys;
7+
use crate::builtin::meta::GodotType;
88

9-
pub trait GodotCompatible {
10-
type Via: GodotType;
11-
}
9+
use super::*;
1210

1311
impl<T: GodotCompatible> GodotCompatible for Option<T>
1412
where
@@ -17,37 +15,6 @@ where
1715
type Via = Option<T::Via>;
1816
}
1917

20-
pub trait ToGodot: Sized + GodotCompatible {
21-
fn to_godot(&self) -> Self::Via;
22-
23-
fn into_godot(self) -> Self::Via {
24-
self.to_godot()
25-
}
26-
27-
fn to_variant(&self) -> Variant {
28-
self.to_godot().to_ffi().ffi_to_variant()
29-
}
30-
}
31-
32-
pub trait FromGodot: Sized + GodotCompatible {
33-
// TODO: better error
34-
fn try_from_godot(via: Self::Via) -> Option<Self>;
35-
36-
fn from_godot(via: Self::Via) -> Self {
37-
Self::try_from_godot(via).unwrap()
38-
}
39-
40-
fn try_from_variant(variant: &Variant) -> Result<Self, VariantConversionError> {
41-
let ffi = <Self::Via as GodotType>::Ffi::ffi_from_variant(variant)?;
42-
let via = Self::Via::try_from_ffi(ffi).ok_or(VariantConversionError::BadValue)?;
43-
Self::try_from_godot(via).ok_or(VariantConversionError::BadValue)
44-
}
45-
46-
fn from_variant(variant: &Variant) -> Self {
47-
Self::try_from_variant(variant).unwrap()
48-
}
49-
}
50-
5118
impl<T: ToGodot> ToGodot for Option<T>
5219
where
5320
Option<T::Via>: GodotType,
@@ -77,49 +44,6 @@ where
7744
}
7845
}
7946

80-
pub(crate) fn into_ffi<T: ToGodot>(t: T) -> <T::Via as GodotType>::Ffi {
81-
let via = t.into_godot();
82-
via.into_ffi()
83-
}
84-
85-
pub(crate) fn try_from_ffi<T: FromGodot>(ffi: <T::Via as GodotType>::Ffi) -> Option<T> {
86-
let via = <T::Via as GodotType>::try_from_ffi(ffi)?;
87-
T::try_from_godot(via)
88-
}
89-
90-
macro_rules! impl_godot_as_self {
91-
($T:ty$(, $param_metadata:expr)?) => {
92-
impl $crate::builtin::meta::GodotCompatible for $T {
93-
type Via = $T;
94-
}
95-
96-
impl $crate::builtin::meta::ToGodot for $T {
97-
#[inline]
98-
fn to_godot(&self) -> Self::Via {
99-
self.clone()
100-
}
101-
102-
#[inline]
103-
fn into_godot(self) -> Self::Via {
104-
self
105-
}
106-
}
107-
108-
impl $crate::builtin::meta::FromGodot for $T {
109-
#[inline]
110-
fn try_from_godot(via: Self::Via) -> Option<Self> {
111-
Some(via)
112-
}
113-
}
114-
};
115-
}
116-
117-
pub(crate) use impl_godot_as_self;
118-
119-
use crate::builtin::{Variant, VariantConversionError};
120-
121-
use super::{GodotFfiVariant, GodotType};
122-
12347
impl GodotCompatible for sys::VariantType {
12448
type Via = i64;
12549
}
@@ -193,8 +117,8 @@ impl<T> FromGodot for *const T {
193117
}
194118

195119
mod scalars {
196-
use super::super::GodotType;
197120
use super::{impl_godot_as_self, FromGodot, GodotCompatible, ToGodot};
121+
use crate::builtin::meta::GodotType;
198122
use godot_ffi as sys;
199123

200124
macro_rules! impl_godot {
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*/
6+
7+
mod impls;
8+
9+
use godot_ffi as sys;
10+
11+
use crate::builtin::{Variant, VariantConversionError};
12+
13+
use super::{GodotFfiVariant, GodotType};
14+
15+
pub trait GodotCompatible {
16+
type Via: GodotType;
17+
}
18+
19+
pub trait ToGodot: Sized + GodotCompatible {
20+
fn to_godot(&self) -> Self::Via;
21+
22+
fn into_godot(self) -> Self::Via {
23+
self.to_godot()
24+
}
25+
26+
fn to_variant(&self) -> Variant {
27+
self.to_godot().to_ffi().ffi_to_variant()
28+
}
29+
}
30+
31+
pub trait FromGodot: Sized + GodotCompatible {
32+
// TODO: better error
33+
fn try_from_godot(via: Self::Via) -> Option<Self>;
34+
35+
fn from_godot(via: Self::Via) -> Self {
36+
Self::try_from_godot(via).unwrap()
37+
}
38+
39+
fn try_from_variant(variant: &Variant) -> Result<Self, VariantConversionError> {
40+
let ffi = <Self::Via as GodotType>::Ffi::ffi_from_variant(variant)?;
41+
let via = Self::Via::try_from_ffi(ffi).ok_or(VariantConversionError::BadValue)?;
42+
Self::try_from_godot(via).ok_or(VariantConversionError::BadValue)
43+
}
44+
45+
fn from_variant(variant: &Variant) -> Self {
46+
Self::try_from_variant(variant).unwrap()
47+
}
48+
}
49+
50+
pub(crate) fn into_ffi<T: ToGodot>(t: T) -> <T::Via as GodotType>::Ffi {
51+
let via = t.into_godot();
52+
via.into_ffi()
53+
}
54+
55+
pub(crate) fn try_from_ffi<T: FromGodot>(ffi: <T::Via as GodotType>::Ffi) -> Option<T> {
56+
let via = <T::Via as GodotType>::try_from_ffi(ffi)?;
57+
T::try_from_godot(via)
58+
}
59+
60+
macro_rules! impl_godot_as_self {
61+
($T:ty$(, $param_metadata:expr)?) => {
62+
impl $crate::builtin::meta::GodotCompatible for $T {
63+
type Via = $T;
64+
}
65+
66+
impl $crate::builtin::meta::ToGodot for $T {
67+
#[inline]
68+
fn to_godot(&self) -> Self::Via {
69+
self.clone()
70+
}
71+
72+
#[inline]
73+
fn into_godot(self) -> Self::Via {
74+
self
75+
}
76+
}
77+
78+
impl $crate::builtin::meta::FromGodot for $T {
79+
#[inline]
80+
fn try_from_godot(via: Self::Via) -> Option<Self> {
81+
Some(via)
82+
}
83+
}
84+
};
85+
}
86+
87+
pub(crate) use impl_godot_as_self;

0 commit comments

Comments
 (0)