Usually, derivative will add a T: Debug bound for each type parameter T
of the current type. If you do not want that, you can specify an explicit bound:
Either on the type. This replaces all bounds:
#![allow(unused)]fnmain() {
externcrate derivative;
use derivative::Derivative;
traitMyDebug {
fnmy_fmt(&self, _: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error>;
}
use std::fmt::Debug;
#[derive(Derivative)]#[derivative(Debug(bound="T: Debug, U: MyDebug"))]structFoo<T, U> {
foo: T,
#[derivative(Debug(format_with="MyDebug::my_fmt"))]
bar: U,
}
}
Or on a field. This replaces the bound derivative guessed for that field. The example below is equivalent to the above:
#![allow(unused)]fnmain() {
externcrate derivative;
use derivative::Derivative;
traitMyDebug {
fnmy_fmt(&self, _: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error>;
}
#[derive(Derivative)]#[derivative(Debug)]structFoo<T, U> {
foo: T,
#[derivative(Debug(format_with="MyDebug::my_fmt", bound="U: MyDebug"))]
bar: U,
}
}
With bound="" it is possible to remove any bound for the type. This is useful
if your type contains a Foo<T> that is Debug even if T is not.
You can use derivative to implement Debug on packed structures. Unlike the standard derive(debug), derivative does not require the structure itself to be Copy, but like the standard derive(debug), it requires each (non-ignored) field to be Copy.
#![allow(unused)]fnmain() {
externcrate derivative;
use derivative::Derivative;
#[derive(Derivative)]#[derivative(Debug)]#[repr(C, packed)]structFoo {
foo: u8,
// `String` isn't `Copy` so it must be ignored to derive `Debug`#[derivative(Debug="ignore")]
bar: String,
}
}