-
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
af3e483
`net` improve interoperability of wrapper types
Alextopher d8f816f
to and `From<u16>` for `websocket::State`
Alextopher febbb20
improve tests for `Ord` types
Alextopher ab32562
nit: fix formatting error
Alextopher 8b77934
add Ord warning to eventsource::State
Alextopher 9efe07a
Merge branch 'master' into c-common-traits
Alextopher 6c32898
make function signatures in http more idiomatic
Alextopher 61d9685
nit: fmt
Alextopher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,13 @@ | ||
use gloo_utils::iter::UncheckedIter; | ||
use js_sys::{Array, Map}; | ||
use std::fmt; | ||
use std::{fmt, iter::FromIterator}; | ||
use wasm_bindgen::{JsCast, UnwrapThrowExt}; | ||
|
||
// I experimented with using `js_sys::Object` for the headers, since this object is marked | ||
// experimental in MDN. However it's in the fetch spec, and it's necessary for appending headers. | ||
/// A wrapper around `web_sys::Headers`. | ||
pub struct Headers { | ||
raw: web_sys::Headers, | ||
} | ||
|
||
impl Default for Headers { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl Headers { | ||
/// Create a new empty headers object. | ||
pub fn new() -> Self { | ||
|
@@ -37,19 +29,33 @@ impl Headers { | |
|
||
/// This method appends a new value onto an existing header, or adds the header if it does not | ||
/// already exist. | ||
pub fn append(&self, name: &str, value: &str) { | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// # use gloo_net::http::Headers; | ||
/// # fn no_run() { | ||
/// let headers = Headers::new(); | ||
/// headers.append("Content-Type", "text/plain"); | ||
/// assert_eq!(headers.get("Content-Type"), Some("text/plain".to_string())); | ||
/// | ||
/// headers.append("Content-Type", "text/html"); | ||
/// assert_eq!(headers.get("Content-Type"), Some("text/plain, text/html".to_string())); | ||
/// # } | ||
/// ``` | ||
pub fn append(&mut self, name: &str, value: &str) { | ||
// XXX Can this throw? WEBIDL says yes, my experiments with forbidden headers and MDN say | ||
// no. | ||
self.raw.append(name, value).unwrap_throw() | ||
} | ||
|
||
/// Deletes a header if it is present. | ||
pub fn delete(&self, name: &str) { | ||
pub fn delete(&mut self, name: &str) { | ||
self.raw.delete(name).unwrap_throw() | ||
} | ||
|
||
/// Gets a header if it is present. | ||
pub fn get(&self, name: &str) -> Option<String> { | ||
pub fn get(&mut self, name: &str) -> Option<String> { | ||
self.raw.get(name).unwrap_throw() | ||
} | ||
|
||
|
@@ -59,7 +65,7 @@ impl Headers { | |
} | ||
|
||
/// Overwrites a header with the given name. | ||
pub fn set(&self, name: &str, value: &str) { | ||
pub fn set(&mut self, name: &str, value: &str) { | ||
self.raw.set(name, value).unwrap_throw() | ||
} | ||
|
||
|
@@ -93,6 +99,42 @@ impl Headers { | |
} | ||
} | ||
|
||
impl Clone for Headers { | ||
fn clone(&self) -> Self { | ||
self.entries().collect() | ||
} | ||
} | ||
|
||
impl Default for Headers { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl<K, V> Extend<(K, V)> for Headers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
where | ||
K: AsRef<str>, | ||
V: AsRef<str>, | ||
{ | ||
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) { | ||
for (key, value) in iter { | ||
self.append(key.as_ref(), value.as_ref()); | ||
} | ||
} | ||
} | ||
|
||
impl<K, V> FromIterator<(K, V)> for Headers | ||
where | ||
K: AsRef<str>, | ||
V: AsRef<str>, | ||
{ | ||
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self { | ||
let mut headers = Self::new(); | ||
headers.extend(iter); | ||
headers | ||
} | ||
} | ||
|
||
impl fmt::Debug for Headers { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
let mut dbg = f.debug_struct("Headers"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 aHashSet<&str, String>
this method would need to be&mut
or we're committing to using a something likeRefCell<_>
forever