Why compiler fires when rlp encode

pub type Bytes = Vec<u8>;

#[derive(Debug, Clone)]
pub struct StateProof {
    address: Address,
    account_proof: Vec<Bytes>,
    key: H256,
    value_proof: Vec<Bytes>,
}

impl Encodable for StateProof {
    fn rlp_append(&self, s: &mut RlpStream) {
        s.begin_list(4);
        s.append(&self.address);
        s.append_list(&self.account_proof);
        s.append(&self.key);
        s.append_list(&self.value_proof);
    }
}

impl Decodable for StateProof {
    fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
        Ok(StateProof {
            address: rlp.val_at(0)?,
            account_proof: rlp.list_at(1)?,
            key: rlp.val_at(2)?,
            value_proof: rlp.list_at(3)?,
        })
    }
}

Error Info:

   |
17 |         s.append_list(&self.account_proof);
   |           ^^^^^^^^^^^ cannot infer type for `E`
=

I have no idea why this error happened.

Rlp Encode have implemented encodable trait for Vec<u8>.

The compiler isn't sure what type it should use.
I think it's because rlp::Encodable is implemented for both &[u8] and Vec<u8> but I'm not sure.
You can decide for the compiler using s.append_list::<E, K>(/*...*/), E and K being the types you want.
You probably don't have to specify both and just put _ for the second.

impl Encodable for StateProof {
    fn rlp_append(&self, s: &mut RlpStream) {
        s.begin_list(4);
        s.append(&self.address);
        s.append_list::<Bytes, Bytes>(&self.account_proof);
        s.append(&self.key);
        s.append_list::<Bytes, Bytes>(&self.value_proof);
    }
}

I add type annotations like this. It's works. thanks.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.