]> git.lizzy.rs Git - rust.git/commitdiff
Add example impl in CLike docs. Fix 13752.
authorSimon Sapin <simon.sapin@exyr.org>
Thu, 6 Nov 2014 23:34:09 +0000 (15:34 -0800)
committerSimon Sapin <simon.sapin@exyr.org>
Fri, 7 Nov 2014 02:16:18 +0000 (18:16 -0800)
src/libcollections/enum_set.rs

index 93dbb7e8b2073b24a86dd5adbc35f8180846f190..1acdaef9c91c1c98617525d28d23bdcf3199647c 100644 (file)
@@ -43,7 +43,27 @@ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
     }
 }
 
-/// 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;