Skip to content
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

WASM: Add try_clone implementations to Request and RequestBuilder #1286

Merged
merged 1 commit into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/wasm/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Body {
inner: Inner,
}

#[derive(Clone)]
enum Inner {
Bytes(Bytes),
#[cfg(feature = "multipart")]
Expand Down Expand Up @@ -56,6 +57,12 @@ impl Body {
Inner::Multipart(form) => form.is_empty(),
}
}

pub(crate) fn clone(&self) -> Body {
Self {
inner: self.inner.clone(),
}
}
}

impl From<Bytes> for Body {
Expand Down
45 changes: 45 additions & 0 deletions src/wasm/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ impl Request {
pub fn body_mut(&mut self) -> &mut Option<Body> {
&mut self.body
}

/// Attempts to clone the `Request`.
///
/// None is returned if a body is which can not be cloned. This method
/// currently always returns `Some`, but that may change in the future.
pub fn try_clone(&self) -> Option<Request> {
Some(Self {
method: self.method.clone(),
url: self.url.clone(),
headers: self.headers.clone(),
body: self.body.as_ref().map(|body| body.clone()),
cors: self.cors,
credentials: self.credentials.clone(),
})
}
}

impl RequestBuilder {
Expand Down Expand Up @@ -335,6 +350,36 @@ impl RequestBuilder {
let req = self.request?;
self.client.execute_request(req).await
}

/// Attempt to clone the RequestBuilder.
///
/// `None` is returned if the RequestBuilder can not be cloned. This method
/// currently always returns `Some`, but that may change in the future.
///
/// # Examples
///
/// ```no_run
/// # use reqwest::Error;
/// #
/// # fn run() -> Result<(), Error> {
/// let client = reqwest::Client::new();
/// let builder = client.post("http://httpbin.org/post")
/// .body("from a &str!");
/// let clone = builder.try_clone();
/// assert!(clone.is_some());
/// # Ok(())
/// # }
/// ```
pub fn try_clone(&self) -> Option<RequestBuilder> {
self.request
.as_ref()
.ok()
.and_then(|req| req.try_clone())
.map(|req| RequestBuilder {
client: self.client.clone(),
request: Ok(req),
})
}
}

impl fmt::Debug for Request {
Expand Down