Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(model)!: preserve unknown bitflag values #2089

Draft
wants to merge 6 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 66 additions & 15 deletions twilight-model/src/channel/flags.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use bitflags::bitflags;
use serde::{
de::{Deserialize, Deserializer},
ser::{Serialize, Serializer},
};
use serde::{Deserialize, Serialize};

bitflags! {
#[allow(clippy::unsafe_derive_deserialize)]
#[derive(Deserialize, Serialize)]
#[serde(transparent)]
pub struct ChannelFlags: u64 {
/// Channel is pinned in a forum.
const PINNED = 1 << 1;
Expand All @@ -13,17 +13,68 @@ bitflags! {
}
}

impl<'de> Deserialize<'de> for ChannelFlags {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Ok(Self::from_bits_truncate(u64::deserialize(deserializer)?))
}
}
#[cfg(test)]
mod tests {
use super::ChannelFlags;
use serde::{Deserialize, Serialize};
use serde_test::Token;
use static_assertions::{assert_impl_all, const_assert_eq};
use std::{
fmt::{Binary, Debug, LowerHex, Octal, UpperHex},
hash::Hash,
ops::{
BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not, Sub, SubAssign,
},
};

assert_impl_all!(
ChannelFlags: Binary,
BitAnd,
BitAndAssign,
BitOr,
BitOrAssign,
BitXor,
BitXorAssign,
Clone,
Copy,
Debug,
Deserialize<'static>,
Eq,
Extend<ChannelFlags>,
FromIterator<ChannelFlags>,
Hash,
LowerHex,
Not,
Octal,
Ord,
PartialEq,
PartialOrd,
Send,
Serialize,
Sub,
SubAssign,
Sync,
UpperHex
);
const_assert_eq!(ChannelFlags::PINNED.bits(), 1 << 1);
const_assert_eq!(ChannelFlags::REQUIRE_TAG.bits(), 1 << 4);

impl Serialize for ChannelFlags {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u64(self.bits())
#[test]
fn serde() {
serde_test::assert_tokens(
&ChannelFlags::PINNED,
&[Token::U64(ChannelFlags::PINNED.bits())],
);
// Safety:
//
// Deserialization doesn't truncate unknown bits.
//
// `bitflags` requires unsafe code to create bitflags with unknown bits
// due to an unorthodox definition of unsafe:
//
// <https://github.com/bitflags/bitflags/issues/262>
#[allow(unsafe_code)]
let value = unsafe { ChannelFlags::from_bits_unchecked(1 << 63) };
serde_test::assert_de_tokens(&value, &[Token::U64(1 << 63)]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
serde_test::assert_de_tokens(&value, &[Token::U64(1 << 63)]);
serde_test::assert_tokens(&value, &[Token::U64(1 << 63)]);

}
}
86 changes: 63 additions & 23 deletions twilight-model/src/channel/message/flags.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use bitflags::bitflags;
use serde::{
de::{Deserialize, Deserializer},
ser::{Serialize, Serializer},
};
use serde::{Deserialize, Serialize};

bitflags! {
/// Flags to signal state and modify the look of a message.
#[allow(clippy::unsafe_derive_deserialize)]
#[derive(Deserialize, Serialize)]
#[serde(transparent)]
pub struct MessageFlags: u64 {
/// Has been published to subscribed channels via Channel Following.
const CROSSPOSTED = 1;
Expand Down Expand Up @@ -34,38 +34,78 @@ bitflags! {
}
}

impl<'de> Deserialize<'de> for MessageFlags {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Ok(Self::from_bits_truncate(u64::deserialize(deserializer)?))
}
}

impl Serialize for MessageFlags {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u64(self.bits())
}
}

#[cfg(test)]
mod tests {
use super::MessageFlags;
use serde::{Deserialize, Serialize};
use static_assertions::assert_impl_all;
use std::{fmt::Debug, hash::Hash};
use serde_test::Token;
use static_assertions::{assert_impl_all, const_assert_eq};
use std::{
fmt::{Binary, Debug, LowerHex, Octal, UpperHex},
hash::Hash,
ops::{
BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not, Sub, SubAssign,
},
};

assert_impl_all!(
MessageFlags: Copy,
MessageFlags: Binary,
BitAnd,
BitAndAssign,
BitOr,
BitOrAssign,
BitXor,
BitXorAssign,
Clone,
Copy,
Debug,
Deserialize<'static>,
Eq,
Extend<MessageFlags>,
FromIterator<MessageFlags>,
Hash,
LowerHex,
Not,
Octal,
Ord,
PartialEq,
PartialOrd,
Send,
Serialize,
Sync
Sub,
SubAssign,
Sync,
UpperHex
);
const_assert_eq!(MessageFlags::CROSSPOSTED.bits(), 1);
const_assert_eq!(MessageFlags::IS_CROSSPOST.bits(), 1 << 1);
const_assert_eq!(MessageFlags::SUPPRESS_EMBEDS.bits(), 1 << 2);
const_assert_eq!(MessageFlags::SOURCE_MESSAGE_DELETED.bits(), 1 << 3);
const_assert_eq!(MessageFlags::URGENT.bits(), 1 << 4);
const_assert_eq!(MessageFlags::HAS_THREAD.bits(), 1 << 5);
const_assert_eq!(MessageFlags::EPHEMERAL.bits(), 1 << 6);
const_assert_eq!(MessageFlags::LOADING.bits(), 1 << 7);
const_assert_eq!(
MessageFlags::FAILED_TO_MENTION_SOME_ROLES_IN_THREAD.bits(),
1 << 8
);

#[test]
fn serde() {
serde_test::assert_tokens(
&MessageFlags::CROSSPOSTED,
&[Token::U64(MessageFlags::CROSSPOSTED.bits())],
);
// Safety:
//
// Deserialization doesn't truncate unknown bits.
//
// `bitflags` requires unsafe code to create bitflags with unknown bits
// due to an unorthodox definition of unsafe:
//
// <https://github.com/bitflags/bitflags/issues/262>
#[allow(unsafe_code)]
let value = unsafe { MessageFlags::from_bits_unchecked(1 << 63) };
serde_test::assert_de_tokens(&value, &[Token::U64(1 << 63)]);
}
}
77 changes: 77 additions & 0 deletions twilight-model/src/gateway/intents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,80 @@ impl Serialize for Intents {
serializer.serialize_u64(self.bits())
}
}

