pub trait HighLevel: Clone + Debug {
type LowLevel: Parcel;
// Required methods
fn into_low_level(self) -> Self::LowLevel;
fn from_low_level(
value: Self::LowLevel,
subsequent_reader: &mut dyn Read,
settings: &Settings,
hints: &mut Hints,
) -> Result<Self, Error>;
}
๐Deprecated: a badly designed abstraction. its blanket implementation often unnecessarily conflicts with custom user Parcel blanket impls. consider copying if you use it
Expand description
A high level abstraction over a lower-level Parcel
.
Automatically marshals values to and from the high level and low level types for serialization.
ยงExample
#[macro_use] extern crate protocol_derive;
extern crate protocol;
use protocol::Parcel;
#[derive(Protocol)]
pub struct RawPacket {
opcode: u8,
magic_number: u8,
payload: Vec<u8>,
version: (u8, u8),
}
#[derive(Debug, Clone)]
pub enum Login {
Success {
message: String,
},
Failure {
code: FailureCode,
response: String,
},
}
impl protocol::HighLevel for Login {
type LowLevel = RawPacket;
fn into_low_level(self) -> RawPacket {
match self {
Login::Success { message } => {
RawPacket {
opcode: 0,
magic_number: 0xaa,
payload: message.into_bytes(),
version: (11, 0),
}
},
Login::Failure { .. } => unimplemented!(),
}
}
fn from_low_level(low_level: RawPacket,
_: &mut std::io::Read,
_: &protocol::Settings,
_: &mut protocol::hint::Hints)
-> Result<Self, protocol::Error> {
match low_level.opcode {
0 => Ok(Login::Success { message: String::from_utf8(low_level.payload).unwrap() }),
1 => Ok(Login::Failure {
code: FailureCode::MyDogAteMyHomework,
response: String::from_utf8(low_level.payload[1..].to_owned()).unwrap() }),
_ => unreachable!(),
}
}
}
#[derive(Debug, Clone)]
pub enum FailureCode {
MyDogAteMyHomework,
}
let high_level = Login::Success { message: "Hi!".to_owned() };
assert_eq!(11, high_level.raw_bytes(&protocol::Settings::default()).unwrap().len());
Required Associated Typesยง
Required Methodsยง
Sourcefn into_low_level(self) -> Self::LowLevel
๐Deprecated: a badly designed abstraction. its blanket implementation often unnecessarily conflicts with custom user Parcel blanket impls. consider copying if you use it
fn into_low_level(self) -> Self::LowLevel
Converts this high level type into its lower-level representation.
Sourcefn from_low_level(
value: Self::LowLevel,
subsequent_reader: &mut dyn Read,
settings: &Settings,
hints: &mut Hints,
) -> Result<Self, Error>
๐Deprecated: a badly designed abstraction. its blanket implementation often unnecessarily conflicts with custom user Parcel blanket impls. consider copying if you use it
fn from_low_level( value: Self::LowLevel, subsequent_reader: &mut dyn Read, settings: &Settings, hints: &mut Hints, ) -> Result<Self, Error>
Creates a high-level abstraction over a lower-level value.
The method has access to the reader post-parsing of the low level type. It is not necessary to use this if not needed.
Dyn Compatibilityยง
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.