Skip to content

Commit c01e57d

Browse files
authored
Use ArrayString in more places via aformat! (#2902)
This also bumps `to-arraystring` up to 0.2, required for `aformat`.
1 parent ff8e844 commit c01e57d

File tree

13 files changed

+91
-52
lines changed

13 files changed

+91
-52
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.74.0
7-
rust_nightly: nightly-2024-02-08
6+
rust_min: 1.79.0
7+
rust_nightly: nightly-2024-06-21
88

99
jobs:
1010
test:

Cargo.toml

Lines changed: 3 additions & 2 deletions
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.74"
13+
rust-version = "1.79"
1414
include = ["src/**/*", "LICENSE.md", "README.md", "CHANGELOG.md", "build.rs"]
1515

1616
[workspace]
@@ -36,8 +36,9 @@ small-fixed-array = { version = "0.4", features = ["serde"] }
3636
bool_to_bitflags = { version = "0.1.2" }
3737
nonmax = { version = "0.5.5", features = ["serde"] }
3838
strum = { version = "0.26", features = ["derive"] }
39-
to-arraystring = "0.1.0"
39+
to-arraystring = "0.2.0"
4040
extract_map = { version = "0.1.0", features = ["serde", "iter_mut"] }
41+
aformat = "0.1.3"
4142
# Optional dependencies
4243
fxhash = { version = "0.2.1", optional = true }
4344
chrono = { version = "0.4.31", default-features = false, features = ["clock", "serde"], optional = true }

src/builder/bot_auth_parameters.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::borrow::Cow;
22

33
use arrayvec::ArrayVec;
4-
use to_arraystring::ToArrayString;
54
use url::Url;
65

76
#[cfg(feature = "http")]

src/gateway/shard.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -770,10 +770,9 @@ impl Shard {
770770
}
771771

772772
async fn connect(base_url: &str) -> Result<WsClient> {
773-
let url =
774-
Url::parse(&format!("{base_url}?v={}", constants::GATEWAY_VERSION)).map_err(|why| {
775-
warn!("Error building gateway URL with base `{}`: {:?}", base_url, why);
776-
773+
let url = Url::parse(&aformat!("{}?v={}", CapStr::<64>(base_url), constants::GATEWAY_VERSION))
774+
.map_err(|why| {
775+
warn!("Error building gateway URL with base `{base_url}`: {why:?}");
777776
Error::Gateway(GatewayError::BuildingUrl)
778777
})?;
779778

src/http/client.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use reqwest::{Client, ClientBuilder, Response as ReqwestResponse, StatusCode};
1414
use secrecy::{ExposeSecret as _, Secret};
1515
use serde::de::DeserializeOwned;
1616
use serde_json::{from_value, json, to_string, to_vec};
17-
use to_arraystring::ToArrayString as _;
1817
use tracing::{debug, trace};
1918

2019
use super::multipart::{Multipart, MultipartUpload};
@@ -219,7 +218,7 @@ fn parse_token(token: &str) -> Arc<str> {
219218
if token.starts_with("Bot ") || token.starts_with("Bearer ") {
220219
Arc::from(token)
221220
} else {
222-
Arc::from(format!("Bot {token}"))
221+
Arc::from(aformat!("Bot {}", CapStr::<128>(token)).as_str())
223222
}
224223
}
225224

src/internal/prelude.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
55
pub use std::result::Result as StdResult;
66

7+
pub use aformat::{aformat, aformat_into, ArrayString, CapStr};
78
pub use extract_map::{ExtractKey, ExtractMap, LendingIterator};
89
pub use serde_json::Value;
910
pub use small_fixed_array::{FixedArray, FixedString, TruncatingInto};
11+
pub use to_arraystring::ToArrayString;
1012

1113
pub(crate) use super::utils::join_to_string;
1214
pub use crate::error::{Error, Result};

src/model/id.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
use std::fmt;
44

55
use nonmax::NonMaxU64;
6-
use to_arraystring::ToArrayString;
76

87
use super::Timestamp;
8+
use crate::internal::prelude::*;
99

1010
macro_rules! newtype_display_impl {
1111
($name:ident) => {
@@ -116,6 +116,8 @@ macro_rules! id_u64 {
116116

117117
impl ToArrayString for $name {
118118
type ArrayString = <u64 as ToArrayString>::ArrayString;
119+
const MAX_LENGTH: usize = <u64 as ToArrayString>::MAX_LENGTH;
120+
119121
fn to_arraystring(self) -> Self::ArrayString {
120122
self.get().to_arraystring()
121123
}

src/model/mention.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ use std::fmt;
44
#[cfg(all(feature = "model", feature = "utils"))]
55
use std::str::FromStr;
66

7-
use to_arraystring::ToArrayString;
8-
97
use super::prelude::*;
8+
use crate::internal::prelude::*;
109
#[cfg(all(feature = "model", feature = "utils"))]
1110
use crate::utils;
1211

@@ -113,19 +112,16 @@ impl fmt::Display for Mention {
113112
}
114113

115114
impl ToArrayString for Mention {
116-
type ArrayString = arrayvec::ArrayString<{ 20 + 4 }>;
115+
const MAX_LENGTH: usize = 20 + 4;
116+
type ArrayString = ArrayString<{ 20 + 4 }>;
117117

118118
fn to_arraystring(self) -> Self::ArrayString {
119-
let (prefix, id) = match self {
120-
Self::Channel(id) => ("<#", id.get()),
121-
Self::Role(id) => ("<@&", id.get()),
122-
Self::User(id) => ("<@", id.get()),
123-
};
124-
125119
let mut out = Self::ArrayString::new();
126-
out.push_str(prefix);
127-
out.push_str(&id.to_arraystring());
128-
out.push('>');
120+
match self {
121+
Self::Channel(id) => aformat_into!(out, "<#{id}>"),
122+
Self::Role(id) => aformat_into!(out, "<@&{id}>"),
123+
Self::User(id) => aformat_into!(out, "<@{id}>"),
124+
};
129125

130126
out
131127
}

src/model/misc.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ use std::fmt::Write;
88
use std::result::Result as StdResult;
99
use std::str::FromStr;
1010

11-
use arrayvec::ArrayString;
12-
1311
use super::prelude::*;
1412
use crate::internal::prelude::*;
1513
#[cfg(all(feature = "model", any(feature = "cache", feature = "utils")))]

src/utils/argument_convert/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub use role::*;
2222
mod emoji;
2323
pub use emoji::*;
2424

25-
use super::DOMAINS;
25+
use super::{DOMAINS, MAX_DOMAIN_LEN};
2626
use crate::model::prelude::*;
2727
use crate::prelude::*;
2828

@@ -124,8 +124,11 @@ pub fn parse_message_id_pair(s: &str) -> Option<(ChannelId, MessageId)> {
124124
/// ```
125125
#[must_use]
126126
pub fn parse_message_url(s: &str) -> Option<(GuildId, ChannelId, MessageId)> {
127+
use aformat::{aformat, CapStr};
128+
127129
for domain in DOMAINS {
128-
if let Some(parts) = s.strip_prefix(&format!("https://{domain}/channels/")) {
130+
let prefix = aformat!("https://{}/channels/", CapStr::<MAX_DOMAIN_LEN>(domain));
131+
if let Some(parts) = s.strip_prefix(prefix.as_str()) {
129132
let mut parts = parts.splitn(3, '/');
130133

131134
let guild_id = parts.next()?.parse().ok()?;

0 commit comments

Comments
 (0)