I'm working on an application that will be messaging over TCP.
pub struct Message<T> where T: IsMessageType{
message_type: T,
length: u16,
payload: [u8],
}
I want message types to be either IncomingMessageType or OutgoingMessageType. Both of these types are enums that represent data types that can be passed back and forth through the TCP stream, however because the operations that can be done on them are different I don't want to represent them as a single type. Is there an idiomatic way to represent this constraint?
My current implementation is below.
//Define empty trait
pub trait IsMessageType{
}
pub enum IncomingMessageType {
GetGPPVersion,
GetDSPVersion,
GetFPGAVersion,
SetGPPVersion(Version),
SetDSPVersion(Version),
SetFPGAVersion(Version),
StopGPP,
StartGPP,
DownloadBinaries,
}
//Implement the trait for the incoming message type
impl IsMessageType for IncomingMessageType{
}
pub enum OutgoingMessageType{
GetGPPVersion,
GetDSPVersion,
GetFPGAVersion,
ACK,
NACK,
}
//And the outgoing message type
impl IsMessageType for OutgoingMessageType{
}
This satisfies the type bound, but I'm wondering if there's a better way to do this