rust-lang / rust-lang/rfcs

Reduce $crate usage (`local` and `private` for macros)

Open
#1,630 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-lang
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No implementation files or tests are named. Start by reviewing the proposed local and private trait behavior in the issue, including macro invocation visibility and reduced $crate usage. Done would require an agreed design, an implementation location, and tests covering the stated visibility differences.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.