Skip to content

Commit

Permalink
Merge branch 'main' into rick/premium-buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
rickmartensnl authored Aug 31, 2024
2 parents 4d50e42 + c4d8327 commit 9a99104
Show file tree
Hide file tree
Showing 23 changed files with 64 additions and 92 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ edition = "2021"
include = ["src/**/*.rs", "README.md"]
license = "ISC"
repository = "https://github.com/twilight-rs/twilight.git"
rust-version = "1.67"
rust-version = "1.79"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ functionality. Please use the individual crates listed below instead!

## Installation

Twilight supports a MSRV of Rust 1.67.
Twilight supports a MSRV of Rust 1.79.

We recommend that most users start out with these crates:

Expand Down Expand Up @@ -201,7 +201,7 @@ All first-party crates are licensed under [ISC][LICENSE.md]
[license badge]: https://img.shields.io/badge/license-ISC-blue.svg?style=for-the-badge&logo=pastebin
[license link]: https://github.com/twilight-rs/twilight/blob/main/LICENSE.md
[logo]: https://raw.githubusercontent.com/twilight-rs/twilight/main/logo.png
[rust badge]: https://img.shields.io/badge/rust-1.67+-93450a.svg?style=for-the-badge&logo=rust
[rust badge]: https://img.shields.io/badge/rust-1.79+-93450a.svg?style=for-the-badge&logo=rust
[`twilight-cache-inmemory`]: https://twilight.rs/chapter_1_crates/section_4_cache_inmemory.html
[`twilight-gateway-queue`]: https://twilight.rs/chapter_1_crates/section_7_first_party/section_5_gateway_queue.html
[`twilight-gateway`]: https://twilight.rs/chapter_1_crates/section_3_gateway.html
Expand Down
2 changes: 1 addition & 1 deletion book/src/versions/0.15/2023-09-10.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ twilight-model no longer depends on `tracing`.

## MSRV

All twilight ecosystem crates now target a MSRV of rustc 1.67. This was necessary
All twilight ecosystem crates now target a MSRV of rustc 1.79. This was necessary
due to MSRV increases in our dependencies.

