pub struct Lock<S: LockState> {
data: LockRecord,
_state: PhantomData<S>,
}Expand description
A lock with compile-time state tracking.
The type parameter S indicates the current state of the lock.
Methods are only available in appropriate states:
Lock<Acquired>: Can be extended or releasedLock<Released>: Cannot be used (transitions consume the lock)
§Example
let lock: Lock<Acquired> = acquire_lock(...);
let extended_lock = lock.extend(Duration::from_secs(30)); // OK
let data = extended_lock.release(); // OK, consumes the lock
// Can't use extended_lock anymore - it was consumed!Fields§
§data: LockRecord§_state: PhantomData<S>Implementations§
Source§impl<S: LockState> Lock<S>
impl<S: LockState> Lock<S>
Sourcepub fn data(&self) -> &LockRecord
pub fn data(&self) -> &LockRecord
Access the underlying lock data (read-only).
Sourcepub fn resource_type(&self) -> &ResourceType
pub fn resource_type(&self) -> &ResourceType
Get the resource type being locked.
Sourcepub fn resource_id(&self) -> Uuid
pub fn resource_id(&self) -> Uuid
Get the resource ID being locked.
Sourcepub fn holder_agent_id(&self) -> AgentId
pub fn holder_agent_id(&self) -> AgentId
Get the agent holding the lock.
Sourcepub fn acquired_at(&self) -> Timestamp
pub fn acquired_at(&self) -> Timestamp
Get when the lock was acquired.
Sourcepub fn expires_at(&self) -> Timestamp
pub fn expires_at(&self) -> Timestamp
Get when the lock expires.
Source§impl Lock<Acquired>
impl Lock<Acquired>
Sourcepub fn new(data: LockRecord) -> Self
pub fn new(data: LockRecord) -> Self
Create a new acquired lock from data.
This should only be called when a lock is successfully acquired.
Sourcepub fn extend(self, additional: Duration) -> Self
pub fn extend(self, additional: Duration) -> Self
Extend the lock duration.
Returns a new Lock<Acquired> with the updated expiry time.
The original lock is consumed.
Sourcepub fn extend_ms(self, additional_ms: i64) -> Self
pub fn extend_ms(self, additional_ms: i64) -> Self
Extend the lock by milliseconds.
Convenience method for extending by a millisecond count.
Sourcepub fn release(self) -> LockRecord
pub fn release(self) -> LockRecord
Release the lock and return the underlying data.
Consumes the lock, preventing further operations. The returned data can be used to update the database.
Sourcepub fn is_expired(&self, now: Timestamp) -> bool
pub fn is_expired(&self, now: Timestamp) -> bool
Check if the lock has expired.
Sourcepub fn remaining_duration(&self, now: Timestamp) -> Option<Duration>
pub fn remaining_duration(&self, now: Timestamp) -> Option<Duration>
Get remaining duration until expiry.
Sourcepub fn into_data(self) -> LockRecord
pub fn into_data(self) -> LockRecord
Consume the lock and return just the data (for serialization).