Implement protocol non-var ints
This commit is contained in:
parent
85743b6e16
commit
eb07412a5e
|
@ -8,6 +8,12 @@ version = "1.4.3"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
|
||||
|
||||
[[package]]
|
||||
name = "paste"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0c520e05135d6e763148b6426a837e239041653ba7becd2e538c076c738025fc"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.39"
|
||||
|
@ -22,6 +28,7 @@ name = "protocol"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"paste",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -5,3 +5,4 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
byteorder = "1"
|
||||
paste = "1"
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use byteorder::{ReadBytesExt, WriteBytesExt};
|
||||
use paste::paste;
|
||||
use std::io::{Read, Result as IoResult, Write};
|
||||
use byteorder::ReadBytesExt;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
pub trait Encode {
|
||||
|
@ -10,6 +11,55 @@ pub trait Decode: Sized {
|
|||
fn decode(reader: &mut impl Read) -> IoResult<Self>;
|
||||
}
|
||||
|
||||
impl Encode for u8 {
|
||||
fn encode(&self, writer: &mut impl Write) -> IoResult<()> {
|
||||
writer.write_u8(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode for u8 {
|
||||
fn decode(reader: &mut impl Read) -> IoResult<Self> {
|
||||
reader.read_u8()
|
||||
}
|
||||
}
|
||||
|
||||
impl Encode for i8 {
|
||||
fn encode(&self, writer: &mut impl Write) -> IoResult<()> {
|
||||
writer.write_i8(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode for i8 {
|
||||
fn decode(reader: &mut impl Read) -> IoResult<Self> {
|
||||
reader.read_i8()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_ordered_int (
|
||||
($type: ident) => (
|
||||
impl Encode for $type {
|
||||
fn encode(&self, writer: &mut impl Write) -> IoResult<()> {
|
||||
paste! { writer.[<write_ $type>]::<byteorder::LittleEndian>(*self) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode for $type {
|
||||
fn decode(reader: &mut impl Read) -> IoResult<Self> {
|
||||
paste! { reader.[<read_ $type>]::<byteorder::LittleEndian>() }
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
impl_ordered_int!(u16);
|
||||
impl_ordered_int!(u32);
|
||||
impl_ordered_int!(u64);
|
||||
impl_ordered_int!(u128);
|
||||
impl_ordered_int!(i16);
|
||||
impl_ordered_int!(i32);
|
||||
impl_ordered_int!(i64);
|
||||
impl_ordered_int!(i128);
|
||||
|
||||
#[repr(transparent)]
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Var<T>(pub T);
|
||||
|
@ -134,6 +184,44 @@ mod tests {
|
|||
assert_eq!(original, decoded, "Round-trip encoded values do not match!");
|
||||
}
|
||||
|
||||
mod int {
|
||||
use super::*;
|
||||
|
||||
macro_rules! test_int (
|
||||
($type: ident) => (
|
||||
mod $type {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn min() {
|
||||
test_roundtrip($type::MIN);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn max() {
|
||||
test_roundtrip($type::MAX);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one() {
|
||||
test_roundtrip(1 as $type);
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
test_int!(u8);
|
||||
test_int!(u16);
|
||||
test_int!(u32);
|
||||
test_int!(u64);
|
||||
test_int!(u128);
|
||||
test_int!(i8);
|
||||
test_int!(i16);
|
||||
test_int!(i32);
|
||||
test_int!(i64);
|
||||
test_int!(i128);
|
||||
}
|
||||
|
||||
mod var {
|
||||
use super::*;
|
||||
|
||||
|
|
Loading…
Reference in New Issue