Skip to content

Commit

Permalink
Merge pull request #45 from EspressoSystems/abdul/add-derive-macro
Browse files Browse the repository at this point in the history
add PartialOrd and Ord to Version
  • Loading branch information
imabdulbasit authored May 29, 2024
2 parents 50c93e6 + c7affde commit 5acf179
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "vbs"
version = "0.1.4"
version = "0.1.5"
edition = "2021"
rust-version = "1.75.0"
authors = ["Espresso Systems <[email protected]>"]
Expand Down
8 changes: 2 additions & 6 deletions src/binary_serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@ pub trait BinarySerializer {

// TODO: `Versioned` trait

fn serialize_no_version<T: ?Sized>(value: &T) -> Result<Vec<u8>>
where
T: Serialize;
fn serialize_no_version<T: ?Sized + Serialize>(value: &T) -> Result<Vec<u8>>;

fn deserialize_no_version<'a, T>(bytes: &'a [u8]) -> Result<T>
where
T: Deserialize<'a>;

fn serialize<T: ?Sized>(value: &T) -> Result<Vec<u8>>
where
T: Serialize;
fn serialize<T: ?Sized + Serialize>(value: &T) -> Result<Vec<u8>>;

fn deserialize<'a, T>(bytes: &'a [u8]) -> Result<T>
where
Expand Down
28 changes: 6 additions & 22 deletions src/bincode_serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ pub struct BincodeSerializer<VER: StaticVersionType>(PhantomData<VER>);
impl<VER: StaticVersionType> BinarySerializer for BincodeSerializer<VER> {
const MAJOR: u16 = VER::MAJOR;
const MINOR: u16 = VER::MINOR;
fn serialize_no_version<T: ?Sized>(value: &T) -> Result<Vec<u8>>
where
T: Serialize,
{
fn serialize_no_version<T: ?Sized + Serialize>(value: &T) -> Result<Vec<u8>> {
Ok(bincode::serialize(value)?)
}

Expand All @@ -26,10 +23,7 @@ impl<VER: StaticVersionType> BinarySerializer for BincodeSerializer<VER> {
Ok(bincode::deserialize(bytes)?)
}

fn serialize<T: ?Sized>(value: &T) -> Result<Vec<u8>>
where
T: Serialize,
{
fn serialize<T: ?Sized + Serialize>(value: &T) -> Result<Vec<u8>> {
let mut vec = Self::version().serialize();
bincode::serialize_into(vec.by_ref(), value)?;
Ok(vec)
Expand Down Expand Up @@ -80,17 +74,13 @@ pub trait VersionedBinarySerializer {

// TODO: `Versioned` trait

fn serialize_no_version<T: ?Sized>(value: &T) -> Result<Vec<u8>>
where
T: Serialize + Versioned;
fn serialize_no_version<T: ?Sized + Serialize + Versioned>(value: &T) -> Result<Vec<u8>>;

fn deserialize_no_version<'a, T>(bytes: &'a [u8]) -> Result<T>
where
T: Deserialize<'a> + Versioned;

fn serialize<T: ?Sized>(value: &T) -> Result<Vec<u8>>
where
T: Serialize + Versioned;
fn serialize<T: ?Sized + Serialize + Versioned>(value: &T) -> Result<Vec<u8>>;

fn deserialize<'a, T>(bytes: &'a [u8]) -> Result<T>
where
Expand All @@ -103,10 +93,7 @@ impl<VER: StaticVersionType> VersionedBinarySerializer for VersionedBincodeSeria
const MINOR: u16 = VER::MINOR;
type StaticVersion = VER;

fn serialize_no_version<T: ?Sized>(value: &T) -> Result<Vec<u8>>
where
T: Serialize + Versioned,
{
fn serialize_no_version<T: ?Sized + Serialize + Versioned>(value: &T) -> Result<Vec<u8>> {
#[allow(clippy::let_unit_value)]
let _ = VersionChecker::<VER, T>::VERSION_MISMATCH;

Expand All @@ -123,10 +110,7 @@ impl<VER: StaticVersionType> VersionedBinarySerializer for VersionedBincodeSeria
Ok(bincode::deserialize(bytes)?)
}

fn serialize<T: ?Sized>(value: &T) -> Result<Vec<u8>>
where
T: Serialize + Versioned,
{
fn serialize<T: ?Sized + Serialize + Versioned>(value: &T) -> Result<Vec<u8>> {
#[allow(clippy::let_unit_value)]
let _ = VersionChecker::<VER, T>::VERSION_MISMATCH;

Expand Down
22 changes: 21 additions & 1 deletion src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use core::fmt::Debug;
use derive_more::Display;
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug, Deserialize, Display, Eq, Hash, PartialEq, Serialize)]
#[derive(
Clone, Copy, Debug, Deserialize, Display, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize,
)]
/// Type for protocol version number
#[display(fmt = "{major}.{minor}")]
pub struct Version {
Expand Down Expand Up @@ -89,6 +91,24 @@ mod private {
mod test {
use super::*;

#[test]
fn test_compare_version() {
let v1 = Version { major: 1, minor: 3 };
let v2 = Version { major: 1, minor: 2 };

assert!(v1 > v2);

let v3 = Version { major: 2, minor: 3 };
let v4 = Version { major: 1, minor: 3 };

assert!(v3 > v4);

let v5 = Version { major: 2, minor: 2 };
let v6 = Version { major: 1, minor: 3 };

assert!(v5 > v6);
}

#[test]
fn test_version_display() {
assert_eq!("1.2", Version { major: 1, minor: 2 }.to_string());
Expand Down

0 comments on commit 5acf179

Please sign in to comment.