grafos_leasekit/
adapter.rs

1//! Adapters that implement [`RenewableLease`] for grafos-std lease types.
2
3use grafos_std::block::BlockLease;
4use grafos_std::cpu::CpuLease;
5use grafos_std::gpu::GpuLease;
6use grafos_std::mem::MemLease;
7use grafos_std::net::NetLease;
8use grafos_std::FabricError;
9
10use crate::renewable::{LeaseStatus, RenewableLease};
11
12fn convert_status(s: grafos_std::lease::LeaseStatus) -> LeaseStatus {
13    match s {
14        grafos_std::lease::LeaseStatus::Active => LeaseStatus::Active,
15        grafos_std::lease::LeaseStatus::Expired => LeaseStatus::Expired,
16        grafos_std::lease::LeaseStatus::Revoked => LeaseStatus::Expired,
17    }
18}
19
20/// Adapter wrapping a [`MemLease`] as a [`RenewableLease`].
21pub struct MemLeaseAdapter {
22    lease: MemLease,
23}
24
25impl MemLeaseAdapter {
26    /// Wrap an existing [`MemLease`] for use with [`RenewalManager`](crate::RenewalManager).
27    pub fn new(lease: MemLease) -> Self {
28        Self { lease }
29    }
30
31    /// Borrow the wrapped lease.
32    pub fn inner(&self) -> &MemLease {
33        &self.lease
34    }
35}
36
37impl RenewableLease for MemLeaseAdapter {
38    fn lease_id(&self) -> u128 {
39        self.lease.lease_id()
40    }
41
42    fn expires_at_unix_secs(&self) -> u64 {
43        self.lease.expires_at_unix_secs()
44    }
45
46    fn renew(&mut self, duration_secs: u64) -> Result<(), FabricError> {
47        self.lease.renew(duration_secs)
48    }
49
50    fn status(&self) -> LeaseStatus {
51        convert_status(self.lease.status())
52    }
53}
54
55/// Adapter wrapping a [`BlockLease`] as a [`RenewableLease`].
56pub struct BlockLeaseAdapter {
57    lease: BlockLease,
58}
59
60impl BlockLeaseAdapter {
61    /// Wrap an existing [`BlockLease`] for use with [`RenewalManager`](crate::RenewalManager).
62    pub fn new(lease: BlockLease) -> Self {
63        Self { lease }
64    }
65
66    /// Borrow the wrapped lease.
67    pub fn inner(&self) -> &BlockLease {
68        &self.lease
69    }
70}
71
72impl RenewableLease for BlockLeaseAdapter {
73    fn lease_id(&self) -> u128 {
74        self.lease.lease_id()
75    }
76
77    fn expires_at_unix_secs(&self) -> u64 {
78        self.lease.expires_at_unix_secs()
79    }
80
81    fn renew(&mut self, duration_secs: u64) -> Result<(), FabricError> {
82        self.lease.renew(duration_secs)
83    }
84
85    fn status(&self) -> LeaseStatus {
86        convert_status(self.lease.status())
87    }
88}
89
90/// Adapter wrapping a [`CpuLease`] as a [`RenewableLease`].
91pub struct CpuLeaseAdapter {
92    lease: CpuLease,
93}
94
95impl CpuLeaseAdapter {
96    /// Wrap an existing [`CpuLease`] for use with [`RenewalManager`](crate::RenewalManager).
97    pub fn new(lease: CpuLease) -> Self {
98        Self { lease }
99    }
100
101    /// Borrow the wrapped lease.
102    pub fn inner(&self) -> &CpuLease {
103        &self.lease
104    }
105}
106
107impl RenewableLease for CpuLeaseAdapter {
108    fn lease_id(&self) -> u128 {
109        self.lease.lease_id()
110    }
111
112    fn expires_at_unix_secs(&self) -> u64 {
113        self.lease.expires_at_unix_secs()
114    }
115
116    fn renew(&mut self, duration_secs: u64) -> Result<(), FabricError> {
117        self.lease.renew(duration_secs)
118    }
119
120    fn status(&self) -> LeaseStatus {
121        convert_status(self.lease.status())
122    }
123}
124
125/// Adapter wrapping a [`GpuLease`] as a [`RenewableLease`].
126pub struct GpuLeaseAdapter {
127    lease: GpuLease,
128}
129
130impl GpuLeaseAdapter {
131    /// Wrap an existing [`GpuLease`] for use with [`RenewalManager`](crate::RenewalManager).
132    pub fn new(lease: GpuLease) -> Self {
133        Self { lease }
134    }
135
136    /// Borrow the wrapped lease.
137    pub fn inner(&self) -> &GpuLease {
138        &self.lease
139    }
140}
141
142impl RenewableLease for GpuLeaseAdapter {
143    fn lease_id(&self) -> u128 {
144        self.lease.lease_id()
145    }
146
147    fn expires_at_unix_secs(&self) -> u64 {
148        self.lease.expires_at_unix_secs()
149    }
150
151    fn renew(&mut self, duration_secs: u64) -> Result<(), FabricError> {
152        self.lease.renew(duration_secs)
153    }
154
155    fn status(&self) -> LeaseStatus {
156        convert_status(self.lease.status())
157    }
158}
159
160/// Adapter wrapping a [`NetLease`] as a [`RenewableLease`].
161pub struct NetLeaseAdapter {
162    lease: NetLease,
163}
164
165impl NetLeaseAdapter {
166    /// Wrap an existing [`NetLease`] for use with [`RenewalManager`](crate::RenewalManager).
167    pub fn new(lease: NetLease) -> Self {
168        Self { lease }
169    }
170
171    /// Borrow the wrapped lease.
172    pub fn inner(&self) -> &NetLease {
173        &self.lease
174    }
175}
176
177impl RenewableLease for NetLeaseAdapter {
178    fn lease_id(&self) -> u128 {
179        self.lease.lease_id()
180    }
181
182    fn expires_at_unix_secs(&self) -> u64 {
183        self.lease.expires_at_unix_secs()
184    }
185
186    fn renew(&mut self, duration_secs: u64) -> Result<(), FabricError> {
187        self.lease.renew(duration_secs)
188    }
189
190    fn status(&self) -> LeaseStatus {
191        convert_status(self.lease.status())
192    }
193}