Skip to content

Commit 2733b18

Browse files
committed
auto merge of #9250 : erickt/rust/num, r=erickt
This PR solves one of the pain points with c-style enums. Simplifies writing a fn to convert from an int/uint to an enum. It does this through a `#[deriving(FromPrimitive)]` syntax extension. Before this is committed though, we need to discuss if `ToPrimitive`/`FromPrimitive` has the right design (cc #4819). I've changed all the `.to_int()` and `from_int()` style functions to return `Option<int>` so we can handle partial functions. For this PR though only enums and `extra::num::bigint::*` take advantage of returning None for unrepresentable values. In the long run it'd be better if `i64.to_i8()` returned `None` if the value was too large, but I'll save this for a future PR. Closes #3868.
2 parents 0c388be + 0e8ad4d commit 2733b18

25 files changed

+1634
-396
lines changed

doc/rust.md

+1
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,7 @@ Supported traits for `deriving` are:
17751775
`obj.to_str()` has similar output as `fmt!("%?", obj)`, but it differs in that
17761776
each constituent field of the type must also implement `ToStr` and will have
17771777
`field.to_str()` invoked to build up the result.
1778+
* `FromPrimitive`, to create an instance from a numeric primitve.
17781779

17791780
### Stability
17801781
One can indicate the stability of an API using the following attributes:

0 commit comments

Comments
 (0)