Crate use_with

Source
Expand description

§use_with

Provides resource management utilities, ensuring that resources are properly utilized and subsequently dropped, similar to patterns found in other programming languages like Kotlin’s use function and C#’s using block.

This module offers two primary functions:

  • use_with: Executes a closure synchronously, consuming the resource.
  • use_with_async: Executes an asynchronous closure, consuming the resource.

These functions facilitate safe and efficient resource handling, ensuring that resources are properly utilized and dropped, even in asynchronous contexts.

§Features

  • Synchronous Resource Management: The use_with function allows for synchronous operations on resources, ensuring that resources are properly utilized and dropped after the operation completes.

  • Asynchronous Resource Management: The use_with_async function facilitates asynchronous operations on resources, ensuring that resources are properly utilized and dropped after the asynchronous operation completes.

§Usage

To use these functions, the Use trait is auto-implemented for your resource types; simply call the appropriate method:

use use_with::Use;

struct Resource;

impl Resource {
    fn new() -> Self {
        Resource
    }
}

let result = Resource::new().use_with(|res| {
    // Perform operations with `res`, return anything.
    42
});

assert_eq!(result, 42);

Macros§

using
Executes a closure with a resource, ensuring the resource is properly utilized and dropped.

Traits§

Use
A trait that facilitates resource management by ensuring proper usage and subsequent dropping.