rstmt_core/pitch/impls/
impl_pitch.rs

1/*
2    Appellation: impl_pitch <module>
3    Created At: 2025.12.20:09:09:08
4    Contrib: @FL03
5*/
6use crate::pitch::{Pitch, RawPitch};
7
8impl<T> Pitch<T>
9where
10    T: RawPitch,
11{
12    /// returns a new instance of the [`Pitch`] wrapping the given value
13    pub const fn new(value: T) -> Self {
14        Pitch(value)
15    }
16    /// initialize a new instance of the pitch using the result of the given function
17    pub fn init<F>(f: F) -> Self
18    where
19        F: FnOnce() -> T,
20    {
21        Pitch::new(f())
22    }
23    /// returns a new Pitch with the value of one
24    pub fn one() -> Self
25    where
26        T: num_traits::One,
27    {
28        Pitch::init(T::one)
29    }
30    /// returns a new Pitch with the value of zero
31    pub fn zero() -> Self
32    where
33        T: num_traits::Zero,
34    {
35        Pitch::init(T::zero)
36    }
37    /// returns a pointer to the inner value
38    pub const fn as_ptr(&self) -> *const T {
39        core::ptr::from_ref(self.get())
40    }
41    /// returns a mutable pointer to the inner value
42    pub const fn as_mut_ptr(&mut self) -> *mut T {
43        core::ptr::from_mut(self.get_mut())
44    }
45    #[inline]
46    /// consumes the index returning the inner value
47    pub fn value(self) -> T {
48        self.0
49    }
50    /// returns an immutable reference to the inner value
51    pub const fn get(&self) -> &T {
52        &self.0
53    }
54    /// returns a mutable reference to the inner value
55    pub const fn get_mut(&mut self) -> &mut T {
56        &mut self.0
57    }
58    /// apply a function to the inner value and returns a new Pitch wrapping the result
59    pub fn map<U, F>(self, f: F) -> Pitch<U>
60    where
61        F: FnOnce(T) -> U,
62    {
63        Pitch(f(self.value()))
64    }
65    #[inline]
66    /// applies the function onto a mutable reference of the inner value
67    pub fn apply_mut<F>(&mut self, mut f: F)
68    where
69        F: FnMut(&mut T),
70    {
71        f(self.get_mut())
72    }
73    /// [`replace`](core::mem::replace) the pitch with the given value, returning the previous state.
74    pub const fn replace(&mut self, index: T) -> T {
75        core::mem::replace(self.get_mut(), index)
76    }
77    #[inline]
78    /// set the index to the given value
79    pub fn set(&mut self, index: T) {
80        *self.get_mut() = index;
81    }
82    /// swap the values of two indices
83    pub const fn swap(&mut self, other: &mut Self) {
84        core::mem::swap(self.get_mut(), other.get_mut());
85    }
86    #[inline]
87    /// takes and returns the inner value, replacing it with the logical [`default`](Default)
88    /// of the type `T`
89    pub fn take(&mut self) -> T
90    where
91        T: Default,
92    {
93        core::mem::take(self.get_mut())
94    }
95    /// returns a new instance containing a reference to the inner value
96    pub const fn view(&self) -> Pitch<&T> {
97        Pitch(self.get())
98    }
99    /// returns a new instance containing a mutable reference to the inner value
100    pub const fn view_mut(&mut self) -> Pitch<&mut T> {
101        Pitch(self.get_mut())
102    }
103    #[inline]
104    /// consumes the current instance to create another with the given value
105    pub fn with<U>(self, value: U) -> Pitch<U> {
106        Pitch(value)
107    }
108}