diff --git a/aya-log-common/src/lib.rs b/aya-log-common/src/lib.rs index 6f48b5d4..c511e4e2 100644 --- a/aya-log-common/src/lib.rs +++ b/aya-log-common/src/lib.rs @@ -160,12 +160,18 @@ pub enum DisplayHint { UpperMac, } -pub trait Argument { +mod sealed { + pub trait Sealed {} +} + +pub trait Argument: sealed::Sealed { fn as_argument(&self) -> (ArgumentKind, impl AsRef<[u8]>); } macro_rules! impl_argument { ($self:ident, $arg_type:expr) => { + impl sealed::Sealed for $self {} + impl Argument for $self { fn as_argument(&self) -> (ArgumentKind, impl AsRef<[u8]>) { ($arg_type, self.to_ne_bytes()) @@ -207,6 +213,7 @@ where } } +impl sealed::Sealed for IpAddr {} impl Argument for IpAddr { fn as_argument(&self) -> (ArgumentKind, impl AsRef<[u8]>) { match self { @@ -222,55 +229,48 @@ impl Argument for IpAddr { } } +impl sealed::Sealed for Ipv4Addr {} impl Argument for Ipv4Addr { fn as_argument(&self) -> (ArgumentKind, impl AsRef<[u8]>) { (ArgumentKind::Ipv4Addr, self.octets()) } } -impl Argument for [u8; 4] { - fn as_argument(&self) -> (ArgumentKind, impl AsRef<[u8]>) { - (ArgumentKind::ArrU8Len4, self) - } -} - +impl sealed::Sealed for Ipv6Addr {} impl Argument for Ipv6Addr { fn as_argument(&self) -> (ArgumentKind, impl AsRef<[u8]>) { (ArgumentKind::Ipv6Addr, self.octets()) } } -impl Argument for [u8; 16] { - fn as_argument(&self) -> (ArgumentKind, impl AsRef<[u8]>) { - (ArgumentKind::ArrU8Len16, self) - } -} - -impl Argument for [u16; 8] { - fn as_argument(&self) -> (ArgumentKind, impl AsRef<[u8]>) { - let bytes = unsafe { core::mem::transmute::<&[u16; 8], &[u8; 16]>(self) }; - (ArgumentKind::ArrU16Len8, bytes) - } -} - -impl Argument for [u8; 6] { +impl sealed::Sealed for [u8; N] {} +impl Argument for [u8; N] { fn as_argument(&self) -> (ArgumentKind, impl AsRef<[u8]>) { - (ArgumentKind::ArrU8Len6, self) + let kind = match N { + 4 => ArgumentKind::ArrU8Len4, + 6 => ArgumentKind::ArrU8Len6, + 16 => ArgumentKind::ArrU8Len16, + _ => ArgumentKind::Bytes, + }; + (kind, *self) } } +impl sealed::Sealed for &[u8] {} impl Argument for &[u8] { fn as_argument(&self) -> (ArgumentKind, impl AsRef<[u8]>) { - (ArgumentKind::Bytes, self) + (ArgumentKind::Bytes, *self) } } +impl sealed::Sealed for &str {} impl Argument for &str { fn as_argument(&self) -> (ArgumentKind, impl AsRef<[u8]>) { (ArgumentKind::Str, self.as_bytes()) } } +impl sealed::Sealed for DisplayHint {} impl Argument for DisplayHint { fn as_argument(&self) -> (ArgumentKind, impl AsRef<[u8]>) { let v: u8 = (*self).into(); diff --git a/xtask/public-api/aya-log-common.txt b/xtask/public-api/aya-log-common.txt index 24b1103f..481b273e 100644 --- a/xtask/public-api/aya-log-common.txt +++ b/xtask/public-api/aya-log-common.txt @@ -177,20 +177,12 @@ impl core::clone::CloneToUninit for aya_log_common::RecordFieldKind where T: pub unsafe fn aya_log_common::RecordFieldKind::clone_to_uninit(&self, dest: *mut u8) impl core::convert::From for aya_log_common::RecordFieldKind pub fn aya_log_common::RecordFieldKind::from(t: T) -> T -pub trait aya_log_common::Argument +pub trait aya_log_common::Argument: aya_log_common::sealed::Sealed pub fn aya_log_common::Argument::as_argument(&self) -> (aya_log_common::ArgumentKind, impl core::convert::AsRef<[u8]>) impl aya_log_common::Argument for &[u8] pub fn &[u8]::as_argument(&self) -> (aya_log_common::ArgumentKind, impl core::convert::AsRef<[u8]>) impl aya_log_common::Argument for &str pub fn &str::as_argument(&self) -> (aya_log_common::ArgumentKind, impl core::convert::AsRef<[u8]>) -impl aya_log_common::Argument for [u16; 8] -pub fn [u16; 8]::as_argument(&self) -> (aya_log_common::ArgumentKind, impl core::convert::AsRef<[u8]>) -impl aya_log_common::Argument for [u8; 16] -pub fn [u8; 16]::as_argument(&self) -> (aya_log_common::ArgumentKind, impl core::convert::AsRef<[u8]>) -impl aya_log_common::Argument for [u8; 4] -pub fn [u8; 4]::as_argument(&self) -> (aya_log_common::ArgumentKind, impl core::convert::AsRef<[u8]>) -impl aya_log_common::Argument for [u8; 6] -pub fn [u8; 6]::as_argument(&self) -> (aya_log_common::ArgumentKind, impl core::convert::AsRef<[u8]>) impl aya_log_common::Argument for aya_log_common::DisplayHint pub fn aya_log_common::DisplayHint::as_argument(&self) -> (aya_log_common::ArgumentKind, impl core::convert::AsRef<[u8]>) impl aya_log_common::Argument for core::net::ip_addr::IpAddr @@ -223,6 +215,8 @@ impl aya_log_common::Argument for u8 pub fn u8::as_argument(&self) -> (aya_log_common::ArgumentKind, impl core::convert::AsRef<[u8]>) impl aya_log_common::Argument for usize pub fn usize::as_argument(&self) -> (aya_log_common::ArgumentKind, impl core::convert::AsRef<[u8]>) +impl aya_log_common::Argument for [u8; N] +pub fn [u8; N]::as_argument(&self) -> (aya_log_common::ArgumentKind, impl core::convert::AsRef<[u8]>) pub trait aya_log_common::DefaultFormatter impl aya_log_common::DefaultFormatter for &str impl aya_log_common::DefaultFormatter for bool