Skip to content

Commit

Permalink
docs(rlp): move example to README.md (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Jul 5, 2023
1 parent 248d798 commit 4172df0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 31 deletions.
2 changes: 1 addition & 1 deletion crates/dyn-abi/benches/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn sol_types_encode(c: &mut Criterion) {

g.bench_function("struct", |b| {
let input = encode_struct_input();
b.iter(|| Input::encode(&input));
b.iter(|| Input::encode(black_box(&input)));
});

g.finish();
Expand Down
24 changes: 24 additions & 0 deletions crates/rlp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@ We strongly recommend deriving RLP traits via the `RlpEncodable` and

Trait methods can then be accessed via the `Encodable` and `Decodable` traits.

## Example

```rust
# #[cfg(feature = "derive")] {
use alloy_rlp::{RlpEncodable, RlpDecodable, Decodable, Encodable};

#[derive(Debug, RlpEncodable, RlpDecodable, PartialEq)]
pub struct MyStruct {
pub a: u64,
pub b: Vec<u8>,
}

let my_struct = MyStruct {
a: 42,
b: vec![1, 2, 3],
};

let mut buffer = Vec::<u8>::new();
let encoded = my_struct.encode(&mut buffer);
let decoded = MyStruct::decode(&mut buffer.as_slice()).unwrap();
assert_eq!(my_struct, decoded);
# }
```

## Provenance note

This crate was originally part of the [reth] project, as [`reth_rlp`].
Expand Down
30 changes: 0 additions & 30 deletions crates/rlp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,6 @@
html_logo_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/alloy.jpg",
html_favicon_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/favicon.ico"
)]
// This doctest uses derive, so it cannot be in the README :(
#![cfg_attr(
feature = "derive",
doc = r##"
## Usage Example
```rust
use alloy_rlp::{RlpEncodable, RlpDecodable, Decodable, Encodable};
#[derive(Debug, RlpEncodable, RlpDecodable, PartialEq)]
pub struct MyStruct {
pub a: u64,
pub b: Vec<u8>,
}
fn main() {
let my_struct = MyStruct {
a: 42,
b: vec![1, 2, 3],
};
let mut buffer = Vec::<u8>::new();
let encoded = my_struct.encode(&mut buffer);
let decoded = MyStruct::decode(&mut buffer.as_slice()).unwrap();
assert_eq!(my_struct, decoded);
}
```
"##
)]
#![warn(
missing_docs,
unreachable_pub,
Expand Down

0 comments on commit 4172df0

Please sign in to comment.