Skip to content

Commit

Permalink
Swap Id from NonZero to NonMax (serenity-rs#2689)
Browse files Browse the repository at this point in the history
Discord seems to internally default Ids to 0, which is a bug whenever
exposed, but this makes ID parsing more resilient. I also took the
liberty to remove the `From<NonZero*>` implementations, to prevent future
headaches, as it was impossible to not break public API as we exposed
`NonZero` in `*Id::parse`.
  • Loading branch information
GnomedDev committed Jun 9, 2024
1 parent 613c04c commit ca917e1
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 189 deletions.
2 changes: 1 addition & 1 deletion src/builder/create_command_permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl CreateCommandPermission {
/// Creates a permission overwrite for all channels in a guild
pub fn all_channels(guild_id: GuildId, allow: bool) -> Self {
Self(CommandPermission {
id: std::num::NonZeroU64::new(guild_id.get() - 1).expect("guild ID was 1").into(),
id: CommandPermissionId::new(guild_id.get() - 1),
kind: CommandPermissionType::Channel,
permission: allow,
})
Expand Down
10 changes: 7 additions & 3 deletions src/http/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![allow(clippy::missing_errors_doc)]

use std::borrow::Cow;
use std::num::NonZeroU64;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

Expand Down Expand Up @@ -142,7 +141,8 @@ impl HttpBuilder {
/// Use the given configuration to build the `Http` client.
#[must_use]
pub fn build(self) -> Http {
let application_id = AtomicU64::new(self.application_id.map_or(0, ApplicationId::get));
let application_id =
AtomicU64::new(self.application_id.map_or(u64::MAX, ApplicationId::get));

let client = self.client.unwrap_or_else(|| {
let builder = configure_client_backend(Client::builder());
Expand Down Expand Up @@ -210,7 +210,11 @@ impl Http {

pub fn application_id(&self) -> Option<ApplicationId> {
let application_id = self.application_id.load(Ordering::Relaxed);
NonZeroU64::new(application_id).map(ApplicationId::from)
if application_id == u64::MAX {
None
} else {
Some(ApplicationId::new(application_id))
}
}

fn try_application_id(&self) -> Result<ApplicationId> {
Expand Down
Loading

0 comments on commit ca917e1

Please sign in to comment.