Skip to main content

PreDestruct

Trait PreDestruct 

Source
pub trait PreDestruct: Send + Sync {
    // Required method
    fn pre_destruct<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Sync + Send>>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             Self: 'async_trait;
}
Expand description

Trait for pre-destruction lifecycle hooks.

When a type has a method annotated with #[injectable(pre_destruct)], the derive macro generates an implementation of this trait that calls the annotated method.

§Execution Order

Pre-destruct hooks run in reverse topological order during container shutdown. Dependencies are destroyed before the types that depend on them.

§Error Handling

If a pre_destruct hook returns an error, it is collected. All remaining destructors still run (best-effort cleanup). After all destructors have been called, the accumulated errors are returned from Container::shutdown().

§Use Cases

  • Graceful database disconnection
  • Flushing buffers
  • Stopping background workers
  • Releasing external resources

§Example

impl Database {
    #[injectable(pre_destruct)]
    async fn shutdown(&self) -> Result<(), std::io::Error> {
        self.close_connections().await?;
        Ok(())
    }
}

Required Methods§

Source

fn pre_destruct<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Sync + Send>>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Run the pre-destruction hook.

Return Ok(()) on success, or an error to report cleanup failures. All destructors run even if some fail (best-effort cleanup).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§