Skip to content

Commit

Permalink
Implement into_buffer() alongside buffer() and into_bytes() alongside…
Browse files Browse the repository at this point in the history
… as_bytes()
  • Loading branch information
alexforster committed Apr 13, 2021
1 parent 765ac67 commit 859aa03
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/arp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,19 @@ impl<'a> ArpPdu<'a> {
self.buffer
}

/// Consumes this object and returns a reference to the entire underlying buffer that was provided during
/// construction
pub fn into_buffer(self) -> &'a [u8] {
self.buffer
}

/// Returns the slice of the underlying buffer that contains this PDU
pub fn as_bytes(&'a self) -> &'a [u8] {
self.clone().into_bytes()
}

/// Consumes this object and returns the slice of the underlying buffer that contains this PDU
pub fn into_bytes(self) -> &'a [u8] {
&self.buffer[0..28]
}

Expand Down
11 changes: 11 additions & 0 deletions src/ethernet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,19 @@ impl<'a> EthernetPdu<'a> {
self.buffer
}

/// Consumes this object and returns a reference to the entire underlying buffer that was provided during
/// construction
pub fn into_buffer(self) -> &'a [u8] {
self.buffer
}

/// Returns the slice of the underlying buffer that contains the header part of this PDU
pub fn as_bytes(&'a self) -> &'a [u8] {
self.clone().into_bytes()
}

/// Consumes this object and returns the slice of the underlying buffer that contains the header part of this PDU
pub fn into_bytes(self) -> &'a [u8] {
&self.buffer[0..self.computed_ihl()]
}

Expand Down
11 changes: 11 additions & 0 deletions src/gre.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,19 @@ impl<'a> GrePdu<'a> {
self.buffer
}

/// Consumes this object and returns a reference to the entire underlying buffer that was provided during
/// construction
pub fn into_buffer(self) -> &'a [u8] {
self.buffer
}

/// Returns the slice of the underlying buffer that contains the header part of this PDU
pub fn as_bytes(&'a self) -> &'a [u8] {
self.clone().into_bytes()
}

/// Consumes this object and returns the slice of the underlying buffer that contains the header part of this PDU
pub fn into_bytes(self) -> &'a [u8] {
&self.buffer[0..self.computed_ihl()]
}

Expand Down
11 changes: 11 additions & 0 deletions src/icmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,19 @@ impl<'a> IcmpPdu<'a> {
self.buffer
}

/// Consumes this object and returns a reference to the entire underlying buffer that was provided during
/// construction
pub fn into_buffer(self) -> &'a [u8] {
self.buffer
}

/// Returns the slice of the underlying buffer that contains the header part of this PDU
pub fn as_bytes(&'a self) -> &'a [u8] {
self.clone().into_bytes()
}

/// Consumes this object and returns the slice of the underlying buffer that contains the header part of this PDU
pub fn into_bytes(self) -> &'a [u8] {
&self.buffer[0..8]
}

Expand Down
20 changes: 17 additions & 3 deletions src/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ impl<'a> Ipv4Pdu<'a> {
if buffer.len() < 20 || pdu.computed_ihl() < 20 {
return Err(Error::Truncated);
}
if buffer.len() < (pdu.computed_ihl() as usize)
|| (pdu.total_length() as usize) < pdu.computed_ihl()
{
if buffer.len() < (pdu.computed_ihl() as usize) || (pdu.total_length() as usize) < pdu.computed_ihl() {
return Err(Error::Malformed);
}
if pdu.version() != 4 {
Expand Down Expand Up @@ -116,8 +114,19 @@ impl<'a> Ipv4Pdu<'a> {
self.buffer
}

/// Consumes this object and returns a reference to the entire underlying buffer that was provided during
/// construction
pub fn into_buffer(self) -> &'a [u8] {
self.buffer
}

/// Returns the slice of the underlying buffer that contains the header part of this PDU
pub fn as_bytes(&'a self) -> &'a [u8] {
self.clone().into_bytes()
}

/// Consumes this object and returns the slice of the underlying buffer that contains the header part of this PDU
pub fn into_bytes(self) -> &'a [u8] {
&self.buffer[0..self.computed_ihl()]
}

Expand Down Expand Up @@ -309,6 +318,11 @@ impl<'a> Ipv6Pdu<'a> {

/// Returns the slice of the underlying buffer that contains the header part of this PDU
pub fn as_bytes(&'a self) -> &'a [u8] {
self.clone().into_bytes()
}

/// Consumes this object and returns the slice of the underlying buffer that contains the header part of this PDU
pub fn into_bytes(self) -> &'a [u8] {
&self.buffer[0..self.computed_ihl()]
}

Expand Down
11 changes: 11 additions & 0 deletions src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,19 @@ impl<'a> TcpPdu<'a> {
self.buffer
}

/// Consumes this object and returns a reference to the entire underlying buffer that was provided during
/// construction
pub fn into_buffer(self) -> &'a [u8] {
self.buffer
}

/// Returns the slice of the underlying buffer that contains the header part of this PDU
pub fn as_bytes(&'a self) -> &'a [u8] {
self.clone().into_bytes()
}

/// Consumes this object and returns the slice of the underlying buffer that contains the header part of this PDU
pub fn into_bytes(self) -> &'a [u8] {
&self.buffer[0..self.computed_data_offset()]
}

Expand Down
11 changes: 11 additions & 0 deletions src/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,19 @@ impl<'a> UdpPdu<'a> {
self.buffer
}

/// Consumes this object and returns a reference to the entire underlying buffer that was provided during
/// construction
pub fn into_buffer(self) -> &'a [u8] {
self.buffer
}

/// Returns the slice of the underlying buffer that contains the header part of this PDU
pub fn as_bytes(&'a self) -> &'a [u8] {
self.clone().into_bytes()
}

/// Consumes this object and returns the slice of the underlying buffer that contains the header part of this PDU
pub fn into_bytes(self) -> &'a [u8] {
&self.buffer[0..8]
}

Expand Down

0 comments on commit 859aa03

Please sign in to comment.