Skip to content

Commit

Permalink
feat(precompile-utils): implement helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
RomarQ authored and gonzamontiel committed Aug 21, 2024
1 parent 7c53ae0 commit 2097ee5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
16 changes: 15 additions & 1 deletion precompiles/src/solidity/codec/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Kind for StringKind {
/// The `bytes/string` type of Solidity.
/// It is different from `Vec<u8>` which will be serialized with padding for each `u8` element
/// of the array, while `Bytes` is tightly packed.
#[derive(Debug, Eq, PartialEq)]
#[derive(Debug)]
pub struct BoundedBytesString<K, S> {
data: Vec<u8>,
_phantom: PhantomData<(K, S)>,
Expand All @@ -68,6 +68,20 @@ impl<K: Kind, S: Get<u32>> Clone for BoundedBytesString<K, S> {
}
}

impl<K1, S1, K2, S2> PartialEq<BoundedBytesString<K2, S2>> for BoundedBytesString<K1, S1> {
fn eq(&self, other: &BoundedBytesString<K2, S2>) -> bool {
self.data.eq(&other.data)
}
}

impl<K, S> Eq for BoundedBytesString<K, S> {}

impl<K, S> Default for BoundedBytesString<K, S> {
fn default() -> Self {
Vec::default().into()
}
}

impl<K, S: Get<u32>> BoundedBytesString<K, S> {
pub fn as_bytes(&self) -> &[u8] {
&self.data
Expand Down
15 changes: 15 additions & 0 deletions precompiles/src/solidity/codec/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,18 @@ impl<T, S> From<BoundedVec<T, S>> for Vec<T> {
value.inner
}
}

impl<T, S> Default for BoundedVec<T, S> {
fn default() -> Self {
Self {
inner: Default::default(),
_phantom: PhantomData,
}
}
}

impl<T, S> BoundedVec<T, S> {
pub fn len(&self) -> usize {
self.inner.len()
}
}

0 comments on commit 2097ee5

Please sign in to comment.