From 7cd36d9c4051399f6a2153366f8df74866afd417 Mon Sep 17 00:00:00 2001 From: Hirrolot Date: Thu, 17 Nov 2022 14:42:36 +0600 Subject: [PATCH 1/3] Make more functions `const` --- CHANGELOG.md | 9 +++++++++ crates/teloxide-core/src/bot.rs | 2 +- crates/teloxide-core/src/net.rs | 2 +- crates/teloxide/src/dispatching/dialogue.rs | 4 ++-- crates/teloxide/src/dispatching/update_listeners.rs | 2 +- .../teloxide/src/dispatching/update_listeners/polling.rs | 2 +- .../dispatching/update_listeners/stateful_listener.rs | 2 +- crates/teloxide/src/utils/command.rs | 2 +- 8 files changed, 17 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 185ffd5d..b627ca35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `rocksdb-storage` feature and associated items (See [PR #761](https://github.com/teloxide/teloxide/pull/761) for reasoning) [**BC**] +## Changed + +- The following functions were made `const`: + - `Dialogue::{new, chat_id}` + - `Polling::builder` + - `StatefulListener::new_with_hints` + - `CommandDescriptions::new` + - `respond` + ## 0.11.1 - 2022-10-31 ### Added diff --git a/crates/teloxide-core/src/bot.rs b/crates/teloxide-core/src/bot.rs index ef0801f8..b8f9d67e 100644 --- a/crates/teloxide-core/src/bot.rs +++ b/crates/teloxide-core/src/bot.rs @@ -130,7 +130,7 @@ impl Bot { /// [`reqwest::Client`]: https://docs.rs/reqwest/0.10.1/reqwest/struct.Client.html /// [issue 223]: https://github.com/teloxide/teloxide/issues/223 pub fn from_env_with_client(client: Client) -> Self { - Self::with_client(&get_env(TELOXIDE_TOKEN), client) + Self::with_client(get_env(TELOXIDE_TOKEN), client) } /// Sets a custom API URL. diff --git a/crates/teloxide-core/src/net.rs b/crates/teloxide-core/src/net.rs index d7f96a76..5bd12342 100644 --- a/crates/teloxide-core/src/net.rs +++ b/crates/teloxide-core/src/net.rs @@ -43,7 +43,7 @@ pub fn client_from_env() -> reqwest::Client { let builder = default_reqwest_settings(); match std::env::var(TELOXIDE_PROXY).ok() { - Some(proxy) => builder.proxy(Proxy::all(&proxy).expect("reqwest::Proxy creation failed")), + Some(proxy) => builder.proxy(Proxy::all(proxy).expect("reqwest::Proxy creation failed")), None => builder, } .build() diff --git a/crates/teloxide/src/dispatching/dialogue.rs b/crates/teloxide/src/dispatching/dialogue.rs index 89d16326..32386891 100644 --- a/crates/teloxide/src/dispatching/dialogue.rs +++ b/crates/teloxide/src/dispatching/dialogue.rs @@ -141,13 +141,13 @@ where /// Constructs a new dialogue with `storage` (where dialogues are stored) /// and `chat_id` of a current dialogue. #[must_use] - pub fn new(storage: Arc, chat_id: ChatId) -> Self { + pub const fn new(storage: Arc, chat_id: ChatId) -> Self { Self { storage, chat_id, _phantom: PhantomData } } /// Returns a chat ID associated with this dialogue. #[must_use] - pub fn chat_id(&self) -> ChatId { + pub const fn chat_id(&self) -> ChatId { self.chat_id } diff --git a/crates/teloxide/src/dispatching/update_listeners.rs b/crates/teloxide/src/dispatching/update_listeners.rs index 4103e398..5594d949 100644 --- a/crates/teloxide/src/dispatching/update_listeners.rs +++ b/crates/teloxide/src/dispatching/update_listeners.rs @@ -133,7 +133,7 @@ pub trait AsUpdateStream<'a> { } #[inline(always)] -pub(crate) fn assert_update_listener(listener: L) -> L +pub(crate) const fn assert_update_listener(listener: L) -> L where L: UpdateListener, { diff --git a/crates/teloxide/src/dispatching/update_listeners/polling.rs b/crates/teloxide/src/dispatching/update_listeners/polling.rs index ffe8aa5a..e212ee06 100644 --- a/crates/teloxide/src/dispatching/update_listeners/polling.rs +++ b/crates/teloxide/src/dispatching/update_listeners/polling.rs @@ -250,7 +250,7 @@ where ::GetUpdates: Send, { /// Returns a builder for polling update listener. - pub fn builder(bot: R) -> PollingBuilder { + pub const fn builder(bot: R) -> PollingBuilder { PollingBuilder { bot, timeout: None, diff --git a/crates/teloxide/src/dispatching/update_listeners/stateful_listener.rs b/crates/teloxide/src/dispatching/update_listeners/stateful_listener.rs index 0eec921d..85140746 100644 --- a/crates/teloxide/src/dispatching/update_listeners/stateful_listener.rs +++ b/crates/teloxide/src/dispatching/update_listeners/stateful_listener.rs @@ -55,7 +55,7 @@ impl StatefulListener, Thfn> { impl StatefulListener { /// Creates a new stateful listener from its components. - pub fn new_with_hints( + pub const fn new_with_hints( state: St, stream: Assf, stop_token: Sf, diff --git a/crates/teloxide/src/utils/command.rs b/crates/teloxide/src/utils/command.rs index 8f96998e..95ef0000 100644 --- a/crates/teloxide/src/utils/command.rs +++ b/crates/teloxide/src/utils/command.rs @@ -298,7 +298,7 @@ pub struct CommandDescription<'a> { impl<'a> CommandDescriptions<'a> { /// Creates new [`CommandDescriptions`] from a list of command descriptions. #[must_use] - pub fn new(descriptions: &'a [CommandDescription<'a>]) -> Self { + pub const fn new(descriptions: &'a [CommandDescription<'a>]) -> Self { Self { global_description: None, descriptions, bot_username: None } } From 762ab2f5e31d4aec9d762a133bc7681868674ee9 Mon Sep 17 00:00:00 2001 From: Hirrolot Date: Thu, 17 Nov 2022 14:54:40 +0600 Subject: [PATCH 2/3] Make more functions `#[must_use]` --- CHANGELOG.md | 2 ++ crates/teloxide-core/CHANGELOG.md | 3 +++ crates/teloxide-core/src/types/mask_position.rs | 2 ++ crates/teloxide-core/src/types/sticker.rs | 3 +++ crates/teloxide/src/dispatching/dispatcher.rs | 2 ++ 5 files changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b627ca35..eef72e3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `StatefulListener::new_with_hints` - `CommandDescriptions::new` - `respond` +- The following functions were made `#[must_use]`: + - `DispatcherBuilder::{enable_ctrlc_handler, distribution_function}` ## 0.11.1 - 2022-10-31 diff --git a/crates/teloxide-core/CHANGELOG.md b/crates/teloxide-core/CHANGELOG.md index c9e9d582..0e5c1487 100644 --- a/crates/teloxide-core/CHANGELOG.md +++ b/crates/teloxide-core/CHANGELOG.md @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - The methods `ChatMember::{can_pin_messages, can_invite_users, can_change_info}` now take into account the permissions of `Restricted` chat member kind ([#764][pr764]) - The method `ChatMemberKind::is_present` now takes into account the value of `Restricted::is_member` field ([#764][pr764]) +- The following functions were made `#[must_use]`: + - `MaskPoint::{new, point}` + - `StickerKind::{premium_animation, mask_position, custom_emoji_id}` ### Added diff --git a/crates/teloxide-core/src/types/mask_position.rs b/crates/teloxide-core/src/types/mask_position.rs index 147654e1..fd736dcf 100644 --- a/crates/teloxide-core/src/types/mask_position.rs +++ b/crates/teloxide-core/src/types/mask_position.rs @@ -35,10 +35,12 @@ pub enum MaskPoint { } impl MaskPosition { + #[must_use] pub const fn new(point: MaskPoint, x_shift: f64, y_shift: f64, scale: f64) -> Self { Self { point, x_shift, y_shift, scale } } + #[must_use] pub const fn point(mut self, val: MaskPoint) -> Self { self.point = val; self diff --git a/crates/teloxide-core/src/types/sticker.rs b/crates/teloxide-core/src/types/sticker.rs index 0424b7ae..6f41fccc 100644 --- a/crates/teloxide-core/src/types/sticker.rs +++ b/crates/teloxide-core/src/types/sticker.rs @@ -200,6 +200,7 @@ impl StickerKind { } /// Getter for [`StickerKind::Regular::premium_animation`]. + #[must_use] pub fn premium_animation(&self) -> Option<&FileMeta> { if let Self::Regular { premium_animation } = self { premium_animation.as_ref() @@ -209,6 +210,7 @@ impl StickerKind { } /// Getter for [`StickerKind::Mask::mask_position`]. + #[must_use] pub fn mask_position(&self) -> Option { if let Self::Mask { mask_position } = self { Some(*mask_position) @@ -218,6 +220,7 @@ impl StickerKind { } /// Getter for [`StickerKind::CustomEmoji::custom_emoji_id`]. + #[must_use] pub fn custom_emoji_id(&self) -> Option<&str> { if let Self::CustomEmoji { custom_emoji_id } = self { Some(custom_emoji_id) diff --git a/crates/teloxide/src/dispatching/dispatcher.rs b/crates/teloxide/src/dispatching/dispatcher.rs index c44ac97e..1d062c85 100644 --- a/crates/teloxide/src/dispatching/dispatcher.rs +++ b/crates/teloxide/src/dispatching/dispatcher.rs @@ -86,6 +86,7 @@ where /// /// [`shutdown`]: ShutdownToken::shutdown #[cfg(feature = "ctrlc_handler")] + #[must_use] pub fn enable_ctrlc_handler(self) -> Self { Self { ctrlc_handler: true, ..self } } @@ -100,6 +101,7 @@ where /// Specifies the distribution function that decides how updates are grouped /// before execution. + #[must_use] pub fn distribution_function( self, f: fn(&Update) -> Option, From cde45ab8eee494a16cdff54620819b0288fe6dd1 Mon Sep 17 00:00:00 2001 From: Hirrolot Date: Fri, 9 Dec 2022 19:16:41 +0600 Subject: [PATCH 3/3] Remove useless `const` --- CHANGELOG.md | 7 +------ crates/teloxide/src/dispatching/dialogue.rs | 4 ++-- .../teloxide/src/dispatching/update_listeners/polling.rs | 2 +- .../src/dispatching/update_listeners/stateful_listener.rs | 2 +- 4 files changed, 5 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eef72e3c..11e2543c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,12 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Changed -- The following functions were made `const`: - - `Dialogue::{new, chat_id}` - - `Polling::builder` - - `StatefulListener::new_with_hints` - - `CommandDescriptions::new` - - `respond` +- `CommandDescriptions::new` is made `const` - The following functions were made `#[must_use]`: - `DispatcherBuilder::{enable_ctrlc_handler, distribution_function}` diff --git a/crates/teloxide/src/dispatching/dialogue.rs b/crates/teloxide/src/dispatching/dialogue.rs index 32386891..89d16326 100644 --- a/crates/teloxide/src/dispatching/dialogue.rs +++ b/crates/teloxide/src/dispatching/dialogue.rs @@ -141,13 +141,13 @@ where /// Constructs a new dialogue with `storage` (where dialogues are stored) /// and `chat_id` of a current dialogue. #[must_use] - pub const fn new(storage: Arc, chat_id: ChatId) -> Self { + pub fn new(storage: Arc, chat_id: ChatId) -> Self { Self { storage, chat_id, _phantom: PhantomData } } /// Returns a chat ID associated with this dialogue. #[must_use] - pub const fn chat_id(&self) -> ChatId { + pub fn chat_id(&self) -> ChatId { self.chat_id } diff --git a/crates/teloxide/src/dispatching/update_listeners/polling.rs b/crates/teloxide/src/dispatching/update_listeners/polling.rs index e212ee06..ffe8aa5a 100644 --- a/crates/teloxide/src/dispatching/update_listeners/polling.rs +++ b/crates/teloxide/src/dispatching/update_listeners/polling.rs @@ -250,7 +250,7 @@ where ::GetUpdates: Send, { /// Returns a builder for polling update listener. - pub const fn builder(bot: R) -> PollingBuilder { + pub fn builder(bot: R) -> PollingBuilder { PollingBuilder { bot, timeout: None, diff --git a/crates/teloxide/src/dispatching/update_listeners/stateful_listener.rs b/crates/teloxide/src/dispatching/update_listeners/stateful_listener.rs index 85140746..0eec921d 100644 --- a/crates/teloxide/src/dispatching/update_listeners/stateful_listener.rs +++ b/crates/teloxide/src/dispatching/update_listeners/stateful_listener.rs @@ -55,7 +55,7 @@ impl StatefulListener, Thfn> { impl StatefulListener { /// Creates a new stateful listener from its components. - pub const fn new_with_hints( + pub fn new_with_hints( state: St, stream: Assf, stop_token: Sf,