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 @   _朝晖  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
历史上的今天:
2017-07-02 imgAreaSelect 中文文档
2017-07-02 JavaScript头像上传器的实现
2017-07-02 中国的UED们
2017-07-02 Win7下 Python中文正则的奇异表现
2017-07-02 QQ connect client request's parameters are invalid, invalid openid 问题的解决
2017-07-02 【干货】张小龙的30条产品法则
2017-07-02 架构设计的抉择
点击右上角即可分享
微信分享提示
主题色彩