rstmt_core/octave/impl_octave.rs
1/*
2 appellation: impl_octave <module>
3 authors: @FL03
4*/
5use crate::octave::Octave;
6
7impl<T> Octave<T> {
8 /// a functional constructor for [`Octave`], essentially wrapping the given value
9 pub const fn new(octave: T) -> Self {
10 Octave(octave)
11 }
12 /// returns a new [`Octave`] with the output of the given initializer function
13 pub fn create<F>(f: F) -> Self
14 where
15 F: FnOnce() -> T,
16 {
17 Octave(f())
18 }
19 /// returns a new Octave with the value of one
20 pub fn one() -> Self
21 where
22 T: num_traits::One,
23 {
24 Octave::create(T::one)
25 }
26 /// returns a new Octave with the value of zero
27 pub fn zero() -> Self
28 where
29 T: num_traits::Zero,
30 {
31 Octave::create(T::zero)
32 }
33 /// returns a pointer to the inner value
34 pub const fn as_ptr(&self) -> *const T {
35 core::ptr::from_ref(&self.0)
36 }
37 /// returns a mutable pointer to the inner value
38 pub const fn as_mut_ptr(&mut self) -> *mut T {
39 core::ptr::from_mut(&mut self.0)
40 }
41 #[inline]
42 /// consumes the index returning the inner value
43 pub fn value(self) -> T {
44 self.0
45 }
46 /// returns an immutable reference to the inner value
47 pub const fn get(&self) -> &T {
48 &self.0
49 }
50 /// returns a mutable reference to the inner value
51 pub const fn get_mut(&mut self) -> &mut T {
52 &mut self.0
53 }
54 /// apply a function to the inner value and returns a new Octave wrapping the result
55 pub fn map<U, F>(self, f: F) -> Octave<U>
56 where
57 F: FnOnce(T) -> U,
58 {
59 Octave(f(self.0))
60 }
61 /// replaces the inner value with the given one and returns the old value
62 pub const fn replace(&mut self, index: T) -> T {
63 core::mem::replace(self.get_mut(), index)
64 }
65 /// set the index to the given value
66 pub fn set(&mut self, index: T) -> &mut Self {
67 *self.get_mut() = index;
68 self
69 }
70 /// swap the values of two indices
71 pub const fn swap(&mut self, other: &mut Self) {
72 core::mem::swap(self.get_mut(), other.get_mut());
73 }
74 /// consumes the current instance to create another with the given value
75 pub fn with<U>(self, other: U) -> Octave<U> {
76 Octave(other)
77 }
78 /// takes and returns the inner value, replacing it with the logical [`default`](Default)
79 /// of the type `T`
80 pub fn take(&mut self) -> T
81 where
82 T: Default,
83 {
84 core::mem::take(self.get_mut())
85 }
86 /// returns a new instance containing a reference to the inner value
87 pub const fn view(&self) -> Octave<&T> {
88 Octave(self.get())
89 }
90 /// returns a new instance containing a mutable reference to the inner value
91 pub fn view_mut(&mut self) -> Octave<&mut T> {
92 Octave(self.get_mut())
93 }
94}