Skip to content

Commit 83351fa

Browse files
committed
Remove usage of private enum variants
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
1 parent 10f94e3 commit 83351fa

File tree

4 files changed

+76
-53
lines changed

4 files changed

+76
-53
lines changed

src/liblibc/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ pub mod types {
229229
*/
230230
#[repr(u8)]
231231
pub enum c_void {
232-
priv variant1,
233-
priv variant2
232+
__variant1,
233+
__variant2,
234234
}
235235
pub enum FILE {}
236236
pub enum fpos_t {}

src/librand/distributions/gamma.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,14 @@ use super::{IndependentSample, Sample, Exp};
4545
/// for Generating Gamma Variables" *ACM Trans. Math. Softw.* 26, 3
4646
/// (September 2000),
4747
/// 363-372. DOI:[10.1145/358407.358414](http://doi.acm.org/10.1145/358407.358414)
48-
pub enum Gamma {
49-
priv Large(GammaLargeShape),
50-
priv One(Exp),
51-
priv Small(GammaSmallShape)
48+
pub struct Gamma {
49+
repr: GammaRepr,
50+
}
51+
52+
enum GammaRepr {
53+
Large(GammaLargeShape),
54+
One(Exp),
55+
Small(GammaSmallShape)
5256
}
5357

5458
// These two helpers could be made public, but saving the
@@ -90,11 +94,12 @@ impl Gamma {
9094
assert!(shape > 0.0, "Gamma::new called with shape <= 0");
9195
assert!(scale > 0.0, "Gamma::new called with scale <= 0");
9296

93-
match shape {
97+
let repr = match shape {
9498
1.0 => One(Exp::new(1.0 / scale)),
9599
0.0 .. 1.0 => Small(GammaSmallShape::new_raw(shape, scale)),
96100
_ => Large(GammaLargeShape::new_raw(shape, scale))
97-
}
101+
};
102+
Gamma { repr: repr }
98103
}
99104
}
100105

@@ -131,7 +136,7 @@ impl Sample<f64> for GammaLargeShape {
131136

132137
impl IndependentSample<f64> for Gamma {
133138
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
134-
match *self {
139+
match self.repr {
135140
Small(ref g) => g.ind_sample(rng),
136141
One(ref g) => g.ind_sample(rng),
137142
Large(ref g) => g.ind_sample(rng),
@@ -183,32 +188,37 @@ impl IndependentSample<f64> for GammaLargeShape {
183188
/// let v = chi.ind_sample(&mut rand::task_rng());
184189
/// println!("{} is from a χ²(11) distribution", v)
185190
/// ```
186-
pub enum ChiSquared {
191+
pub struct ChiSquared {
192+
repr: ChiSquaredRepr,
193+
}
194+
195+
enum ChiSquaredRepr {
187196
// k == 1, Gamma(alpha, ..) is particularly slow for alpha < 1,
188197
// e.g. when alpha = 1/2 as it would be for this case, so special-
189198
// casing and using the definition of N(0,1)^2 is faster.
190-
priv DoFExactlyOne,
191-
priv DoFAnythingElse(Gamma)
199+
DoFExactlyOne,
200+
DoFAnythingElse(Gamma),
192201
}
193202

194203
impl ChiSquared {
195204
/// Create a new chi-squared distribution with degrees-of-freedom
196205
/// `k`. Fails if `k < 0`.
197206
pub fn new(k: f64) -> ChiSquared {
198-
if k == 1.0 {
207+
let repr = if k == 1.0 {
199208
DoFExactlyOne
200209
} else {
201210
assert!(k > 0.0, "ChiSquared::new called with `k` < 0");
202211
DoFAnythingElse(Gamma::new(0.5 * k, 2.0))
203-
}
212+
};
213+
ChiSquared { repr: repr }
204214
}
205215
}
206216
impl Sample<f64> for ChiSquared {
207217
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
208218
}
209219
impl IndependentSample<f64> for ChiSquared {
210220
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
211-
match *self {
221+
match self.repr {
212222
DoFExactlyOne => {
213223
// k == 1 => N(0,1)^2
214224
let StandardNormal(norm) = rng.gen::<StandardNormal>();

src/librustc/middle/trans/debuginfo.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,19 @@ impl CrateDebugContext {
206206
}
207207
}
208208

209-
pub enum FunctionDebugContext {
210-
priv FunctionDebugContext(~FunctionDebugContextData),
211-
priv DebugInfoDisabled,
212-
priv FunctionWithoutDebugInfo,
209+
pub struct FunctionDebugContext {
210+
repr: FunctionDebugContextRepr,
211+
}
212+
213+
enum FunctionDebugContextRepr {
214+
FunctionDebugContext(~FunctionDebugContextData),
215+
DebugInfoDisabled,
216+
FunctionWithoutDebugInfo,
213217
}
214218

215219
impl FunctionDebugContext {
216220
fn get_ref<'a>(&'a self, cx: &CrateContext, span: Span) -> &'a FunctionDebugContextData {
217-
match *self {
221+
match self.repr {
218222
FunctionDebugContext(~ref data) => data,
219223
DebugInfoDisabled => {
220224
cx.sess().span_bug(span, FunctionDebugContext::debuginfo_disabled_message());
@@ -544,7 +548,7 @@ pub fn create_argument_metadata(bcx: &Block, arg: &ast::Arg) {
544548
pub fn set_source_location(fcx: &FunctionContext,
545549
node_id: ast::NodeId,
546550
span: Span) {
547-
match fcx.debug_context {
551+
match fcx.debug_context.repr {
548552
DebugInfoDisabled => return,
549553
FunctionWithoutDebugInfo => {
550554
set_debug_location(fcx.ccx, UnknownLocation);
@@ -585,7 +589,7 @@ pub fn clear_source_location(fcx: &FunctionContext) {
585589
/// and must therefore be called before the first real statement/expression of the function is
586590
/// translated.
587591
pub fn start_emitting_source_locations(fcx: &FunctionContext) {
588-
match fcx.debug_context {
592+
match fcx.debug_context.repr {
589593
FunctionDebugContext(~ref data) => {
590594
data.source_locations_enabled.set(true)
591595
},
@@ -603,15 +607,15 @@ pub fn create_function_debug_context(cx: &CrateContext,
603607
param_substs: Option<@param_substs>,
604608
llfn: ValueRef) -> FunctionDebugContext {
605609
if cx.sess().opts.debuginfo == NoDebugInfo {
606-
return DebugInfoDisabled;
610+
return FunctionDebugContext { repr: DebugInfoDisabled };
607611
}
608612

609613
// Clear the debug location so we don't assign them in the function prelude. Do this here
610614
// already, in case we do an early exit from this function.
611615
set_debug_location(cx, UnknownLocation);
612616

613617
if fn_ast_id == -1 {
614-
return FunctionWithoutDebugInfo;
618+
return FunctionDebugContext { repr: FunctionWithoutDebugInfo };
615619
}
616620

617621
let empty_generics = ast::Generics { lifetimes: Vec::new(), ty_params: OwnedSlice::empty() };
@@ -678,15 +682,15 @@ pub fn create_function_debug_context(cx: &CrateContext,
678682
ast_map::NodeForeignItem(..) |
679683
ast_map::NodeVariant(..) |
680684
ast_map::NodeStructCtor(..) => {
681-
return FunctionWithoutDebugInfo;
685+
return FunctionDebugContext { repr: FunctionWithoutDebugInfo };
682686
}
683687
_ => cx.sess().bug(format!("create_function_debug_context: \
684688
unexpected sort of node: {:?}", fnitem))
685689
};
686690

687691
// This can be the case for functions inlined from another crate
688692
if span == codemap::DUMMY_SP {
689-
return FunctionWithoutDebugInfo;
693+
return FunctionDebugContext { repr: FunctionWithoutDebugInfo };
690694
}
691695

692696
let loc = span_start(cx, span);
@@ -761,7 +765,7 @@ pub fn create_function_debug_context(cx: &CrateContext,
761765
fn_metadata,
762766
&mut *fn_debug_context.scope_map.borrow_mut());
763767

764-
return FunctionDebugContext(fn_debug_context);
768+
return FunctionDebugContext { repr: FunctionDebugContext(fn_debug_context) };
765769

766770
fn get_function_signature(cx: &CrateContext,
767771
fn_ast_id: ast::NodeId,
@@ -2335,7 +2339,7 @@ fn DIB(cx: &CrateContext) -> DIBuilderRef {
23352339
}
23362340

23372341
fn fn_should_be_ignored(fcx: &FunctionContext) -> bool {
2338-
match fcx.debug_context {
2342+
match fcx.debug_context.repr {
23392343
FunctionDebugContext(_) => false,
23402344
_ => true
23412345
}

src/libsyntax/util/small_vector.rs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ use std::mem;
1212
use std::vec;
1313

1414
/// A vector type optimized for cases where the size is almost always 0 or 1
15-
pub enum SmallVector<T> {
16-
priv Zero,
17-
priv One(T),
18-
priv Many(Vec<T> ),
15+
pub struct SmallVector<T> {
16+
repr: SmallVectorRepr<T>,
17+
}
18+
19+
enum SmallVectorRepr<T> {
20+
Zero,
21+
One(T),
22+
Many(Vec<T> ),
1923
}
2024

2125
impl<T> Container for SmallVector<T> {
2226
fn len(&self) -> uint {
23-
match *self {
27+
match self.repr {
2428
Zero => 0,
2529
One(..) => 1,
2630
Many(ref vals) => vals.len()
@@ -30,7 +34,7 @@ impl<T> Container for SmallVector<T> {
3034

3135
impl<T> FromIterator<T> for SmallVector<T> {
3236
fn from_iter<I: Iterator<T>>(iter: I) -> SmallVector<T> {
33-
let mut v = Zero;
37+
let mut v = SmallVector::zero();
3438
v.extend(iter);
3539
v
3640
}
@@ -46,24 +50,24 @@ impl<T> Extendable<T> for SmallVector<T> {
4650

4751
impl<T> SmallVector<T> {
4852
pub fn zero() -> SmallVector<T> {
49-
Zero
53+
SmallVector { repr: Zero }
5054
}
5155

5256
pub fn one(v: T) -> SmallVector<T> {
53-
One(v)
57+
SmallVector { repr: One(v) }
5458
}
5559

56-
pub fn many(vs: Vec<T> ) -> SmallVector<T> {
57-
Many(vs)
60+
pub fn many(vs: Vec<T>) -> SmallVector<T> {
61+
SmallVector { repr: Many(vs) }
5862
}
5963

6064
pub fn push(&mut self, v: T) {
61-
match *self {
62-
Zero => *self = One(v),
65+
match self.repr {
66+
Zero => self.repr = One(v),
6367
One(..) => {
64-
let one = mem::replace(self, Zero);
68+
let one = mem::replace(&mut self.repr, Zero);
6569
match one {
66-
One(v1) => mem::replace(self, Many(vec!(v1, v))),
70+
One(v1) => mem::replace(&mut self.repr, Many(vec!(v1, v))),
6771
_ => unreachable!()
6872
};
6973
}
@@ -78,15 +82,15 @@ impl<T> SmallVector<T> {
7882
}
7983

8084
pub fn get<'a>(&'a self, idx: uint) -> &'a T {
81-
match *self {
85+
match self.repr {
8286
One(ref v) if idx == 0 => v,
8387
Many(ref vs) => vs.get(idx),
8488
_ => fail!("out of bounds access")
8589
}
8690
}
8791

8892
pub fn expect_one(self, err: &'static str) -> T {
89-
match self {
93+
match self.repr {
9094
One(v) => v,
9195
Many(v) => {
9296
if v.len() == 1 {
@@ -100,27 +104,32 @@ impl<T> SmallVector<T> {
100104
}
101105

102106
pub fn move_iter(self) -> MoveItems<T> {
103-
match self {
107+
let repr = match self.repr {
104108
Zero => ZeroIterator,
105109
One(v) => OneIterator(v),
106110
Many(vs) => ManyIterator(vs.move_iter())
107-
}
111+
};
112+
MoveItems { repr: repr }
108113
}
109114
}
110115

111-
pub enum MoveItems<T> {
112-
priv ZeroIterator,
113-
priv OneIterator(T),
114-
priv ManyIterator(vec::MoveItems<T>),
116+
pub struct MoveItems<T> {
117+
repr: MoveItemsRepr<T>,
118+
}
119+
120+
enum MoveItemsRepr<T> {
121+
ZeroIterator,
122+
OneIterator(T),
123+
ManyIterator(vec::MoveItems<T>),
115124
}
116125

117126
impl<T> Iterator<T> for MoveItems<T> {
118127
fn next(&mut self) -> Option<T> {
119-
match *self {
128+
match self.repr {
120129
ZeroIterator => None,
121130
OneIterator(..) => {
122131
let mut replacement = ZeroIterator;
123-
mem::swap(self, &mut replacement);
132+
mem::swap(&mut self.repr, &mut replacement);
124133
match replacement {
125134
OneIterator(v) => Some(v),
126135
_ => unreachable!()
@@ -131,7 +140,7 @@ impl<T> Iterator<T> for MoveItems<T> {
131140
}
132141

133142
fn size_hint(&self) -> (uint, Option<uint>) {
134-
match *self {
143+
match self.repr {
135144
ZeroIterator => (0, Some(0)),
136145
OneIterator(..) => (1, Some(1)),
137146
ManyIterator(ref inner) => inner.size_hint()

0 commit comments

Comments
 (0)