// ... PRIMITIVE TYPES ...
use crate::utils::*;
use crate::uuid::UUID4;
use crate::*;
#[cfg(test)]
use crate::protocol::TestRandom;
// bool
impl Serialize for bool {
fn mc_serialize<S: Serializer>(&self, to: &mut S) -> SerializeResult {
to.serialize_byte(if *self { 1 } else { 0 })
}
}
impl Deserialize for bool {
fn mc_deserialize(data: &[u8]) -> DeserializeResult<'_, Self> {
read_one_byte(data)?.try_map(move |b| match b {
0x00 => Ok(false),
0x01 => Ok(true),
other => Err(DeserializeErr::InvalidBool(other)),
})
}
}
#[cfg(test)]
impl TestRandom for bool {
fn test_gen_random() -> Self {
rand::random()
}
}
// u8
impl Serialize for u8 {
fn mc_serialize<S: Serializer>(&self, to: &mut S) -> SerializeResult {
to.serialize_byte(*self)
}
}
impl Deserialize for u8 {
fn mc_deserialize(data: &[u8]) -> DeserializeResult<'_, Self> {
read_one_byte(data)
}
}
#[cfg(test)]
impl TestRandom for u8 {
fn test_gen_random() -> Self {
rand::random()
}
}
// i8
impl Serialize for i8 {
fn mc_serialize<S: Serializer>(&self, to: &mut S) -> SerializeResult {
to.serialize_byte(*self as u8)
}
}
impl Deserialize for i8 {
fn mc_deserialize(data: &[u8]) -> DeserializeResult<'_, Self> {
Ok(read_one_byte(data)?.map(move |byte| byte as i8))
}
}
#[cfg(test)]
impl TestRandom for i8 {
fn test_gen_random() -> Self {
rand::random()
}
}
// u16
impl Serialize for u16 {
fn mc_serialize<S: Serializer>(&self, to: &mut S) ->