Struct Module

Source
pub struct Module {
    pub name: String,
    /* private fields */
}
Expand description

Defines a module.

Fields§

§name: String

The module’s name.

Implementations§

Source§

impl Module

Source

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

Return a new, blank module.

§Arguments
  • name - The name of the module.
§Examples
use rust_codegen::Module;
 
let foo_module = Module::new("Foo");
Source

pub fn scope(&mut self) -> &mut Scope

Returns a mutable reference to the module’s scope.

§Examples
use rust_codegen::Module;
 
let mut foo_module = Module::new("Foo");
println!("{:?}", foo_module.scope());
Source

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

Set the module visibility.

§Arguments
  • vis - The visibility of the module.
§Examples
use rust_codegen::Module;
 
let mut foo_module = Module::new("Foo");
foo_module.vis("pub");
Source

pub fn import(&mut self, path: &str, ty: &str) -> &mut Self

Import a type into the module’s scope.

This results in a new use statement bein added to the beginning of the module.

§Arguments
  • path - The path to the type to import.
  • ty - The type to import.
§Examples
use rust_codegen::Module;
 
let mut foo_module = Module::new("Foo");
foo_module.import("rust_codegen", "Module");
Source

pub fn new_module(&mut self, name: &str) -> &mut Module

Push a new module definition, returning a mutable reference to it.

§Panics

Since a module’s name must uniquely identify it within the scope in which it is defined, pushing a module whose name is already defined in this scope will cause this function to panic.

In many cases, the get_or_new_module function is preferrable, as it will return the existing definition instead.

§Arguments
  • name - The name of the module.
§Examples
use rust_codegen::Module;
 
let mut foo_module = Module::new("Foo");
foo_module.new_module("Bar");
Source

pub fn get_module<Q: ?Sized>(&self, name: &Q) -> Option<&Module>
where String: PartialEq<Q>,

Returns a reference to a module if it is exists in this scope.

§Arguments
  • name - The name of the module to get.
§Examples
use rust_codegen::Module;
 
let mut foo_module = Module::new("Foo");
foo_module.new_module("Bar");
 
foo_module.get_module("Bar");
Source

pub fn get_module_mut<Q: ?Sized>(&mut self, name: &Q) -> Option<&mut Module>
where String: PartialEq<Q>,

Returns a mutable reference to a module if it is exists in this scope.

§Arguments
  • name - The name of the module to get.
§Examples
use rust_codegen::Module;
 
let mut foo_module = Module::new("Foo");
foo_module.new_module("Bar");
 
foo_module.get_module("Bar");
Source

pub fn get_or_new_module(&mut self, name: &str) -> &mut Module

Returns a mutable reference to a module, creating it if it does not exist.

§Arguments
  • name - The name of the module to get or create if it doesn’t exist.
§Examples
use rust_codegen::Module;
 
let mut foo_module = Module::new("Foo");
foo_module.get_or_new_module("Bar");
Source

pub fn push_module(&mut self, item: Module) -> &mut Self

Push a module definition.

§Panics

Since a module’s name must uniquely identify it within the scope in which it is defined, pushing a module whose name is already defined in this scope will cause this function to panic.

In many cases, the get_or_new_module function is preferrable, as it will return the existing definition instead.

§Arguments
  • item - The module to push.
§Examples
use rust_codegen::Module;
 
let mut foo_module = Module::new("Foo");
let mut bar_module = Module::new("Bar");
 
foo_module.push_module(bar_module);
Source

pub fn new_struct(&mut self, name: &str) -> &mut Struct

Push a new struct definition, returning a mutable reference to it.

§Arguments
  • name - The name of the struct to push.
§Examples
use rust_codegen::Module;
 
let mut foo_module = Module::new("Foo");
foo_module.new_struct("Bar");
Source

pub fn push_struct(&mut self, item: Struct) -> &mut Self

Push a structure definition.

§Arguments
  • item - The struct definition to push.
§Examples
use rust_codegen::{Module,Struct};
 
let mut foo_module = Module::new("Foo");
let mut bar_struct = Struct::new("Bar");
 
foo_module.push_struct(bar_struct);
Source

pub fn new_fn(&mut self, name: &str) -> &mut Function

Push a new function definition, returning a mutable reference to it.

§Arguments
  • name - The name of the function to push.
§Examples
use rust_codegen::Module;
 
let mut foo_module = Module::new("Foo");
foo_module.new_fn("bar_fn");
Source

pub fn push_fn(&mut self, item: Function) -> &mut Self

Push a function definition.

§Arguments
  • item - The function definition to push.
§Examples
use rust_codegen::{Function,Module};
 
let mut foo_module = Module::new("Foo");
let mut bar_fn = Function::new("bar_fn");
 
foo_module.push_fn(bar_fn);
Source

pub fn new_enum(&mut self, name: &str) -> &mut Enum

Push a new enum definition, returning a mutable reference to it.

§Arguments
  • name - The name of the enum.
§Examples
use rust_codegen::Module;
 
let mut foo_module = Module::new("Foo");
foo_module.new_enum("Bar");
Source

pub fn push_enum(&mut self, item: Enum) -> &mut Self

Push an enum definition.

§Arguments
  • item - The enum definition to push.
§Examples
use rust_codegen::{Enum,Module};
 
let mut foo_module = Module::new("Foo");
let mut bar_enum = Enum::new("Bar");
 
foo_module.push_enum(bar_enum);
Source

pub fn new_impl(&mut self, target: &str) -> &mut Impl

Push a new impl block, returning a mutable reference to it.

§Arguments
  • target - The impl block to push.
§Examples
use rust_codegen::Module;
 
let mut foo_module = Module::new("Foo");
foo_module.new_impl("Bar");
Source

pub fn push_impl(&mut self, item: Impl) -> &mut Self

Push an impl block.

§Arguments
  • item - The impl definition to push.
§Examples
use rust_codegen::{Impl,Module};
 
let mut foo_module = Module::new("Foo");
let mut bar_impl = Impl::new("Bar");
 
foo_module.push_impl(bar_impl);
Source

pub fn push_trait(&mut self, item: Trait) -> &mut Self

Push a trait definition.

§Arguments
  • item - The trait to push.
§Examples
use rust_codegen::{Module,Trait};
 
let mut foo_module = Module::new("Foo");
let mut bar_trait = Trait::new("Bar");
 
foo_module.push_trait(bar_trait);
Source

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

Formats the module using the given formatter.

§Arguments
  • fmt - The formatter to use.
§Examples
use rust_codegen::*;
 
let mut dest = String::new();
let mut fmt = Formatter::new(&mut dest);
 
let mut foo_module = Module::new("Foo");
foo_module.fmt(&mut fmt);

Trait Implementations§

Source§

impl Clone for Module

Source§

fn clone(&self) -> Module

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 Module

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Module

§

impl RefUnwindSafe for Module

§

impl Send for Module

§

impl Sync for Module

§

impl Unpin for Module

§

impl UnwindSafe for Module

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.