Add example impl in CLike docs. Fix 13752.
This commit is contained in:
parent
a22772d6a6
commit
d8ab2f87c1
@ -43,7 +43,27 @@ impl<E:CLike+fmt::Show> fmt::Show for EnumSet<E> {
|
||||
}
|
||||
}
|
||||
|
||||
/// An interface for casting C-like enum to uint and back.
|
||||
/**
|
||||
An interface for casting C-like enum to uint and back.
|
||||
A typically implementation is as below.
|
||||
|
||||
```{rust,ignore}
|
||||
#[repr(uint)]
|
||||
enum Foo {
|
||||
A, B, C
|
||||
}
|
||||
|
||||
impl CLike for Foo {
|
||||
fn to_uint(&self) -> uint {
|
||||
*self as uint
|
||||
}
|
||||
|
||||
fn from_uint(v: uint) -> Foo {
|
||||
unsafe { mem::transmute(v) }
|
||||
}
|
||||
}
|
||||
```
|
||||
*/
|
||||
pub trait CLike {
|
||||
/// Converts a C-like enum to a `uint`.
|
||||
fn to_uint(&self) -> uint;
|
||||
|
Loading…
x
Reference in New Issue
Block a user