rstmt_core/octave/
octave.rs1#[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 pub fn default() -> Self
20 where
21 T: Default,
22 {
23 Octave::create(T::default)
24 }
25 pub const fn new(index: T) -> Self {
27 Octave(index)
28 }
29 pub fn create<F>(f: F) -> Self
31 where
32 F: FnOnce() -> T,
33 {
34 Octave(f())
35 }
36 pub fn one() -> Self
38 where
39 T: num_traits::One,
40 {
41 Octave::create(T::one)
42 }
43 pub fn zero() -> Self
45 where
46 T: num_traits::Zero,
47 {
48 Octave::create(T::zero)
49 }
50 pub const fn as_ptr(&self) -> *const T {
52 core::ptr::from_ref(&self.0)
53 }
54 pub const fn as_mut_ptr(&mut self) -> *mut T {
56 core::ptr::from_mut(&mut self.0)
57 }
58 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 pub const fn get(&self) -> &T {
71 &self.0
72 }
73 pub const fn get_mut(&mut self) -> &mut T {
75 &mut self.0
76 }
77 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 pub const fn replace(&mut self, index: T) -> T {
86 core::mem::replace(self.get_mut(), index)
87 }
88 pub fn set(&mut self, index: T) -> &mut Self {
90 *self.get_mut() = index;
91 self
92 }
93 pub const fn swap(&mut self, other: &mut Self) {
95 core::mem::swap(self.get_mut(), other.get_mut());
96 }
97 pub fn with<U>(self, other: U) -> Octave<U> {
99 Octave(other)
100 }
101 pub fn take(&mut self) -> T
104 where
105 T: Default,
106 {
107 core::mem::take(self.get_mut())
108 }
109 pub const fn view(&self) -> Octave<&T> {
111 Octave(self.get())
112 }
113 pub fn view_mut(&mut self) -> Octave<&mut T> {
115 Octave(self.get_mut())
116 }
117}