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