Skip to content

Commit

Permalink
auto merge of #9250 : erickt/rust/num, r=erickt
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Oct 5, 2013
2 parents 0c388be + 0e8ad4d commit 2733b18
Show file tree
Hide file tree
Showing 25 changed files with 1,634 additions and 396 deletions.
1 change: 1 addition & 0 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,7 @@ Supported traits for `deriving` are:
`obj.to_str()` has similar output as `fmt!("%?", obj)`, but it differs in that
each constituent field of the type must also implement `ToStr` and will have
`field.to_str()` invoked to build up the result.
* `FromPrimitive`, to create an instance from a numeric primitve.

### Stability
One can indicate the stability of an API using the following attributes:
Expand Down
Loading

0 comments on commit 2733b18

Please sign in to comment.