From 6ee951669794e41965e54d8114a4f56582e50df4 Mon Sep 17 00:00:00 2001 From: Andrew Cann Date: Thu, 8 Feb 2018 16:54:50 +0800 Subject: [PATCH] impl `AsRef<[u8]>` for packet types Closes: #158 Approved by: whitequark --- src/wire/arp.rs | 6 ++++++ src/wire/ethernet.rs | 6 ++++++ src/wire/icmpv4.rs | 6 ++++++ src/wire/icmpv6.rs | 6 ++++++ src/wire/ipv4.rs | 6 ++++++ src/wire/ipv6.rs | 6 ++++++ src/wire/tcp.rs | 6 ++++++ src/wire/udp.rs | 6 ++++++ 8 files changed, 48 insertions(+) diff --git a/src/wire/arp.rs b/src/wire/arp.rs index 03f1635..26f6266 100644 --- a/src/wire/arp.rs +++ b/src/wire/arp.rs @@ -242,6 +242,12 @@ impl + AsMut<[u8]>> Packet { } } +impl> AsRef<[u8]> for Packet { + fn as_ref(&self) -> &[u8] { + self.buffer.as_ref() + } +} + use super::{EthernetAddress, Ipv4Address}; /// A high-level representation of an Address Resolution Protocol packet. diff --git a/src/wire/ethernet.rs b/src/wire/ethernet.rs index 3b7b512..d203b75 100644 --- a/src/wire/ethernet.rs +++ b/src/wire/ethernet.rs @@ -196,6 +196,12 @@ impl + AsMut<[u8]>> Frame { } } +impl> AsRef<[u8]> for Frame { + fn as_ref(&self) -> &[u8] { + self.buffer.as_ref() + } +} + impl> fmt::Display for Frame { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "EthernetII src={} dst={} type={}", diff --git a/src/wire/icmpv4.rs b/src/wire/icmpv4.rs index 942aee8..c795e5f 100644 --- a/src/wire/icmpv4.rs +++ b/src/wire/icmpv4.rs @@ -358,6 +358,12 @@ impl<'a, T: AsRef<[u8]> + AsMut<[u8]> + ?Sized> Packet<&'a mut T> { } } +impl> AsRef<[u8]> for Packet { + fn as_ref(&self) -> &[u8] { + self.buffer.as_ref() + } +} + /// A high-level representation of an Internet Control Message Protocol version 4 packet header. #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum Repr<'a> { diff --git a/src/wire/icmpv6.rs b/src/wire/icmpv6.rs index 8d70db7..3135115 100644 --- a/src/wire/icmpv6.rs +++ b/src/wire/icmpv6.rs @@ -367,6 +367,12 @@ impl + AsMut<[u8]>> Packet { } } +impl> AsRef<[u8]> for Packet { + fn as_ref(&self) -> &[u8] { + self.buffer.as_ref() + } +} + /// A high-level representation of an Internet Control Message Protocol version 6 packet header. #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum Repr<'a> { diff --git a/src/wire/ipv4.rs b/src/wire/ipv4.rs index ea5e341..5a83159 100644 --- a/src/wire/ipv4.rs +++ b/src/wire/ipv4.rs @@ -534,6 +534,12 @@ impl + AsMut<[u8]>> Packet { } } +impl> AsRef<[u8]> for Packet { + fn as_ref(&self) -> &[u8] { + self.buffer.as_ref() + } +} + /// A high-level representation of an Internet Protocol version 4 packet header. #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub struct Repr { diff --git a/src/wire/ipv6.rs b/src/wire/ipv6.rs index a92debd..d292343 100644 --- a/src/wire/ipv6.rs +++ b/src/wire/ipv6.rs @@ -560,6 +560,12 @@ impl<'a, T: AsRef<[u8]> + ?Sized> fmt::Display for Packet<&'a T> { } } +impl> AsRef<[u8]> for Packet { + fn as_ref(&self) -> &[u8] { + self.buffer.as_ref() + } +} + /// A high-level representation of an Internet Protocol version 6 packet header. #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub struct Repr { diff --git a/src/wire/tcp.rs b/src/wire/tcp.rs index 6ded0f6..3e1400f 100644 --- a/src/wire/tcp.rs +++ b/src/wire/tcp.rs @@ -510,6 +510,12 @@ impl + AsMut<[u8]>> Packet { } } +impl> AsRef<[u8]> for Packet { + fn as_ref(&self) -> &[u8] { + self.buffer.as_ref() + } +} + /// A representation of a single TCP option. #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum TcpOption<'a> { diff --git a/src/wire/udp.rs b/src/wire/udp.rs index 2fc7c56..9bf89dd 100644 --- a/src/wire/udp.rs +++ b/src/wire/udp.rs @@ -190,6 +190,12 @@ impl + AsMut<[u8]>> Packet { } } +impl> AsRef<[u8]> for Packet { + fn as_ref(&self) -> &[u8] { + self.buffer.as_ref() + } +} + /// A high-level representation of an User Datagram Protocol packet. #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub struct Repr<'a> {