Skip to content

Commit 4b2a211

Browse files
author
David Bailey
committed
update text to improve clarity
1 parent dbc032b commit 4b2a211

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

text/0000-result-ext-trait.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77

88
[summary]: #summary
99

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`.
1111

1212
# Motivation
1313

1414
[motivation]: #motivation
1515

1616
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.
1717

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:
1919

2020
```rust
2121
do_something().or_else(|e| cx.throw_error(e.to_string()))?
@@ -63,7 +63,7 @@ fn create_file(mut cx: FunctionContext) -> JsResult<JsUndefined> {
6363
}
6464
```
6565

66-
Using `or_throw`, we can achieve the same as the previous example using a shorter syntax:
66+
Using `or_throw`, we can achieve the same as the previous example using a less verbose, repetitive syntax:
6767

6868
```rust
6969
use neon::prelude::*;
@@ -77,13 +77,13 @@ fn create_file(mut cx: FunctionContext) -> JsResult<JsUndefined> {
7777
}
7878
```
7979

80-
In this example, `or_throw` automatically creates a JavaScript error if either of the file methods fail, including the original error text.
80+
In this example, `or_throw` automatically throws a JavaScript error if either of the file methods return an error, including the original error text.
8181

8282
# Reference-level explanation
8383

8484
[reference-level-explanation]: #reference-level-explanation
8585

86-
This could be added to `result/mod.rs` in the following manner:
86+
This feature could be implemented in `result/mod.rs` in the following manner:
8787

8888
```rust
8989
/// An extension trait for `Result` values that can be converted into `NeonResult` values by throwing a JavaScript
@@ -102,9 +102,9 @@ impl<V, E: Display> ResultExt<V> for Result<V, E> {
102102
}
103103
```
104104

105-
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.
106106

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`.
108108

109109
> This section should return to the examples given in the previous section, and explain more fully how the detailed proposal makes those examples work.
110110
@@ -114,8 +114,8 @@ In the second example under [Guide-level explanation](#guide-level-explanation),
114114

115115
[drawbacks]: #drawbacks
116116

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)
119119
- Isn't useful if a developer wants to write a custom error message
120120
- Only supports JS `Error` (see [Unresolved questions](#unresolved-questions))
121121

@@ -125,23 +125,23 @@ In the second example under [Guide-level explanation](#guide-level-explanation),
125125

126126
> Why is this design the best in the space of possible designs?
127127
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.
131129

132130
> What other designs have been considered and what is the rationale for not choosing them?
133131
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!
135135

136136
> What is the impact of not doing this?
137137
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.
139139

140140
# Unresolved questions
141141

142142
[unresolved]: #unresolved-questions
143143

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`?
146146
- Should similar methods be written to throw other types of JavaScript errors, e.g. `TypeError`?
147147
- Should there be an extra parameter to specify the JS error type?

0 commit comments

Comments
 (0)