pfn 0.1.0

Provide fn_trait's `call`, `call_mut`, and `call_once` on Stable Rust.
Documentation
  • Coverage
  • 50%
    4 out of 8 items documented1 out of 7 items with examples
  • Size
  • Source code size: 6.65 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.76 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • wishawa/pfn
    2 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • wishawa

PFn

Provide fn_trait's call, call_mut, and call_once on Stable Rust for functions / closures with ≤ 12 arguments.

Examples

Basic usage

let closure = |x: i32, y: i32, z: String| {
	println!("{}", z);
	x + y
};

// Once the `fn_trait` feature has stabilized, you would do this.
let result = closure.call((5, 42, "Hello World"));

// For now, use PFn.
let result = closure.pfn_call((5, 42, "Hello World"));

Generalizing over functions with different number of arguments

// Here, Func can be a function or closure that takes any number of arguments (actually 1 - 12 arguments).
struct Runnable<Args, Func: PFnOnce<Args>> {
	func: Func,
	args: Args
}
impl<Args, Func: PFnOnce<Args>> Runnable<Args, Func> {
	fn run(self) -> Func::PFnOutput {
		(self.func).pfn_call_once(self.args)
	}
}
let runnable = Runnable {
	func: |mut x: String| {
		x.push_str("!!!");
		x
	},
	args: ("hello world".into(),)
};
assert_eq!(runnable.run(), "hello world!!!");