forked from alloy-rs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall_derives.rs
More file actions
41 lines (35 loc) · 1.19 KB
/
Copy pathall_derives.rs
File metadata and controls
41 lines (35 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//! This example demonstrates the `all_derives` attribute in the `sol!` macro.
//!
//! The `all_derives` attribute enables the derivation of standard Rust traits.
use alloy::{
primitives::{Address, U256},
sol,
};
use std::hash::{DefaultHasher, Hash, Hasher};
sol! (
#![sol(all_derives)]
// The `all_derives` attribute enables derivation of std rust traits such as
// `Default`, `Debug`, `PartialEq`, `Eq`, and `Hash`.
struct Foo {
uint256 a;
uint64 b;
address c;
}
);
fn main() {
// `Default` is derived.
let foo = Foo::default();
let foo_bar = Foo { a: U256::from(1), b: 2, c: Address::with_last_byte(1) };
let mut foo_list = vec![foo.clone(), foo, foo_bar];
// `Debug` derived as well.
println!("Initial foo_list: {foo_list:?}");
// `PartialEq` is derived, enabling us to apply `.dedup()`.
foo_list.dedup();
assert_eq!(foo_list.len(), 2);
// `Hash` is derived, enabling us to apply `.hash()`.
let baz = Foo { a: U256::from(1), b: 2, c: Address::with_last_byte(1) };
let mut hasher = DefaultHasher::default();
baz.hash(&mut hasher);
let hash = hasher.finish();
println!("Hash of baz: {hash}");
}