rand/distr/weighted/
mod.rs1use core::fmt;
18mod weighted_index;
19
20pub use weighted_index::WeightedIndex;
21
22pub trait Weight: Clone {
26 const ZERO: Self;
28
29 #[allow(clippy::result_unit_err)]
39 fn checked_add_assign(&mut self, v: &Self) -> Result<(), ()>;
40}
41
42macro_rules! impl_weight_int {
43 ($t:ty) => {
44 impl Weight for $t {
45 const ZERO: Self = 0;
46 fn checked_add_assign(&mut self, v: &Self) -> Result<(), ()> {
47 match self.checked_add(*v) {
48 Some(sum) => {
49 *self = sum;
50 Ok(())
51 }
52 None => Err(()),
53 }
54 }
55 }
56 };
57 ($t:ty, $($tt:ty),*) => {
58 impl_weight_int!($t);
59 impl_weight_int!($($tt),*);
60 }
61}
62impl Weight for isize {
const ZERO: Self = 0;
fn checked_add_assign(&mut self, v: &Self) -> Result<(), ()> {
match self.checked_add(*v) {
Some(sum) => { *self = sum; Ok(()) }
None => Err(()),
}
}
}impl_weight_int!(i8, i16, i32, i64, i128, isize);
63impl Weight for usize {
const ZERO: Self = 0;
fn checked_add_assign(&mut self, v: &Self) -> Result<(), ()> {
match self.checked_add(*v) {
Some(sum) => { *self = sum; Ok(()) }
None => Err(()),
}
}
}impl_weight_int!(u8, u16, u32, u64, u128, usize);
64
65macro_rules! impl_weight_float {
66 ($t:ty) => {
67 impl Weight for $t {
68 const ZERO: Self = 0.0;
69
70 fn checked_add_assign(&mut self, v: &Self) -> Result<(), ()> {
71 *self += *v;
73 Ok(())
74 }
75 }
76 };
77}
78impl Weight for f32 {
const ZERO: Self = 0.0;
fn checked_add_assign(&mut self, v: &Self) -> Result<(), ()> {
*self += *v;
Ok(())
}
}impl_weight_float!(f32);
79impl Weight for f64 {
const ZERO: Self = 0.0;
fn checked_add_assign(&mut self, v: &Self) -> Result<(), ()> {
*self += *v;
Ok(())
}
}impl_weight_float!(f64);
80
81#[derive(#[automatically_derived]impl ::core::fmt::Debug for Error {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
Error::InvalidInput => "InvalidInput",
Error::InvalidWeight => "InvalidWeight",
Error::InsufficientNonZero => "InsufficientNonZero",
Error::Overflow => "Overflow",
})
}
}Debug, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
for Error {
}
#[automatically_derived]impl ::core::clone::Clone for Error {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self { *self }
}Clone, #[automatically_derived]impl ::core::marker::Copy for Error { }Copy, #[automatically_derived]impl ::core::marker::StructuralPartialEq for Error { }
#[automatically_derived]impl ::core::cmp::PartialEq for Error {
#[inline]
fn eq(&self, other: &Self) -> bool {
::core::intrinsics::discriminant_value(self) ==
::core::intrinsics::discriminant_value(other)
}
}PartialEq, #[automatically_derived]impl ::core::cmp::Eq for Error { }Eq)]
86#[non_exhaustive]
88pub enum Error {
89 InvalidInput,
91
92 InvalidWeight,
94
95 InsufficientNonZero,
101
102 Overflow,
104}
105
106impl core::error::Error for Error {}
107
108impl fmt::Display for Error {
109 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
110 f.write_str(match *self {
111 Error::InvalidInput => "Weights sequence is empty/too long/unordered",
112 Error::InvalidWeight => "A weight is negative, too large or not a valid number",
113 Error::InsufficientNonZero => "Not enough weights > zero",
114 Error::Overflow => "Overflow when summing weights",
115 })
116 }
117}