Skip to content
volt
GitHub

std::derive

What @derive(...) attaches. Each derive is a trait with no functions, and its methods below are generic over the types that attach it: a comptime for over the type's fields, through @field. A derive of your own is written the same way: a trait, and methods bounded on it. (copy needs no derive: every struct copies field by field with copy x.)

Source: std/derive.volt

Types

trait std::derive::eq

trait eq

eq(other: T&): every field equal (by its own eq); == and != on the type call it

trait std::derive::hash

trait hash

hash(): the fields' hashes, combined (a map key)

trait std::derive::fmt

trait fmt

to_string(): the text println prints for the value

trait std::derive::json

trait json

to_json(): a JSON object with a member each field

Functions

std::derive::payload_eq

<P: type> fn payload_eq(a: P&, b: P&) -> bool

a variant's payload compared, hashed or made JSON: a tuple's element by element (a tuple has none of these of its own)

std::derive::payload_hash

<P: type> fn payload_hash(a: P&) -> u64

std::derive::payload_json

<P: type> fn payload_json(a: P&) -> std::json::value

Methods on other types

Attached to built-in types, or to every type (T).

<T: std::derive::eq> attach fn eq(this: T&, other: T&) -> bool
<T: std::derive::hash> attach fn hash(this: T&) -> u64
<T: std::derive::fmt> attach fn to_string(this: T&) -> std::string

println already prints any struct field by field: this gives that text as a string

<T: std::derive::json> attach fn to_json(this: T&) -> std::json::value
attach fn to_json(this: i8&) -> std::json::value

what a field's to_json is for the types std knows: numbers, bool, text, optionals and vectors. One each number type (there's no trait for numbers); a JSON number is an f64, so an integer past 2^53 loses its low bits

attach fn to_json(this: i16&) -> std::json::value
attach fn to_json(this: i32&) -> std::json::value
attach fn to_json(this: i64&) -> std::json::value
attach fn to_json(this: u8&) -> std::json::value
attach fn to_json(this: u16&) -> std::json::value
attach fn to_json(this: u32&) -> std::json::value
attach fn to_json(this: u64&) -> std::json::value
attach fn to_json(this: usize&) -> std::json::value
attach fn to_json(this: f32&) -> std::json::value
attach fn to_json(this: f64&) -> std::json::value
attach fn to_json(this: bool&) -> std::json::value
attach fn to_json(this: str&) -> std::json::value
<T: type> attach fn to_json(this: T?&) -> std::json::value
<T: type> attach fn hash(this: T?&) -> u64

optionals and vectors as map keys (and fields of a derived hash)