I have a struct that wraps a value. For example:
struct PageSize {
value: usize
}
I now want to pass this around, but use it as if it was its wrapped value. E.g.:
let home_size = PageSize { value: 100 }
let about_size = PageSize { value: 200 }
let average = (homesize + about_size) / 2
Arithmatic is but one example. Others could be length() or hour() on resp. a wrapped String, or wrapped DateTime.
Is there a trait, or pattern that lets me forward any method calls to its wrapped value? And that enables operators like arithmetic or logic on its wrapped number or boolean?
Additionally, I would like to take over the "behaviour" of the wrapped value. e.g. via a derive, macro or trait. Where currently I need to derive or impl Display, in order to display:
#[derive(Display)]
#[display(fmt = "{} bytes", value)]
struct PageSize {
value: usize
}
println!("size: {}", PageSize { value: 100 });
I'd prefer to configure my struct so that it inherits this behaviour from the wrapped value. When the wrapped value can be Debug, PartialEq, Display etc etc, then I'd like my PageSize
Struct to inherit this. Not sure if this is possible at all, though.
I'm pretty sure I've seen this mentioned in either a rust book or article, but I forgot the terms and names, and location. So I cannot find the concept. An unfortunate issue is that Wrapped
and unwrap()
and Boxed etc already have meaning in Rust, which makes searching harder.