X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibrand%2Fdistributions%2Fmod.rs;h=47967a719d397a8972d39bf4e90ac356721af550;hb=6f4ab9458a7ad06c8ce630604f533c8c0c0acef4;hp=41175c81df8918bbd5302e8f816296ab150a01f9;hpb=0491a231777735ba050c208ce621df93f863bf7c;p=rust.git diff --git a/src/librand/distributions/mod.rs b/src/librand/distributions/mod.rs index 41175c81df8..47967a719d3 100644 --- a/src/librand/distributions/mod.rs +++ b/src/librand/distributions/mod.rs @@ -17,6 +17,8 @@ //! internally. The `IndependentSample` trait is for generating values //! that do not need to record state. +use core::fmt; + #[cfg(not(test))] // only necessary for no_std use core::num::Float; @@ -51,7 +53,7 @@ pub trait Sample { // trait called `Sample` and the other should be `DependentSample`. pub trait IndependentSample: Sample { /// Generate a random value. - fn ind_sample(&self, &mut R) -> Support; + fn ind_sample(&self, _: &mut R) -> Support; } /// A wrapper for generating types that implement `Rand` via the @@ -78,6 +80,12 @@ fn ind_sample(&self, rng: &mut R) -> Sup { } } +impl fmt::Debug for RandSample { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.pad("RandSample { .. }") + } +} + /// A value with a particular weight for use with `WeightedChoice`. pub struct Weighted { /// The numerical weight of this item @@ -86,6 +94,15 @@ pub struct Weighted { pub item: T, } +impl fmt::Debug for Weighted { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("Weighted") + .field("weight", &self.weight) + .field("item", &self.item) + .finish() + } +} + /// A distribution that selects from a finite collection of weighted items. /// /// Each item has an associated weight that influences how likely it @@ -132,7 +149,7 @@ pub fn new(items: &'a mut [Weighted]) -> WeightedChoice<'a, T> { "WeightedChoice::new called with a total weight of 0"); WeightedChoice { - items: items, + items, // we're likely to be generating numbers in this range // relatively often, so might as well cache it weight_range: Range::new(0, running_total), @@ -189,6 +206,15 @@ fn ind_sample(&self, rng: &mut R) -> T { } } +impl<'a, T: fmt::Debug> fmt::Debug for WeightedChoice<'a, T> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("WeightedChoice") + .field("items", &self.items) + .field("weight_range", &self.weight_range) + .finish() + } +} + mod ziggurat_tables; /// Sample a random number using the Ziggurat method (specifically the