Skip to main content

agsol_common/
locked_item.rs

1use super::MaxSerializedLen;
2use borsh::{BorshDeserialize, BorshSerialize};
3use solana_program::clock::UnixTimestamp;
4use std::cmp::Ordering;
5
6#[derive(BorshDeserialize, BorshSerialize, Debug, Clone)]
7pub struct LockedItem<T: BorshDeserialize + BorshSerialize + MaxSerializedLen> {
8    pub item: T,
9    pub expires: UnixTimestamp,
10}
11
12impl<T> MaxSerializedLen for LockedItem<T>
13where
14    T: BorshSerialize + BorshDeserialize + MaxSerializedLen,
15{
16    const MAX_SERIALIZED_LEN: usize = T::MAX_SERIALIZED_LEN + UnixTimestamp::MAX_SERIALIZED_LEN;
17}
18
19impl<T> PartialOrd for LockedItem<T>
20where
21    T: BorshSerialize + BorshDeserialize + MaxSerializedLen,
22{
23    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
24        Some(self.expires.cmp(&other.expires))
25    }
26}
27
28impl<T> Ord for LockedItem<T>
29where
30    T: BorshSerialize + BorshDeserialize + MaxSerializedLen,
31{
32    fn cmp(&self, other: &Self) -> Ordering {
33        self.expires.cmp(&other.expires)
34    }
35}
36
37impl<T> PartialEq for LockedItem<T>
38where
39    T: BorshSerialize + BorshDeserialize + MaxSerializedLen,
40{
41    fn eq(&self, other: &Self) -> bool {
42        self.expires == other.expires
43    }
44}
45
46impl<T> Eq for LockedItem<T> where T: BorshSerialize + BorshDeserialize + MaxSerializedLen {}
47
48impl<T> LockedItem<T>
49where
50    T: BorshSerialize + BorshDeserialize + MaxSerializedLen,
51{
52    pub fn expired(&self, current_time: UnixTimestamp) -> bool {
53        self.expires < current_time
54    }
55}