rstmt_core/
octave.rs

1/*
2    Appellation: octave <module>
3    Created At: 2025.12.31:16:11:45
4    Contrib: @FL03
5*/
6mod impl_octave;
7mod impl_octave_ext;
8#[cfg(feature = "rand")]
9mod impl_octave_rand;
10mod impl_octave_repr;
11
12/// A trait for converting a reference into an [`Octave`].
13pub trait AsOctave<T> {
14    fn as_octave(&self) -> Octave<T>;
15}
16/// A trait for converting a type into an [`Octave`].
17pub trait IntoOctave<T> {
18    fn into_octave(self) -> Octave<T>;
19}
20/// [`RawOctave`] is a marker trait denoting objects allowed to define octaves; it is
21/// implemented for all (un)signed integer types.
22pub trait RawOctave {
23    private! {}
24}
25
26/// A type defining an octave
27#[derive(Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
28#[cfg_attr(
29    feature = "serde",
30    derive(serde::Deserialize, serde::Serialize),
31    serde(transparent)
32)]
33#[repr(transparent)]
34pub struct Octave<T = isize>(pub T);
35
36/*
37 ************* Implementations *************
38*/
39
40macro_rules! impl_raw_octave {
41    ($($t:ty),* $(,)?) => {
42        $(
43            impl RawOctave for $t {
44                seal! {}
45            }
46        )*
47    };
48}
49
50impl_raw_octave! {
51    u8, u16, u32, u64, u128, usize,
52    i8, i16, i32, i64, i128, isize,
53}
54
55impl<U, T> AsOctave<T> for U
56where
57    U: Clone + IntoOctave<T> + RawOctave,
58{
59    fn as_octave(&self) -> Octave<T> {
60        self.clone().into_octave()
61    }
62}
63
64impl<U, T> IntoOctave<T> for U
65where
66    U: Into<Octave<T>>,
67{
68    fn into_octave(self) -> Octave<T> {
69        self.into()
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn test_as_into_octave() {
79        let octave: Octave = 4isize.into_octave();
80        assert_eq!(octave, 4);
81
82        let octave_ref: Octave = octave.as_octave();
83        assert_eq!(octave_ref.0, 4);
84    }
85}