1
Fork 0

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
This commit is contained in:
Alex Crichton 2014-04-15 18:05:38 -07:00
parent 10f94e3fe5
commit 83351fa02e
4 changed files with 76 additions and 53 deletions

View file

@ -229,8 +229,8 @@ pub mod types {
*/
#[repr(u8)]
pub enum c_void {
priv variant1,
priv variant2
__variant1,
__variant2,
}
pub enum FILE {}
pub enum fpos_t {}

View file

@ -45,10 +45,14 @@ use super::{IndependentSample, Sample, Exp};
/// 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 @@ impl 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 @@ impl Sample<f64> for GammaLargeShape {
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 @@ impl IndependentSample<f64> for GammaLargeShape {
/// 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 @@ impl Sample<f64> for ChiSquared {
}
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>();

View file

@ -206,15 +206,19 @@ impl 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
}

View file

@ -12,15 +12,19 @@ use std::mem;
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 @@ impl<T> Container for SmallVector<T> {
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 @@ impl<T> Extendable<T> for SmallVector<T> {
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 @@ impl<T> 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 @@ impl<T> SmallVector<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 @@ impl<T> SmallVector<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 @@ impl<T> Iterator<T> for MoveItems<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()