## Performance Improvements
Expand Down
2 changes: 1 addition & 1 deletion twilight-cache-inmemory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ All first-party crates are licensed under [ISC][LICENSE.md]
[github link]: https://github.com/twilight-rs/twilight
[license badge]: https://img.shields.io/badge/license-ISC-blue.svg?style=for-the-badge&logo=pastebin
[license link]: https://github.com/twilight-rs/twilight/blob/main/LICENSE.md
[rust badge]: https://img.shields.io/badge/rust-1.67+-93450a.svg?style=for-the-badge&logo=rust
[rust badge]: https://img.shields.io/badge/rust-1.79+-93450a.svg?style=for-the-badge&logo=rust
3 changes: 1 addition & 2 deletions twilight-cache-inmemory/src/event/reaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for ReactionAdd {
} else {
let me = cache
.current_user()
.map(|user| user.id() == self.0.user_id)
.unwrap_or_default();
.is_some_and(|user| user.id() == self.0.user_id);

message.add_reaction(Reaction {
burst_colors: Vec::new(),
Expand Down
9 changes: 3 additions & 6 deletions twilight-cache-inmemory/src/event/sticker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,11 @@ mod tests {
assert!(cache
.stickers
.get(&STICKER_ONE_ID)
.map(|r| r.id == STICKER_ONE_ID)
.unwrap_or_default());
.is_some_and(|r| r.id == STICKER_ONE_ID));
assert!(cache
.stickers
.get(&STICKER_TWO_ID)
.map(|r| r.id == STICKER_TWO_ID)
.unwrap_or_default());
.is_some_and(|r| r.id == STICKER_TWO_ID));

let guild_stickers = cache
.guild_stickers
Expand All @@ -145,8 +143,7 @@ mod tests {
assert!(cache
.stickers
.get(&STICKER_ONE_ID)
.map(|r| r.id == STICKER_ONE_ID)
.unwrap_or_default());
.is_some_and(|r| r.id == STICKER_ONE_ID));
let guild_stickers = cache
.guild_stickers
.get(&GUILD_ID)
Expand Down
22 changes: 10 additions & 12 deletions twilight-cache-inmemory/src/event/voice_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ impl<CacheModels: CacheableModels> InMemoryCache<CacheModels> {
let remove_channel_mapping = self
.voice_state_channels
.get_mut(&voice_state.channel_id())
.map(|mut channel_voice_states| {
.is_some_and(|mut channel_voice_states| {
channel_voice_states.remove(&(guild_id, user_id));

channel_voice_states.is_empty()
})
.unwrap_or_default();
});

if remove_channel_mapping {
self.voice_state_channels.remove(&voice_state.channel_id());
Expand All @@ -54,15 +53,14 @@ impl<CacheModels: CacheableModels> InMemoryCache<CacheModels> {
} else {
// voice channel_id does not exist, signifying that the user has left
{
let remove_guild = self
.voice_state_guilds
.get_mut(&guild_id)
.map(|mut guild_users| {
guild_users.remove(&user_id);

guild_users.is_empty()
})
.unwrap_or_default();
let remove_guild =
self.voice_state_guilds
.get_mut(&guild_id)
.is_some_and(|mut guild_users| {
guild_users.remove(&user_id);

guild_users.is_empty()
});

if remove_guild {
self.voice_state_guilds.remove(&guild_id);
Expand Down
3 changes: 1 addition & 2 deletions twilight-cache-inmemory/src/permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,7 @@ impl<'a, CacheModels: CacheableModels> InMemoryCachePermissions<'a, CacheModels>
self.cache
.guilds
.get(&guild_id)
.map(|r| r.owner_id() == user_id)
.unwrap_or_default()
.is_some_and(|r| r.owner_id() == user_id)
}

/// Retrieve a member's roles' permissions and the guild's `@everyone`
Expand Down
2 changes: 1 addition & 1 deletion twilight-gateway-queue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ a HTTP client and server implementation, respectively.
[github link]: https://github.com/twilight-rs/twilight
[license badge]: https://img.shields.io/badge/license-ISC-blue.svg?style=for-the-badge&logo=pastebin
[license link]: https://github.com/twilight-rs/twilight/blob/main/LICENSE.md
[rust badge]: https://img.shields.io/badge/rust-1.67+-93450a.svg?style=for-the-badge&logo=rust
[rust badge]: https://img.shields.io/badge/rust-1.79+-93450a.svg?style=for-the-badge&logo=rust
8 changes: 1 addition & 7 deletions twilight-gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ async fn runner(mut shard: Shard) {
let event = match item {
Ok(Event::GatewayClose(_)) if SHUTDOWN.load(Ordering::Relaxed) => break,
Ok(event) => event,
Err(source)
if SHUTDOWN.load(Ordering::Relaxed)
&& matches!(source.kind(), ReceiveMessageErrorType::WebSocket) =>
{
break
}
Err(source) => {
tracing::warn!(?source, "error receiving event");
Expand Down Expand Up @@ -126,4 +120,4 @@ There are a few additional examples located in the
[github link]: https://github.com/twilight-rs/twilight
[license badge]: https://img.shields.io/badge/license-ISC-blue.svg?style=for-the-badge&logo=pastebin
[license link]: https://github.com/twilight-rs/twilight/blob/main/LICENSE.md
[rust badge]: https://img.shields.io/badge/rust-1.67+-93450a.svg?style=for-the-badge&logo=rust
[rust badge]: https://img.shields.io/badge/rust-1.79+-93450a.svg?style=for-the-badge&logo=rust
14 changes: 1 addition & 13 deletions twilight-gateway/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,6 @@ impl ReceiveMessageError {
source: Some(Box::new(source)),
}
}

/// Shortcut to create a new error for a websocket error.
pub(crate) fn from_websocket(source: tokio_websockets::Error) -> Self {
Self {
kind: ReceiveMessageErrorType::WebSocket,
source: Some(Box::new(source)),
}
}
}

impl Display for ReceiveMessageError {
Expand All @@ -197,7 +189,6 @@ impl Display for ReceiveMessageError {
f.write_str(event)
}
ReceiveMessageErrorType::Reconnect => f.write_str("failed to reconnect to the gateway"),
ReceiveMessageErrorType::WebSocket => f.write_str("websocket connection error"),
}
}
}
Expand Down Expand Up @@ -228,8 +219,6 @@ pub enum ReceiveMessageErrorType {
},
/// Shard failed to reconnect to the gateway.
Reconnect,
/// WebSocket connection error.
WebSocket,
}

#[cfg(test)]
Expand All @@ -243,7 +232,7 @@ mod tests {

#[test]
fn receive_message_error_display() {
let messages: [(ReceiveMessageErrorType, &str); 4] = [
let messages: [(ReceiveMessageErrorType, &str); 3] = [
(
ReceiveMessageErrorType::Compression,
"binary message could not be decompressed",
Expand All @@ -258,7 +247,6 @@ mod tests {
ReceiveMessageErrorType::Reconnect,
"failed to reconnect to the gateway",
),
(ReceiveMessageErrorType::WebSocket, "websocket connection error"),
];

for (kind, message) in messages {
Expand Down
3 changes: 3 additions & 0 deletions twilight-gateway/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub enum Message {
}

impl Message {
/// Close message indicating the connection was closed abnormally.
pub(crate) const ABNORMAL_CLOSE: Self = Self::Close(Some(CloseFrame::new(1006, "")));

/// Whether the message is a close message.
pub const fn is_close(&self) -> bool {
matches!(self, Self::Close(_))
Expand Down
56 changes: 27 additions & 29 deletions twilight-gateway/src/shard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,7 @@ impl<Q> Shard<Q> {
/// continue showing the bot as online until its presence times out.
///
/// To read all remaining messages, continue calling [`poll_next`] until it
/// returns [`Message::Close`] or a [`ReceiveMessageErrorType::WebSocket`]
/// error type.
/// returns [`Message::Close`].
///
/// # Example
///
Expand All @@ -457,7 +456,6 @@ impl<Q> Shard<Q> {
/// match item {
/// Ok(Message::Close(_)) => break,
/// Ok(Message::Text(_)) => unimplemented!(),
/// Err(source) if matches!(source.kind(), ReceiveMessageErrorType::WebSocket) => break,
/// Err(source) => unimplemented!(),
/// }
/// }
Expand Down Expand Up @@ -550,19 +548,16 @@ impl<Q> Shard<Q> {
}

/// Send and flush the pending message.
fn poll_flush_pending(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Result<(), ReceiveMessageError>> {
fn poll_flush_pending(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), WebsocketError>> {
if self.pending.is_none() {
return Poll::Ready(Ok(()));
}

ready!(Pin::new(self.connection.as_mut().unwrap()).poll_ready(cx)).map_err(|source| {
if let Err(e) = ready!(Pin::new(self.connection.as_mut().unwrap()).poll_ready(cx)) {
self.disconnect(CloseInitiator::Transport);
self.connection = None;
ReceiveMessageError::from_websocket(source)
})?;
return Poll::Ready(Err(e));
}

let pending = self.pending.as_mut().unwrap();

Expand All @@ -574,18 +569,17 @@ impl<Q> Shard<Q> {
}

let ws_message = pending.gateway_event.take().unwrap().into_websocket_msg();
if let Err(source) = Pin::new(self.connection.as_mut().unwrap()).start_send(ws_message)
{
if let Err(e) = Pin::new(self.connection.as_mut().unwrap()).start_send(ws_message) {
self.disconnect(CloseInitiator::Transport);
self.connection = None;
return Poll::Ready(Err(ReceiveMessageError::from_websocket(source)));
return Poll::Ready(Err(e));
}
}

if let Err(source) = ready!(Pin::new(self.connection.as_mut().unwrap()).poll_flush(cx)) {
if let Err(e) = ready!(Pin::new(self.connection.as_mut().unwrap()).poll_flush(cx)) {
self.disconnect(CloseInitiator::Transport);
self.connection = None;
return Poll::Ready(Err(ReceiveMessageError::from_websocket(source)));
return Poll::Ready(Err(e));
}

if pending.is_heartbeat {
Expand Down Expand Up @@ -798,7 +792,9 @@ impl<Q: Queue + Unpin> Stream for Shard<Q> {
_ => {}
}

ready!(self.poll_flush_pending(cx))?;
if ready!(self.poll_flush_pending(cx)).is_err() {
return Poll::Ready(Some(Ok(Message::ABNORMAL_CLOSE)));
}

if !self.state.is_disconnected() {
if let Poll::Ready(frame) = self.user_channel.close_rx.poll_recv(cx) {
Expand All @@ -807,7 +803,9 @@ impl<Q: Queue + Unpin> Stream for Shard<Q> {
tracing::debug!("sending close frame from user channel");
self.disconnect(CloseInitiator::Shard(frame));

ready!(self.poll_flush_pending(cx))?;
if ready!(self.poll_flush_pending(cx)).is_err() {
return Poll::Ready(Some(Ok(Message::ABNORMAL_CLOSE)));
}
}
}

Expand All @@ -834,7 +832,9 @@ impl<Q: Queue + Unpin> Stream for Shard<Q> {
self.heartbeat_interval_event = false;
}

ready!(self.poll_flush_pending(cx))?;
if ready!(self.poll_flush_pending(cx)).is_err() {
return Poll::Ready(Some(Ok(Message::ABNORMAL_CLOSE)));
}
}

let not_ratelimited = self
Expand Down Expand Up @@ -873,7 +873,9 @@ impl<Q: Queue + Unpin> Stream for Shard<Q> {
);
self.identify_rx = None;

ready!(self.poll_flush_pending(cx))?;
if ready!(self.poll_flush_pending(cx)).is_err() {
return Poll::Ready(Some(Ok(Message::ABNORMAL_CLOSE)));
}
}
}

Expand All @@ -884,7 +886,9 @@ impl<Q: Queue + Unpin> Stream for Shard<Q> {
tracing::debug!("sending command from user channel");
self.pending = Pending::text(command, false);

ready!(self.poll_flush_pending(cx))?;
if ready!(self.poll_flush_pending(cx)).is_err() {
return Poll::Ready(Some(Ok(Message::ABNORMAL_CLOSE)));
}
}
}

Expand Down Expand Up @@ -921,24 +925,18 @@ impl<Q: Queue + Unpin> Stream for Shard<Q> {
{
continue
}
Some(Err(source)) => {
Some(Err(_)) => {
self.disconnect(CloseInitiator::Transport);

return Poll::Ready(Some(Err(ReceiveMessageError {
kind: ReceiveMessageErrorType::WebSocket,
source: Some(Box::new(source)),
})));
return Poll::Ready(Some(Ok(Message::ABNORMAL_CLOSE)));
}
None => {
let res = ready!(Pin::new(self.connection.as_mut().unwrap()).poll_close(cx));
_ = ready!(Pin::new(self.connection.as_mut().unwrap()).poll_close(cx));
tracing::debug!("gateway WebSocket connection closed");
// Unclean closure.
if !self.state.is_disconnected() {
self.disconnect(CloseInitiator::Transport);
}
self.connection = None;

res.map_err(ReceiveMessageError::from_websocket)?;
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion twilight-http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,4 @@ DNS resolver on the application level.
[github link]: https://github.com/twilight-rs/twilight
[license badge]: https://img.shields.io/badge/license-ISC-blue.svg?style=for-the-badge&logo=pastebin
[license link]: https://github.com/twilight-rs/twilight/blob/main/LICENSE.md
[rust badge]: https://img.shields.io/badge/rust-1.67+-93450a.svg?style=for-the-badge&logo=rust
[rust badge]: https://img.shields.io/badge/rust-1.79+-93450a.svg?style=for-the-badge&logo=rust
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl<'a> UpdateCurrentUserVoiceState<'a> {
/// # Caveats
///
/// - You must have the `MUTE_MEMBERS` permission to unsuppress yourself.
/// You can always suppress yourself.
/// - You can always suppress yourself.
pub const fn suppress(mut self) -> Self {
self.fields.suppress = Some(true);

Expand Down
2 changes: 1 addition & 1 deletion twilight-lavalink/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,4 @@ There is also an example of a basic bot located in the [root of the
[license link]: https://github.com/twilight-rs/twilight/blob/main/LICENSE.md
[node]: Node
[process]: Lavalink::process
[rust badge]: https://img.shields.io/badge/rust-1.67+-93450a.svg?style=for-the-badge&logo=rust
[rust badge]: https://img.shields.io/badge/rust-1.79+-93450a.svg?style=for-the-badge&logo=rust
2 changes: 1 addition & 1 deletion twilight-mention/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ let message = format!("Hey there, {}!", user_id.mention());
[github link]: https://github.com/twilight-rs/twilight
[license badge]: https://img.shields.io/badge/license-ISC-blue.svg?style=for-the-badge&logo=pastebin
[license link]: https://github.com/twilight-rs/twilight/blob/main/LICENSE.md
[rust badge]: https://img.shields.io/badge/rust-1.67+-93450a.svg?style=for-the-badge&logo=rust
[rust badge]: https://img.shields.io/badge/rust-1.79+-93450a.svg?style=for-the-badge&logo=rust
2 changes: 1 addition & 1 deletion twilight-model/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ Some models have associated builders, which can be found in the
[github link]: https://github.com/twilight-rs/twilight
[license badge]: https://img.shields.io/badge/license-ISC-blue.svg?style=for-the-badge&logo=pastebin
[license link]: https://github.com/twilight-rs/twilight/blob/main/LICENSE.md
[rust badge]: https://img.shields.io/badge/rust-1.67+-93450a.svg?style=for-the-badge&logo=rust
[rust badge]: https://img.shields.io/badge/rust-1.79+-93450a.svg?style=for-the-badge&logo=rust
Loading

0 comments on commit 9a99104

Please sign in to comment.