#[cfg(test)]
mod tests {
#![allow(deprecated)]

use super::Intents;
use serde::{Deserialize, Serialize};
use serde_test::Token;
use static_assertions::{assert_impl_all, const_assert_eq};
use std::{
fmt::{Binary, Debug, LowerHex, Octal, UpperHex},
hash::Hash,
ops::{
BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not, Sub, SubAssign,
},
};

assert_impl_all!(
Intents: Binary,
BitAnd,
BitAndAssign,
BitOr,
BitOrAssign,
BitXor,
BitXorAssign,
Clone,
Copy,
Debug,
Deserialize<'static>,
Eq,
Extend<Intents>,
FromIterator<Intents>,
Hash,
LowerHex,
Not,
Octal,
Ord,
PartialEq,
PartialOrd,
Send,
Serialize,
Sub,
SubAssign,
Sync,
UpperHex
);
const_assert_eq!(Intents::GUILDS.bits(), 1);
const_assert_eq!(Intents::GUILD_MEMBERS.bits(), 1 << 1);
const_assert_eq!(Intents::GUILD_BANS.bits(), 1 << 2);
const_assert_eq!(Intents::GUILD_MODERATION.bits(), 1 << 2);
const_assert_eq!(Intents::GUILD_EMOJIS_AND_STICKERS.bits(), 1 << 3);
const_assert_eq!(Intents::GUILD_INTEGRATIONS.bits(), 1 << 4);
const_assert_eq!(Intents::GUILD_WEBHOOKS.bits(), 1 << 5);
const_assert_eq!(Intents::GUILD_INVITES.bits(), 1 << 6);
const_assert_eq!(Intents::GUILD_VOICE_STATES.bits(), 1 << 7);
const_assert_eq!(Intents::GUILD_PRESENCES.bits(), 1 << 8);
const_assert_eq!(Intents::GUILD_MESSAGES.bits(), 1 << 9);
const_assert_eq!(Intents::GUILD_MESSAGE_REACTIONS.bits(), 1 << 10);
const_assert_eq!(Intents::GUILD_MESSAGE_TYPING.bits(), 1 << 11);
const_assert_eq!(Intents::DIRECT_MESSAGES.bits(), 1 << 12);
const_assert_eq!(Intents::DIRECT_MESSAGE_REACTIONS.bits(), 1 << 13);
const_assert_eq!(Intents::DIRECT_MESSAGE_TYPING.bits(), 1 << 14);
const_assert_eq!(Intents::MESSAGE_CONTENT.bits(), 1 << 15);
const_assert_eq!(Intents::GUILD_SCHEDULED_EVENTS.bits(), 1 << 16);
const_assert_eq!(Intents::AUTO_MODERATION_CONFIGURATION.bits(), 1 << 20);
const_assert_eq!(Intents::AUTO_MODERATION_EXECUTION.bits(), 1 << 21);

#[test]
fn serde() {
serde_test::assert_tokens(
&Intents::MESSAGE_CONTENT,
&[Token::U64(Intents::MESSAGE_CONTENT.bits())],
);
// Deserialization truncates unknown bits.
serde_test::assert_de_tokens(&Intents::empty(), &[Token::U64(1 << 63)]);
}
}
83 changes: 68 additions & 15 deletions twilight-model/src/gateway/presence/activity_flags.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use bitflags::bitflags;
use serde::{
de::{Deserialize, Deserializer},
ser::{Serialize, Serializer},
};
use serde::{Deserialize, Serialize};

