virtue::generate

Struct FnBuilder

Source
pub struct FnBuilder<'a, P> { /* private fields */ }
Expand description

A builder for functions.

Implementations§

Source§

impl<'a, P: FnParent> FnBuilder<'a, P>

Source

pub fn with_attr(self, attr: impl Into<String>) -> Self

Add an outer attribute

Source

pub fn with_lifetime(self, name: impl Into<String>) -> Self

Add a lifetime parameter.

generator
    .r#impl()
    .generate_fn("foo") // fn foo()
    .with_lifetime("a") // fn foo<'a>()
Source

pub fn as_async(self) -> Self

Make the function async

generator
    .r#impl()
    .generate_fn("foo") // fn foo()
    .as_async() // async fn foo()
Source

pub fn with_lifetime_deps<ITER, I>( self, name: impl Into<String>, dependencies: ITER, ) -> Self
where ITER: IntoIterator<Item = I>, I: Into<String>,

Add a lifetime parameter.

dependencies are the lifetime dependencies of the given lifetime.

generator
    .r#impl()
    .generate_fn("foo") // fn foo()
    .with_lifetime("a") // fn foo<'a>()
    .with_lifetime_deps("b", ["a"]) // fn foo<'b: 'a>()
Source

pub fn with_generic(self, name: impl Into<String>) -> Self

Add a generic parameter. Keep in mind that will not work for lifetimes.

generator
    .r#impl()
    .generate_fn("foo") // fn foo()
    .with_generic("D") // fn foo<D>()
Source

pub fn with_generic_deps<DEP, I>( self, name: impl Into<String>, dependencies: DEP, ) -> Self
where DEP: IntoIterator<Item = I>, I: Into<String>,

Add a generic parameter. Keep in mind that will not work for lifetimes.

dependencies are the dependencies of the parameter.

generator
    .r#impl()
    .generate_fn("foo") // fn foo()
    .with_generic("D") // fn foo<D>()
    .with_generic_deps("E", ["Encodable"]) // fn foo<D, E: Encodable>();
Source

pub fn with_self_arg(self, self_arg: FnSelfArg) -> Self

Set the value for self. See FnSelfArg for more information.

generator
    .r#impl()
    .generate_fn("foo") // fn foo()
    .with_self_arg(FnSelfArg::RefSelf) // fn foo(&self)
Source

pub fn with_arg(self, name: impl Into<String>, ty: impl Into<String>) -> Self

Add an argument with a name and a ty.

generator
    .r#impl()
    .generate_fn("foo") // fn foo()
    .with_arg("a", "u32") // fn foo(a: u32)
    .with_arg("b", "u32") // fn foo(a: u32, b: u32)
Source

pub fn with_return_type(self, ret_type: impl Into<String>) -> Self

Set the return type for the function. By default the function will have no return type.

generator
    .r#impl()
    .generate_fn("foo") // fn foo()
    .with_return_type("u32") // fn foo() -> u32
Source

pub fn make_pub(self) -> Self

Make the function pub. If this is not called, the function will have no visibility modifier.

Source

pub fn body( self, body_builder: impl FnOnce(&mut StreamBuilder) -> Result, ) -> Result

Complete the function definition. This function takes a callback that will form the body of the function.

generator
    .r#impl()
    .generate_fn("foo") // fn foo()
    .body(|b| {
        b.push_parsed("println!(\"hello world\");")?;
        Ok(())
    })
    .unwrap();
// fn foo() {
//     println!("Hello world");
// }

Auto Trait Implementations§

§

impl<'a, P> Freeze for FnBuilder<'a, P>

§

impl<'a, P> RefUnwindSafe for FnBuilder<'a, P>
where P: RefUnwindSafe,

§

impl<'a, P> Send for FnBuilder<'a, P>
where P: Send,

§

impl<'a, P> Sync for FnBuilder<'a, P>
where P: Sync,

§

impl<'a, P> Unpin for FnBuilder<'a, P>

§

impl<'a, P> !UnwindSafe for FnBuilder<'a, P>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.