gmsol_solana_utils/utils/
with_slot.rs

1/// With Slot.
2#[derive(Debug, Clone, Copy)]
3pub struct WithSlot<T> {
4    /// Slot.
5    slot: u64,
6    /// Value.
7    value: T,
8}
9
10impl<T> WithSlot<T> {
11    /// Create a new [`WithSlot`].
12    pub fn new(slot: u64, value: T) -> Self {
13        Self { slot, value }
14    }
15
16    /// Get slot.
17    pub fn slot(&self) -> u64 {
18        self.slot
19    }
20
21    /// Get the mutable reference of the slot.
22    pub fn slot_mut(&mut self) -> &mut u64 {
23        &mut self.slot
24    }
25
26    /// Get value.
27    pub fn value(&self) -> &T {
28        &self.value
29    }
30
31    /// Get the mutable reference for the value.
32    pub fn value_mut(&mut self) -> &mut T {
33        &mut self.value
34    }
35
36    /// Into value.
37    pub fn into_value(self) -> T {
38        self.split().1
39    }
40
41    /// Apply a function on the value.
42    pub fn map<U>(self, f: impl FnOnce(T) -> U) -> WithSlot<U> {
43        WithSlot {
44            slot: self.slot,
45            value: (f)(self.value),
46        }
47    }
48
49    /// Split.
50    pub fn split(self) -> (u64, T) {
51        (self.slot, self.value)
52    }
53}
54
55impl<T, E> WithSlot<Result<T, E>> {
56    /// Transpose.
57    pub fn transpose(self) -> Result<WithSlot<T>, E> {
58        match self.value {
59            Ok(value) => Ok(WithSlot {
60                slot: self.slot,
61                value,
62            }),
63            Err(err) => Err(err),
64        }
65    }
66}