reth icon indicating copy to clipboard operation
reth copied to clipboard

db-api: trait Compress and Encode use reference self

Open nkysg opened this issue 1 year ago • 3 comments
trafficstars

ref https://github.com/paradigmxyz/reth/issues/11185.

Trait Encode fn encodes use self. It's a bit weird.

I see many crate use &self, like

This is alloyed rlp Encode trait. https://github.com/alloy-rs/rlp/blob/9aef28e6da7c2769460dd6bf22eeacd53adf0ffc/crates/rlp/src/encode.rs#L15

/// A type that can be encoded via RLP.
pub trait Encodable {
    /// Encodes the type into the `out` buffer.
    fn encode(&self, out: &mut dyn BufMut);

    /// Returns the length of the encoding of this type in bytes.
    ///
    /// The default implementation computes this by encoding the type.
    /// When possible, we recommender implementers override this with a
    /// specialized implementation.
    #[inline]
    fn length(&self) -> usize {
        let mut out = Vec::new();
        self.encode(&mut out);
        out.len()
    }
}

And then maybe we can remove some clone. I think some db fn like put also can use reference

nkysg avatar Sep 25 '24 06:09 nkysg