-
Notifications
You must be signed in to change notification settings - Fork 645
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
ccd6f57
commit 8bb952f
Showing
14 changed files
with
280 additions
and
153 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
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
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
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 |
---|---|---|
@@ -1,68 +1,28 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::any::TypeId; | ||
use std::collections::hash_map::DefaultHasher; | ||
use std::hash::{Hash, Hasher}; | ||
|
||
use once_cell::sync::Lazy; | ||
use std::sync::Mutex; | ||
|
||
#[derive(Clone, Serialize, Deserialize)] | ||
pub struct FieldInfo { | ||
pub name: String, | ||
pub type_name: String, | ||
pub hash: u64, | ||
} | ||
|
||
#[derive(Clone, Serialize, Deserialize)] | ||
pub struct ProtocolStructInfo { | ||
pub name: String, | ||
pub hash: u64, | ||
pub fields: Vec<FieldInfo>, | ||
} | ||
|
||
pub trait ProtocolStruct { | ||
fn type_name() -> String { | ||
std::any::type_name::<Self>().to_string() | ||
} | ||
|
||
fn calculate_hash() -> u64 { | ||
let mut hasher = DefaultHasher::new(); | ||
Self::type_name().hash(&mut hasher); | ||
hasher.finish() | ||
} | ||
|
||
fn get_info() -> ProtocolStructInfo { | ||
ProtocolStructInfo { name: Self::type_name(), hash: Self::calculate_hash(), fields: vec![] } | ||
} | ||
use serde::{Deserialize, Serialize}; | ||
use std::sync::Arc; | ||
|
||
#[derive(Copy, Clone, Serialize)] | ||
pub enum ProtocolStructInfo { | ||
Struct { | ||
name: &'static str, | ||
fields: &'static [(&'static str, &'static str)], | ||
}, | ||
Enum { | ||
name: &'static str, | ||
variants: &'static [(&'static str, Option<&'static [(&'static str, &'static str)]>)], | ||
}, | ||
} | ||
|
||
// Implement ProtocolStruct for primitive types | ||
macro_rules! impl_protocol_struct_for_primitive { | ||
($($t:ty),*) => { | ||
$( | ||
impl ProtocolStruct for $t {} | ||
)* | ||
impl ProtocolStructInfo { | ||
pub fn name(&self) -> &'static str { | ||
match self { | ||
ProtocolStructInfo::Struct { name, .. } => name, | ||
ProtocolStructInfo::Enum { name, .. } => name, | ||
} | ||
} | ||
} | ||
|
||
impl_protocol_struct_for_primitive!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, bool, String); | ||
|
||
// Implement for arrays | ||
impl<T: ProtocolStruct, const N: usize> ProtocolStruct for [T; N] {} | ||
|
||
pub fn calculate_struct_hash<T: 'static>() -> u64 { | ||
let mut hasher = DefaultHasher::new(); | ||
TypeId::of::<T>().hash(&mut hasher); | ||
hasher.finish() | ||
} | ||
|
||
static PROTOCOL_STRUCTS: Lazy<Mutex<Vec<ProtocolStructInfo>>> = | ||
Lazy::new(|| Mutex::new(Vec::new())); | ||
inventory::collect!(ProtocolStructInfo); | ||
|
||
pub fn register_protocol_struct(info: ProtocolStructInfo) { | ||
PROTOCOL_STRUCTS.lock().unwrap().push(info); | ||
} | ||
|
||
pub fn collect_protocol_structs() -> Vec<ProtocolStructInfo> { | ||
PROTOCOL_STRUCTS.lock().unwrap().clone() | ||
} | ||
pub trait ProtocolStruct {} |
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
Oops, something went wrong.