Skip to content

Commit c21fa34

Browse files
committed
---
yaml --- r: 52038 b: refs/heads/dist-snap c: efb8711 h: refs/heads/master v: v3
1 parent 283f609 commit c21fa34

File tree

12 files changed

+74
-18
lines changed

12 files changed

+74
-18
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c
99
refs/heads/incoming: 44d4d6de762f3f9aae1fedcf454c66b79b3ad58d
10-
refs/heads/dist-snap: 2b6c456bf648e5a7b408b550649d409378e39539
10+
refs/heads/dist-snap: efb8711f90778db1031053e3f379a8dcc92be6c6
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1313
refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0

branches/dist-snap/doc/rust.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,19 @@ let mut a: Animal = Dog;
11071107
a = Cat;
11081108
~~~~
11091109

1110+
Enumeration constructors can have either named or unnamed fields:
1111+
~~~~
1112+
enum Animal {
1113+
Dog (~str, float),
1114+
Cat { name: ~str, weight: float }
1115+
}
1116+
1117+
let mut a: Animal = Dog(~"Cocoa", 37.2);
1118+
a = Cat{ name: ~"Spotty", weight: 2.7 };
1119+
~~~~
1120+
1121+
In this example, `Cat` is a _struct-like enum variant_,
1122+
whereas `Dog` is simply called an enum variant.
11101123
### Constants
11111124

11121125
~~~~~~~~ {.ebnf .gram}

branches/dist-snap/doc/tutorial.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,24 @@ fn point_from_direction(dir: Direction) -> Point {
733733
}
734734
~~~~
735735

736+
A special kind of enum variant, called _struct-like enums_,
737+
can have its fields extracted with dot notation and not just destructuring.
738+
For example:
739+
740+
~~~~
741+
# struct Point {x: float, y: float}
742+
# fn square(x: float) -> float { x * x }
743+
enum Shape {
744+
Circle { center: Point, radius: float },
745+
Rectangle { left: Point, right: Point }
746+
}
747+
fn area(sh: Shape) -> float {
748+
match sh {
749+
Circle(c) => float::consts::pi * square(c.radius),
750+
Rectangle(r) => r.right.x - r.left.x * r.right.y - r.right.y
751+
}
752+
}
753+
~~~~
736754
## Tuples
737755

738756
Tuples in Rust behave exactly like structs, except that their fields

branches/dist-snap/src/etc/vim/syntax/rust.vim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
" Language: Rust
33
" Maintainer: Patrick Walton <[email protected]>
44
" Maintainer: Ben Blum <[email protected]>
5-
" Last Change: 2012 Dec 14
5+
" Last Change: 2012 Dec 25
66

77
if version < 600
88
syntax clear
@@ -44,8 +44,8 @@ syn keyword rustType off_t dev_t ino_t pid_t mode_t ssize_t
4444

4545
syn keyword rustTrait Const Copy Send Owned " inherent traits
4646
syn keyword rustTrait Eq Ord Num Ptr
47-
syn keyword rustTrait Add Sub Mul Div Modulo Neg BitAnd BitOr BitXor
48-
syn keyword rustTrait Shl Shr Index
47+
syn keyword rustTrait Drop Add Sub Mul Div Modulo Neg BitAnd BitOr
48+
syn keyword rustTrait BitXor Shl Shr Index
4949

5050
syn keyword rustSelf self
5151
syn keyword rustBoolean true false

branches/dist-snap/src/librustc/middle/trans/base.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,10 @@ fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t,
545545
j += 1u;
546546
}
547547
}
548-
_ => cx.tcx().sess.bug(~"iter_variant: not a function type")
548+
_ => cx.tcx().sess.bug(fmt!("iter_variant: not a function type: \
549+
%s (variant name = %s)",
550+
cx.ty_to_str(fn_ty),
551+
cx.sess().str_of(variant.name)))
549552
}
550553
return cx;
551554
}

