Skip to content

Commit 9cd9c97

Browse files
authored
Add Context::data_ref (#3017)
This is needed for use cases like returning a borrow into the user data from a function which takes a reference to Context.
1 parent 80c657e commit 9cd9c97

File tree

27 files changed

+45
-43
lines changed

27 files changed

+45
-43
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ name: CI
33
on: [push, pull_request]
44

55
env:
6-
rust_min: 1.79.0
7-
rust_nightly: nightly-2024-06-21
6+
rust_min: 1.82.0
7+
rust_nightly: nightly-2024-11-01
88

99
jobs:
1010
test:

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ readme = "README.md"
1010
repository = "https://github.com/serenity-rs/serenity.git"
1111
version = "0.12.2"
1212
edition = "2021"
13-
rust-version = "1.79"
13+
rust-version = "1.82.0"
1414
include = ["src/**/*", "LICENSE.md", "README.md", "CHANGELOG.md", "build.rs"]
1515

1616
[workspace]

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] }
100100

101101
## MSRV Policy
102102

103-
Serenity's minimum supported Rust version (MSRV) is Rust 1.79.
103+
Serenity's minimum supported Rust version (MSRV) is Rust 1.82.
104104

105105
We opt to keep MSRV stable on the `current` branch. This means it will remain
106106
unchanged between minor releases. Occasionally, dependencies may violate SemVer
@@ -244,5 +244,5 @@ a Rust-native cloud development platform that allows deploying Serenity bots for
244244
[repo:andesite]: https://github.com/natanbc/andesite
245245
[repo:lavaplayer]: https://github.com/sedmelluq/lavaplayer
246246
[logo]: https://raw.githubusercontent.com/serenity-rs/serenity/current/logo.png
247-
[rust-version-badge]: https://img.shields.io/badge/rust-1.79.0+-93450a.svg?style=flat-square
248-
[rust-version-link]: https://blog.rust-lang.org/2024/06/13/Rust-1.79.0.html
247+
[rust-version-badge]: https://img.shields.io/badge/rust-1.82.0+-93450a.svg?style=flat-square
248+
[rust-version-link]: https://blog.rust-lang.org/2024/10/17/Rust-1.82.0.html

src/builder/create_attachment.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde::ser::{Serialize, SerializeSeq, Serializer};
55
use tokio::fs::File;
66
use tokio::io::AsyncReadExt;
77

8-
#[allow(unused)] // Error is used in docs
8+
#[expect(unused)] // Error is used in docs
99
use crate::error::{Error, Result};
1010
#[cfg(feature = "http")]
1111
use crate::http::Http;
@@ -232,7 +232,7 @@ impl<'a> EditAttachments<'a> {
232232
///
233233
/// Opposite of [`Self::keep`].
234234
pub fn remove(mut self, id: AttachmentId) -> Self {
235-
#[allow(clippy::match_like_matches_macro)] // `matches!` is less clear here
235+
#[expect(clippy::match_like_matches_macro)] // `matches!` is less clear here
236236
self.new_and_existing_attachments.retain(|a| match a {
237237
NewOrExisting::Existing(a) if a.id == id => false,
238238
_ => true,
@@ -241,7 +241,7 @@ impl<'a> EditAttachments<'a> {
241241
}
242242

243243
/// Adds a new attachment to the attachment list.
244-
#[allow(clippy::should_implement_trait)] // Clippy thinks add == std::ops::Add::add
244+
#[expect(clippy::should_implement_trait)] // Clippy thinks add == std::ops::Add::add
245245
pub fn add(mut self, attachment: CreateAttachment<'a>) -> Self {
246246
self.new_and_existing_attachments.push(NewOrExisting::New(attachment));
247247
self

src/builder/create_components.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl Serialize for CreateSelectMenuKind<'_> {
242242
default_values: &'a [CreateSelectMenuDefault],
243243
}
244244

245-
#[allow(clippy::ref_option)]
245+
#[expect(clippy::ref_option)]
246246
fn map<'a>(
247247
values: &'a Option<Cow<'a, [impl Into<Mention> + Copy]>>,
248248
) -> impl Iterator<Item = CreateSelectMenuDefault> + 'a {

src/gateway/client/context.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ impl Context {
8585
.expect("Type provided to Context should be the same as ClientBuilder::data.")
8686
}
8787

88+
/// A version of [`Self::data`] which returns a reference to the Data.
89+
///
90+
/// This is useful if you need to borrow `Data` with the lifetime of `Context`, but otherwise
91+
/// [`Self::data`] should be used.
92+
#[must_use]
93+
#[expect(clippy::needless_lifetimes, reason = "Easier to understand when explicitly written")]
94+
pub fn data_ref<'a, Data: Send + Sync + 'static>(&'a self) -> &'a Data {
95+
self.data
96+
.downcast_ref()
97+
.expect("Type provided to Context should be the same as ClientBuilder::data.")
98+
}
99+
88100
/// Sets the current user as being [`Online`]. This maintains the current activity.
89101
///
90102
/// # Examples

src/gateway/ws.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl WsClient {
162162
Ok(())
163163
}
164164

165-
#[allow(clippy::missing_errors_doc)]
165+
#[expect(clippy::missing_errors_doc)]
166166
pub async fn send_chunk_guild(
167167
&mut self,
168168
guild_id: GuildId,

src/http/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3063,7 +3063,7 @@ impl Http {
30633063
.await
30643064
}
30653065

3066-
#[allow(clippy::too_many_arguments)]
3066+
#[expect(clippy::too_many_arguments)]
30673067
/// Gets all entitlements for the current app, active and expired.
30683068
pub async fn get_entitlements(
30693069
&self,

src/http/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ impl StdError for HttpError {
419419
}
420420
}
421421

422-
#[allow(clippy::missing_errors_doc)]
422+
#[expect(clippy::missing_errors_doc)]
423423
pub fn deserialize_errors<'de, D: Deserializer<'de>>(
424424
deserializer: D,
425425
) -> StdResult<FixedArray<DiscordJsonSingleError>, D::Error> {

src/http/ratelimiting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ mod tests {
474474
}
475475

476476
#[test]
477-
#[allow(clippy::float_cmp)]
477+
#[expect(clippy::float_cmp)]
478478
fn test_parse_header_good() -> Result<()> {
479479
let headers = headers();
480480

0 commit comments

Comments
 (0)