macro_rules! derive_deftly_template_HasMemoryCost {
({ $($driver:tt)* } [$($aoptions:tt)*] ($($future:tt)*) $($tpassthrough:tt)*) => { ... };
($($wrong:tt)*) => { ... };
}
Expand description
Derive HasMemoryCost
Each field must implement HasMemoryCostStructural
.
Valid for structs and enums.
§Top-level attributes
#[deftly(has_memory_cost(bounds = "BOUNDS"))]
: Additional bounds to apply to the implementation.
§Field attributes
#[deftly(has_memory_cost(copy))]
: This field isCopy + 'static
so does not reference any data that should be accounted.#[deftly(has_memory_cost(indirect_fn = "FUNCTION"))]
:FUNCTION
is a function with the signature and semantics ofHasMemoryCostStructural::indirect_memory_cost
,#[deftly(has_memory_cost(indirect_size = "EXPR"))]
:EXPR
is an expression of type usize with the semantics of a return value fromHasMemoryCostStructural::indirect_memory_cost
.
With one of these, the field doesn’t need to implement HasMemoryCostStructural
.
§Example
use derive_deftly::Deftly;
use std::mem::size_of;
use tor_memquota::{HasMemoryCost, HasMemoryCostStructural};
use tor_memquota::derive_deftly_template_HasMemoryCost;
#[derive(Deftly)]
#[derive_deftly(HasMemoryCost)]
#[deftly(has_memory_cost(bounds = "Data: HasMemoryCostStructural"))]
struct Struct<Data> {
data: Data,
#[deftly(has_memory_cost(indirect_size = "0"))] // this is a good guess
num: serde_json::Number,
#[deftly(has_memory_cost(copy))]
msg: &'static str,
#[deftly(has_memory_cost(indirect_fn = "|info, _et| String::capacity(info)"))]
info: safelog::Sensitive<String>,
}
let s = Struct {
data: String::with_capacity(33),
num: serde_json::Number::from_f64(0.0).unwrap(),
msg: "hello",
info: String::with_capacity(12).into(),
};
let Some(et) = tor_memquota::EnabledToken::new_if_compiled_in() else { return };
assert_eq!(
s.memory_cost(et),
size_of::<Struct<String>>() + 33 + 12,
);
This is a derive_deftly
template. Do not invoke it directly.
To use it, write: #[derive(Deftly)] #[derive_deftly(HasMemoryCost)]
.