-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
RFC: Allow external #[deriving(...)] implementations #11813
Comments
I think we need a namespacing solution. One possibility: extern mod some_crate;
#[deriving(some_crate::Trait)]
struct Foo { ... } but this would mean that |
Another possibility would be to have a If we wanted to resolve different deriving implementations, I can imagine things getting tricky fairly quickly, but it would certainly solve the namespace problem! |
Instead of hardcoding to The whole deriving infrastructure could then just switch to providing implementations for a argument to |
I think we need a namespacing solution for macros in general. I'm not totally sure how it should work, especially in relation to |
I'm interested in this. Here's a possible design: mylib.rs: pub trait MyTrait { ... } mylib_plugin.rs: extern crate mylib;
use mylib::MyTrait;
// signaure is like the functions in src/libsyntax/ext/deriving
fn expand_deriving_mytrait(cx: &mut ExtCtxt,
span: Span,
mitem: Gc<MetaItem>,
item: Gc<Item>,
push: |Gc<Item>|) {
...
}
#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
reg.register_deriving(quote_full_path!(MyTrait), expand_deriving_mytrait);
} user.rs: extern crate mylib;
#[phase(plugin)]
extern crate mylib_plugin;
use mylib::MyTrait;
#[deriving(MyTrait)]
struct Foo; The The implementation of What do people think? |
Adding support for name resolution to expansion would be awesome, but very nontrivial. The internals for that will definitely need to be thought through. |
@kmcallister Is it necessary to provide a full function like the ones from Almost all of these are of the form that there are one or two functions (let's call it Offshoot idea ahead, this is just something I realized could be done if we manage to do the above. Reducing boilerplate might even pave the way for easy-deriving generators:
Now we only need to define the scheme by which the return values of the method for the members get treed up to create the return value for the method called on the parent. For example, one can define
where we define how to increment ints and a few other objects, and then objects containing solely The plugin writer can define new schemes too -- which is something that probably is rather rare given the variety of deriving schemes which we already use. |
The above proposal has been pre-rfc'd here |
This issue has been moved to the RFCs repo: rust-lang/rfcs#480 |
Background
Now that #11151 has landed, third-party code can define syntax extensions just as the compiler does - with the exception of adding new types to
#[deriving(..)]
. It is possible to write an extension that does the same thing as#[deriving(..)]
does, but it has to be implemented as#[deriving_MyTrait]
instead of#[deriving(MyTrait)]
. This unfortunate, especially when a user wants to derive multiple first- and third-party traits:The repetition of multiple
#[deriving_Foo]
s was the driving force for the creation of#[deriving(..)]
in the first place!The Proposal
Change the signature of the macro registration function to this:
#[deriving(..)]
types will be registered by the extension infrastructure just like other syntax extensions are.This does have a (potentially) large downside: the extension infrastructure will have to have special knowledge of
#[deriving(..)]
to keep track of the registered extensions. I think it's probably worth it, and there are already some other "special" extensions liketrace_macros!
. What does everyone else think?The text was updated successfully, but these errors were encountered: