收到错误“ std :: result :: Result类型上没有字段”错误时,如何打印结构的字段?

时间:2019-05-14 01:40:12

标签: rust

我正在尝试修改以下代码中的priority字段:

let results = xts
    .into_iter()
    .map(|xt| -> Result<_, B::Error> {
        let (hash, bytes) = self.api.hash_and_length(&xt);
        if self.rotator.is_banned(&hash) {
            info!(target: "txpool","Baning Transaction with hash {:?}",hash);
            info!(target: "txpool","{:?}",xt);
            bail!(error::Error::from(error::ErrorKind::TemporarilyBanned))
        }

        match self.api.validate_transaction(at, xt.clone())? {
            TransactionValidity::Valid {
                priority,
                requires,
                provides,
                longevity,
            } => Ok(base::Transaction {
                data: xt,
                bytes,
                hash,
                priority,
                requires,
                provides,
                valid_till: block_number.as_().saturating_add(longevity),
            }),
            TransactionValidity::Invalid(e) => {
                bail!(error::Error::from(error::ErrorKind::InvalidTransaction(e)))
            }
            TransactionValidity::Unknown(e) => {
                self.listener.write().invalid(&hash);
                bail!(error::Error::from(
                    error::ErrorKind::UnknownTransactionValidity(e)
                ))
            }
        }
    })
    .map(|tx| {
        info!(target: "txpool","Map TX begins");
        info!(target: "txpool","{:?}",tx);
        info!(target: "txpool","Map TX ends");
        let imported = self.pool.write().import(tx?)?;
        info!(target: "txpool","imported={:?}, priority={:?}",imported,tx.priority);
        if let base::Imported::Ready { .. } = imported {
            self.import_notification_sinks
                .lock()
                .retain(|sink| sink.unbounded_send(()).is_ok());
        }

        let mut listener = self.listener.write();
        fire_events(&mut *listener, &imported);
        Ok(imported.hash().clone())
    })
    .collect::<Vec<_>>();

但是,我无法打印priority字段。如果我修改此行以转储tx.priority

info!(target: "txpool", "imported={:?}, priority={:?}", imported, tx.priority);

然后我收到此错误:

error[E0609]: no field `priority` on type `std::result::Result<base_pool::Transaction<<B as pool::ChainApi>::Hash, <<B as pool::ChainApi>::Block as sr_primitives::traits::Block>::Extrinsic>, <B as pool::ChainApi>::Error>`
   --> core/transaction-pool/graph/src/pool.rs:161:77
    |
161 |                 info!(target: "txpool","imported={:?}, priority={:?}",imported,tx.priority);
    |                                                                                         ^^^^^^^^

我应该如何访问tx.priority字段?

程序的输出如下:

2019-05-13 20:23:47 Map TX begins
2019-05-13 20:23:47 Ok(Transaction { hash: 0x502cf6c53b3415d69644b6a5e06134ff52abc26a80ac639b65b0ee2aab6dc994, priority: 139, valid_till: 18446744073709551615, bytes: 139, requires: [], provides: [d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d0100000000000000], data: 81ffd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d6088729020cdc0fdf5d89443dbb1aa5a295863b1aa52549466618ce7c91bac0c4da883bc6a3216315d9ea568c45d139c1b3e3f795a10075001637b44a13d820c04000300ffc0412a7e530249d8c87d573bf5a25f4cf4bff339053360d87503338856d86137419c})
2019-05-13 20:23:47 Map TX ends
2019-05-13 20:23:47 imported=Ready { hash: 0x502cf6c53b3415d69644b6a5e06134ff52abc26a80ac639b65b0ee2aab6dc994, promoted: [], failed: [], removed: [] }

这里有tx.priority字段,其值为139,但是由于某种原因我无法访问它。为什么?

Original sources can be seen

0 个答案:

没有答案