use super::Metadata; use crate::authenticated::discovery::actors::tracker::ingress::Releaser; use commonware_cryptography::PublicKey; /// Reservation for a peer in the network. This is used to ensure that the peer is reserved only /// once, and that the reservation is released when the peer connection fails or is closed. pub struct Reservation { /// Metadata about the reservation. metadata: Metadata

, /// Used to automatically notify the completion of the reservation when it is dropped. /// /// Stored as an `Option` to avoid unnecessary cloning by `take`ing the value. releaser: Option>, } impl Reservation

{ /// Create a new reservation for a peer. pub fn new(metadata: Metadata

, releaser: Releaser

) -> Self { Self { metadata, releaser: Some(releaser), } } } impl Reservation

{ /// Returns the metadata associated with this reservation. pub fn metadata(&self) -> &Metadata

{ &self.metadata } } impl Drop for Reservation

{ fn drop(&mut self) { let mut releaser = self .releaser .take() .expect("Reservation::drop called twice"); releaser.release(self.metadata.clone()); } }