Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove generic in parse and emit functions #879

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ fn main() {

match remote_addr {
IpAddress::Ipv4(_) => {
let icmp_packet = Icmpv4Packet::new_checked(&payload).unwrap();
let icmp_packet = Icmpv4Packet::new_checked(payload).unwrap();
let icmp_repr = Icmpv4Repr::parse(&icmp_packet, &device_caps.checksum).unwrap();
get_icmp_pong!(
Icmpv4Repr,
Expand All @@ -221,7 +221,7 @@ fn main() {
);
}
IpAddress::Ipv6(address) => {
let icmp_packet = Icmpv6Packet::new_checked(&payload).unwrap();
let icmp_packet = Icmpv6Packet::new_checked(payload).unwrap();
let icmp_repr = Icmpv6Repr::parse(
&remote_addr,
&iface
Expand Down
4 changes: 2 additions & 2 deletions src/iface/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1517,8 +1517,8 @@ impl InterfaceInner {
};

// Emit function for the IP header and payload.
let emit_ip = |repr: &IpRepr, mut tx_buffer: &mut [u8]| {
repr.emit(&mut tx_buffer, &self.caps.checksum);
let emit_ip = |repr: &IpRepr, tx_buffer: &mut [u8]| {
repr.emit(tx_buffer, &self.caps.checksum);

let payload = &mut tx_buffer[repr.header_len()..];
packet.emit_payload(repr, payload, &caps)
Expand Down
4 changes: 2 additions & 2 deletions src/iface/interface/sixlowpan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ impl InterfaceInner {
tx_buf = &mut tx_buf[ieee_len..];

// Add the first fragment header
let mut frag1_packet = SixlowpanFragPacket::new_unchecked(&mut tx_buf);
let mut frag1_packet = SixlowpanFragPacket::new_unchecked(&mut tx_buf[..]);
frag1.emit(&mut frag1_packet);
tx_buf = &mut tx_buf[frag1.buffer_len()..];

Expand Down Expand Up @@ -752,7 +752,7 @@ mod tests {
0xfd, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
])];

let ieee_frame = Ieee802154Frame::new_checked(&SIXLOWPAN_COMPRESSED_RPL_DAO).unwrap();
let ieee_frame = Ieee802154Frame::new_checked(&SIXLOWPAN_COMPRESSED_RPL_DAO[..]).unwrap();
let ieee_repr = Ieee802154Repr::parse(&ieee_frame).unwrap();

let mut buffer = [0u8; 256];
Expand Down
4 changes: 2 additions & 2 deletions src/iface/interface/tests/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ fn test_icmp_error_port_unreachable(#[case] medium: Medium) {

let mut udp_bytes_unicast = vec![0u8; 20];
let mut udp_bytes_broadcast = vec![0u8; 20];
let mut packet_unicast = UdpPacket::new_unchecked(&mut udp_bytes_unicast);
let mut packet_broadcast = UdpPacket::new_unchecked(&mut udp_bytes_broadcast);
let mut packet_unicast = UdpPacket::new_unchecked(&mut udp_bytes_unicast[..]);
let mut packet_broadcast = UdpPacket::new_unchecked(&mut udp_bytes_broadcast[..]);

let udp_repr = UdpRepr {
src_port: 67,
Expand Down
2 changes: 1 addition & 1 deletion src/iface/interface/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn test_handle_udp_broadcast(
let udp_socket = udp::Socket::new(rx_buffer, tx_buffer);

let mut udp_bytes = vec![0u8; 13];
let mut packet = UdpPacket::new_unchecked(&mut udp_bytes);
let mut packet = UdpPacket::new_unchecked(&mut udp_bytes[..]);

let socket_handle = sockets.add(udp_socket);

Expand Down
16 changes: 8 additions & 8 deletions src/socket/icmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ mod test_ipv4 {
assert!(socket.can_send());

let mut bytes = [0xff; 24];
let mut packet = Icmpv4Packet::new_unchecked(&mut bytes);
let mut packet = Icmpv4Packet::new_unchecked(&mut bytes[..]);
ECHOV4_REPR.emit(&mut packet, &checksum);

assert_eq!(
Expand Down Expand Up @@ -769,7 +769,7 @@ mod test_ipv4 {
let checksum = ChecksumCapabilities::default();

let mut bytes = [0xff; 24];
let mut packet = Icmpv4Packet::new_unchecked(&mut bytes);
let mut packet = Icmpv4Packet::new_unchecked(&mut bytes[..]);
ECHOV4_REPR.emit(&mut packet, &checksum);

s.set_hop_limit(Some(0x2a));
Expand Down Expand Up @@ -839,7 +839,7 @@ mod test_ipv4 {

let checksum = ChecksumCapabilities::default();
let mut bytes = [0xff; 20];
let mut packet = Icmpv4Packet::new_unchecked(&mut bytes);
let mut packet = Icmpv4Packet::new_unchecked(&mut bytes[..]);
let icmp_repr = Icmpv4Repr::EchoRequest {
ident: 0x4321,
seq_no: 0x5678,
Expand All @@ -865,7 +865,7 @@ mod test_ipv4 {
let checksum = ChecksumCapabilities::default();

let mut bytes = [0xff; 18];
let mut packet = UdpPacket::new_unchecked(&mut bytes);
let mut packet = UdpPacket::new_unchecked(&mut bytes[..]);
UDP_REPR.emit(
&mut packet,
&REMOTE_IPV4.into(),
Expand Down Expand Up @@ -985,7 +985,7 @@ mod test_ipv6 {
assert!(socket.can_send());

let mut bytes = vec![0xff; 24];
let mut packet = Icmpv6Packet::new_unchecked(&mut bytes);
let mut packet = Icmpv6Packet::new_unchecked(&mut bytes[..]);
ECHOV6_REPR.emit(
&LOCAL_IPV6.into(),
&REMOTE_IPV6.into(),
Expand Down Expand Up @@ -1037,7 +1037,7 @@ mod test_ipv6 {
let checksum = ChecksumCapabilities::default();

let mut bytes = vec![0xff; 24];
let mut packet = Icmpv6Packet::new_unchecked(&mut bytes);
let mut packet = Icmpv6Packet::new_unchecked(&mut bytes[..]);
ECHOV6_REPR.emit(
&LOCAL_IPV6.into(),
&REMOTE_IPV6.into(),
Expand Down Expand Up @@ -1153,7 +1153,7 @@ mod test_ipv6 {

let checksum = ChecksumCapabilities::default();
let mut bytes = [0xff; 20];
let mut packet = Icmpv6Packet::new_unchecked(&mut bytes);
let mut packet = Icmpv6Packet::new_unchecked(&mut bytes[..]);
let icmp_repr = Icmpv6Repr::EchoRequest {
ident: 0x4321,
seq_no: 0x5678,
Expand Down Expand Up @@ -1184,7 +1184,7 @@ mod test_ipv6 {
let checksum = ChecksumCapabilities::default();

let mut bytes = [0xff; 18];
let mut packet = UdpPacket::new_unchecked(&mut bytes);
let mut packet = UdpPacket::new_unchecked(&mut bytes[..]);
UDP_REPR.emit(
&mut packet,
&REMOTE_IPV6.into(),
Expand Down
10 changes: 5 additions & 5 deletions src/wire/arp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ pub enum Repr {
impl Repr {
/// Parse an Address Resolution Protocol packet and return a high-level representation,
/// or return `Err(Error)` if the packet is not recognized.
pub fn parse<T: AsRef<[u8]>>(packet: &Packet<T>) -> Result<Repr> {
pub fn parse(packet: &Packet<&[u8]>) -> Result<Repr> {
packet.check_len()?;

match (
Expand Down Expand Up @@ -298,7 +298,7 @@ impl Repr {
}

/// Emit a high-level representation into an Address Resolution Protocol packet.
pub fn emit<T: AsRef<[u8]> + AsMut<[u8]>>(&self, packet: &mut Packet<T>) {
pub fn emit(&self, packet: &mut Packet<&mut [u8]>) {
match *self {
Repr::EthernetIpv4 {
operation,
Expand All @@ -321,7 +321,7 @@ impl Repr {
}
}

impl<T: AsRef<[u8]>> fmt::Display for Packet<T> {
impl fmt::Display for Packet<&[u8]> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match Repr::parse(self) {
Ok(repr) => write!(f, "{repr}"),
Expand Down Expand Up @@ -377,7 +377,7 @@ impl<T: AsRef<[u8]>> PrettyPrint for Packet<T> {
f: &mut fmt::Formatter,
indent: &mut PrettyIndent,
) -> fmt::Result {
match Packet::new_checked(buffer) {
match Packet::new_checked(buffer.as_ref()) {
Err(err) => write!(f, "{indent}({err})"),
Ok(packet) => write!(f, "{indent}{packet}"),
}
Expand Down Expand Up @@ -453,7 +453,7 @@ mod test {
#[test]
fn test_emit() {
let mut bytes = vec![0xa5; 28];
let mut packet = Packet::new_unchecked(&mut bytes);
let mut packet = Packet::new_unchecked(&mut bytes[..]);
packet_repr().emit(&mut packet);
assert_eq!(&*packet.into_inner(), &PACKET_BYTES[..]);
}
Expand Down
19 changes: 7 additions & 12 deletions src/wire/dhcpv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,11 +703,9 @@ impl<'a> Repr<'a> {
}

/// Parse a DHCP packet and return a high-level representation.
pub fn parse<T>(packet: &'a Packet<&'a T>) -> Result<Self>
where
T: AsRef<[u8]> + ?Sized,
{
pub fn parse(packet: &'a Packet<&'a [u8]>) -> Result<Self> {
packet.check_len()?;

let transaction_id = packet.transaction_id();
let client_hardware_address = packet.client_hardware_address();
let client_ip = packet.client_ip();
Expand Down Expand Up @@ -835,10 +833,7 @@ impl<'a> Repr<'a> {

/// Emit a high-level representation into a Dynamic Host
/// Configuration Protocol packet.
pub fn emit<T>(&self, packet: &mut Packet<&mut T>) -> Result<()>
where
T: AsRef<[u8]> + AsMut<[u8]> + ?Sized,
{
pub fn emit(&self, packet: &mut Packet<&mut [u8]>) -> Result<()> {
packet.set_sname_and_boot_file_to_zero();
packet.set_opcode(self.message_type.opcode());
packet.set_hardware_type(Hardware::Ethernet);
Expand Down Expand Up @@ -1212,7 +1207,7 @@ mod test {
fn test_emit_discover() {
let repr = discover_repr();
let mut bytes = vec![0xa5; repr.buffer_len()];
let mut packet = Packet::new_unchecked(&mut bytes);
let mut packet = Packet::new_unchecked(&mut bytes[..]);
repr.emit(&mut packet).unwrap();
let packet = &*packet.into_inner();
let packet_len = packet.len();
Expand All @@ -1226,7 +1221,7 @@ mod test {
fn test_emit_offer() {
let repr = offer_repr();
let mut bytes = vec![0xa5; repr.buffer_len()];
let mut packet = Packet::new_unchecked(&mut bytes);
let mut packet = Packet::new_unchecked(&mut bytes[..]);
repr.emit(&mut packet).unwrap();
}

Expand All @@ -1245,10 +1240,10 @@ mod test {
repr
};
let mut bytes = vec![0xa5; repr.buffer_len()];
let mut packet = Packet::new_unchecked(&mut bytes);
let mut packet = Packet::new_unchecked(&mut bytes[..]);
repr.emit(&mut packet).unwrap();

let packet = Packet::new_unchecked(&bytes);
let packet = Packet::new_unchecked(&bytes[..]);
let repr_parsed = Repr::parse(&packet).unwrap();

assert_eq!(
Expand Down
5 changes: 1 addition & 4 deletions src/wire/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,7 @@ impl<'a> Repr<'a> {
}

/// Emit a high-level representation into a DNS packet.
pub fn emit<T: ?Sized>(&self, packet: &mut Packet<&mut T>)
where
T: AsRef<[u8]> + AsMut<[u8]>,
{
pub fn emit(&self, packet: &mut Packet<&mut [u8]>) {
packet.set_transaction_id(self.transaction_id);
packet.set_flags(self.flags);
packet.set_opcode(self.opcode);
Expand Down
5 changes: 2 additions & 3 deletions src/wire/ethernet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ pub struct Repr {

impl Repr {
/// Parse an Ethernet II frame and return a high-level representation.
pub fn parse<T: AsRef<[u8]> + ?Sized>(frame: &Frame<&T>) -> Result<Repr> {
pub fn parse(frame: &Frame<&[u8]>) -> Result<Repr> {
frame.check_len()?;
Ok(Repr {
src_addr: frame.src_addr(),
Expand All @@ -282,8 +282,7 @@ impl Repr {
}

/// Emit a high-level representation into an Ethernet II frame.
pub fn emit<T: AsRef<[u8]> + AsMut<[u8]>>(&self, frame: &mut Frame<T>) {
assert!(frame.buffer.as_ref().len() >= self.buffer_len());
pub fn emit(&self, frame: &mut Frame<&mut [u8]>) {
frame.set_src_addr(self.src_addr);
frame.set_dst_addr(self.dst_addr);
frame.set_ethertype(self.ethertype);
Expand Down
22 changes: 8 additions & 14 deletions src/wire/icmpv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,10 @@ pub enum Repr<'a> {
impl<'a> Repr<'a> {
/// Parse an Internet Control Message Protocol version 4 packet and return
/// a high-level representation.
pub fn parse<T>(
packet: &Packet<&'a T>,
pub fn parse(
packet: &Packet<&'a [u8]>,
checksum_caps: &ChecksumCapabilities,
) -> Result<Repr<'a>>
where
T: AsRef<[u8]> + ?Sized,
{
) -> Result<Repr<'a>> {
packet.check_len()?;

// Valid checksum is expected.
Expand Down Expand Up @@ -484,10 +481,7 @@ impl<'a> Repr<'a> {

/// Emit a high-level representation into an Internet Control Message Protocol version 4
/// packet.
pub fn emit<T>(&self, packet: &mut Packet<&mut T>, checksum_caps: &ChecksumCapabilities)
where
T: AsRef<[u8]> + AsMut<[u8]> + ?Sized,
{
pub fn emit(&self, packet: &mut Packet<&mut [u8]>, checksum_caps: &ChecksumCapabilities) {
packet.set_msg_code(0);
match *self {
Repr::EchoRequest {
Expand Down Expand Up @@ -555,7 +549,7 @@ impl<'a> Repr<'a> {
}
}

impl<'a, T: AsRef<[u8]> + ?Sized> fmt::Display for Packet<&'a T> {
impl<'a> fmt::Display for Packet<&'a [u8]> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match Repr::parse(self, &ChecksumCapabilities::default()) {
Ok(repr) => write!(f, "{repr}"),
Expand Down Expand Up @@ -619,7 +613,7 @@ impl<T: AsRef<[u8]>> PrettyPrint for Packet<T> {
f: &mut fmt::Formatter,
indent: &mut PrettyIndent,
) -> fmt::Result {
let packet = match Packet::new_checked(buffer) {
let packet = match Packet::new_checked(buffer.as_ref()) {
Err(err) => return write!(f, "{indent}({err})"),
Ok(packet) => packet,
};
Expand Down Expand Up @@ -689,9 +683,9 @@ mod test {
fn test_echo_emit() {
let repr = echo_packet_repr();
let mut bytes = vec![0xa5; repr.buffer_len()];
let mut packet = Packet::new_unchecked(&mut bytes);
let mut packet = Packet::new_unchecked(&mut bytes[..]);
repr.emit(&mut packet, &ChecksumCapabilities::default());
assert_eq!(&packet.into_inner()[..], &ECHO_PACKET_BYTES[..]);
assert_eq!(packet.into_inner(), &ECHO_PACKET_BYTES[..]);
}

#[test]
Expand Down