alloy-rs / rlp

Fast implementation of Ethereum RLP serialization
Apache License 2.0
77 stars 16 forks source link

`RlpEncodableWrapper` for structs with more than one field #12

Open Wollac opened 5 months ago

Wollac commented 5 months ago

Currently, RlpEncodableWrapper/RlpDecodableWrapper can only be derived for single-field structs. This contradicts the documentation, which explicitly mentions more than one field:

Derives Encodable for the type which encodes the fields as-is, without a header: <fields...>

There are several use cases where "headerless" encoding would be very helpful. So it should be considered if the documented behavior should be added. Otherwise, at least the documentation should be corrected to match the implementation.

An example of such a use case would be to implement something like #[serde(flatten)] by manually calling the headerless encoding of the structs to be flattened. This could, e.g., be useful for simplifying EIP-2718 transaction encoding.

Rjected commented 5 months ago

This makes sense, although I'd say the current documentation should be changed to say field instead of fields since it's mainly used for the newtype pattern.

Traits for headerless encoding, length, and rlp(flatten) would make it possible to replace lots of existing manual rlp implementations with derived encoding / decoding, basically this suggestion: https://github.com/alloy-rs/rlp/issues/10