alloy-rs / alloy-rs/rlp

New and improved library and macro API

Đang mở
#14 6 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Rust
Star
113
Fork
30
Merge trung bình
21 phút
Pull request đã merge (30 ngày)
1

Mô tả

## Library API

```rust
pub trait RlpEncodable: RlpLength {
fn rlp_encode(&self, encoder: &mut Encoder);

fn rlp_encode_raw(&self, encoder: &mut Encoder);

fn rlp_len(&self) -> usize {
let raw_len = self.rlp_len_raw();
length_of_length(raw_len) + raw_len
}

fn rlp_len_raw(&self) -> usize; // Important: no default!!
}

// Might not be needed.
// pub trait DynRlpEncodable: RlpLength { /* ... */ }
// pub trait RlpLength { /* rlp_len, rlp_len_raw */ }
// impl DynRlpEncodable for T where T: RlpEncodable { /* ... */ }

pub trait RlpDecodable<'de> {
fn rlp_decode(decoder: &mut Decoder<'de>) -> Result;

fn rlp_decode_raw(decoder: &mut Decoder<'de>) -> Result {
Self::rlp_decode(decoder)
}
}

pub struct Encoder {
out: T,
}

// `struct Rlp` is replaced with `Decoder`.
pub struct Decoder<'de> {
buf: &'de [u8],
}

impl<'de> Decoder<'de> {
// Methods moved from `Header` ...

// Methods for creating errors that contain source information like byte position
}

pub struct Error {
bytepos: usize,
kind: ErrorKind,
}

pub enum ErrorKind {
// Previous `enum Error` ...
}
```

Other, not pictured:
- [ ] Add trait implementations for tuples, with the same behavior as structs with derived impls
- This with the improved macro attributes should remove the need of `dyn Encodable`, enabling use of a generic `Encoder`.

Improvements:
- trait and method names prefixed with `rlp` to avoid conflicts on common types
- separate methods for "raw" encoding/decoding, meaning without header, to allow `#[rlp(flatten)]`, see below

## Derive macros

New attributes:
- [ ] (field) `#[rlp(flatten)]`: encode and decode using `raw`
- [ ] (container) `#[rlp(tagged)]`: impls for enums
- `tagged`: defaults to the variant's discriminant value, or the variant can have an `#[rlp(tag = )]` attribute.
- Example: [`P2PMessage`](https://github.com/paradigmxyz/reth/blob/3e8d5c69cf67906909d41fa5b2737bd9667e46e5/crates/net/eth-wire/src/p2pstream.rs#L649)
- not specifying this fails, to leave room for future enum representations
- [ ] (container) `#[rlp(transparent)]`: replaces the separate `*Wrapper` macros
- also account for `#[rlp(skip)]` in field number calculation

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.