https://github.com/sunsided/use-with
Provides resource management utilities inspired by Kotlin's 'use' function and C#'s 'using' block, ensuring proper utilization and disposal of resources in Rust.
https://github.com/sunsided/use-with
disposable rust using
Last synced: about 2 months ago
JSON representation
Provides resource management utilities inspired by Kotlin's 'use' function and C#'s 'using' block, ensuring proper utilization and disposal of resources in Rust.
- Host: GitHub
- URL: https://github.com/sunsided/use-with
- Owner: sunsided
- License: mit
- Created: 2024-12-17T13:37:19.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-24T08:43:01.000Z (9 months ago)
- Last Synced: 2025-09-23T08:43:53.680Z (6 months ago)
- Topics: disposable, rust, using
- Language: Rust
- Homepage: https://crates.io/crates/use-with
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# use_with
[](https://codecov.io/gh/sunsided/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:
```rust
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);
}
```