-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Open
Labels
T-langRelevant to the language team, which will review and decide on the RFC.Relevant to the language team, which will review and decide on the RFC.
Description
Proposal
Macros can define traits, but not local and private traits. Local traits would be local to the macro invocation, private traits would be private to the macro.
E.g.
Before:
pub trait RwLockEvent<T: ::eventbus::Event> {
fn metadata_lock() -> &'static ::std::sync::RwLock<::eventbus::EventMetadata<T>>;
}
macro_rules! rwlock_event {
($t:ty) => (
impl $crate::macros::event::RwLockEvent<$t> for $t {
fn metadata_lock() -> &'static ::std::sync::RwLock<$crate::eventbus::EventMetadata<$t>> {
// TODO test whether this actually works
lazy_static! {
static ref EVENT_METADATA: ::std::sync::RwLock<$crate::eventbus::EventMetadata<$t>> = ::std::sync::RwLock::new($crate::eventbus::EventMetadata::new());
}
&EVENT_METADATA
}
}
impl Event for $t {
fn event_metadata<F, R>(f: F) -> R where F: FnOnce(&$crate::eventbus::EventMetadata<Self>) -> R {
f(&<Self as $crate::macros::event::RwLockEvent<$t>>::metadata_lock().read().unwrap())
}
fn mut_metadata<F, R>(f: F) -> R where F: FnOnce(&mut $crate::eventbus::EventMetadata<Self>) -> R {
f(&mut <Self as $crate::macros::event::RwLockEvent<$t>>::metadata_lock().write().unwrap())
}
}
);
}RwLockEvent is pub and can be manually implemented.
After:
macro_rules! rwlock_event {
($t:ty) => (
local trait RwLockEvent<T: ::eventbus::Event> {
fn metadata_lock() -> &'static ::std::sync::RwLock<::eventbus::EventMetadata<T>>;
}
impl RwLockEvent<$t> for $t {
fn metadata_lock() -> &'static ::std::sync::RwLock<$crate::eventbus::EventMetadata<$t>> {
// TODO test whether this actually works
lazy_static! {
static ref EVENT_METADATA: ::std::sync::RwLock<$crate::eventbus::EventMetadata<$t>> = ::std::sync::RwLock::new($crate::eventbus::EventMetadata::new());
}
&EVENT_METADATA
}
}
impl Event for $t {
fn event_metadata<F, R>(f: F) -> R where F: FnOnce(&$crate::eventbus::EventMetadata<Self>) -> R {
f(&<Self as RwLockEvent<$t>>::metadata_lock().read().unwrap())
}
fn mut_metadata<F, R>(f: F) -> R where F: FnOnce(&mut $crate::eventbus::EventMetadata<Self>) -> R {
f(&mut <Self as RwLockEvent<$t>>::metadata_lock().write().unwrap())
}
}
);
}RwLockEvent is macro-local and an implementation detail. Other invocations of the same macro cannot see the impl, and the trait doesn't pollute the invocation context.
For private, multiple invocations of the same macro would be able to see eachother, but the invocation context wouldn't.
Benefits
Significantly reduces dependency on $crate. private could be extended to extern crate and use for maximum $crate reduction.
Metadata
Metadata
Assignees
Labels
T-langRelevant to the language team, which will review and decide on the RFC.Relevant to the language team, which will review and decide on the RFC.