¿Se considera una mala práctica implementar Deref para los nuevos tipos?
A menudo uso el patrón newtype, pero estoy cansado de escribirmy_type.0.call_to_whatever(...)
. Estoy tentado a implementar elDeref
rasgo porque permite escribir código más simple ya que puedo usar mi nuevo tipo como si fuera el tipo subyacente en algunas situaciones,p.ej.:
use std::ops::Deref;
type Underlying = [i32; 256];
struct MyArray(Underlying);
impl Deref for MyArray {
type Target = Underlying;
fn deref(&self) -> &Self::Target {
&self.0
}
}
fn main() {
let my_array = MyArray([0; 256]);
println!("{}", my_array[0]); // I can use my_array just like a regular array
}
¿Es esta una buena o mala práctica? ¿Por qué? ¿Cuáles pueden ser las desventajas?