use smallvec::SmallVec;
use super::*;
#[derive(Clone, Copy, Debug)]
pub struct DisconnectionComplete {
pub status: Status,
pub handle: ConnHandle,
pub reason: Status,
}
impl FromEvent for DisconnectionComplete {
#[inline(always)]
fn matches(c: EventCode) -> bool {
matches!(c, EventCode::DisconnectionComplete)
}
fn unpack(e: &Event, p: &mut Unpacker) -> Self {
Self {
status: e.status(),
handle: e.conn_handle().unwrap(),
reason: Status::from(p.u8()),
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct EncryptionChange {
pub status: Status,
pub handle: ConnHandle,
pub enabled: bool,
}
impl FromEvent for EncryptionChange {
#[inline(always)]
fn matches(c: EventCode) -> bool {
matches!(
c,
EventCode::EncryptionChangeV1 | EventCode::EncryptionChangeV2
)
}
fn unpack(e: &Event, p: &mut Unpacker) -> Self {
let enabled = p.bool();
if matches!(e.code(), EventCode::EncryptionChangeV2) {
let _ = p.u8();
}
Self {
status: e.status(),
handle: e.conn_handle().unwrap(),
enabled,
}
}
}
#[derive(Clone, Debug)]
#[repr(transparent)]
pub struct NumberOfCompletedPackets(SmallVec<[(ConnHandle, u16); 4]>);
impl FromEvent for NumberOfCompletedPackets {
#[inline(always)]
fn matches(c: EventCode) -> bool {
matches!(c, EventCode::NumberOfCompletedPackets)
}
fn unpack(_: &Event, p: &mut Unpacker) -> Self {
let n = usize::from(p.u8());
let mut v = SmallVec::with_capacity(n);
for _ in 0..n {
if let (Some(cn), n) = (ConnHandle::new(p.u16()), p.u16()) {
v.push((cn, n));
}
}
Self(v)
}
}
impl AsRef<[(ConnHandle, u16)]> for NumberOfCompletedPackets {
#[inline]
fn as_ref(&self) -> &[(ConnHandle, u16)] {
self.0.as_ref()
}
}