rstmt_core/freq/
frequency.rs1use super::RawFrequency;
6
7#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
12#[cfg_attr(
13 feature = "serde",
14 derive(serde_derive::Deserialize, serde_derive::Serialize),
15 serde(transparent)
16)]
17#[repr(transparent)]
18pub struct Frequency<T = f64>(pub T);
19
20impl<T> Frequency<T>
21where
22 T: RawFrequency,
23{
24 pub const fn new(index: T) -> Self {
26 Frequency(index)
27 }
28 pub fn create<F>(f: F) -> Self
31 where
32 F: FnOnce() -> T,
33 {
34 Frequency(f())
35 }
36 pub fn one() -> Self
38 where
39 T: num_traits::One,
40 {
41 Frequency::create(T::one)
42 }
43 pub fn zero() -> Self
45 where
46 T: num_traits::Zero,
47 {
48 Frequency::create(T::zero)
49 }
50 pub const fn as_ptr(&self) -> *const T {
52 core::ptr::from_ref(self.get())
53 }
54 pub const fn as_mut_ptr(&mut self) -> *mut T {
56 core::ptr::from_mut(self.get_mut())
57 }
58 pub fn value(self) -> T {
60 self.0
61 }
62 pub const fn get(&self) -> &T {
64 &self.0
65 }
66 pub const fn get_mut(&mut self) -> &mut T {
68 &mut self.0
69 }
70 pub fn map<U, F>(self, f: F) -> Frequency<U>
72 where
73 F: FnOnce(T) -> U,
74 {
75 Frequency(f(self.value()))
76 }
77 pub const fn replace(&mut self, index: T) -> T {
79 core::mem::replace(self.get_mut(), index)
80 }
81 pub fn set(&mut self, index: T) -> &mut Self {
83 *self.get_mut() = index;
84 self
85 }
86 pub const fn swap(&mut self, other: &mut Self) {
88 core::mem::swap(self.get_mut(), other.get_mut());
89 }
90 pub fn with<U>(self, other: U) -> Frequency<U> {
92 Frequency(other)
93 }
94 pub fn take(&mut self) -> T
97 where
98 T: Default,
99 {
100 core::mem::take(self.get_mut())
101 }
102 pub const fn view(&self) -> Frequency<&T> {
104 Frequency(self.get())
105 }
106 pub fn view_mut(&mut self) -> Frequency<&mut T> {
108 Frequency(self.get_mut())
109 }
110}
111
112impl<T> Default for Frequency<T>
113where
114 T: Default,
115{
116 fn default() -> Self {
117 Frequency(T::default())
118 }
119}
120
121scsys::fmt_wrapper! {
122 Frequency<T>(Binary, Debug, Display, LowerExp, LowerHex, Octal, Pointer, UpperExp, UpperHex)
123}