Skip to content

Commit

Permalink
Make Bytes map to Bytes in SolType (#545)
Browse files Browse the repository at this point in the history
* feat: Bytes maps to Bytes

* feat: vec-like bytes macro

* lint: clippy, expand test, move to bytes macro usage

* lint: fmt

* fix: trybuild overwrite

---------

Co-authored-by: James <james@prestwi.ch>
  • Loading branch information
rachel-bousfield and prestwich authored Mar 13, 2024
1 parent db41140 commit 9ba8081
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 22 deletions.
28 changes: 28 additions & 0 deletions crates/primitives/src/bits/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,12 +806,40 @@ macro_rules! bytes {
const STATIC_BYTES: &'static [u8] = &$crate::hex!($($s)+);
$crate::Bytes::from_static(STATIC_BYTES)
}};

[$($inner:literal),+ $(,)?] => {{
// force const eval
const STATIC_BYTES: &'static [u8] = &[$($inner),+];
$crate::Bytes::from_static(STATIC_BYTES)
}};

[$inner:literal; $size:literal] => {{
// force const eval
const STATIC_BYTES: &'static [u8; $size] = &[$inner; $size];
$crate::Bytes::from_static(STATIC_BYTES)
}};
}

#[cfg(test)]
mod tests {
use crate::{hex, Address, Bytes, FixedBytes};

#[test]
fn bytes_macros() {
static B1: Bytes = bytes!("010203040506070809");
static B2: Bytes = bytes![1, 2, 3, 4, 5, 6, 7, 8, 9];
static B3: Bytes = bytes![1, 2, 3, 4, 5, 6, 7, 8, 9,];

assert_eq!(B1, B2);
assert_eq!(B1, B3);

static B4: Bytes = bytes!("0000");
static B5: Bytes = bytes![0; 2];
static B6: Bytes = bytes![0, 0];
assert_eq!(B4, B5);
assert_eq!(B4, B6);
}

#[test]
fn fixed_byte_macros() {
const A0: Address = address!();
Expand Down
2 changes: 1 addition & 1 deletion crates/sol-macro/src/expand/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub(crate) fn rec_expand_rust_type(ty: &Type, crates: &ExternCrates, tokens: &mu
Type::Address(span, _) => quote_spanned! {span=> #alloy_sol_types::private::Address },
Type::Bool(span) => return Ident::new("bool", span).to_tokens(tokens),
Type::String(span) => quote_spanned! {span=> #alloy_sol_types::private::String },
Type::Bytes(span) => quote_spanned! {span=> #alloy_sol_types::private::Vec<u8> },
Type::Bytes(span) => quote_spanned! {span=> #alloy_sol_types::private::Bytes },

Type::FixedBytes(span, size) => {
assert!(size.get() <= 32);
Expand Down
8 changes: 4 additions & 4 deletions crates/sol-types/src/abi/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ pub fn decode_sequence<'de, T: TokenSeq<'de>>(data: &'de [u8], validate: bool) -
mod tests {
use crate::{sol, sol_data, utils::pad_usize, SolType, SolValue};
use alloc::string::ToString;
use alloy_primitives::{address, hex, Address, B256, U256};
use alloy_primitives::{address, bytes, hex, Address, B256, U256};

#[test]
fn dynamic_array_of_dynamic_arrays() {
Expand Down Expand Up @@ -729,7 +729,7 @@ mod tests {

let ty = Ty {
arr: [[0x11u8; 32].into(), [0x22u8; 32].into(), [0x33u8; 32].into()],
r#dyn: vec![0x44u8; 4],
r#dyn: bytes![0x44u8; 4],
};
let encoded = hex!(
"0000000000000000000000000000000000000000000000000000000000000020"
Expand Down Expand Up @@ -757,8 +757,8 @@ mod tests {
}

let ty = Ty {
arr: [vec![0x11u8; 32], vec![0x22u8; 32], vec![0x33u8; 32]],
r#dyn: vec![0x44u8; 4],
arr: [bytes![0x11u8; 32], bytes![0x22u8; 32], bytes![0x33u8; 32]],
r#dyn: bytes![0x44u8; 4],
};
let encoded = hex!(
"0000000000000000000000000000000000000000000000000000000000000020" // struct offset
Expand Down
9 changes: 7 additions & 2 deletions crates/sol-types/src/abi/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
Result, Word,
};
use alloc::vec::Vec;
use alloy_primitives::{utils::vec_try_with_capacity, FixedBytes, I256, U256};
use alloy_primitives::{utils::vec_try_with_capacity, Bytes, FixedBytes, I256, U256};
use core::fmt;

mod sealed {
Expand Down Expand Up @@ -497,14 +497,19 @@ impl<'de: 'a, 'a> Token<'de> for PackedSeqToken<'a> {
}

impl PackedSeqToken<'_> {
/// Consumes `self` to return the underlying vector.
/// Instantiate a new [`Vec`] by copying the underlying slice.
// https://github.com/rust-lang/rust-clippy/issues/4979
#[allow(clippy::missing_const_for_fn)]
#[inline]
pub fn into_vec(self) -> Vec<u8> {
self.0.to_vec()
}

/// Instantiate a new [`Bytes`] by copying the underlying slice.
pub fn into_bytes(self) -> Bytes {
Bytes::copy_from_slice(self.0)
}

/// Returns a reference to the slice.
#[inline]
pub const fn as_slice(&self) -> &[u8] {
Expand Down
8 changes: 4 additions & 4 deletions crates/sol-types/src/types/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use crate::{abi::token::*, private::SolTypeValue, utils, SolType, Word};
use alloc::{string::String as RustString, vec::Vec};
use alloy_primitives::{
keccak256, Address as RustAddress, FixedBytes as RustFixedBytes, Function as RustFunction,
I256, U256,
keccak256, Address as RustAddress, Bytes as RustBytes, FixedBytes as RustFixedBytes,
Function as RustFunction, I256, U256,
};
use core::{borrow::Borrow, fmt::*, hash::Hash, marker::PhantomData, ops::*};

Expand Down Expand Up @@ -309,7 +309,7 @@ impl<T: ?Sized + AsRef<[u8]>> SolTypeValue<Bytes> for T {
}

impl SolType for Bytes {
type RustType = Vec<u8>;
type RustType = RustBytes;
type Token<'a> = PackedSeqToken<'a>;

const SOL_NAME: &'static str = "bytes";
Expand All @@ -322,7 +322,7 @@ impl SolType for Bytes {

#[inline]
fn detokenize(token: Self::Token<'_>) -> Self::RustType {
token.into_vec()
token.into_bytes()
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/sol-types/tests/doctests/events.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::assertions_on_constants)]

use alloy_primitives::{hex, keccak256, Log, B256, U256};
use alloy_primitives::{hex, keccak256, Bytes, Log, B256, U256};
use alloy_rlp::{Decodable, Encodable};
use alloy_sol_types::{abi::token::WordToken, sol, SolEvent};

Expand Down Expand Up @@ -31,7 +31,7 @@ fn event() {
a: [0x11; 32].into(),
b: U256::from(1u64),
c: keccak256("Hello World"),
d: Vec::new(),
d: Bytes::default(),
};
// topics are `(SELECTOR, a, keccak256(c))`
assert_eq!(
Expand Down Expand Up @@ -68,7 +68,7 @@ fn event_rlp_roundtrip() {
a: [0x11; 32].into(),
b: U256::from(1u64),
c: keccak256("Hello World"),
d: Vec::new(),
d: Vec::new().into(),
};

let rlpable_log = Log::<MyEvent>::new_from_event_unchecked(Default::default(), event);
Expand Down
12 changes: 6 additions & 6 deletions crates/sol-types/tests/macros/sol/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloy_primitives::{b256, hex, keccak256, Address, I256, U256};
use alloy_primitives::{b256, bytes, hex, keccak256, Address, I256, U256};
use alloy_sol_types::{sol, SolCall, SolError, SolEvent, SolStruct, SolType};
use serde::Serialize;
use serde_json::Value;
Expand Down Expand Up @@ -93,7 +93,7 @@ fn function() {
let call = someFunctionCall {
basic: U256::from(1),
string_: "Hello World".to_owned(),
longBytes: vec![0; 36],
longBytes: bytes![0; 36],
array: vec![Address::ZERO, Address::ZERO, Address::ZERO],
fixedArray: [true, false],
struct_: CustomStruct { a: Address::ZERO, b: 2 },
Expand Down Expand Up @@ -226,10 +226,10 @@ fn getters() {
}

assert_eq!(data1Call::SIGNATURE, "data1(uint256,bool,uint256)");
let _ = data1Return { _0: U256::ZERO, _1: [0, 0, 0].into(), _2: vec![] };
let _ = data1Return { _0: U256::ZERO, _1: [0, 0, 0].into(), _2: bytes![] };

assert_eq!(data2Call::SIGNATURE, "data2(uint256,bool)");
let _ = data2Return { _0: U256::ZERO, _1: [0, 0, 0].into(), _2: vec![] };
let _ = data2Return { _0: U256::ZERO, _1: [0, 0, 0].into(), _2: bytes![] };

assert_eq!(
nestedMapArrayCall::SIGNATURE,
Expand Down Expand Up @@ -413,8 +413,8 @@ fn nested_items() {
}
use nested::{InterfaceTest::*, *};

let _ = FilAddress { data: vec![] };
let _ = BigInt { val: vec![], neg: false };
let _ = FilAddress { data: bytes![] };
let _ = BigInt { val: bytes![], neg: false };
assert_eq!(f1Call::SIGNATURE, "f1((bytes),uint256)");
assert_eq!(f2Call::SIGNATURE, "f2((bytes,bool))");
}
Expand Down
4 changes: 2 additions & 2 deletions crates/sol-types/tests/ui/type.stderr

Large diffs are not rendered by default.

0 comments on commit 9ba8081

Please sign in to comment.