hi, I am working on the parity Ethereum code. However, I faced some weird error when I tried to initialize a structure with newtype. please help me.
here is the troublesome code
let transaction = Transaction::AssetTransfer {
network_id: NetworkId([0u8; 2]),
burns: Vec::new(),
inputs: Vec::new(),
outputs: Vec::new(),
nonce: 0,
};
and here is compile error
error[E0423]: expected function, found struct `NetworkId`
--> vm/src/tests/executor.rs:76:21
|
76 | network_id: NetworkId([0u8; 2]),
| ^^^^^^^^^ did you mean `NetworkId { /* fields */ }`?
and NetworkId is defined as follow
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub struct NetworkId([u8; 2]);
impl fmt::Display for NetworkId {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let s = str::from_utf8(&self.0).expect("network_id a valid utf8 string");
write!(f, "{}", s)
}
}
impl FromStr for NetworkId {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() != 2 {
return Err("Invalid network_id length".to_string())
}
let mut network_id = [0u8; 2];
network_id.copy_from_slice(s.as_bytes());
Ok(NetworkId(network_id))
}
}
impl From<&'static str> for NetworkId {
fn from(s: &'static str) -> Self {
s.parse().unwrap()
}
}
impl Default for NetworkId {
fn default() -> Self {
"tc".into()
}
}
and Transaction::AssetTransfer is defined as follow
pub enum Transaction {
...
AssetTransfer {
network_id: NetworkId,
burns: Vec<AssetTransferInput>,
inputs: Vec<AssetTransferInput>,
outputs: Vec<AssetTransferOutput>,
nonce: u64,
},
}
I think it should work..