rstmt_core/octave/
octave.rs

1/*
2    Appellation: octave <types>
3    Contrib: @FL03
4*/
5
6/// A type defining an octave
7#[derive(Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
8#[cfg_attr(
9    feature = "serde",
10    derive(serde_derive::Deserialize, serde_derive::Serialize),
11    serde(default, transparent)
12)]
13#[repr(transparent)]
14pub struct Octave<T = isize>(pub T);
15
16impl<T> Octave<T> {
17    #[allow(clippy::should_implement_trait)]
18    /// returns a new instance initialized using the default value of the type
19    pub fn default() -> Self
20    where
21        T: Default,
22    {
23        Octave::create(T::default)
24    }
25    /// returns a new instance of the [`Octave`] wrapping the given value
26    pub const fn new(index: T) -> Self {
27        Octave(index)
28    }
29    /// returns a new [`Octave`] with the output of the given initializer function
30    pub fn create<F>(f: F) -> Self
31    where
32        F: FnOnce() -> T,
33    {
34        Octave(f())
35    }
36    /// returns a new Octave with the value of one
37    pub fn one() -> Self
38    where
39        T: num_traits::One,
40    {
41        Octave::create(T::one)
42    }
43    /// returns a new Octave with the value of zero
44    pub fn zero() -> Self
45    where
46        T: num_traits::Zero,
47    {
48        Octave::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.0)
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(&mut self.0)
57    }
58    /// consumes the index returning the inner value
59    pub fn into_inner(self) -> T {
60        self.0
61    }
62    #[deprecated(
63        since = "0.0.5",
64        note = "use `into_inner` instead; this method will be removed in the next major version."
65    )]
66    pub fn value(self) -> T {
67        self.0
68    }
69    /// returns an immutable reference to the inner value
70    pub const fn get(&self) -> &T {
71        &self.0
72    }
73    /// returns a mutable reference to the inner value
74    pub const fn get_mut(&mut self) -> &mut T {
75        &mut self.0
76    }
77    /// apply a function to the inner value and returns a new Octave wrapping the result
78    pub fn map<U, F>(self, f: F) -> Octave<U>
79    where
80        F: FnOnce(T) -> U,
81    {
82        Octave(f(self.0))
83    }
84    /// replaces the inner value with the given one and returns the old value
85    pub const fn replace(&mut self, index: T) -> T {
86        core::mem::replace(self.get_mut(), index)
87    }
88    /// set the index to the given value
89    pub fn set(&mut self, index: T) -> &mut Self {
90        *self.get_mut() = index;
91        self
92    }
93    /// swap the values of two indices
94    pub const fn swap(&mut self, other: &mut Self) {
95        core::mem::swap(self.get_mut(), other.get_mut());
96    }
97    /// consumes the current instance to create another with the given value
98    pub fn with<U>(self, other: U) -> Octave<U> {
99        Octave(other)
100    }
101    /// takes and returns the inner value, replacing it with the logical [`default`](Default)
102    /// of the type `T`
103    pub fn take(&mut self) -> T
104    where
105        T: Default,
106    {
107        core::mem::take(self.get_mut())
108    }
109    /// returns a new instance containing a reference to the inner value
110    pub const fn view(&self) -> Octave<&T> {
111        Octave(self.get())
112    }
113    /// returns a new instance containing a mutable reference to the inner value
114    pub fn view_mut(&mut self) -> Octave<&mut T> {
115        Octave(self.get_mut())
116    }
117}