From d8ab2f87c1705bff4f212cfb6d00ee4770abc1e1 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Thu, 6 Nov 2014 15:34:09 -0800 Subject: [PATCH] Add example impl in CLike docs. Fix 13752. --- src/libcollections/enum_set.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/libcollections/enum_set.rs b/src/libcollections/enum_set.rs index 93dbb7e8b207..1acdaef9c91c 100644 --- a/src/libcollections/enum_set.rs +++ b/src/libcollections/enum_set.rs @@ -43,7 +43,27 @@ impl fmt::Show for EnumSet { } } -/// 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;