// Copyright (c) 2015 Robert Clipsham // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #![feature(custom_attribute, plugin, slice_bytes, vec_push_all)] #![plugin(pnet_macros_plugin)] extern crate pnet; #[packet] pub struct PacketWithPayload { banana: u8, length: u8, header_length: u8, #[length_fn = "length_fn"] packet_option: Vec, #[payload] payload: Vec, } #[packet] pub struct PacketOption { pineapple: u8, length: u8, #[length_fn = "option_length_fn"] #[payload] payload: Vec, } fn length_fn(packet_with_payload: &PacketWithPayloadPacket) -> usize { packet_with_payload.get_header_length() as usize - 2 } fn option_length_fn(packet_option: &PacketOptionPacket) -> usize { packet_option.get_length() as usize - 2 } fn main() { let data = [1, 8, 5, 6, 3, 1, 9, 10]; let packet = PacketWithPayloadPacket::new(&data[..]).unwrap(); let packet_option = packet.get_packet_option(); assert_eq!(packet_option.first().unwrap().pineapple, 6); assert_eq!(packet_option.first().unwrap().length, 3); }