]> git.lizzy.rs Git - rust.git/commitdiff
Remove usage of private enum variants
authorAlex Crichton <alex@alexcrichton.com>
Wed, 16 Apr 2014 01:05:38 +0000 (18:05 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 16 Apr 2014 02:17:44 +0000 (19:17 -0700)
This replaces all uses of private enum variants with a struct that has
one private field pointing at a private enum.

RFC: 0006-remove-priv

src/liblibc/lib.rs
src/librand/distributions/gamma.rs
src/librustc/middle/trans/debuginfo.rs
src/libsyntax/util/small_vector.rs

index 18e815e9b7c03001cff84de8dc205ca9bbd5c7ed..379d3cdc94e4f73e184f3e2bbf9d0eb82fdbd793 100644 (file)
@@ -229,8 +229,8 @@ pub mod c95 {
             */
             #[repr(u8)]
             pub enum c_void {
-                priv variant1,
-                priv variant2
+                __variant1,
+                __variant2,
             }
             pub enum FILE {}
             pub enum fpos_t {}
index 6ec8411495a53f80ff3dce78d932f051bbe72ff7..dd249a1fbcac8eb5cd6936dbba1cb3da357f24f2 100644 (file)
 /// for Generating Gamma Variables" *ACM Trans. Math. Softw.* 26, 3
 /// (September 2000),
 /// 363-372. DOI:[10.1145/358407.358414](http://doi.acm.org/10.1145/358407.358414)
-pub enum Gamma {
-    priv Large(GammaLargeShape),
-    priv One(Exp),
-    priv Small(GammaSmallShape)
+pub struct Gamma {
+    repr: GammaRepr,
+}
+
+enum GammaRepr {
+    Large(GammaLargeShape),
+    One(Exp),
+    Small(GammaSmallShape)
 }
 
 // These two helpers could be made public, but saving the
@@ -90,11 +94,12 @@ pub fn new(shape: f64, scale: f64) -> Gamma {
         assert!(shape > 0.0, "Gamma::new called with shape <= 0");
         assert!(scale > 0.0, "Gamma::new called with scale <= 0");
 
-        match shape {
+        let repr = match shape {
             1.0        => One(Exp::new(1.0 / scale)),
             0.0 .. 1.0 => Small(GammaSmallShape::new_raw(shape, scale)),
             _          => Large(GammaLargeShape::new_raw(shape, scale))
-        }
+        };
+        Gamma { repr: repr }
     }
 }
 
@@ -131,7 +136,7 @@ fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
 
 impl IndependentSample<f64> for Gamma {
     fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
-        match *self {
+        match self.repr {
             Small(ref g) => g.ind_sample(rng),
             One(ref g) => g.ind_sample(rng),
             Large(ref g) => g.ind_sample(rng),
@@ -183,24 +188,29 @@ fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
 /// let v = chi.ind_sample(&mut rand::task_rng());
 /// println!("{} is from a χ²(11) distribution", v)
 /// ```
-pub enum ChiSquared {
+pub struct ChiSquared {
+    repr: ChiSquaredRepr,
+}
+
+enum ChiSquaredRepr {
     // k == 1, Gamma(alpha, ..) is particularly slow for alpha < 1,
     // e.g. when alpha = 1/2 as it would be for this case, so special-
     // casing and using the definition of N(0,1)^2 is faster.
-    priv DoFExactlyOne,
-    priv DoFAnythingElse(Gamma)
+    DoFExactlyOne,
+    DoFAnythingElse(Gamma),
 }
 
 impl ChiSquared {
     /// Create a new chi-squared distribution with degrees-of-freedom
     /// `k`. Fails if `k < 0`.
     pub fn new(k: f64) -> ChiSquared {
-        if k == 1.0 {
+        let repr = if k == 1.0 {
             DoFExactlyOne
         } else {
             assert!(k > 0.0, "ChiSquared::new called with `k` < 0");
             DoFAnythingElse(Gamma::new(0.5 * k, 2.0))
-        }
+        };
+        ChiSquared { repr: repr }
     }
 }
 impl Sample<f64> for ChiSquared {
@@ -208,7 +218,7 @@ fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
 }
 impl IndependentSample<f64> for ChiSquared {
     fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
-        match *self {
+        match self.repr {
             DoFExactlyOne => {
                 // k == 1 => N(0,1)^2
                 let StandardNormal(norm) = rng.gen::<StandardNormal>();
index ae58e153258ef467d9726aab94a31858b37c623e..0514642c583908d5e65a8f184fe70b08d0b65ee0 100644 (file)
@@ -206,15 +206,19 @@ pub fn new(llmod: ModuleRef) -> CrateDebugContext {
     }
 }
 
-pub enum FunctionDebugContext {
-    priv FunctionDebugContext(~FunctionDebugContextData),
-    priv DebugInfoDisabled,
-    priv FunctionWithoutDebugInfo,
+pub struct FunctionDebugContext {
+    repr: FunctionDebugContextRepr,
+}
+
+enum FunctionDebugContextRepr {
+    FunctionDebugContext(~FunctionDebugContextData),
+    DebugInfoDisabled,
+    FunctionWithoutDebugInfo,
 }
 
 impl FunctionDebugContext {
     fn get_ref<'a>(&'a self, cx: &CrateContext, span: Span) -> &'a FunctionDebugContextData {
-        match *self {
+        match self.repr {
             FunctionDebugContext(~ref data) => data,
             DebugInfoDisabled => {
                 cx.sess().span_bug(span, FunctionDebugContext::debuginfo_disabled_message());
@@ -544,7 +548,7 @@ pub fn create_argument_metadata(bcx: &Block, arg: &ast::Arg) {
 pub fn set_source_location(fcx: &FunctionContext,
                            node_id: ast::NodeId,
                            span: Span) {
-    match fcx.debug_context {
+    match fcx.debug_context.repr {
         DebugInfoDisabled => return,
         FunctionWithoutDebugInfo => {
             set_debug_location(fcx.ccx, UnknownLocation);
@@ -585,7 +589,7 @@ pub fn clear_source_location(fcx: &FunctionContext) {
 /// and must therefore be called before the first real statement/expression of the function is
 /// translated.
 pub fn start_emitting_source_locations(fcx: &FunctionContext) {
-    match fcx.debug_context {
+    match fcx.debug_context.repr {
         FunctionDebugContext(~ref data) => {
             data.source_locations_enabled.set(true)
         },
@@ -603,7 +607,7 @@ pub fn create_function_debug_context(cx: &CrateContext,
                                      param_substs: Option<@param_substs>,
                                      llfn: ValueRef) -> FunctionDebugContext {
     if cx.sess().opts.debuginfo == NoDebugInfo {
-        return DebugInfoDisabled;
+        return FunctionDebugContext { repr: DebugInfoDisabled };
     }
 
     // Clear the debug location so we don't assign them in the function prelude. Do this here
@@ -611,7 +615,7 @@ pub fn create_function_debug_context(cx: &CrateContext,
     set_debug_location(cx, UnknownLocation);
 
     if fn_ast_id == -1 {
-        return FunctionWithoutDebugInfo;
+        return FunctionDebugContext { repr: FunctionWithoutDebugInfo };
     }
 
     let empty_generics = ast::Generics { lifetimes: Vec::new(), ty_params: OwnedSlice::empty() };
@@ -678,7 +682,7 @@ pub fn create_function_debug_context(cx: &CrateContext,
         ast_map::NodeForeignItem(..) |
         ast_map::NodeVariant(..) |
         ast_map::NodeStructCtor(..) => {
-            return FunctionWithoutDebugInfo;
+            return FunctionDebugContext { repr: FunctionWithoutDebugInfo };
         }
         _ => cx.sess().bug(format!("create_function_debug_context: \
                                     unexpected sort of node: {:?}", fnitem))
@@ -686,7 +690,7 @@ pub fn create_function_debug_context(cx: &CrateContext,
 
     // This can be the case for functions inlined from another crate
     if span == codemap::DUMMY_SP {
-        return FunctionWithoutDebugInfo;
+        return FunctionDebugContext { repr: FunctionWithoutDebugInfo };
     }
 
     let loc = span_start(cx, span);
@@ -761,7 +765,7 @@ pub fn create_function_debug_context(cx: &CrateContext,
                        fn_metadata,
                        &mut *fn_debug_context.scope_map.borrow_mut());
 
-    return FunctionDebugContext(fn_debug_context);
+    return FunctionDebugContext { repr: FunctionDebugContext(fn_debug_context) };
 
     fn get_function_signature(cx: &CrateContext,
                               fn_ast_id: ast::NodeId,
@@ -2335,7 +2339,7 @@ fn DIB(cx: &CrateContext) -> DIBuilderRef {
 }
 
 fn fn_should_be_ignored(fcx: &FunctionContext) -> bool {
-    match fcx.debug_context {
+    match fcx.debug_context.repr {
         FunctionDebugContext(_) => false,
         _ => true
     }
index 1dcebd7a01617b6d0ac536c3d54208fae166d7dc..792673e32981714b82e3b14f3ccfab39f2edd1c9 100644 (file)
 use std::vec;
 
 /// A vector type optimized for cases where the size is almost always 0 or 1
-pub enum SmallVector<T> {
-    priv Zero,
-    priv One(T),
-    priv Many(Vec<T> ),
+pub struct SmallVector<T> {
+    repr: SmallVectorRepr<T>,
+}
+
+enum SmallVectorRepr<T> {
+    Zero,
+    One(T),
+    Many(Vec<T> ),
 }
 
 impl<T> Container for SmallVector<T> {
     fn len(&self) -> uint {
-        match *self {
+        match self.repr {
             Zero => 0,
             One(..) => 1,
             Many(ref vals) => vals.len()
@@ -30,7 +34,7 @@ fn len(&self) -> uint {
 
 impl<T> FromIterator<T> for SmallVector<T> {
     fn from_iter<I: Iterator<T>>(iter: I) -> SmallVector<T> {
-        let mut v = Zero;
+        let mut v = SmallVector::zero();
         v.extend(iter);
         v
     }
@@ -46,24 +50,24 @@ fn extend<I: Iterator<T>>(&mut self, mut iter: I) {
 
 impl<T> SmallVector<T> {
     pub fn zero() -> SmallVector<T> {
-        Zero
+        SmallVector { repr: Zero }
     }
 
     pub fn one(v: T) -> SmallVector<T> {
-        One(v)
+        SmallVector { repr: One(v) }
     }
 
-    pub fn many(vs: Vec<T> ) -> SmallVector<T> {
-        Many(vs)
+    pub fn many(vs: Vec<T>) -> SmallVector<T> {
+        SmallVector { repr: Many(vs) }
     }
 
     pub fn push(&mut self, v: T) {
-        match *self {
-            Zero => *self = One(v),
+        match self.repr {
+            Zero => self.repr = One(v),
             One(..) => {
-                let one = mem::replace(self, Zero);
+                let one = mem::replace(&mut self.repr, Zero);
                 match one {
-                    One(v1) => mem::replace(self, Many(vec!(v1, v))),
+                    One(v1) => mem::replace(&mut self.repr, Many(vec!(v1, v))),
                     _ => unreachable!()
                 };
             }
@@ -78,7 +82,7 @@ pub fn push_all(&mut self, other: SmallVector<T>) {
     }
 
     pub fn get<'a>(&'a self, idx: uint) -> &'a T {
-        match *self {
+        match self.repr {
             One(ref v) if idx == 0 => v,
             Many(ref vs) => vs.get(idx),
             _ => fail!("out of bounds access")
@@ -86,7 +90,7 @@ pub fn get<'a>(&'a self, idx: uint) -> &'a T {
     }
 
     pub fn expect_one(self, err: &'static str) -> T {
-        match self {
+        match self.repr {
             One(v) => v,
             Many(v) => {
                 if v.len() == 1 {
@@ -100,27 +104,32 @@ pub fn expect_one(self, err: &'static str) -> T {
     }
 
     pub fn move_iter(self) -> MoveItems<T> {
-        match self {
+        let repr = match self.repr {
             Zero => ZeroIterator,
             One(v) => OneIterator(v),
             Many(vs) => ManyIterator(vs.move_iter())
-        }
+        };
+        MoveItems { repr: repr }
     }
 }
 
-pub enum MoveItems<T> {
-    priv ZeroIterator,
-    priv OneIterator(T),
-    priv ManyIterator(vec::MoveItems<T>),
+pub struct MoveItems<T> {
+    repr: MoveItemsRepr<T>,
+}
+
+enum MoveItemsRepr<T> {
+    ZeroIterator,
+    OneIterator(T),
+    ManyIterator(vec::MoveItems<T>),
 }
 
 impl<T> Iterator<T> for MoveItems<T> {
     fn next(&mut self) -> Option<T> {
-        match *self {
+        match self.repr {
             ZeroIterator => None,
             OneIterator(..) => {
                 let mut replacement = ZeroIterator;
-                mem::swap(self, &mut replacement);
+                mem::swap(&mut self.repr, &mut replacement);
                 match replacement {
                     OneIterator(v) => Some(v),
                     _ => unreachable!()
@@ -131,7 +140,7 @@ fn next(&mut self) -> Option<T> {
     }
 
     fn size_hint(&self) -> (uint, Option<uint>) {
-        match *self {
+        match self.repr {
             ZeroIterator => (0, Some(0)),
             OneIterator(..) => (1, Some(1)),
             ManyIterator(ref inner) => inner.size_hint()