Skip to content

Commit

Permalink
refactor(core/message): decompose
Browse files Browse the repository at this point in the history
  • Loading branch information
loyd committed May 25, 2024
1 parent 63fa14f commit a55d923
Show file tree
Hide file tree
Showing 5 changed files with 849 additions and 806 deletions.
50 changes: 0 additions & 50 deletions elfo-core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,56 +33,6 @@ macro_rules! assert_msg_eq {
}};
}

/// Overrides the protocol name for all messages in the current module.
/// Can be used only once per module. Submodules inherit this override
/// if `use super::*` is used.
#[macro_export]
macro_rules! set_protocol {
($protocol:literal) => {
#[doc(hidden)]
struct _ElfoProtocolHolder;
impl $crate::_priv::ProtocolHolder for _ElfoProtocolHolder {
const PROTOCOL: Option<&'static str> = Some($protocol);
}

#[doc(hidden)]
trait _ElfoProtocolOverride {
fn holder(&self) -> _ElfoProtocolHolder {
_ElfoProtocolHolder
}
}
impl _ElfoProtocolOverride for $crate::_priv::ProtocolExtractor {}
};
}

// See https://github.com/GoldsteinE/gh-blog/blob/master/const_deref_specialization/src/lib.md
#[doc(hidden)]
#[macro_export]
macro_rules! get_protocol {
() => {{
use $crate::_priv::{ProtocolExtractor, ProtocolHolder};

const fn extract_protocol_name<H: ProtocolHolder>(_: &impl FnOnce() -> H) -> &'static str {
// Get a protocol name from the relevant `set_protocol!` call.
if let Some(proto_override) = H::PROTOCOL {
return proto_override;
}

// If `set_protocol!` hasn't been used, take the package name.
if let Some(pkg_name) = option_env!("CARGO_PKG_NAME") {
return pkg_name;
}

panic!(
"if the crate is built without cargo, \
the protocol must be set explicitly by calling `elfo::set_protocol!(..)`"
);
}

extract_protocol_name(&|| ProtocolExtractor.holder())
}};
}

macro_rules! cfg_network {
// Force `{..}` to make rustfmt work.
({$($item:item)*}) => {
Expand Down
Loading

0 comments on commit a55d923

Please sign in to comment.