如何为实现该特征的现有类型的枚举实现范围内的特征?

时间:2018-08-02 20:06:32

标签: enums rust traits

如何为实现了特征的现有类型的枚举在范围内实现特征?

我有这个:

extern crate pnet;

use pnet::packet::ipv4::Ipv4Packet;
use pnet::packet::ipv6::Ipv6Packet;

enum EthernetType {
    IPv4,
    ARP,
    VLAN,
    IPv6,
    Unknown(u16),
}

enum IPPacket<'a> {
    IPv4(Ipv4Packet<'a>),
    IPv6(Ipv6Packet<'a>),
}

fn ip_decode(pkt: &[u8]) -> IPPacket {
    let version = (pkt[0] & 0xf0) >> 4;
    if version == 4 {
        IPPacket::IPv4(Ipv4Packet::new(&pkt).unwrap())
    } else {
        IPPacket::IPv6(Ipv6Packet::new(&pkt).unwrap())
    }
}

fn main() {
    // Parse ethernet packet here...
    // ...
    let ip_packet = ip_decode(b"deadbeef");
    println!("{:?}", ip_packet.payload());
}

编译器抱怨我没有为我的枚举实现Packet trait

error[E0599]: no method named `payload` found for type `IPPacket<'_>` in the current scope
  --> src/main.rs:32:32
   |
14 | enum IPPacket<'a> {
   | ----------------- method `payload` not found for this
...
32 |     println!("{:?}", ip_packet.payload());
   |                                ^^^^^^^
   |
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `payload`, perhaps you need to implement it:
           candidate #1: `pnet::packet::Packet`

我认为Packet特性是通过Ipv4Packet<'a>Ipv6Packet<'a>衍生的吗?

1 个答案:

答案 0 :(得分:2)

  

如何为现有类型的枚举实现范围内的特征

为枚举实现特征的方式与为结构实现特征的方式相同:

trait Noise {
    fn noise(&self);
}

enum Foo {
    Bar,
    Baz,
}

impl Noise for Foo {
    fn noise(&self) {
        match self {
            Foo::Bar => println!("bar bar bar"),
            Foo::Baz => println!("baz baz baz"),
        }
    }
}
  

已实现特征的现有类型的枚举

  

我认为Packet特质将会得到

不是。这样做会阻止人们在需要时实现他们的 own 代码。它也不能在所有情况下都起作用,例如当一个变体未实现时。

trait Noise {
    fn noise(&self);
}

struct Bar;

impl Noise for Bar {
    fn noise(&self) {
        println!("bar bar bar");
    }
}

struct Baz;

impl Noise for Baz {
    fn noise(&self) {
        println!("baz baz baz");
    }
}

enum Foo {
    Bar(Bar),
    Baz(Baz),
}

impl Noise for Foo {
    fn noise(&self) {
        match self {
            Foo::Bar(bar) => bar.noise(),
            Foo::Baz(baz) => baz.noise(),
        }
    }
}

从概念上讲,可以对该语言进行扩展以支持某些注释来执行此操作,但我从未听说有人提出过建议。您可以考虑创建一个RFC来添加它。

也许您可以回到教导您的信息源并从根本上纠正问题,以防止其他人以相同的方式感到困惑。

另请参阅: