Skip to content

Commit

Permalink
deprecate builder if-x methods (#1760)
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Oct 30, 2020
1 parent 9963a5e commit 4cb8336
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
4 changes: 4 additions & 0 deletions actix-http/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
### Changed
* Upgrade `base64` to `0.13`.
* Upgrade `pin-project` to `1.0`.
* Deprecate `ResponseBuilder::{if_some, if_true}`. [#1760]

[#1760]: https://github.com/actix/actix-web/pull/1760


[#1754]: https://github.com/actix/actix-web/pull/1754

Expand Down
10 changes: 6 additions & 4 deletions actix-http/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,9 @@ impl ResponseBuilder {
self
}

/// This method calls provided closure with builder reference if value is
/// true.
/// This method calls provided closure with builder reference if value is `true`.
#[doc(hidden)]
#[deprecated = "Use an if statement."]
pub fn if_true<F>(&mut self, value: bool, f: F) -> &mut Self
where
F: FnOnce(&mut ResponseBuilder),
Expand All @@ -566,8 +567,9 @@ impl ResponseBuilder {
self
}

/// This method calls provided closure with builder reference if value is
/// Some.
/// This method calls provided closure with builder reference if value is `Some`.
#[doc(hidden)]
#[deprecated = "Use an if-let construction."]
pub fn if_some<T, F>(&mut self, value: Option<T>, f: F) -> &mut Self
where
F: FnOnce(T, &mut ResponseBuilder),
Expand Down
2 changes: 2 additions & 0 deletions awc/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
## Unreleased - 2020-xx-xx
### Changed
* Upgrade `base64` to `0.13`.
* Deprecate `ClientRequest::{if_some, if_true}`. [#1760]

### Fixed
* Use `Accept-Encoding: identity` instead of `Accept-Encoding: br` when no compression feature is enabled [#1737]

[#1737]: https://github.com/actix/actix-web/pull/1737
[#1760]: https://github.com/actix/actix-web/pull/1760


## 2.0.0 - 2020-09-11
Expand Down
38 changes: 24 additions & 14 deletions awc/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,9 @@ impl ClientRequest {
self
}

/// This method calls provided closure with builder reference if
/// value is `true`.
/// This method calls provided closure with builder reference if value is `true`.
#[doc(hidden)]
#[deprecated = "Use an if statement."]
pub fn if_true<F>(self, value: bool, f: F) -> Self
where
F: FnOnce(ClientRequest) -> ClientRequest,
Expand All @@ -367,8 +368,9 @@ impl ClientRequest {
}
}

/// This method calls provided closure with builder reference if
/// value is `Some`.
/// This method calls provided closure with builder reference if value is `Some`.
#[doc(hidden)]
#[deprecated = "Use an if-let construction."]
pub fn if_some<T, F>(self, value: Option<T>, f: F) -> Self
where
F: FnOnce(T, ClientRequest) -> ClientRequest,
Expand Down Expand Up @@ -601,27 +603,35 @@ mod tests {

#[actix_rt::test]
async fn test_basics() {
let mut req = Client::new()
let req = Client::new()
.put("/")
.version(Version::HTTP_2)
.set(header::Date(SystemTime::now().into()))
.content_type("plain/text")
.if_true(true, |req| req.header(header::SERVER, "awc"))
.if_true(false, |req| req.header(header::EXPECT, "awc"))
.if_some(Some("server"), |val, req| {
req.header(header::USER_AGENT, val)
})
.if_some(Option::<&str>::None, |_, req| {
req.header(header::ALLOW, "1")
})
.content_length(100);
.header(header::SERVER, "awc");

let req = if let Some(val) = Some("server") {
req.header(header::USER_AGENT, val)
} else {
req
};

let req = if let Some(_val) = Option::<&str>::None {
req.header(header::ALLOW, "1")
} else {
req
};

let mut req = req.content_length(100);

assert!(req.headers().contains_key(header::CONTENT_TYPE));
assert!(req.headers().contains_key(header::DATE));
assert!(req.headers().contains_key(header::SERVER));
assert!(req.headers().contains_key(header::USER_AGENT));
assert!(!req.headers().contains_key(header::ALLOW));
assert!(!req.headers().contains_key(header::EXPECT));
assert_eq!(req.head.version, Version::HTTP_2);

let _ = req.headers_mut();
let _ = req.send_body("");
}
Expand Down

0 comments on commit 4cb8336

Please sign in to comment.