1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#![deny(
	future_incompatible,
	nonstandard_style,
	rust_2018_compatibility,
	rust_2018_idioms,
	unused,
	warnings
)]
// rustc's additional allowed by default lints
#![deny(
	absolute_paths_not_starting_with_crate,
	deprecated_in_future,
	elided_lifetimes_in_paths,
	explicit_outlives_requirements,
	keyword_idents,
	macro_use_extern_crate,
	meta_variable_misuse,
	missing_abi,
	missing_copy_implementations,
	missing_debug_implementations,
	missing_docs,
	non_ascii_idents,
	noop_method_call,
	or_patterns_back_compat,
	pointer_structural_match,
	semicolon_in_expressions_from_macros,
	single_use_lifetimes,
	trivial_casts,
	trivial_numeric_casts,
	unreachable_pub,
	unsafe_code,
	unsafe_op_in_unsafe_fn,
	unstable_features,
	unused_crate_dependencies,
	unused_extern_crates,
	unused_import_braces,
	unused_lifetimes,
	unused_qualifications,
	unused_results,
	variant_size_differences
)]
// enable all of Clippy's lints
#![deny(clippy::all, clippy::cargo, clippy::nursery, clippy::pedantic, clippy::restriction)]
#![allow(
	clippy::blanket_clippy_restriction_lints,
	clippy::implicit_return,
	clippy::missing_docs_in_private_items,
	clippy::redundant_pub_crate,
	clippy::tabs_in_doc_comments
)]
#![deny(
	rustdoc::bare_urls,
	rustdoc::broken_intra_doc_links,
	rustdoc::invalid_codeblock_attributes,
	rustdoc::invalid_html_tags,
	rustdoc::missing_crate_level_docs,
	rustdoc::private_doc_tests,
	rustdoc::private_intra_doc_links
)]

//! # 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.
//!
//! # Example
//! ```
//! use captur::capture;
//! struct SomeStruct {
//! 	a: String,
//! 	b: String,
//! }
//!
//! impl SomeStruct {
//! 	fn new() -> Self {
//! 		Self {
//! 			a: String::from("a"),
//! 			b: String::from("b"),
//! 		}
//! 	}
//! }
//!
//! let some_struct = SomeStruct::new();
//! let result = || {
//! 	captur::capture!(some_struct);
//! 	format!("{}", some_struct.b)
//! };
//!
//! println!("{}", result());
//! ```

/// Create a reference to a struct, that will ensure it is captured by a closure.
#[macro_export]
macro_rules! capture {
	($( $v:expr ),*) => {
		$(let _ = &$v;)*
	};
}

#[cfg(test)]
mod tests {
	use super::*;

	struct TestStruct {
		a: String,
		b: String,
	}

	impl TestStruct {
		fn new() -> Self {
			Self {
				a: String::from("a"),
				b: String::from("b"),
			}
		}
	}

	#[test]
	fn single() {
		let a = TestStruct::new();
		let result = || {
			capture!(a);
			format!("Value: {}", a.a)
		};

		assert_eq!(result(), "Value: a");
	}

	#[test]
	fn multiple() {
		let a = TestStruct::new();
		let b = TestStruct::new();
		let result = || {
			capture!(a, b);
			format!("Value: {} {}", a.a, b.b)
		};

		assert_eq!(result(), "Value: a b");
	}
}