-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
net
improve interoperability of wrapper types
#332
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the pull request. I have only reviewed the first section (http) as the master branch has conflicts with this pull request.
I do wish to provide some of my personal opinion on the first part of the implementation which I think is applicable for the rest of the pull request as well.
Feel free to request another review after you have resolved the conflicts.
Request
Response
Similar to request.
Headers
QueryParams
Similar to Headers
.
Method
We switched to Method
type from the http
crate. This is now only a type alias.
Legend
❌: I do not think a sound implementation is possible.
Just so that I understand, the So to improve this we either
|
In this case, the The derived Clone method for See: https://rustwasm.github.io/wasm-bindgen/api/src/wasm_bindgen/lib.rs.html#1091-1099
I am fine with both ways as long as it does not significantly increase the bundle size. However, I do not think a non-fallible In the latest master branch, the Request struct is separated into Request and RequestBuilder, it might be easier if only implementing cloning on RequestBuilder as the builder does not contain a body? |
I think cloning a request & response could be made infallible if the other apis were more rust idiomatic. For example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
This was mostly oversight when implementing the types. Thanks for addressing it. In addition to what futursolo has mentioned, I have one question. nevermind... it seems they mentioned it as well
/// assert_eq!(headers.get("Content-Type"), Some("text/plain, text/html".to_string())); | ||
/// # } | ||
/// ``` | ||
pub fn append(&mut self, name: &str, value: &str) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know web_sys::Headers
(and all? web_sys
types) allow inner mutability through a &T
but I'm not sure if it's idiomatic to do the same on our wrapper types.
For example, if someday we choose to change the implementation of Header
wrap a HashSet<&str, String>
this method would need to be &mut
or we're committing to using a something like RefCell<_>
forever
} | ||
} | ||
|
||
impl<K, V> Extend<(K, V)> for Headers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -38,10 +38,9 @@ impl RequestBuilder { | |||
} | |||
|
|||
/// Set the body for this request. | |||
pub fn body(mut self, body: impl Into<JsValue>) -> Result<Request, Error> { | |||
pub fn body(mut self, body: impl Into<JsValue>) -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't see a reason why Request body
should build
, this allows us to set the body in any order.
I will add a test to verify this is sound.
/// # Errors | ||
/// | ||
/// This method may return a "RangeError" | ||
pub async fn binary(self) -> Result<Vec<u8>, Error> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Methods that consume Request/Response bodies now take ownership of self
. This makes clone
infallible and prevents the class of bugs where users read the body multiple times.
I haven't gone back to websocket or eventsource yet. |
After re-examining the updated pull request, I think some of criteria has exceeded simply adding additional traits and I think is kind of difficult to review them with all the changes mixed in the same PR. Could you please split the pull request into multiple smaller ones and limit to adding traits? I think the some of API changes contained in this pull request to certain types are in the right direction but significant. If you wish to change them, would you mind start with an RFC? |
(draft) RFC: #335 I agree the changes I've suggested have surpassed what this PR was originally for. I think it's worth closing 😃 |
net
improve interoperability of wrapper typesSpecifically C-COMMON-TRAITS.
This PR currently only covers the
gloo_net
crate. I'd be happy to (slowly) extend this idea to other crates if this is well received.HTTP
Request
Request
to send it multiple times.web_sys
types don't implementCopy
PartialEq
, reflexivity is guaranteed.Hash
Display
implementation could be implemented to follow the HTTP spec, but that's a larger discussion.Response
Implement the same traits as the underlying
web_sys
type.web_sys
types don't implementCopy
Hash
Headers
Implement the same traits as the underlying
web_sys
type.QueryParams
Implement the same traits as the underlying
web_sys
type.Method
Display
implementationPartialOrd
Added
as_str
method toMethod
to get a&'static str
representation of the method. This avoids some unnecessary allocations.eventsource
EventSource
Implement the same traits as the underlying
web_sys
type.EventSourceSubscription
I don't think any traits are applicable here.
Closure
prevents most traits from being implemented.eventsource::State
In JS the
readyState
property returns a Number 0, 1, or 2 (connecting, open, closed). I've implemented the expected traits for this type.Added
as_str
to get a&'static str
representation of the state. This avoids some unnecessary allocations.Added
From<u16> for State
andFrom<State> for u16
to convert between the JS number and the Rust enum.EventSourceError
I've left this trait unchanged, since its
non_exhaustive
I'm not confident if it will remain as simple as it is now. If it does remain "simple" I'd suggest implementingCopy
andHash
.Ord
doesn't make sense for an error type.websocket
CloseEvent
Copy
WebSocket
No traits are applicable here. The underlying types prevent most traits from being implemented. Particularly the
Closure
fields.Message
String
andVec
aren'tCopy
Display
aVec<u8>
websocket::State
Similar to eventsource::State.
In JS the
readyState
property returns a Number (0, 1, 2, 3) (connecting, open, closing, closed). I've implemented the expected traits for this type.Added
as_str
to get a&'static str
representation of the state. This avoids some unnecessary allocations.Added
From<u16> for State
andFrom<State> for u16
to convert between the JS number and the Rust enum.WebSocketError
This enum depends on
JsError
which does not implement many traits. If that changes then this type should implement more traits.JsError
doesn't implementClone
JsError
doesn't implementCopy
JsError
doesn't implementPartialEq
JsError
doesn't implementEq
JsError
doesn't implementHash
net
Error
Same as
WebSocketError
,JsError
hasn't been covered by this PR. OnceJsError
implements more traits this type should implement more traits.