Skip to content

Commit fc43834

Browse files
authored
Merge pull request #1189 from sanpii/oneditor-debug
Implement `Debug` for `OnEditor`
2 parents 08fcd27 + 2186799 commit fc43834

File tree

2 files changed

+47
-24
lines changed

2 files changed

+47
-24
lines changed

godot-core/src/obj/on_editor.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,17 @@ use crate::registry::property::{BuiltinExport, Export, Var};
173173
///
174174
/// When used with `#[class(tool)]`, the before-ready checks are omitted.
175175
/// Otherwise, `OnEditor<T>` behaves the same — accessing an uninitialized value will cause a panic.
176+
#[derive(Debug)]
176177
pub struct OnEditor<T> {
177-
inner: OnEditorState<T>,
178+
state: OnEditorState<T>,
178179
}
179180

181+
#[derive(Debug)]
180182
pub(crate) enum OnEditorState<T> {
181183
/// Uninitialized null value.
182-
Null,
184+
UninitNull,
183185
/// Uninitialized state, but with a value marked as invalid (required to represent non-nullable type in the editor).
184-
Uninitialized(T),
186+
UninitSentinel(T),
185187
/// Initialized with a value.
186188
Initialized(T),
187189
}
@@ -195,10 +197,10 @@ impl<T: Var + FromGodot + PartialEq> OnEditor<T> {
195197
/// # Panics
196198
/// If `init()` was called before.
197199
pub fn init(&mut self, val: T) {
198-
match self.inner {
199-
OnEditorState::Null | OnEditorState::Uninitialized(_) => {
200+
match self.state {
201+
OnEditorState::UninitNull | OnEditorState::UninitSentinel(_) => {
200202
*self = OnEditor {
201-
inner: OnEditorState::Initialized(val),
203+
state: OnEditorState::Initialized(val),
202204
};
203205
}
204206
OnEditorState::Initialized(_) => {
@@ -215,7 +217,7 @@ impl<T: Var + FromGodot + PartialEq> OnEditor<T> {
215217
T::Via: BuiltinExport,
216218
{
217219
OnEditor {
218-
inner: OnEditorState::Uninitialized(val),
220+
state: OnEditorState::UninitSentinel(val),
219221
}
220222
}
221223

@@ -224,23 +226,23 @@ impl<T: Var + FromGodot + PartialEq> OnEditor<T> {
224226
/// Not a part of public API – available only via `Default` implementation on `OnEditor<Gd<T>>` and `OnEditor<DynGd<D, T>>`.
225227
pub(crate) fn gd_invalid() -> Self {
226228
OnEditor {
227-
inner: OnEditorState::Null,
229+
state: OnEditorState::UninitNull,
228230
}
229231
}
230232

231233
#[doc(hidden)]
232234
pub fn is_invalid(&self) -> bool {
233-
match self.inner {
234-
OnEditorState::Null | OnEditorState::Uninitialized(_) => true,
235+
match self.state {
236+
OnEditorState::UninitNull | OnEditorState::UninitSentinel(_) => true,
235237
OnEditorState::Initialized(_) => false,
236238
}
237239
}
238240

239241
/// `Var::get_property` implementation that works both for nullable and non-nullable types.
240242
pub(crate) fn get_property_inner(&self) -> Option<T::Via> {
241-
match &self.inner {
242-
OnEditorState::Null => None,
243-
OnEditorState::Uninitialized(val) | OnEditorState::Initialized(val) => {
243+
match &self.state {
244+
OnEditorState::UninitNull => None,
245+
OnEditorState::UninitSentinel(val) | OnEditorState::Initialized(val) => {
244246
Some(val.get_property())
245247
}
246248
}
@@ -251,18 +253,18 @@ impl<T: Var + FromGodot + PartialEq> OnEditor<T> {
251253
/// All the state transitions are valid, since it is being run only in the editor.
252254
/// See also [`Option::set_property()`].
253255
pub(crate) fn set_property_inner(&mut self, value: Option<T::Via>) {
254-
match (value, &mut self.inner) {
255-
(None, _) => self.inner = OnEditorState::Null,
256+
match (value, &mut self.state) {
257+
(None, _) => self.state = OnEditorState::UninitNull,
256258
(Some(value), OnEditorState::Initialized(current_value)) => {
257259
current_value.set_property(value);
258260
}
259-
(Some(value), OnEditorState::Null) => {
260-
self.inner = OnEditorState::Initialized(FromGodot::from_godot(value))
261+
(Some(value), OnEditorState::UninitNull) => {
262+
self.state = OnEditorState::Initialized(FromGodot::from_godot(value))
261263
}
262-
(Some(value), OnEditorState::Uninitialized(current_value)) => {
264+
(Some(value), OnEditorState::UninitSentinel(current_value)) => {
263265
let value = FromGodot::from_godot(value);
264266
if value != *current_value {
265-
self.inner = OnEditorState::Initialized(value)
267+
self.state = OnEditorState::Initialized(value)
266268
}
267269
}
268270
}
@@ -272,8 +274,8 @@ impl<T: Var + FromGodot + PartialEq> OnEditor<T> {
272274
impl<T> std::ops::Deref for OnEditor<T> {
273275
type Target = T;
274276
fn deref(&self) -> &Self::Target {
275-
match &self.inner {
276-
OnEditorState::Null | OnEditorState::Uninitialized(_) => {
277+
match &self.state {
278+
OnEditorState::UninitNull | OnEditorState::UninitSentinel(_) => {
277279
panic!("OnEditor field hasn't been initialized.")
278280
}
279281
OnEditorState::Initialized(v) => v,
@@ -283,8 +285,8 @@ impl<T> std::ops::Deref for OnEditor<T> {
283285

284286
impl<T> std::ops::DerefMut for OnEditor<T> {
285287
fn deref_mut(&mut self) -> &mut Self::Target {
286-
match &mut self.inner {
287-
OnEditorState::Null | OnEditorState::Uninitialized(_) => {
288+
match &mut self.state {
289+
OnEditorState::UninitNull | OnEditorState::UninitSentinel(_) => {
288290
panic!("OnEditor field hasn't been initialized.")
289291
}
290292
OnEditorState::Initialized(v) => v,
@@ -307,7 +309,7 @@ where
307309
T::Via: BuiltinExport,
308310
{
309311
fn get_property(&self) -> Self::Via {
310-
// Will never fail – `PrimitiveGodotType` can not be represented by the `OnEditorState::Null`.
312+
// Will never fail – `PrimitiveGodotType` can not be represented by the `OnEditorState::UninitNull`.
311313
OnEditor::<T>::get_property_inner(self).expect("DirectExport is not nullable.")
312314
}
313315

itest/rust/src/object_tests/oneditor_test.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,24 @@ impl INode for OnEditorNoDefault {
8383
self.was_ready_run = true;
8484
}
8585
}
86+
87+
#[itest]
88+
fn oneditor_debug() {
89+
let val = OnEditor::from_sentinel(-1);
90+
assert_eq!(format!("{val:?}"), "OnEditor { state: UninitSentinel(-1) }");
91+
92+
let mut val = OnEditor::<Gd<Node>>::default();
93+
assert_eq!(format!("{val:?}"), "OnEditor { state: UninitNull }");
94+
95+
let obj = Node::new_alloc();
96+
val.init(obj.clone());
97+
98+
let id = obj.instance_id();
99+
100+
let actual = format!(".:{val:?}:.");
101+
let expected = format!(".:OnEditor {{ state: Initialized(Gd {{ id: {id}, class: Node }}) }}:.");
102+
103+
assert_eq!(actual, expected);
104+
105+
obj.free();
106+
}

0 commit comments

Comments
 (0)