Struct aldrin::LifetimeScope

source ·
pub struct LifetimeScope { /* private fields */ }
Expand description

A scope that notifies other clients when ends.

Aldrin services are inherently stateless with respect to the clients that are using them. Sometimes it becomes necessary to know when a client is no longer interested in using a service.

Scopes and Lifetimes solve this problem in a robust way. One client can create a LifetimeScope while another binds a Lifetime to it. The Lifetime will then notify when the scope ends. This can be triggered explicitly (end) or implicitly by dropping the scope. A scope also ends when the client that owns it disconnects from be bus for some reason.

§Examples

// Assume client 1 is using a service from client 2. In turn, client 2 needs to know when
// client 1 is done.

// Client 1 creates a scope and passes its id to client 2.
let scope = client1.create_lifetime_scope().await?;
let id = scope.id();

// Client 2 creates a lifetime from the id.
let lifetime = client2.create_lifetime(id).await?;

tokio::spawn(async move {
    // Move in the scope and do some work. The scope is dropped at the end.
    let _scope = scope;
});

// Resolves when the associated scope ends.
lifetime.await;

Implementations§

source§

impl LifetimeScope

source

pub async fn new(client: &Handle) -> Result<Self, Error>

Creates a new scope.

source

pub fn id(&self) -> LifetimeId

Return the scope’s id.

source

pub fn client(&self) -> &Handle

Returns a handle to the client that was used to create the scope.

source

pub async fn end(&self) -> Result<(), Error>

Ends the scope.

If the scope has already ended, Error::InvalidLifetime is returned.

Trait Implementations§

source§

impl Debug for LifetimeScope

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Serialize for LifetimeScope

source§

fn serialize(&self, serializer: Serializer<'_>) -> Result<(), SerializeError>

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.