Loading

rust的枚举多态

父类的定义

pub enum  XMSSAddress{
    HashTreeAddress(HashTreeAddress),
    LTreeAddress(LTreeAddress),
    OTSHashAddress(OTSHashAddress),
}

用匹配表达式判断父类对象是那个子类:

let resultAddress=match address {
            XMSSAddress::LTreeAddress(tempAddress)=>{
                address=XMSSAddress::LTreeAddress(crate::XMSS::LTreeAddress::LTreeBuilder::new1().withLayerAddress(tempAddress.getLayerAddress())
                    .withTreeAddress(tempAddress.getTreeAddress()).withLTreeAddress(tempAddress.getLTreeAddress())
                    .withTreeHeight(tempAddress.getTreeHeight()).withTreeIndex(tempAddress.getTreeIndex())
                    .withKeyAndMask(0).build());
                address
            },
            XMSSAddress::HashTreeAddress(tempAddress)=>{
                address = XMSSAddress::HashTreeAddress(crate::XMSS::HashTreeAddress::HashTreeBuilder::new1().withLayerAddress(tempAddress.getLayerAddress())
                    .withTreeAddress(tempAddress.getTreeAddress()).withTreeHeight(tempAddress.getTreeHeight())
                    .withTreeIndex(tempAddress.getTreeIndex()).withKeyAndMask(0).build());
                address
            }
        };

把子类对象转换成父类对象:

let tempAddress: XMSSAddress = XMSSAddress::HashTreeAddress(hashTreeAddress);

 

posted @ 2024-07-02 11:15  _朝晖  阅读(7)  评论(0编辑  收藏  举报