bitflags! {
#[allow(clippy::unsafe_derive_deserialize)]
#[derive(Deserialize, Serialize)]
#[serde(transparent)]
pub struct ActivityFlags: u64 {
const INSTANCE = 1;
const JOIN = 1 << 1;
Expand All @@ -18,17 +18,70 @@ bitflags! {
}
}

impl<'de> Deserialize<'de> for ActivityFlags {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Ok(Self::from_bits_truncate(u64::deserialize(deserializer)?))
}
}
#[cfg(test)]
mod tests {
use super::ActivityFlags;
use serde::{Deserialize, Serialize};
use serde_test::Token;
use static_assertions::{assert_impl_all, const_assert_eq};
use std::{
fmt::{Binary, Debug, LowerHex, Octal, UpperHex},
hash::Hash,
ops::{
BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not, Sub, SubAssign,
},
};

assert_impl_all!(
ActivityFlags: Binary,
BitAnd,
BitAndAssign,
BitOr,
BitOrAssign,
BitXor,
BitXorAssign,
Clone,
Copy,
Debug,
Deserialize<'static>,
Eq,
Extend<ActivityFlags>,
FromIterator<ActivityFlags>,
Hash,
LowerHex,
Not,
Octal,
Ord,
PartialEq,
PartialOrd,
Send,
Serialize,
Sub,
SubAssign,
Sync,
UpperHex
);
const_assert_eq!(ActivityFlags::INSTANCE.bits(), 1);
const_assert_eq!(ActivityFlags::JOIN.bits(), 1 << 1);
const_assert_eq!(ActivityFlags::SPECTATE.bits(), 1 << 2);
const_assert_eq!(ActivityFlags::JOIN_REQUEST.bits(), 1 << 3);
const_assert_eq!(ActivityFlags::SYNC.bits(), 1 << 4);
const_assert_eq!(ActivityFlags::PLAY.bits(), 1 << 5);
const_assert_eq!(ActivityFlags::PARTY_PRIVACY_FRIENDS.bits(), 1 << 6);
const_assert_eq!(ActivityFlags::PARTY_PRIVACY_VOICE_CHANNEL.bits(), 1 << 7);
const_assert_eq!(ActivityFlags::EMBEDDED.bits(), 1 << 8);

impl Serialize for ActivityFlags {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u64(self.bits())
#[test]
fn serde() {
serde_test::assert_tokens(
&ActivityFlags::EMBEDDED,
&[Token::U64(ActivityFlags::EMBEDDED.bits())],
);
// Deserialization doesn't truncate unknown bits.
//
// `bitflags` requires unsafe code to create bitflags with unknown bits.
zeylahellyer marked this conversation as resolved.
Show resolved Hide resolved
#[allow(unsafe_code)]
let value = unsafe { ActivityFlags::from_bits_unchecked(1 << 63) };
serde_test::assert_de_tokens(&value, &[Token::U64(1 << 63)]);
}
}
Loading