Use the derive_more crate by adding it to your Cargo.toml with the specific feature flags you need, then apply the corresponding derive macro to your struct or enum.
[dependencies]
derive_more = { version = "2", features = ["deref", "deref_mut", "display", "error", "from"] }
use derive_more::{Deref, DerefMut, Display};
#[derive(Debug, Deref, DerefMut, Display)]
pub struct NamedFile {
#[deref]
#[deref_mut]
file: File,
path: PathBuf,
}
The Deref and DerefMut attributes on the file field allow NamedFile to automatically behave like a File when called with & or &mut, while Display generates the fmt implementation based on the struct's fields.