Skip to content

Commit

Permalink
fix: macro hygiene
Browse files Browse the repository at this point in the history
Some macros where not hygienic, requiring the user to have specific
items in scope.
  • Loading branch information
CBenoit committed Nov 21, 2024
1 parent 2e59014 commit 69eba11
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
14 changes: 7 additions & 7 deletions crates/ironrdp-core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ macro_rules! not_enough_bytes_err {
$crate::not_enough_bytes_err($context, $received, $expected)
}};
( $received:expr , $expected:expr $(,)? ) => {{
not_enough_bytes_err!($crate::function!(), $received, $expected)
$crate::not_enough_bytes_err!($crate::function!(), $received, $expected)
}};
}

Expand Down Expand Up @@ -96,7 +96,7 @@ macro_rules! invalid_field_err {
$crate::invalid_field_err($context, $field, $reason)
}};
( $field:expr , $reason:expr $(,)? ) => {{
invalid_field_err!($crate::function!(), $field, $reason)
$crate::invalid_field_err!($crate::function!(), $field, $reason)
}};
}

Expand Down Expand Up @@ -127,7 +127,7 @@ macro_rules! unexpected_message_type_err {
$crate::unexpected_message_type_err($context, $got)
}};
( $got:expr $(,)? ) => {{
unexpected_message_type_err!($crate::function!(), $got)
$crate::unexpected_message_type_err!($crate::function!(), $got)
}};
}

Expand Down Expand Up @@ -158,7 +158,7 @@ macro_rules! unsupported_version_err {
$crate::unsupported_version_err($context, $got)
}};
( $got:expr $(,)? ) => {{
unsupported_version_err!($crate::function!(), $got)
$crate::unsupported_version_err!($crate::function!(), $got)
}};
}

Expand Down Expand Up @@ -190,7 +190,7 @@ macro_rules! unsupported_value_err {
$crate::unsupported_value_err($context, $name, $value)
}};
( $name:expr, $value:expr $(,)? ) => {{
unsupported_value_err!($crate::function!(), $name, $value)
$crate::unsupported_value_err!($crate::function!(), $name, $value)
}};
}

Expand Down Expand Up @@ -237,10 +237,10 @@ macro_rules! other_err {
$crate::other_err($context, $description)
}};
( source: $source:expr $(,)? ) => {{
other_err!($crate::function!(), source: $source)
$crate::other_err!($crate::function!(), source: $source)
}};
( $description:expr $(,)? ) => {{
other_err!($crate::function!(), $description)
$crate::other_err!($crate::function!(), $description)
}};
}

Expand Down
4 changes: 4 additions & 0 deletions crates/ironrdp-pdu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,7 @@ mod legacy {
fn buffer_length(&self) -> usize;
}
}

// Private! Used by the macros.
#[doc(hidden)]
pub use ironrdp_core;
34 changes: 18 additions & 16 deletions crates/ironrdp-pdu/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
#[macro_export]
macro_rules! decode_err {
($source:expr $(,)? ) => {
<$crate::PduError as $crate::PduErrorExt>::decode(ironrdp_core::function!(), $source)
<$crate::PduError as $crate::PduErrorExt>::decode($crate::ironrdp_core::function!(), $source)
};
}

#[macro_export]
macro_rules! encode_err {
($source:expr $(,)? ) => {
<$crate::PduError as $crate::PduErrorExt>::encode(ironrdp_core::function!(), $source)
<$crate::PduError as $crate::PduErrorExt>::encode($crate::ironrdp_core::function!(), $source)
};
}

Expand All @@ -25,13 +25,15 @@ macro_rules! pdu_other_err {
$crate::PduError::new($context, $crate::PduErrorKind::Other { description: $description })
}};
( source: $source:expr $(,)? ) => {{
pdu_other_err!(ironrdp_core::function!(), "", source: $source)
$crate::pdu_other_err!($crate::ironrdp_core::function!(), "", source: $source)
}};
( $description:expr $(,)? ) => {{
pdu_other_err!(ironrdp_core::function!(), $description)
$crate::pdu_other_err!($crate::ironrdp_core::function!(), $description)
}};
}

