Add `#[derive_Rand]`, drop-in `#[deriving(Rand)]` replacement.
diff --git a/.gitignore b/.gitignore
index 4fffb2f..46db1ee 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,4 @@
/target
/Cargo.lock
+/derive_rand/target
+/derive_rand/Cargo.lock
diff --git a/Cargo.toml b/Cargo.toml
index 364c2d5..6821c7f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -15,3 +15,6 @@
[dependencies]
log = "0.2.1"
libc = "0.1.1"
+
+[dev-dependencies.derive_rand]
+path = "derive_rand"
diff --git a/derive_rand/Cargo.toml b/derive_rand/Cargo.toml
new file mode 100644
index 0000000..45e6af1
--- /dev/null
+++ b/derive_rand/Cargo.toml
@@ -0,0 +1,17 @@
+[package]
+
+name = "derive_rand"
+version = "0.1.1"
+authors = ["The Rust Project Developers"]
+license = "MIT/Apache-2.0"
+readme = "README.md"
+repository = "https://github.com/rust-lang/rand"
+documentation = "http://doc.rust-lang.org/rand"
+homepage = "https://github.com/rust-lang/rand"
+description = """
+`#[derive]`-like functionality for the `rand::Rand` trait.
+"""
+
+[lib]
+name = "derive_rand"
+plugin = true
diff --git a/derive_rand/README.md b/derive_rand/README.md
new file mode 100644
index 0000000..83bfbf9
--- /dev/null
+++ b/derive_rand/README.md
@@ -0,0 +1,22 @@
+`#[derive]`-like functionality for the `rand::Rand` trait.
+
+## Example
+
+```rust
+#![feature(plugin)]
+
+#[plugin] #[no_link] extern crate derive_rand;
+extern crate rand;
+
+#[derive_Rand]
+struct Foo {
+ x: u8,
+ y: isize
+}
+
+#[derive_Rand]
+enum Bar {
+ X(char),
+ Y(f64)
+}
+```
diff --git a/derive_rand/src/lib.rs b/derive_rand/src/lib.rs
new file mode 100644
index 0000000..74f4b8c
--- /dev/null
+++ b/derive_rand/src/lib.rs
@@ -0,0 +1,179 @@
+// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(rustc_private, plugin_registrar)]
+
+extern crate syntax;
+extern crate rustc;
+
+use syntax::ast::{MetaItem, Item, Expr};
+use syntax::ast;
+use syntax::codemap::Span;
+use syntax::ext::base;
+use syntax::ext::base::ExtCtxt;
+use syntax::ext::build::AstBuilder;
+use syntax::ext::deriving::generic::*;
+use syntax::ext::deriving::generic::ty::*;
+use syntax::parse::token;
+use syntax::ptr::P;
+use rustc::plugin::Registry;
+
+#[plugin_registrar]
+pub fn plugin_registrar(reg: &mut Registry) {
+ reg.register_syntax_extension(token::intern("derive_Rand"),
+ base::Decorator(Box::new(expand_deriving_rand)));
+}
+
+
+
+
+pub fn expand_deriving_rand(cx: &mut ExtCtxt,
+ span: Span,
+ mitem: &MetaItem,
+ item: &Item,
+ mut push: Box<FnMut(P<Item>)>) {
+ let trait_def = TraitDef {
+ span: span,
+ attributes: Vec::new(),
+ path: Path::new(vec!("rand", "Rand")),
+ additional_bounds: Vec::new(),
+ generics: LifetimeBounds::empty(),
+ methods: vec!(
+ MethodDef {
+ name: "rand",
+ generics: LifetimeBounds {
+ lifetimes: Vec::new(),
+ bounds: vec!(("R",
+ vec!( Path::new(vec!("rand", "Rng")) )))
+ },
+ explicit_self: None,
+ args: vec!(
+ Ptr(Box::new(Literal(Path::new_local("R"))),
+ Borrowed(None, ast::MutMutable))
+ ),
+ ret_ty: Self,
+ attributes: Vec::new(),
+ combine_substructure: combine_substructure(Box::new(|a, b, c| {
+ rand_substructure(a, b, c)
+ }))
+ }
+ ),
+ associated_types: Vec::new(),
+ };
+ trait_def.expand(cx, mitem, item, |i| push(i))
+}
+
+fn rand_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> {
+ let rng = match substr.nonself_args {
+ [ref rng] => rng,
+ _ => cx.bug("Incorrect number of arguments to `rand` in `derive(Rand)`")
+ };
+ let rand_ident = vec!(
+ cx.ident_of("rand"),
+ cx.ident_of("Rand"),
+ cx.ident_of("rand")
+ );
+ let mut rand_call = |&mut: cx: &mut ExtCtxt, span| {
+ cx.expr_call_global(span,
+ rand_ident.clone(),
+ vec!(rng.clone()))
+ };
+
+ return match *substr.fields {
+ StaticStruct(_, ref summary) => {
+ let path = cx.path_ident(trait_span, substr.type_ident);
+ rand_thing(cx, trait_span, path, summary, rand_call)
+ }
+ StaticEnum(_, ref variants) => {
+ if variants.is_empty() {
+ cx.span_err(trait_span, "`Rand` cannot be derived for enums with no variants");
+ // let compilation continue
+ return cx.expr_usize(trait_span, 0);
+ }
+
+ let variant_count = cx.expr_usize(trait_span, variants.len());
+
+ let rand_name = cx.path_all(trait_span,
+ true,
+ rand_ident.clone(),
+ Vec::new(),
+ Vec::new(),
+ Vec::new());
+ let rand_name = cx.expr_path(rand_name);
+
+ // ::rand::Rand::rand(rng)
+ let rv_call = cx.expr_call(trait_span,
+ rand_name,
+ vec!(rng.clone()));
+
+ // need to specify the usize-ness of the random number
+ let usize_ty = cx.ty_ident(trait_span, cx.ident_of("usize"));
+ let value_ident = cx.ident_of("__value");
+ let let_statement = cx.stmt_let_typed(trait_span,
+ false,
+ value_ident,
+ usize_ty,
+ rv_call);
+
+ // rand() % variants.len()
+ let value_ref = cx.expr_ident(trait_span, value_ident);
+ let rand_variant = cx.expr_binary(trait_span,
+ ast::BiRem,
+ value_ref,
+ variant_count);
+
+ let mut arms = variants.iter().enumerate().map(|(i, &(ident, v_span, ref summary))| {
+ let i_expr = cx.expr_usize(v_span, i);
+ let pat = cx.pat_lit(v_span, i_expr);
+
+ let path = cx.path(v_span, vec![substr.type_ident, ident]);
+ let thing = rand_thing(cx, v_span, path, summary, |cx, sp| rand_call(cx, sp));
+ cx.arm(v_span, vec!( pat ), thing)
+ }).collect::<Vec<ast::Arm> >();
+
+ // _ => {} at the end. Should never occur
+ arms.push(cx.arm_unreachable(trait_span));
+
+ let match_expr = cx.expr_match(trait_span, rand_variant, arms);
+
+ let block = cx.block(trait_span, vec!( let_statement ), Some(match_expr));
+ cx.expr_block(block)
+ }
+ _ => cx.bug("Non-static method in `derive(Rand)`")
+ };
+
+ fn rand_thing<F>(cx: &mut ExtCtxt,
+ trait_span: Span,
+ ctor_path: ast::Path,
+ summary: &StaticFields,
+ mut rand_call: F)
+ -> P<Expr> where
+ F: FnMut(&mut ExtCtxt, Span) -> P<Expr>,
+ {
+ let path = cx.expr_path(ctor_path.clone());
+ match *summary {
+ Unnamed(ref fields) => {
+ if fields.is_empty() {
+ path
+ } else {
+ let exprs = fields.iter().map(|span| rand_call(cx, *span)).collect();
+ cx.expr_call(trait_span, path, exprs)
+ }
+ }
+ Named(ref fields) => {
+ let rand_fields = fields.iter().map(|&(ident, span)| {
+ let e = rand_call(cx, span);
+ cx.field_imm(span, ident, e)
+ }).collect();
+ cx.expr_struct(trait_span, ctor_path, rand_fields)
+ }
+ }
+ }
+}
diff --git a/tests/derive_rand.rs b/tests/derive_rand.rs
new file mode 100755
index 0000000..17acd5f
--- /dev/null
+++ b/tests/derive_rand.rs
@@ -0,0 +1,30 @@
+#![feature(plugin)]
+
+#[plugin] #[no_link] extern crate derive_rand;
+extern crate rand;
+
+use rand::Rng;
+
+
+#[derive_Rand]
+struct Foo {
+ x: u8,
+ y: isize
+}
+
+#[derive_Rand]
+enum Bar {
+ X(char),
+ Y(f64)
+}
+
+#[test]
+fn smoke() {
+ let mut rng = rand::XorShiftRng::new_unseeded();
+
+ // check nothing horrible happens internally:
+ for _ in 0..100 {
+ let _: Foo = rng.gen();
+ let _: Bar = rng.gen();
+ }
+}