From 729df92dd28ed2aff37bd2c8e5eab2ae8c01d53d Mon Sep 17 00:00:00 2001 From: victor koenders Date: Wed, 10 Aug 2022 20:45:01 +0200 Subject: [PATCH] Added `as_async` to the `FnBuilder` --- src/generate/generate_fn.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/generate/generate_fn.rs b/src/generate/generate_fn.rs index 5b7275d..2ec1cdf 100644 --- a/src/generate/generate_fn.rs +++ b/src/generate/generate_fn.rs @@ -9,6 +9,7 @@ pub struct FnBuilder<'a, P> { parent: &'a mut P, name: String, + is_async: bool, lifetimes: Vec<(String, Vec)>, generics: Vec<(String, Vec)>, self_arg: FnSelfArg, @@ -22,6 +23,7 @@ impl<'a, P: FnParent> FnBuilder<'a, P> { Self { parent, name: name.into(), + is_async: false, lifetimes: Vec::new(), generics: Vec::new(), self_arg: FnSelfArg::None, @@ -47,6 +49,22 @@ impl<'a, P: FnParent> FnBuilder<'a, P> { self } + /// Make the function async + /// + /// ```no_run + /// # use virtue::prelude::Generator; + /// # let mut generator: Generator = unsafe { std::mem::zeroed() }; + /// generator + /// .r#impl() + /// .generate_fn("foo") // fn foo() + /// .as_async(); // async fn foo() + /// ``` + #[must_use] + pub fn as_async(mut self) -> Self { + self.is_async = true; + self + } + /// Add a lifetime parameter. /// /// `dependencies` are the lifetime dependencies of the given lifetime. @@ -199,6 +217,7 @@ impl<'a, P: FnParent> FnBuilder<'a, P> { let FnBuilder { parent, name, + is_async, lifetimes, generics, self_arg, @@ -213,6 +232,9 @@ impl<'a, P: FnParent> FnBuilder<'a, P> { if vis == Visibility::Pub { builder.ident_str("pub"); } + if is_async { + builder.ident_str("async"); + } builder.ident_str("fn"); builder.ident_str(name);