-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from ralexstokes/transparent-attr
add support for `transparent` mode of derive operation
- Loading branch information
Showing
4 changed files
with
184 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,28 @@ | ||
# ssz_rs_derive | ||
|
||
This package provides two proc derive macros: `SimpleSerialize` and `Serializable`. | ||
This package provides the following proc derive macros: | ||
* `Serializable` | ||
* `Merkleized` | ||
* `SimpleSerialize` | ||
|
||
`SimpleSerialize` derives the functionality required to implement the main package's `SimpleSerialize` trait. | ||
|
||
`Serializable` only derives the encoding and decoding routines, in the event a user does not want to pull in the merkleization machinery. | ||
|
||
`Merkleized` only provides an implementation of that trait, if your type only needs to provide the hashing functionality. | ||
|
||
Supports: | ||
- struct (as SSZ container) where each field is also `SimpleSerialize` or `Serializable` | ||
- tuple struct with one field where the field is `SimpleSerialize` or `Serializable` | ||
- struct where each field is also `SimpleSerialize` or `Serializable` | ||
- enums with "unnamed" and unit members while respecting the rules of SSZ unions | ||
- tuple struct with one field where the field is `SimpleSerialize` or `Serializable` | ||
- enums in "wrapper" mode, requiring the `transparent` attribute. | ||
|
||
Derivations on structs provide implementations of the relevant traits for a custom struct definition to represent a SSZ container type. | ||
|
||
Derivations on enums (without `transparent`) provide implementations of the relevant traits for SSZ union types. | ||
|
||
Derivations on tuple structs facilitates the "newtype" pattern and delegates to the inner type for its implementation of the relevant traits. | ||
|
||
Derivations on enums *with* `transparent` supports delegation to the inner variants for the implementation of the relevant traits. | ||
|
||
Note: example usage can be found in the tests of the `container` and `union` | ||
modules of the `ssz_rs` crate, along with the `examples` in that crate. | ||
Example usage can be found in the tests of the `container` and `union` modules of the `ssz_rs` crate, along with the `examples` in that crate. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use ssz_rs::prelude::*; | ||
use ssz_rs_derive::SimpleSerialize; | ||
|
||
#[derive(Debug, SimpleSerialize)] | ||
struct Foo { | ||
a: u8, | ||
b: u32, | ||
} | ||
|
||
#[derive(Debug, SimpleSerialize)] | ||
#[ssz(transparent)] | ||
enum Bar { | ||
A(u8), | ||
B(Foo), | ||
} | ||
|
||
#[derive(Debug, SimpleSerialize)] | ||
struct Wrapper(Foo); | ||
|
||
#[test] | ||
fn test_transparent_helper() { | ||
let mut f = Foo { a: 23, b: 445 }; | ||
let f_root = f.hash_tree_root().unwrap(); | ||
let mut bar = Bar::B(f); | ||
let bar_root = bar.hash_tree_root().unwrap(); | ||
assert_eq!(f_root, bar_root); | ||
} |