branches/dist-snap/src/librustc/middle/typeck/collect.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,26 @@ fn get_enum_variant_types(ccx: @crate_ctxt,
158158
result_ty = Some(enum_ty);
159159
}
160160
ast::struct_variant_kind(struct_def) => {
161-
result_ty = Some(enum_ty);
162161
// XXX: Merge with computation of the the same value below?
163162
let tpt = {bounds: ty_param_bounds(ccx, ty_params),
164163
region_param: rp,
165164
ty: enum_ty};
166165
convert_struct(
167166
ccx, rp, struct_def, ty_params, tpt, variant.node.id);
167+
// Compute the ctor arg types from the struct fields
168+
let struct_fields = do struct_def.fields.map |struct_field| {
169+
{mode: ast::expl(ast::by_val),
170+
ty: ty::node_id_to_type(ccx.tcx, (*struct_field).node.id)
171+
}
172+
};
173+
result_ty = Some(ty::mk_fn(tcx, FnTyBase {
174+
meta: FnMeta {purity: ast::pure_fn,
175+
proto: ast::ProtoBare,
176+
onceness: ast::Many,
177+
bounds: @~[],
178+
region: ty::re_static,
179+
ret_style: ast::return_val},
180+
sig: FnSig {inputs: struct_fields, output: enum_ty }}));
168181
}
169182
ast::enum_variant_kind(ref enum_definition) => {
170183
get_enum_variant_types(ccx,

branches/dist-snap/src/libstd/ebml.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ pub mod reader {
360360
f()
361361
}
362362

363-
fn read_struct<T>(&self, name: &str, _len: uint, f: fn() -> T) -> T {
363+
fn read_struct<T>(&self, name: &str, f: fn() -> T) -> T {
364364
debug!("read_struct(name=%s)", name);
365365
f()
366366
}
@@ -631,7 +631,7 @@ pub mod writer {
631631
}
632632

633633
fn emit_rec(&self, f: fn()) { f() }
634-
fn emit_struct(&self, _name: &str, _len: uint, f: fn()) { f() }
634+
fn emit_struct(&self, _name: &str, f: fn()) { f() }
635635
fn emit_field(&self, name: &str, _idx: uint, f: fn()) {
636636
self._emit_label(name);
637637
f()

branches/dist-snap/src/libstd/json.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub impl Encoder: serialize::Encoder {
148148
f();
149149
self.wr.write_char('}');
150150
}
151-
fn emit_struct(&self, _name: &str, _len: uint, f: fn()) {
151+
fn emit_struct(&self, _name: &str, f: fn()) {
152152
self.wr.write_char('{');
153153
f();
154154
self.wr.write_char('}');
@@ -261,7 +261,7 @@ pub impl PrettyEncoder: serialize::Encoder {
261261
self.indent -= 2;
262262
self.wr.write_char('}');
263263
}
264-
fn emit_struct(&self, _name: &str, _len: uint, f: fn()) {
264+
fn emit_struct(&self, _name: &str, f: fn()) {
265265
self.emit_rec(f)
266266
}
267267
fn emit_field(&self, name: &str, idx: uint, f: fn()) {
@@ -861,7 +861,7 @@ pub impl Decoder: serialize::Decoder {
861861
move value
862862
}
863863

864-
fn read_struct<T>(&self, _name: &str, _len: uint, f: fn() -> T) -> T {
864+
fn read_struct<T>(&self, _name: &str, f: fn() -> T) -> T {
865865
debug!("read_struct()");
866866
let value = f();
867867
self.pop();

branches/dist-snap/src/libstd/prettyprint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ pub impl Encoder: serialize::Encoder {
160160
self.wr.write_str(~"}");
161161
}
162162

163-
fn emit_struct(&self, name: &str, _len: uint, f: fn()) {
163+
fn emit_struct(&self, name: &str, f: fn()) {
164164
self.wr.write_str(fmt!("%s {", name));
165165
f();
166166
self.wr.write_str(~"}");

branches/dist-snap/src/libstd/serialize.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub trait Encoder {
5454
fn emit_vec_elt(&self, idx: uint, f: fn());
5555

5656
fn emit_rec(&self, f: fn());
57-
fn emit_struct(&self, name: &str, _len: uint, f: fn());
57+
fn emit_struct(&self, name: &str, f: fn());
5858
fn emit_field(&self, f_name: &str, f_idx: uint, f: fn());
5959

6060
fn emit_tup(&self, len: uint, f: fn());
@@ -95,7 +95,7 @@ pub trait Decoder {
9595
fn read_vec_elt<T>(&self, idx: uint, f: fn() -> T) -> T;
9696

9797
fn read_rec<T>(&self, f: fn() -> T) -> T;
98-
fn read_struct<T>(&self, name: &str, _len: uint, f: fn() -> T) -> T;
98+
fn read_struct<T>(&self, name: &str, f: fn() -> T) -> T;
9999
fn read_field<T>(&self, name: &str, idx: uint, f: fn() -> T) -> T;
100100

101101
fn read_tup<T>(&self, sz: uint, f: fn() -> T) -> T;

branches/dist-snap/src/libsyntax/ext/auto_encode.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ would generate two implementations like:
2525
2626
impl<S: Encoder> node_id: Encodable<S> {
2727
fn encode(s: &S) {
28-
do s.emit_struct("Node", 1) {
28+
do s.emit_struct("Node") {
2929
s.emit_field("id", 0, || s.emit_uint(self))
3030
}
3131
}
3232
}
3333
3434
impl<D: Decoder> node_id: Decodable {
3535
static fn decode(d: &D) -> Node {
36-
do d.read_struct("Node", 1) {
36+
do d.read_struct("Node") {
3737
Node {
3838
id: d.read_field(~"x", 0, || decode(d))
3939
}
@@ -686,7 +686,6 @@ fn mk_struct_ser_impl(
686686
),
687687
~[
688688
cx.lit_str(span, @cx.str_of(ident)),
689-
cx.lit_uint(span, vec::len(fields)),
690689
cx.lambda_stmts(span, fields),
691690
]
692691
);
@@ -713,7 +712,6 @@ fn mk_struct_deser_impl(
713712
),
714713
~[
715714
cx.lit_str(span, @cx.str_of(ident)),
716-
cx.lit_uint(span, vec::len(fields)),
717715
cx.lambda_expr(
718716
cx.expr(
719717
span,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
enum Animal {
2+
Dog (~str, float),
3+
Cat { name: ~str, weight: float }
4+
}
5+
6+
fn main() {
7+
let mut a: Animal = Dog(~"Cocoa", 37.2);
8+
a = Cat{ name: ~"Spotty", weight: 2.7 };
9+
// permuting the fields should work too
10+
let c = Cat { weight: 3.1, name: ~"Spreckles" };
11+
}

0 commit comments

Comments
 (0)