use-with 0.2.0

Provides resource management utilities inspired by Kotlin's 'use' function and C#'s 'using' block, ensuring proper utilization and disposal of resources in Rust.
Documentation
  • Coverage
  • 100%
    5 out of 5 items documented4 out of 5 items with examples
  • Size
  • Source code size: 26.38 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.28 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • sunsided/use-with
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • sunsided

use_with

codecov

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
    }
}

#[test]
fn it_works() {
    let resource = Resource::new();
    let result = resource.use_with(|res| {
        // Perform operations with `res`, return anything.
        42
    });

    // The resource is now dropped.
    assert_eq!(result, 42);
}