// FIXME: some of these macros should be in ironrdp_core, and some should be private to ironrdp_pdu.

/// Asserts that constant expressions evaluate to `true`.
///
/// From <https://docs.rs/static_assertions/1.1.0/src/static_assertions/const_assert.rs.html#51-57>
Expand All @@ -50,17 +52,17 @@ macro_rules! const_assert {
#[macro_export]
macro_rules! impl_pdu_pod {
($pdu_ty:ty) => {
impl ::ironrdp_core::IntoOwned for $pdu_ty {
impl $crate::ironrdp_core::IntoOwned for $pdu_ty {
type Owned = Self;

fn into_owned(self) -> Self::Owned {
self
}
}

impl ironrdp_core::DecodeOwned for $pdu_ty {
impl $crate::ironrdp_core::DecodeOwned for $pdu_ty {
fn decode_owned(src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
<Self as ironrdp_core::Decode>::decode(src)
<Self as $crate::ironrdp_core::Decode>::decode(src)
}
}
};
Expand All @@ -70,17 +72,17 @@ macro_rules! impl_pdu_pod {
#[macro_export]
macro_rules! impl_x224_pdu_pod {
($pdu_ty:ty) => {
impl ::ironrdp_core::IntoOwned for $pdu_ty {
impl $crate::ironrdp_core::IntoOwned for $pdu_ty {
type Owned = Self;

fn into_owned(self) -> Self::Owned {
self
}
}

impl ::ironrdp_core::DecodeOwned for $pdu_ty {
impl $crate::ironrdp_core::DecodeOwned for $pdu_ty {
fn decode_owned(src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
<$crate::x224::X224<Self> as ::ironrdp_core::Decode>::decode(src).map(|p| p.0)
<$crate::x224::X224<Self> as $crate::ironrdp_core::Decode>::decode(src).map(|p| p.0)
}
}
};
Expand All @@ -92,10 +94,10 @@ macro_rules! impl_pdu_borrowing {
($pdu_ty:ident $(<$($lt:lifetime),+>)?, $owned_ty:ident) => {
pub type $owned_ty = $pdu_ty<'static>;

impl ironrdp_core::DecodeOwned for $owned_ty {
impl $crate::ironrdp_core::DecodeOwned for $owned_ty {
fn decode_owned(src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
let pdu = <$pdu_ty $(<$($lt),+>)? as ::ironrdp_core::Decode>::decode(src)?;
Ok(::ironrdp_core::IntoOwned::into_owned(pdu))
let pdu = <$pdu_ty $(<$($lt),+>)? as $crate::ironrdp_core::Decode>::decode(src)?;
Ok($crate::ironrdp_core::IntoOwned::into_owned(pdu))
}
}
};
Expand All @@ -107,10 +109,10 @@ macro_rules! impl_x224_pdu_borrowing {
($pdu_ty:ident $(<$($lt:lifetime),+>)?, $owned_ty:ident) => {
pub type $owned_ty = $pdu_ty<'static>;

impl ::ironrdp_core::DecodeOwned for $owned_ty {
impl $crate::ironrdp_core::DecodeOwned for $owned_ty {
fn decode_owned(src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
let pdu = <$crate::x224::X224<$pdu_ty $(<$($lt),+>)?> as ::ironrdp_core::Decode>::decode(src).map(|r| r.0)?;
Ok(::ironrdp_core::IntoOwned::into_owned(pdu))
let pdu = <$crate::x224::X224<$pdu_ty $(<$($lt),+>)?> as $crate::ironrdp_core::Decode>::decode(src).map(|r| r.0)?;
Ok($crate::ironrdp_core::IntoOwned::into_owned(pdu))
}
}
};
Expand Down

0 comments on commit 69eba11

Please sign in to comment.