From c7e32fa247dea37942660257b3719946129726d5 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Fri, 18 Mar 2022 14:13:38 +0400 Subject: [PATCH] Add `HasPayload::with_payload_mut` function `HasPayload::with_payload_mut` allows to easily apply multiple changes to the payload without calling `payload_mut()` multiple times and creating temporary variable for the request. e.g.: ```rust // without `with_payload_mut` { let mut req = bot.set_webhook(url.clone()); req.payload_mut().certificate = certificate.take(); req.payload_mut().drop_pending_updates = drop_pending_updates; req.send().await?; } // with `with_payload_mut` bot .set_webhook(url.clone()) .with_payload_mut(|payload| { payload.certificate = certificate.take(); payload.drop_pending_updates = drop_pending_updates; }) .send() .await? ``` --- CHANGELOG.md | 6 ++++++ src/requests/has_payload.rs | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8462f08a..91c9af7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## unreleased +### Added + +- `HasPayload::with_payload_mut` function ([#189][pr189]) + +[pr189]: https://github.com/teloxide/teloxide-core/pull/189 + ## 0.4.3 - 2022-03-08 ### Added diff --git a/src/requests/has_payload.rs b/src/requests/has_payload.rs index 870bc50c..d7d12100 100644 --- a/src/requests/has_payload.rs +++ b/src/requests/has_payload.rs @@ -28,6 +28,16 @@ pub trait HasPayload { /// Gain immutable access to the underlying payload. fn payload_ref(&self) -> &Self::Payload; + + /// Update payload with a function + fn with_payload_mut(mut self, f: F) -> Self + where + Self: Sized, + F: FnOnce(&mut Self::Payload), + { + f(self.payload_mut()); + self + } } impl

HasPayload for P