Skip to content

Commit

Permalink
Merge pull request #155 from ralexstokes/add-ord
Browse files Browse the repository at this point in the history
add `PartialOrd` and `Ord` to custom types
  • Loading branch information
ralexstokes committed May 4, 2024
2 parents 52cf042 + 394b646 commit 84ef2b7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ssz-rs/src/bitlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn byte_length(bound: usize) -> usize {
type BitlistInner = BitVec<u8, Lsb0>;

/// A homogenous collection of a variable number of boolean values.
#[derive(PartialEq, Eq, Clone)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct Bitlist<const N: usize>(BitlistInner);

impl<const N: usize> fmt::Debug for Bitlist<N> {
Expand Down
2 changes: 1 addition & 1 deletion ssz-rs/src/bitvector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type BitvectorInner = BitVec<u8, Lsb0>;
// bitvec::array::BitArray<T, {N / 8}> where T: BitRegister, [T; {N / 8}]: BitViewSized
//
// Refer: <https://stackoverflow.com/a/65462213>
#[derive(PartialEq, Eq, Clone)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct Bitvector<const N: usize>(BitvectorInner);

impl<const N: usize> fmt::Debug for Bitvector<N> {
Expand Down
25 changes: 23 additions & 2 deletions ssz-rs/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
};

/// A homogenous collection of a variable number of values.
#[derive(Clone)]
#[derive(PartialOrd, Ord, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde(transparent))]
pub struct List<T: Serializable, const N: usize> {
data: Vec<T>,
Expand Down Expand Up @@ -334,7 +334,7 @@ impl<'de, T: Serializable + serde::Deserialize<'de>, const N: usize> serde::Dese
#[cfg(test)]
mod tests {
use super::*;
use crate::{serialize, U256};
use crate::{lib::cmp::Ordering, serialize, U256};

const COUNT: usize = 32;

Expand Down Expand Up @@ -495,4 +495,25 @@ mod tests {
let path = &[0.into()];
crate::proofs::tests::compute_and_verify_proof_for_path(&data, path)
}

#[test]
fn test_ord() {
type L = List<u8, 4>;
let data = vec![1u8, 22];
let input = L::try_from(data).unwrap();

let mut other = L::default();
assert_eq!(input.cmp(&other), Ordering::Greater);

other.push(0);
assert_eq!(input.cmp(&other), Ordering::Greater);
other.push(0);
assert_eq!(input.cmp(&other), Ordering::Greater);
other.push(0);
assert_eq!(input.cmp(&other), Ordering::Greater);
other.push(0);
assert_eq!(input.cmp(&other), Ordering::Greater);
other[0] = 244;
assert_eq!(input.cmp(&other), Ordering::Less);
}
}
46 changes: 44 additions & 2 deletions ssz-rs/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
/// A homogenous collection of a fixed number of values.
///
/// NOTE: a `Vector` of length `0` is illegal.
#[derive(Clone)]
#[derive(PartialOrd, Ord, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde(transparent))]
pub struct Vector<T: Serializable, const N: usize> {
data: Vec<T>,
Expand Down Expand Up @@ -321,7 +321,7 @@ impl<'de, T: Serializable + serde::Deserialize<'de>, const N: usize> serde::Dese
#[cfg(test)]
mod tests {
use super::*;
use crate::{list::List, serialize, U256};
use crate::{lib::cmp::Ordering, list::List, serialize, U256};

const COUNT: usize = 32;

Expand Down Expand Up @@ -701,4 +701,46 @@ mod tests {
}
}
}

#[test]
fn test_ord() {
type V = Vector<U256, 7>;

let data = V::try_from(vec![
U256::from(23),
U256::from(34),
U256::from(45),
U256::from(56),
U256::from(67),
U256::from(78),
U256::from(11),
])
.unwrap();

let other = V::try_from(vec![
U256::from(23),
U256::from(34),
U256::from(45),
U256::from(56),
U256::from(67),
U256::from(78),
U256::from(11),
])
.unwrap();

assert_eq!(data.cmp(&other), Ordering::Equal);

let another = V::try_from(vec![
U256::from(23),
U256::from(34),
U256::from(45),
U256::from(56),
U256::from(67),
U256::from(78),
U256::from(12),
])
.unwrap();

assert_eq!(data.cmp(&another), Ordering::Less);
}
}

0 comments on commit 84ef2b7

Please sign in to comment.