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 authored Jan 5, 2024
1 parent 4e4e274 commit fb1f8ba
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 179 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
Expand Up @@ -2,7 +2,6 @@

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

Expand Down Expand Up @@ -134,7 +133,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 @@ -200,7 +200,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 fb1f8ba

Please sign in to comment.