Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New and improved library and macro API #14

Open
4 tasks
DaniPopes opened this issue Apr 14, 2024 · 5 comments · May be fixed by #29
Open
4 tasks

New and improved library and macro API #14

DaniPopes opened this issue Apr 14, 2024 · 5 comments · May be fixed by #29

Comments

@DaniPopes
Copy link
Member

DaniPopes commented Apr 14, 2024

Library API

pub trait RlpEncodable: RlpLength {
    fn rlp_encode<T: BufMut>(&self, encoder: &mut Encoder<T>);

    fn rlp_encode_raw<T: BufMut>(&self, encoder: &mut Encoder<T>);

    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<T> DynRlpEncodable for T where T: RlpEncodable { /* ... */ }

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

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

pub struct Encoder<T: BufMut> {
    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 = <expr>)] attribute.
    • 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
@mahmudsudo
Copy link

Hi can I take on this ?

@gakonst
Copy link
Member

gakonst commented Apr 14, 2024

Before assigning this, can you unpack a little more about how you're planning to approach this? Dani wrote a great initial spec above, worth splitting this into sub-tracking issues and sub-tasks that you may own and we can split with others.

@mahmudsudo
Copy link

mahmudsudo commented Apr 15, 2024

A. Trait and Struct Implementations
RlpEncodable and RlpDecodable Traits:
Implement basic methods and ensure compatibility with tuples (this could be in the form of derivable macros that output a trait impl block so it can be used for other structs/types or just a native impl block for tuples only)
Provide detailed documentation and examples for each method.

B. Macro Attributes and Derive Macros
Derive Macros for Auto-Implementation:
Develop macros that automatically implement the RlpEncodable and RlpDecodable traits for structs and enums.
Ensure that the macros handle various attributes correctly, such as rlp(flatten) and rlp(tagged).
Attribute Handling:
Implement and test new attributes like rlp(transparent) and rlp(skip).
Ensure proper handling of tagged enums and transparent wrappers.

@mahmudsudo
Copy link

@gakonst

@mahmudsudo
Copy link

@gakonst @DaniPopes ; is there any adjustments to be noted before taken on this ?

@prestwich prestwich linked a pull request Oct 21, 2024 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants