Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce $crate usage (local and private for macros) #1630

Open
SoniEx2 opened this issue May 24, 2016 · 1 comment
Open

Reduce $crate usage (local and private for macros) #1630

SoniEx2 opened this issue May 24, 2016 · 1 comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.

Comments

@SoniEx2
Copy link

SoniEx2 commented May 24, 2016

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.

@nrc nrc added the T-lang Relevant to the language team, which will review and decide on the RFC. label Aug 17, 2016
@nrc
Copy link
Member

nrc commented Aug 17, 2016

See #1561 for how we intend to eliminate $crate. Long-term plans for macro hygiene should address privacy issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

2 participants