captur

Crates.iocaptur
lib.rscaptur
version1.0.0
sourcesrc
created_at2021-07-01 05:50:56.394918
updated_at2024-06-30 16:14:27.829063
descriptionMacro to capture whole structs from disjoint fields in a closure.
homepage
repositoryhttps://github.com/MitMaro/captur
max_upload_size
id417346
size11,806
Tim Oram (MitMaro)

documentation

https://docs.rs/captur/

README

Crates.io docs GitHub license

Captur

Starting in Rust 2021, Rust will no longer capture whole structs and instead will only capture a disjoint set of the fields used in a closure. In some cases, it is necessary to capture the structs to retain a particular drop order. This macro will capture the struct within the closure, ensuring the correct drop order.

The Fix

The typical fix to this problem is to create an unused reference to the struct.

let some_struct = SomeStruct::new();
let result = || {
    // capture some_struct within the closure
    let _ = &some_struct;
    println!("{}", some_struct.y);
}

While this is trivial to implement in closures where capturing is required, without a comment, the meaning of the unused line is difficult to determine. This macro provides a self documenting and potentially more concise way to capture the structs.

Installation and Usage

[dependencies]
captur = "1"
use captur::capture;

fn send_event_and_action(action: &Action, event: Event) {
    send(|sender| {
        capture!(action, event);
        sender.send(action.name.as_str(), event.code);
    });
}

Supported Rust Versions

This project will support all Rust versions since 1.51 when Rust first supported Rust 2021.

Dropping support for a Rust version will result in a major version bump, following Semantic Versioning.

License

Captur is released under the ISC license. See LICENSE.

Commit count: 10

cargo fmt