-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5efa8db
commit 6c5fd1b
Showing
5 changed files
with
83 additions
and
138 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
pub mod content_container; | ||
mod macros; | ||
pub mod message_status; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#[macro_export] | ||
macro_rules! serde_enum_as_u8 { | ||
($enum_name:ident) => { | ||
// https://doc.rust-lang.org/reference/items/enumerations.html?search=#pointer-casting | ||
impl $enum_name { | ||
fn discriminant(&self) -> u8 { | ||
// This is safe if the enum only contains primitive types | ||
let pointer = self as *const Self as *const u8; | ||
unsafe { *pointer } | ||
} | ||
} | ||
|
||
impl Serialize for $enum_name { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
match self { | ||
Self::Custom(custom) => *custom, | ||
known => known.discriminant(), | ||
} | ||
.serialize(serializer) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for $enum_name { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: de::Deserializer<'de>, | ||
{ | ||
let value = u8::deserialize(deserializer)?; | ||
|
||
// This assumes that Custom is the last variant of the enum | ||
let variant = if value < Self::Custom(0).discriminant() { | ||
// The value corresponds to the discriminant of the enum | ||
let result = unsafe { *(&value as *const u8 as *const Self) }; | ||
assert_eq!(result.discriminant(), value); | ||
|
||
result | ||
} else { | ||
Self::Custom(value) | ||
}; | ||
|
||
Ok(variant) | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters