rstmt_core/pitch/
mod.rs

1/*
2    appellation: pitch <module>
3    authors: @FL03
4*/
5//! this module implements the [`Pitch`] type and its associated traits and types.
6#[doc(inline)]
7pub use self::{pbase::Pitch, types::*};
8
9mod pbase;
10mod wrapper;
11
12mod types {
13    #[doc(inline)]
14    pub use self::prelude::*;
15
16    mod pitch_class;
17
18    pub(crate) mod prelude {
19        #[doc(inline)]
20        pub use super::pitch_class::*;
21    }
22}
23
24pub(crate) mod prelude {
25    #[doc(inline)]
26    pub use super::types::*;
27    #[doc(inline)]
28    pub use super::wrapper::*;
29    #[doc(inline)]
30    pub use super::{AsPitch, IntoPitch, PitchNum, RawPitch};
31}
32
33/// A trait for converting a reference into a [`Pitch`].
34pub trait AsPitch {
35    fn as_pitch(&self) -> Pitch;
36}
37/// A trait for converting a type into a [`Pitch`].
38pub trait IntoPitch {
39    fn into_pitch(self) -> Pitch;
40}
41
42/// [`RawPitch`] defines an interface for all raw pitch types.
43///
44/// **note:** This trait is sealed and cannot be implemented outside of this crate.
45pub trait RawPitch:
46    'static + Default + Send + Sync + core::fmt::Debug + core::fmt::Display
47{
48    private!();
49}
50/// The [`PitchNum`] trait extends the [`RawPitch`] trait with additional numeric operations
51/// and traits.
52pub trait PitchNum: RawPitch + Sized
53where
54    Self: Copy
55        + Eq
56        + PartialEq
57        + PartialOrd
58        + core::ops::Add<Output = Self>
59        + core::ops::Sub<Output = Self>
60        + core::ops::Mul<Output = Self>
61        + core::ops::Div<Output = Self>
62        + core::ops::Rem<Output = Self>
63        + core::ops::Neg<Output = Self>
64        + core::ops::AddAssign
65        + core::ops::SubAssign
66        + core::ops::MulAssign
67        + core::ops::DivAssign
68        + core::ops::RemAssign
69        + num_traits::FromPrimitive
70        + num_traits::ToPrimitive
71        + num_traits::Zero
72        + num_traits::One,
73{
74}
75
76/*
77 ************* Implementations *************
78*/
79impl<T> AsPitch for T
80where
81    T: Clone + IntoPitch,
82{
83    fn as_pitch(&self) -> Pitch {
84        self.clone().into_pitch()
85    }
86}
87
88impl<T> IntoPitch for T
89where
90    T: Into<Pitch>,
91{
92    fn into_pitch(self) -> Pitch {
93        self.into()
94    }
95}
96
97macro_rules! impl_raw_pitch {
98    ($($t:ty),* $(,)?) => {
99        $(
100            impl_raw_pitch!(@impl $t);
101        )*
102    };
103    (@impl $t:ty) => {
104        impl RawPitch for $t {
105            seal!();
106        }
107    };
108}
109
110impl_raw_pitch!(
111    u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64
112);
113
114impl<T> PitchNum for T where
115    T: RawPitch
116        + Copy
117        + Eq
118        + PartialEq
119        + PartialOrd
120        + core::ops::Add<Output = T>
121        + core::ops::Sub<Output = T>
122        + core::ops::Mul<Output = T>
123        + core::ops::Div<Output = T>
124        + core::ops::Rem<Output = T>
125        + core::ops::Neg<Output = T>
126        + core::ops::AddAssign
127        + core::ops::SubAssign
128        + core::ops::MulAssign
129        + core::ops::DivAssign
130        + core::ops::RemAssign
131        + num_traits::FromPrimitive
132        + num_traits::ToPrimitive
133        + num_traits::Zero
134        + num_traits::One
135{
136}