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.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl CompileConst for &str
impl CompileConst for &str
Source§impl CompileConst for IpAddr
impl CompileConst for IpAddr
Source§impl CompileConst for SocketAddr
impl CompileConst for SocketAddr
Source§impl CompileConst for bool
impl CompileConst for bool
Source§impl CompileConst for char
impl CompileConst for char
Source§impl CompileConst for f32
impl CompileConst for f32
Source§impl CompileConst for f64
impl CompileConst for f64
Source§impl CompileConst for i16
impl CompileConst for i16
Source§impl CompileConst for i32
impl CompileConst for i32
Source§impl CompileConst for i64
impl CompileConst for i64
Source§impl CompileConst for i128
impl CompileConst for i128
Source§impl CompileConst for isize
impl CompileConst for isize
Source§impl CompileConst for str
impl CompileConst for str
Source§impl CompileConst for u16
impl CompileConst for u16
Source§impl CompileConst for u32
impl CompileConst for u32
Source§impl CompileConst for u64
impl CompileConst for u64
Source§impl CompileConst for u128
impl CompileConst for u128
Source§impl CompileConst for usize
impl CompileConst for usize
Source§impl CompileConst for String
impl CompileConst for String
Source§impl CompileConst for Ipv4Addr
impl CompileConst for Ipv4Addr
Source§impl CompileConst for Ipv6Addr
impl CompileConst for Ipv6Addr
Source§impl CompileConst for SocketAddrV4
impl CompileConst for SocketAddrV4
Source§impl CompileConst for SocketAddrV6
impl CompileConst for SocketAddrV6
Source§impl<A: CompileConst, B: CompileConst> CompileConst for (A, B)
impl<A: CompileConst, B: CompileConst> CompileConst for (A, B)
Source§impl<A: CompileConst, B: CompileConst, C: CompileConst> CompileConst for (A, B, C)
impl<A: CompileConst, B: CompileConst, C: CompileConst> CompileConst for (A, B, C)
Source§impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst> CompileConst for (A, B, C, D)
impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst> CompileConst for (A, B, C, D)
Source§impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst> CompileConst for (A, B, C, D, E)
impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst> CompileConst for (A, B, C, D, E)
Source§impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst> CompileConst for (A, B, C, D, E, F)
impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst> CompileConst for (A, B, C, D, E, F)
Source§impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst> CompileConst for (A, B, C, D, E, F, G)
impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst> CompileConst for (A, B, C, D, E, F, G)
Source§impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst, H: CompileConst> CompileConst for (A, B, C, D, E, F, G, H)
impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst, H: CompileConst> CompileConst for (A, B, C, D, E, F, G, H)
Source§impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst, H: CompileConst, I: CompileConst> CompileConst for (A, B, C, D, E, F, G, H, I)
impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst, H: CompileConst, I: CompileConst> CompileConst for (A, B, C, D, E, F, G, H, I)
Source§impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst, H: CompileConst, I: CompileConst, J: CompileConst> CompileConst for (A, B, C, D, E, F, G, H, I, J)
impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst, H: CompileConst, I: CompileConst, J: CompileConst> CompileConst for (A, B, C, D, E, F, G, H, I, J)
Source§impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst, H: CompileConst, I: CompileConst, J: CompileConst, K: CompileConst> CompileConst for (A, B, C, D, E, F, G, H, I, J, K)
impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst, H: CompileConst, I: CompileConst, J: CompileConst, K: CompileConst> CompileConst for (A, B, C, D, E, F, G, H, I, J, K)
Source§impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst, H: CompileConst, I: CompileConst, J: CompileConst, K: CompileConst, L: CompileConst> CompileConst for (A, B, C, D, E, F, G, H, I, J, K, L)
impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst, H: CompileConst, I: CompileConst, J: CompileConst, K: CompileConst, L: CompileConst> CompileConst for (A, B, C, D, E, F, G, H, I, J, K, L)
Source§impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst, H: CompileConst, I: CompileConst, J: CompileConst, K: CompileConst, L: CompileConst, M: CompileConst> CompileConst for (A, B, C, D, E, F, G, H, I, J, K, L, M)
impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst, H: CompileConst, I: CompileConst, J: CompileConst, K: CompileConst, L: CompileConst, M: CompileConst> CompileConst for (A, B, C, D, E, F, G, H, I, J, K, L, M)
Source§impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst, H: CompileConst, I: CompileConst, J: CompileConst, K: CompileConst, L: CompileConst, M: CompileConst, N: CompileConst> CompileConst for (A, B, C, D, E, F, G, H, I, J, K, L, M, N)
impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst, H: CompileConst, I: CompileConst, J: CompileConst, K: CompileConst, L: CompileConst, M: CompileConst, N: CompileConst> CompileConst for (A, B, C, D, E, F, G, H, I, J, K, L, M, N)
Source§impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst, H: CompileConst, I: CompileConst, J: CompileConst, K: CompileConst, L: CompileConst, M: CompileConst, N: CompileConst, O: CompileConst> CompileConst for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O)
impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst, H: CompileConst, I: CompileConst, J: CompileConst, K: CompileConst, L: CompileConst, M: CompileConst, N: CompileConst, O: CompileConst> CompileConst for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O)
Source§impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst, H: CompileConst, I: CompileConst, J: CompileConst, K: CompileConst, L: CompileConst, M: CompileConst, N: CompileConst, O: CompileConst, P: CompileConst> CompileConst for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P)
impl<A: CompileConst, B: CompileConst, C: CompileConst, D: CompileConst, E: CompileConst, F: CompileConst, G: CompileConst, H: CompileConst, I: CompileConst, J: CompileConst, K: CompileConst, L: CompileConst, M: CompileConst, N: CompileConst, O: CompileConst, P: CompileConst> CompileConst for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P)
Source§impl<E: CompileConst> CompileConst for HashSet<E>
Available on crate feature phf only.
impl<E: CompileConst> CompileConst for HashSet<E>
phf only.Source§impl<K: CompileConst, V: CompileConst> CompileConst for HashMap<K, V>
Available on crate feature phf only.
impl<K: CompileConst, V: CompileConst> CompileConst for HashMap<K, V>
phf only.