pub trait Inspect {
// Required method
fn inspect(&self, req: Request<'_>);
}
Expand description
Trait implemented by objects whose state can be inspected but not mutated.
For most cases, users should use the derive version of Inspect
as this will automatically update the implementation as new fields are added.
However there are cases where a user may want to manually implement this trait, such as when
an inspection result may require multiple struct fields for a single inspection field.
Users should take advantage of Rust’s struct destructuring to introduce compiler errors to
keep manual Inspect
trait implementations up to date as the struct changes.
§Example
The following code compiles.
struct Foo {
id: u32,
more_stuff: usize,
super_string: String,
}
impl Inspect for Foo {
fn inspect(&self, req: inspect::Request<'_>) {
let Foo {
id,
more_stuff,
super_string,
} = self;
let mut resp = req.respond();
resp.hex("id", id);
resp.field("more_stuff", more_stuff);
// Only provide the super_string field if id is 2.
if *id == 2 {
resp.field("super_string", super_string);
}
}
}
However, someone adding a new field to Foo
without updating the inspect implementation will no longer compile.
ⓘ
struct Foo {
id: u32,
more_stuff: usize,
super_string: String,
awesome_new_field: String,
}
impl Inspect for Foo {
fn inspect(&self, req: inspect::Request<'_>) {
let Foo {
id,
more_stuff,
super_string,
// Missing awesome_new_field!
} = self;
let mut resp = respond();
resp.hex("id", id);
resp.field("more_stuff", more_stuff);
// Only provide the super_string field if id is 2.
if id == 2 {
resp.field("super_string", super_string);
}
}
}