Trait const_gen::CompileConst
source · pub trait CompileConst {
// Required methods
fn const_type() -> String;
fn const_val(&self) -> String;
// Provided methods
fn declaration(
&self,
attrs: &str,
vis: &str,
declaration_type: DeclarationType,
name: &str
) -> String { ... }
fn const_declaration(&self, attrs: &str, vis: &str, name: &str) -> String { ... }
fn static_declaration(&self, attrs: &str, vis: &str, name: &str) -> String { ... }
fn const_definition(_attrs: &str, _vis: &str) -> String { ... }
}
Expand description
Trait which defines how a type should be represented as a constant
Required Methods§
sourcefn const_type() -> String
fn const_type() -> String
Get a string representation of a type. This must be implemented for each type. Note that this is not necessarily a representation of the ACTUAL type, but rather the type that should be used if this data is going to be represented as a compile-time constant.
Provided Methods§
sourcefn declaration(
&self,
attrs: &str,
vis: &str,
declaration_type: DeclarationType,
name: &str
) -> String
fn declaration( &self, attrs: &str, vis: &str, declaration_type: DeclarationType, name: &str ) -> String
Generates the declaration statement string.
Takes 3 strings: Attributes, a visibility (eg pub) and a name (a SCREAMING_SNAKE_CASE string is preferred), plus a declaration type (e.g. const or static).
It then constructs a valid Rust declaration statement using the type and value of the current object by calling const_val() and const_type().
use const_gen::{CompileConst, DeclarationType};
let test_str_declaration = "I'm a string!".declaration(
"#[allow(dead_code)]",
"pub(crate)",
DeclarationType::Const,
"TEST_STR",
);
assert_eq!(
test_str_declaration,
r#"#[allow(dead_code)] pub(crate) const TEST_STR: &str = "I'm a string!";"#
);
sourcefn const_declaration(&self, attrs: &str, vis: &str, name: &str) -> String
fn const_declaration(&self, attrs: &str, vis: &str, name: &str) -> String
Generates the declaration statement string for a const
declaration.
See declaration() for more information.
sourcefn static_declaration(&self, attrs: &str, vis: &str, name: &str) -> String
fn static_declaration(&self, attrs: &str, vis: &str, name: &str) -> String
Generates the declaration statement string for a static
declaration.
See declaration() for more information.
sourcefn const_definition(_attrs: &str, _vis: &str) -> String
fn const_definition(_attrs: &str, _vis: &str) -> String
Return a const definition for this type. Attributes may be included, and
must be formatted as the compiler would expect to see them (including
the pound sign and square brackets "#[...]"
). Always returns an empty
string for types defined in the standard library. Typically this is
easier to call instead through the const_definition! macro. Visibility
modifiers (eg, pub(…)) may be used, or an empty string passed in to
generate a private item.