简体   繁体   English

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

[英]How can I implement a trait in scope for an enum of existing types for which the trait is implemented?

How can I implement a trait in scope for an enum of existing types for which the trait is implemented? 如何为实现该特征的现有类型的枚举实现范围内的特征?

I have this: 我有这个:

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());
}

The compiler complains that I have not implemented the Packet trait for my enum: 编译器抱怨我没有为我的枚举实现Packet特性

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`

I thought that the Packet trait would be derived through Ipv4Packet<'a> and Ipv6Packet<'a> ? 我以为Packet特质将通过Ipv4Packet<'a>Ipv6Packet<'a>派生出来?

How can I implement a trait in scope for an enum of existing types 如何为现有类型的枚举实现范围内的特征

You implement a trait for an enum the same way you implement a trait for a struct: 您为枚举实现特征的方式与为结构实现特征的方式相同:

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"),
        }
    }
}

an enum of existing types for which the trait is implemented 已实现特征的现有类型的枚举

I thought that the Packet trait would be derived 我以为Packet特质会得到

It is not. 它不是。 Doing so would prevent people from implementing their own code for the trait if they needed. 这样做会阻止人们在需要时实现自己的特征代码。 It also wouldn't work in all cases, such as when one variant didn't implement it. 它也不能在所有情况下都起作用,例如当一个变体没有实现它时。

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(),
        }
    }
}

It's conceptually possible that the language could be extended to support some annotation to do this, but I've never heard of anyone suggesting it. 从概念上讲,可以对该语言进行扩展以支持某些注释来执行此操作,但是我从未听说有人建议这样做。 You could consider creating an RFC to add it. 您可以考虑创建一个RFC来添加它。

Perhaps you can go back to the source that taught you this and correct the problem at the root to prevent other peopl from being confused in the same manner. 也许您可以回到教导您的信息源,并从根本上纠正问题,以防止其他人以相同的方式感到困惑。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM