You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/0000-result-ext-trait.md
+16-16Lines changed: 16 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,15 +7,15 @@
7
7
8
8
[summary]: #summary
9
9
10
-
This RFC proposes a new trait `ResultExt` with an implementation for `std::result::Result`. The trait includes a method named `or_throw` which works similarly to that of `JsResultExt`, except that it does not require the success value to implement `neon::types::Value`.
10
+
This RFC proposes a new trait `ResultExt` with an implementation for `std::result::Result`. The trait includes a method named `or_throw` which works similarly to that of `JsResultExt`, throwing a JavaScript error if the result is an error, except that it does not require the success value to implement `neon::types::Value`.
11
11
12
12
# Motivation
13
13
14
14
[motivation]: #motivation
15
15
16
16
This feature would allow developers using Neon to call functions from `std` or third party libraries which return `Result`, and properly handle errors which may occur by converting these to `NeonResult` without needing to write their own boilerplate code, or repetitive code in function bodies.
17
17
18
-
Currently, developers using Neon with non-Neon `Result`-returning functions either have to implement a similar trait themselves, or write something like the following on a line-by-line basis, which is rather verbose:
18
+
Currently, Neon users calling third party functions returning a `Result` either have to implement a similar trait themselves, or write something like the following on a line-by-line basis, which is rather verbose:
It would also be advisable to include this in `prelude.rs` so that users don't have to `use` it explicitly.
105
+
It should also be re-exported in `prelude.rs` so that users don't need to `use` it explicitly.
106
106
107
-
In this proposed code, the trait is implemented for `Result` types where the error variant implements `Display`, which means it should automatically work for `std` and third-party library functions which return a `Result`. Advanced users could implement the trait for custom result types whose errors may not implement `Display`.
107
+
In this proposed code, the trait is implemented for `Result` types where the error variant implements `Display`, which means it should automatically work for most `std` and third-party library functions which return a `Result`. Advanced users could implement the trait for custom result types whose errors may not implement `Display`.
108
108
109
109
> This section should return to the examples given in the previous section, and explain more fully how the detailed proposal makes those examples work.
110
110
@@ -114,8 +114,8 @@ In the second example under [Guide-level explanation](#guide-level-explanation),
114
114
115
115
[drawbacks]: #drawbacks
116
116
117
-
-Developers using Neon could trivially implement this themselves
118
-
-Only benefit over using `or_else`is length + less verbosity
117
+
- Neon users could trivially implement this themselves in their code
118
+
-Minimal benefit over using `or_else`(shorter length + less verbosity)
119
119
- Isn't useful if a developer wants to write a custom error message
120
120
- Only supports JS `Error` (see [Unresolved questions](#unresolved-questions))
121
121
@@ -125,23 +125,23 @@ In the second example under [Guide-level explanation](#guide-level-explanation),
125
125
126
126
> Why is this design the best in the space of possible designs?
127
127
128
-
I believe this is the most elegant way to 'translate' Rust errors to JavaScript errors as it requires the least amount of repetitive code.
129
-
130
-
As I understand, it is not possible to automatically convert these errors because the context object is needed to create a JavaScript error, although this would be ideal if possible.
128
+
I believe this is the most elegant way to 'translate' Rust errors to JavaScript errors as it requires the least amount of boilerplate or repetitive code from Neon users.
131
129
132
130
> What other designs have been considered and what is the rationale for not choosing them?
133
131
134
-
As of yet I have not considered any alternatives as various other ways of converting errors are already possible - suggestions welcome!
132
+
As I understand, it would not be possible to automatically convert `Result` types using a `From` implementation, because the context object is needed to throw a JavaScript error. If this were possible then it would be the most elegant solution by not requiring any additional method calls.
133
+
134
+
I am not aware of any other alternative solutions to this problem which are not already possible, e.g. using `or_else` as shown in [Guide-level explanation](#guide-level-explanation). Suggestions welcome!
135
135
136
136
> What is the impact of not doing this?
137
137
138
-
Developers can continue to use `or_else`or other techniques to handle `Result` values that aren't compatible with `NeonResult`, however these do not seem as elegant.
138
+
Users can call `or_else` to handle `Result` values and convert errors to JavaScript exceptions, or implement the proposed trait and implementation themselves. In doing this, there is more boilerplate and repetitive code in Neon projects which need to handle `Result` values from third party functions.
139
139
140
140
# Unresolved questions
141
141
142
142
[unresolved]: #unresolved-questions
143
143
144
-
- Are there any 'more elegant' ways to convert errors that I haven't considered?
145
-
- Should this trait also be implemented for other`Result` types, e.g. where `E` doesn't implement `Display`?
144
+
- Are there any simpler alternative ways to convert errors that I haven't considered?
145
+
- Should this trait also be implemented for all`Result` types where `E` doesn't implement `Display`?
146
146
- Should similar methods be written to throw other types of JavaScript errors, e.g. `TypeError`?
147
147
- Should there be an extra parameter to specify the JS error type?
0 commit comments