use std::cmp::Ordering;
use std::convert::From;
use std::fmt::{self, Debug, Display};
use windows::Win32::NetworkManagement::Ndis::NET_LUID_LH;
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct IfLuid(NET_LUID_LH);
impl IfLuid {
pub fn new(net_luid_lh: &NET_LUID_LH) -> Self {
Self(*net_luid_lh)
}
}
impl From<NET_LUID_LH> for IfLuid {
fn from(luid: NET_LUID_LH) -> Self {
IfLuid(luid)
}
}
impl From<IfLuid> for NET_LUID_LH {
fn from(val: IfLuid) -> Self {
val.0
}
}
impl From<u64> for IfLuid {
fn from(value: u64) -> Self {
let net_luid_lh = NET_LUID_LH { Value: value };
IfLuid(net_luid_lh)
}
}
impl PartialEq for IfLuid {
fn eq(&self, other: &Self) -> bool {
unsafe { self.0.Value == other.0.Value }
}
}
impl Eq for IfLuid {}
impl PartialOrd for IfLuid {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for IfLuid {
fn cmp(&self, other: &Self) -> Ordering {
unsafe { self.0.Value.cmp(&other.0.Value) }
}
}
impl Debug for IfLuid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("IfLuid")
.field("Value", unsafe { &self.0.Value })
.finish()
}
}
impl Display for IfLuid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "IfLuid({})", unsafe { self.0.Value })
}
}
#[cfg(test)]
mod tests {
use super::IfLuid;
use windows::Win32::NetworkManagement::Ndis::NET_LUID_LH;
#[test]
fn test_equality() {
let luid1 = IfLuid(NET_LUID_LH { Value: 42 });
let luid2 = IfLuid(NET_LUID_LH { Value: 42 });
let luid3 = IfLuid(NET_LUID_LH { Value: 84 });
assert_eq!(luid1, luid2);
assert_ne!(luid1, luid3);
}
#[test]
fn test_ordering() {
let luid1 = IfLuid(NET_LUID_LH { Value: 42 });
let luid2 = IfLuid(NET_LUID_LH { Value: 84 });
assert!(luid1 < luid2);
assert!(luid2 > luid1);
}
#[test]
fn test_debug() {
let luid = IfLuid(NET_LUID_LH { Value: 42 });
let debug_output = format!("{:?}", luid);
assert_eq!(debug_output, "IfLuid { Value: 42 }");
}
#[test]
fn test_display() {
let luid = IfLuid(NET_LUID_LH { Value: 42 });
let display_output = format!("{}", luid);
assert_eq!(display_output, "IfLuid(42)");
}
}