rstmt_core/freq/
frequency.rs

1/*
2    Appellation: frequency <module>
3    Contrib: @FL03
4*/
5use super::RawFrequency;
6
7/// The [`Frequency`] type is a generic wrapper around type `T` that implements the
8/// [`RawFrequency`] trait. This implementation is designed to provide a consistent interface
9/// for dealing with frequencies within the crate, enabling conversion, arithmetic operations,
10/// and other utilities that are common to frequency values.
11#[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    /// returns a new instance of the [`Frequency`] wrapping the given value
25    pub const fn new(index: T) -> Self {
26        Frequency(index)
27    }
28    /// returns a new instance of the [`Frequency`] wrapping the output of the given
29    /// initializer function
30    pub fn create<F>(f: F) -> Self
31    where
32        F: FnOnce() -> T,
33    {
34        Frequency(f())
35    }
36    /// returns a new Frequency with the value of one
37    pub fn one() -> Self
38    where
39        T: num_traits::One,
40    {
41        Frequency::create(T::one)
42    }
43    /// returns a new Frequency with the value of zero
44    pub fn zero() -> Self
45    where
46        T: num_traits::Zero,
47    {
48        Frequency::create(T::zero)
49    }
50    /// returns a pointer to the inner value
51    pub const fn as_ptr(&self) -> *const T {
52        core::ptr::from_ref(self.get())
53    }
54    /// returns a mutable pointer to the inner value
55    pub const fn as_mut_ptr(&mut self) -> *mut T {
56        core::ptr::from_mut(self.get_mut())
57    }
58    /// consumes the index returning the inner value
59    pub fn value(self) -> T {
60        self.0
61    }
62    /// returns an immutable reference to the inner value
63    pub const fn get(&self) -> &T {
64        &self.0
65    }
66    /// returns a mutable reference to the inner value
67    pub const fn get_mut(&mut self) -> &mut T {
68        &mut self.0
69    }
70    /// apply a function to the inner value and returns a new Frequency wrapping the result
71    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    /// replaces the inner value with the given one and returns the old value
78    pub const fn replace(&mut self, index: T) -> T {
79        core::mem::replace(self.get_mut(), index)
80    }
81    /// set the index to the given value
82    pub fn set(&mut self, index: T) -> &mut Self {
83        *self.get_mut() = index;
84        self
85    }
86    /// swap the values of two indices
87    pub const fn swap(&mut self, other: &mut Self) {
88        core::mem::swap(self.get_mut(), other.get_mut());
89    }
90    /// consumes the current instance to create another with the given value
91    pub fn with<U>(self, other: U) -> Frequency<U> {
92        Frequency(other)
93    }
94    /// takes and returns the inner value, replacing it with the logical [`default`](Default)
95    /// of the type `T`
96    pub fn take(&mut self) -> T
97    where
98        T: Default,
99    {
100        core::mem::take(self.get_mut())
101    }
102    /// returns a new instance containing a reference to the inner value
103    pub const fn view(&self) -> Frequency<&T> {
104        Frequency(self.get())
105    }
106    /// returns a new instance containing a mutable reference to the inner value
107    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}