Struct Function

Source
pub struct Function {
    pub body: Option<Vec<Body>>,
    /* private fields */
}
Expand description

Defines a function.

Fields§

§body: Option<Vec<Body>>

Body contents.

Implementations§

Source§

impl Function

Source

pub fn new(name: &str) -> Self

Return a new function definition.

§Arguments
  • name - The name of the function.
§Examples
use rust_codegen::Function;
 
let foo_fn = Function::new("foo_fn");
Source

pub fn doc(&mut self, docs: &str) -> &mut Self

Set the function documentation.

§Arguments
  • docs - The docs to set for the function.
§Examples
use rust_codegen::Function;
 
let mut foo_fn = Function::new("foo_fn");
foo_fn.doc("Sample Foo function documentation.");
Source

pub fn allow(&mut self, allow: &str) -> &mut Self

Specify lint attribute to supress a warning or error.

§Arguments
  • allow - The lint attribute to add.
§Examples
use rust_codegen::Function;
 
let mut foo_fn = Function::new("foo_fn");
foo_fn.allow("dead_code");
Source

pub fn vis(&mut self, vis: &str) -> &mut Self

Set the function visibility.

§Arguments
  • vis - The visiblity to set for the function.
§Examples
use rust_codegen::Function;
 
let mut foo_fn = Function::new("foo_fn");
foo_fn.vis("pub");
Source

pub fn set_async(&mut self, async: bool) -> &mut Self

Set whether this function is async or not.

§Arguments
  • async - Indicates whether this function is async or not.
§Examples
use rust_codegen::Function;
 
let mut foo_fn = Function::new("foo_fn");
foo_fn.set_async(true);
Source

pub fn generic(&mut self, name: &str) -> &mut Self

Add a generic to the function.

§Arguments
  • name - The name of the generic to add.
§Examples
use rust_codegen::Function;
 
let mut foo_fn = Function::new("foo_fn");
foo_fn.generic("T");
Source

pub fn arg_self(&mut self) -> &mut Self

Add self as a function argument.

§Examples
use rust_codegen::Function;
 
let mut foo_fn = Function::new("foo_fn");
foo_fn.arg_self();
Source

pub fn arg_ref_self(&mut self) -> &mut Self

Add &self as a function argument.

§Examples
use rust_codegen::Function;
 
let mut foo_fn = Function::new("foo_fn");
foo_fn.arg_ref_self();
Source

pub fn arg_mut_self(&mut self) -> &mut Self

Add &mut self as a function argument.

§Examples
use rust_codegen::Function;
 
let mut foo_fn = Function::new("foo_fn");
foo_fn.arg_mut_self();
Source

pub fn arg<T>(&mut self, name: &str, ty: T) -> &mut Self
where T: Into<Type>,

Add a function argument.

§Arguments
  • name - The name of the argument.
  • ty - The type of the argument.
§Examples
use rust_codegen::Function;
 
let mut foo_fn = Function::new("foo_fn");
foo_fn.arg("name", "&str");
Source

pub fn ret<T>(&mut self, ty: T) -> &mut Self
where T: Into<Type>,

Set the function return type.

§Arguments
  • ty - The return type.
§Examples
use rust_codegen::Function;
 
let mut foo_fn = Function::new("foo_fn");
foo_fn.ret("String");
Source

pub fn bound<T>(&mut self, name: &str, ty: T) -> &mut Self
where T: Into<Type>,

Add a where bound to the function.

§Arguments
  • name - The name of the bound.
  • ty - The type of the bound.
§Examples
use rust_codegen::Function;
 
let mut foo_fn = Function::new("foo_fn");
foo_fn.bound("A", "TraitA");
Source

pub fn line<T>(&mut self, line: T) -> &mut Self
where T: ToString,

Push a line to the function implementation.

§Arguments
  • line - The line to add to the function.
§Examples
use rust_codegen::Function;
 
let mut foo_fn = Function::new("foo_fn");
foo_fn.line("println!(\"Hello, world!\")");
Source

pub fn attr(&mut self, attribute: &str) -> &mut Self

Add an attribute to the function.

§Arguments
  • attribute - The attribute to add.
§Examples
use rust_codegen::Function;

let mut foo_fn = Function::new("foo_fn");
foo_fn.attr("test");
Source

pub fn extern_abi(&mut self, abi: &str) -> &mut Self

Specify an extern ABI for the function.

§Arguments
  • abi - The extern ABI to add.
§Examples
use rust_codegen::Function;

let mut foo_fn = Function::new("foo_fn");
foo_fn.extern_abi("C");
Source

pub fn push_block(&mut self, block: Block) -> &mut Self

Push a block to the function implementation.

§Arguments
  • block - The block to push to the function.
§Examples
use rust_codegen::*;

let mut foo_fn = Function::new("foo_fn");
 
let mut block = Block::new("");
block.line("println!(\"Hello, world!\");");
 
foo_fn.push_block(block);
Source

pub fn fmt(&self, is_trait: bool, fmt: &mut Formatter<'_>) -> Result

Formats the function using the given formatter.

§Arguments
  • is_trait - Indicates whether it is a trait or not.
  • fmt - The formatter to use.
§Examples
use rust_codegen::*;
 
let mut dest = String::new();
let mut fmt = Formatter::new(&mut dest);
 
let mut foo_fn = Function::new("foo_fn");
foo_fn.fmt(false, &mut fmt);

Trait Implementations§

Source§

impl Clone for Function

Source§

fn clone(&self) -> Function

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Function

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.