|
15 | 15 | use crate::builtin::NodePath; |
16 | 16 | use crate::classes::{Node, PackedScene}; |
17 | 17 | use crate::global::Error; |
18 | | -use crate::meta::error::ConvertError; |
19 | | -use crate::meta::{AsArg, ByValue, FromGodot, GodotConvert, GodotShape, ToGodot, arg_into_ref}; |
| 18 | +use crate::meta::{AsArg, arg_into_ref}; |
20 | 19 | use crate::obj::{Gd, Inherits}; |
21 | 20 |
|
22 | 21 | /// Manual extensions for the `Node` class. |
@@ -91,44 +90,39 @@ impl Error { |
91 | 90 | /// This is a convenience method that may be used to convert this type into one that can be used with the try operator (`?`) |
92 | 91 | /// for easy short circuiting of Godot `Error`s. |
93 | 92 | /// |
94 | | - /// To assist with this, `Result<(), Error>` has [a `ToGodot` implementation](ToGodot#impl-ToGodot-for-Result%3C(),+Error%3E) |
95 | | - /// that turns it back into an `Error` on Godot's side, so that it can be used as the return value of `#[func]` functions. |
| 93 | + /// ``` |
| 94 | + /// use godot::global::Error; |
| 95 | + /// |
| 96 | + /// assert_eq!(Error::OK.err(), Ok(())); |
| 97 | + /// assert_eq!(Error::FAILED.err(), Err(Error::FAILED)); |
| 98 | + /// ``` |
96 | 99 | pub fn err(self) -> Result<(), Self> { |
97 | 100 | if self == Error::OK { Ok(()) } else { Err(self) } |
98 | 101 | } |
99 | 102 |
|
100 | 103 | /// Creates an `Error` from a `Result<(), Error>`, the inverse of [`.err()`](Self::err). |
101 | | - /// |
| 104 | + /// |
102 | 105 | /// `Ok(())` becomes `Error::OK`, and `Err(e)` becomes `e`. |
| 106 | + /// |
| 107 | + /// This can be used to implement "makeshift try blocks", |
| 108 | + /// or somehow else process the return value of a function that returns `Result<(), Error>`. |
| 109 | + /// ``` |
| 110 | + /// use godot::global::Error; |
| 111 | + /// |
| 112 | + /// // Makeshift try block |
| 113 | + /// assert_eq!( |
| 114 | + /// Error::from_result((|| { |
| 115 | + /// Error::OK.err()?; |
| 116 | + /// Error::FAILED.err()?; |
| 117 | + /// Ok(()) |
| 118 | + /// })()), |
| 119 | + /// Error::FAILED |
| 120 | + /// ); |
| 121 | + /// ``` |
103 | 122 | pub fn from_result(result: Result<(), Self>) -> Self { |
104 | 123 | match result { |
105 | 124 | Ok(()) => Error::OK, |
106 | 125 | Err(e) => e, |
107 | 126 | } |
108 | 127 | } |
109 | 128 | } |
110 | | - |
111 | | -/// Transparent `GodotConvert` implementation that uses `<Error as GodotConvert>`'s properties, so it uses the same representation. |
112 | | -impl GodotConvert for Result<(), Error> { |
113 | | - type Via = <Error as GodotConvert>::Via; |
114 | | - |
115 | | - fn godot_shape() -> GodotShape { |
116 | | - Error::godot_shape() |
117 | | - } |
118 | | -} |
119 | | - |
120 | | -/// Shim that can turn incoming `Error` values into `Result<(), Error>`s, where `Error::OK` becomes `Ok(())`. |
121 | | -impl FromGodot for Result<(), Error> { |
122 | | - fn try_from_godot(via: Self::Via) -> Result<Self, ConvertError> { |
123 | | - Ok(Error::try_from_godot(via)?.err()) |
124 | | - } |
125 | | -} |
126 | | - |
127 | | -/// Shim that turns outgoing `Result<(), Error>` values into `Error`s, where `Ok(())` becomes `Error::OK`. |
128 | | -impl ToGodot for Result<(), Error> { |
129 | | - type Pass = ByValue; |
130 | | - |
131 | | - fn to_godot(&self) -> Self::Via { |
132 | | - Error::from_result(*self).to_godot() |
133 | | - } |
134 | | -} |
0 commit comments