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 all 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
36 changes: 15 additions & 21 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,21 +13,6 @@ 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)?))
}
}

impl Serialize for ChannelFlags {
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::ChannelFlags;
Expand Down Expand Up @@ -80,7 +65,16 @@ mod tests {
&ChannelFlags::PINNED,
&[Token::U64(ChannelFlags::PINNED.bits())],
);
// Deserialization truncates unknown bits.
serde_test::assert_de_tokens(&ChannelFlags::empty(), &[Token::U64(1 << 63)]);
// 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)]);

}
}
36 changes: 15 additions & 21 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,21 +34,6 @@ 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;
Expand Down Expand Up @@ -111,7 +96,16 @@ mod tests {
&MessageFlags::CROSSPOSTED,
&[Token::U64(MessageFlags::CROSSPOSTED.bits())],
);
// Deserialization truncates unknown bits.
serde_test::assert_de_tokens(&MessageFlags::empty(), &[Token::U64(1 << 63)]);
// 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)]);
}
}
36 changes: 15 additions & 21 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,21 +18,6 @@ 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)?))
}
}

impl Serialize for ActivityFlags {
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::ActivityFlags;
Expand Down Expand Up @@ -92,7 +77,16 @@ mod tests {
&ActivityFlags::EMBEDDED,
&[Token::U64(ActivityFlags::EMBEDDED.bits())],
);
// Deserialization truncates unknown bits.
serde_test::assert_de_tokens(&ActivityFlags::empty(), &[Token::U64(1 << 63)]);
// 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 { ActivityFlags::from_bits_unchecked(1 << 63) };
serde_test::assert_de_tokens(&value, &[Token::U64(1 << 63)]);
}
}
32 changes: 27 additions & 5 deletions twilight-model/src/guild/permissions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use bitflags::bitflags;
use serde::{
de::{Deserialize, Deserializer, Error as DeError, Visitor},
ser::{Serialize, Serializer},
de::{Deserializer, Error as DeError, Visitor},
ser::Serializer,
Deserialize, Serialize,
};
use std::fmt::{Formatter, Result as FmtResult};

Expand Down Expand Up @@ -78,7 +79,19 @@ impl<'de> Visitor<'de> for PermissionsVisitor {
}

fn visit_u64<E: DeError>(self, v: u64) -> Result<Self::Value, E> {
Ok(Permissions::from_bits_truncate(v))
// Safety:
//
// `bitflags` requires unsafe code to create bitflags with unknown bits
// due to an unorthodox definition of unsafe:
//
// <https://github.com/bitflags/bitflags/issues/262>
//
// This is, funnily enough, how bitflags itself implements
// deserializers.
#[allow(unsafe_code)]
let permissions = unsafe { Permissions::from_bits_unchecked(v) };

Ok(permissions)
}

fn visit_str<E: DeError>(self, v: &str) -> Result<Self::Value, E> {
Expand Down Expand Up @@ -191,7 +204,16 @@ mod tests {
#[test]
fn serde() {
serde_test::assert_tokens(&Permissions::CREATE_INVITE, &[Token::Str("1")]);
// Deserialization truncates unknown bits.
serde_test::assert_de_tokens(&Permissions::empty(), &[Token::Str("9223372036854775808")]);
// 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 { Permissions::from_bits_unchecked(1 << 63) };
serde_test::assert_de_tokens(&value, &[Token::Str("9223372036854775808")]);
}
}
36 changes: 15 additions & 21 deletions twilight-model/src/guild/system_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 SystemChannelFlags: u64 {
/// Suppress member join notifications.
const SUPPRESS_JOIN_NOTIFICATIONS = 1;
Expand All @@ -21,21 +21,6 @@ bitflags! {
}
}

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

impl Serialize for SystemChannelFlags {
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::SystemChannelFlags;
Expand Down Expand Up @@ -109,7 +94,16 @@ mod tests {
SystemChannelFlags::SUPPRESS_JOIN_NOTIFICATION_REPLIES.bits(),
)],
);
// Deserialization truncates unknown bits.
serde_test::assert_de_tokens(&SystemChannelFlags::empty(), &[Token::U64(1 << 63)]);
// 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 { SystemChannelFlags::from_bits_unchecked(1 << 63) };
serde_test::assert_de_tokens(&value, &[Token::U64(1 << 63)]);
}
}
36 changes: 15 additions & 21 deletions twilight-model/src/oauth/application_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 ApplicationFlags: u64 {
/// Intent required for bots in 100 guilds or more to receive
/// [`PresenceUpdate`] events.
Expand Down Expand Up @@ -48,21 +48,6 @@ bitflags! {
}
}

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

impl Serialize for ApplicationFlags {
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::ApplicationFlags;
Expand Down Expand Up @@ -131,7 +116,16 @@ mod tests {
&ApplicationFlags::GATEWAY_MESSAGE_CONTENT,
&[Token::U64(ApplicationFlags::GATEWAY_MESSAGE_CONTENT.bits())],
);
// Deserialization truncates unknown bits.
serde_test::assert_de_tokens(&ApplicationFlags::empty(), &[Token::U64(1 << 63)]);
// 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 { ApplicationFlags::from_bits_unchecked(1 << 63) };
serde_test::assert_de_tokens(&value, &[Token::U64(1 << 63)]);
}
}
36 changes: 15 additions & 21 deletions twilight-model/src/user/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 UserFlags: u64 {
/// Discord Employee.
const STAFF = 1;
Expand Down Expand Up @@ -45,21 +45,6 @@ bitflags! {
}
}

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

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

#[cfg(test)]
mod tests {
#![allow(deprecated)]
Expand Down Expand Up @@ -129,7 +114,16 @@ mod tests {
&UserFlags::PARTNER,
&[Token::U64(UserFlags::PARTNER.bits())],
);
// Deserialization truncates unknown bits.
serde_test::assert_de_tokens(&UserFlags::empty(), &[Token::U64(1 << 63)]);
// 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 { UserFlags::from_bits_unchecked(1 << 63) };
serde_test::assert_de_tokens(&value, &[Token::U64(1 << 63)]);
}
}