use std::ops::{Deref, DerefMut};
use crate::ffi::nlmsghdr;
use super::{Cursor, Bytes};
#[derive(Debug, Default, Eq, PartialEq)]
pub struct Message<'a, T: Bytes> {
head: nlmsghdr,
data: T,
tail: Cursor<'a>
}
impl<'a, T: Bytes + Default> Message<'a, T> {
pub fn new(kind: u16) -> Self {
let mut msg = Self::default();
msg.head.nlmsg_type = kind;
msg
}
}
impl<'a, T: Bytes> Message<'a, T> {
pub fn with(head: nlmsghdr, data: T, tail: Cursor<'a>) -> Self {
Self { head, data, tail }
}
pub fn set_flags(&mut self, flags: u16) {
self.head.nlmsg_flags = flags;
}
pub fn set_seq(&mut self, seq: u32) {
self.head.nlmsg_seq = seq;
}
pub fn set_pid(&mut self, pid: u32) {
self.head.nlmsg_pid = pid;
}
pub fn nlmsg_type(&self) -> u16 {
self.head.nlmsg_type
}
pub fn tail(&self) -> Cursor<'a> {
self.tail.clone()
}
pub(crate) fn parts(&self) -> (&nlmsghdr, &T, Cursor<'a>) {
(&self.head, &self.data, self.tail())
}
}
impl<T: Bytes> Deref for Message<'_, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<T: Bytes> DerefMut for Message<'_, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.data
}
}